Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ clean:
unittests:
@echo "Running unit tests"
go clean -testcache
go test -v ./...
go test -v ./internal/... ./types/...

benchtests: build
@echo "Running bench tests"
Expand All @@ -32,7 +32,7 @@ build: clean
endtoendtests: build
@echo "Running endtoend tests"
find tests/endtoend/ -name 'validator__.go' -exec rm \{} \;
cd tests/endtoend/generate_tests/; rm -f numeric_*.go; go run generate_numeric_tests_main.go; mv numeric_*.go ..
cd tests/endtoend/generate_tests/; rm -f generated*tests.go; go run *.go; mv generated*tests.go ..
$(VALIDGEN_BIN) tests/endtoend
cd tests/endtoend; go run .

Expand Down
2 changes: 1 addition & 1 deletion internal/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func checkForInvalidOperations(structs []*Struct) error {

// Check if is a valid operation for this type.
if !ops.IsValidByType(op, fdType.ToNormalizedString()) {
return types.NewValidationError("operation %s: invalid %s type", op, fdType.BaseType)
return types.NewValidationError("operation %s: invalid %s(%s) type", op, fdType.BaseType, fdType.ToNormalizedString())
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func TestAnalyzeStructsWithInvalidInnerFieldOperations(t *testing.T) {
},
},
},
wantErr: types.NewValidationError("operation ltfield: invalid string type"),
wantErr: types.NewValidationError("operation ltfield: invalid string(<STRING>) type"),
},
}

Expand Down Expand Up @@ -359,7 +359,7 @@ func TestAnalyzeStructsWithInvalidNestedFieldOperations(t *testing.T) {
},
},
},
wantErr: types.NewValidationError("operation ltfield: invalid string type"),
wantErr: types.NewValidationError("operation ltfield: invalid string(<STRING>) type"),
},
}

Expand Down
10 changes: 10 additions & 0 deletions internal/analyzer/operations/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package operations

import (
"slices"
"strings"

"github.com/opencodeco/validgen/internal/common"
)
Expand Down Expand Up @@ -29,6 +30,15 @@ func (o *Operations) IsValid(op string) bool {
}

func (o *Operations) IsValidByType(op, fieldType string) bool {
// * is a modifier and can be ignored for type validation.
fieldType, pointer := strings.CutPrefix(fieldType, "*")

// Required can be used with all pointer types.
if pointer && op == "required" {
// Required can be used with all pointers types.
return true
}

return slices.Contains(o.operations[op].ValidTypes, fieldType)
}

Expand Down
34 changes: 28 additions & 6 deletions internal/analyzer/operations/operations_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ var operationsList = map[string]Operation{
"required": {
CountValues: common.ZeroValue,
IsFieldOperation: false,
ValidTypes: []string{"<STRING>", "<INT>", "<FLOAT>", "[]<STRING>", "[]<INT>", "map[<STRING>]", "map[<INT>]"},
ValidTypes: []string{
"<STRING>", "<INT>", "<FLOAT>", "<BOOL>",
"[]<STRING>", "[]<INT>", "[]<FLOAT>", "[]<BOOL>",
"map[<STRING>]", "map[<INT>]", "map[<FLOAT>]", "map[<BOOL>]"},
},
"gt": {
CountValues: common.OneValue,
Expand All @@ -36,12 +39,18 @@ var operationsList = map[string]Operation{
"min": {
CountValues: common.OneValue,
IsFieldOperation: false,
ValidTypes: []string{"<STRING>", "[]<STRING>", "[]<INT>", "map[<STRING>]", "map[<INT>]"},
ValidTypes: []string{
"<STRING>", "[]<STRING>", "[]<INT>", "[]<FLOAT>", "[]<BOOL>",
"map[<STRING>]", "map[<INT>]", "map[<FLOAT>]", "map[<BOOL>]",
},
},
"max": {
CountValues: common.OneValue,
IsFieldOperation: false,
ValidTypes: []string{"<STRING>", "[]<STRING>", "[]<INT>", "map[<STRING>]", "map[<INT>]"},
ValidTypes: []string{
"<STRING>", "[]<STRING>", "[]<INT>", "[]<FLOAT>", "[]<BOOL>",
"map[<STRING>]", "map[<INT>]", "map[<FLOAT>]", "map[<BOOL>]",
},
},
"eq_ignore_case": {
CountValues: common.OneValue,
Expand All @@ -51,7 +60,10 @@ var operationsList = map[string]Operation{
"len": {
CountValues: common.OneValue,
IsFieldOperation: false,
ValidTypes: []string{"<STRING>", "[]<STRING>", "[]<INT>", "map[<STRING>]", "map[<INT>]"},
ValidTypes: []string{
"<STRING>", "[]<STRING>", "[]<INT>", "[]<FLOAT>", "[]<BOOL>",
"map[<STRING>]", "map[<INT>]", "map[<FLOAT>]", "map[<BOOL>]",
},
},
"neq": {
CountValues: common.OneValue,
Expand All @@ -66,12 +78,22 @@ var operationsList = map[string]Operation{
"in": {
CountValues: common.ManyValues,
IsFieldOperation: false,
ValidTypes: []string{"<STRING>", "<INT>", "<FLOAT>", "[]<STRING>", "[]<INT>", "[N]<STRING>", "[N]<INT>", "map[<STRING>]", "map[<INT>]"},
ValidTypes: []string{
"<STRING>", "<INT>", "<FLOAT>", "<BOOL>",
"[]<STRING>", "[]<INT>", "[]<FLOAT>", "[]<BOOL>",
"[N]<STRING>", "[N]<INT>", "[N]<FLOAT>", "[N]<BOOL>",
"map[<STRING>]", "map[<INT>]", "map[<FLOAT>]", "map[<BOOL>]",
},
},
"nin": {
CountValues: common.ManyValues,
IsFieldOperation: false,
ValidTypes: []string{"<STRING>", "<INT>", "<FLOAT>", "[]<STRING>", "[]<INT>", "[N]<STRING>", "[N]<INT>", "map[<STRING>]", "map[<INT>]"},
ValidTypes: []string{
"<STRING>", "<INT>", "<FLOAT>", "<BOOL>",
"[]<STRING>", "[]<INT>", "[]<FLOAT>", "[]<BOOL>",
"[N]<STRING>", "[N]<INT>", "[N]<FLOAT>", "[N]<BOOL>",
"map[<STRING>]", "map[<INT>]", "map[<FLOAT>]", "map[<BOOL>]",
},
},
"email": {
CountValues: common.ZeroValue,
Expand Down
Loading
Loading