diff --git a/Makefile b/Makefile index 48b817d..4fd8827 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ build: clean testgen: @echo "Generating tests" - cd testgen/ && rm -f generated_*.go && go run *.go && mv generated_endtoend_*tests.go ../tests/endtoend/ && mv generated_validation_*_test.go ../internal/codegenerator/ && mv generated_function_code_*_test.go ../internal/codegenerator/ && mv generated_cmp_perf_*.go ../tests/cmpbenchtests/generated_tests/ + cd testgen/ && rm -f generated_*.go && go run *.go && mv generated_endtoend_*tests.go ../tests/endtoend/ && mv generated_validation_*_test.go ../internal/codegenerator/ && mv generated_function_code_*_test.go ../internal/codegenerator/ && mv generated_cmp_perf_*.go ../tests/cmpbenchtests/ endtoendtests: build @echo "Running endtoend tests" @@ -41,11 +41,9 @@ endtoendtests: build cmpbenchtests: build @echo "Running cmp bench tests" - rm -f tests/cmpbenchtests/generated_tests/valid*.go && rm -f tests/cmpbenchtests/generated_tests/types.go - cd tests/cmpbenchtests; go run . - $(VALIDGEN_BIN) tests/cmpbenchtests/generated_tests + $(VALIDGEN_BIN) tests/cmpbenchtests/ go clean -testcache - go test -bench=. -v -benchmem -benchtime=$(BENCH_TIME) ./tests/cmpbenchtests/generated_tests + go test -bench=. -v -benchmem -benchtime=$(BENCH_TIME) ./tests/cmpbenchtests/ setup: @echo "Setting up" diff --git a/internal/codegenerator/build_func_validator_test.go b/internal/codegenerator/build_func_validator_test.go index 6108d9a..27f9048 100644 --- a/internal/codegenerator/build_func_validator_test.go +++ b/internal/codegenerator/build_func_validator_test.go @@ -9,7 +9,7 @@ import ( "github.com/sergi/go-diff/diffmatchpatch" ) -func TestBuildFuncValidatorCode(t *testing.T) { +func TestBuildFuncValidatorCodeFieldOperations(t *testing.T) { type fields struct { Struct *analyzer.Struct } @@ -18,79 +18,6 @@ func TestBuildFuncValidatorCode(t *testing.T) { fields fields want string }{ - { - name: "Valid struct", - fields: fields{ - Struct: &analyzer.Struct{ - Struct: parser.Struct{ - PackageName: "main", - StructName: "User", - Fields: []parser.Field{ - { - FieldName: "FirstName", - Type: common.FieldType{BaseType: "string"}, - Tag: `validate:"required"`, - }, - { - FieldName: "MyAge", - Type: common.FieldType{BaseType: "uint8"}, - Tag: `validate:"required"`, - }, - }, - }, - FieldsValidations: []analyzer.FieldValidations{ - { - Validations: []*analyzer.Validation{AssertParserValidation(t, "required")}, - }, - { - Validations: []*analyzer.Validation{AssertParserValidation(t, "required")}, - }, - }, - }, - }, - want: `func UserValidate(obj *User) []error { -var errs []error -if !(obj.FirstName != "") { -errs = append(errs, types.NewValidationError("FirstName is required")) -} -if !(obj.MyAge != 0) { -errs = append(errs, types.NewValidationError("MyAge is required")) -} -return errs -} -`, - }, - { - name: "FirstName must have 5 characters or more", - fields: fields{ - Struct: &analyzer.Struct{ - Struct: parser.Struct{ - PackageName: "main", - StructName: "User", - Fields: []parser.Field{ - { - FieldName: "FirstName", - Type: common.FieldType{BaseType: "string"}, - Tag: `validate:"min=5"`, - }, - }, - }, - FieldsValidations: []analyzer.FieldValidations{ - { - Validations: []*analyzer.Validation{AssertParserValidation(t, "min=5")}, - }, - }, - }, - }, - want: `func UserValidate(obj *User) []error { -var errs []error -if !(len(obj.FirstName) >= 5) { -errs = append(errs, types.NewValidationError("FirstName length must be >= 5")) -} -return errs -} -`, - }, { name: "Field inner op", fields: fields{ diff --git a/internal/codegenerator/build_validator_test.go b/internal/codegenerator/build_validator_test.go index ae53669..24694e8 100644 --- a/internal/codegenerator/build_validator_test.go +++ b/internal/codegenerator/build_validator_test.go @@ -1,7 +1,6 @@ package codegenerator import ( - "fmt" "testing" "github.com/opencodeco/validgen/internal/analyzer" @@ -9,7 +8,7 @@ import ( "github.com/opencodeco/validgen/internal/parser" ) -func TestBuildValidationCodeOld(t *testing.T) { +func TestBuildValidationCodeFieldOperations(t *testing.T) { type args struct { fieldName string fieldType common.FieldType @@ -20,152 +19,6 @@ func TestBuildValidationCodeOld(t *testing.T) { args args want string }{ - { - name: "if code with string", - args: args{ - fieldName: "strField", - fieldType: common.FieldType{BaseType: "string"}, - fieldValidation: "eq=abc", - }, - want: `if !(obj.strField == "abc") { -errs = append(errs, types.NewValidationError("strField must be equal to 'abc'")) -} -`, - }, - { - name: "if code with uint8", - args: args{ - fieldName: "intField", - fieldType: common.FieldType{BaseType: "uint8"}, - fieldValidation: "gte=123", - }, - want: `if !(obj.intField >= 123) { -errs = append(errs, types.NewValidationError("intField must be >= 123")) -} -`, - }, - { - name: "if code with string and in", - args: args{ - fieldName: "strField", - fieldType: common.FieldType{BaseType: "string"}, - fieldValidation: "in=a b c", - }, - want: `if !(obj.strField == "a" || obj.strField == "b" || obj.strField == "c") { -errs = append(errs, types.NewValidationError("strField must be one of 'a' 'b' 'c'")) -} -`, - }, - { - name: "if code with string and not in", - args: args{ - fieldName: "strField", - fieldType: common.FieldType{BaseType: "string"}, - fieldValidation: "nin=a b c", - }, - want: `if !(obj.strField != "a" && obj.strField != "b" && obj.strField != "c") { -errs = append(errs, types.NewValidationError("strField must not be one of 'a' 'b' 'c'")) -} -`, - }, - { - name: "Required slice string", - args: args{ - fieldName: "strField", - fieldType: common.FieldType{BaseType: "string", ComposedType: "[]"}, - fieldValidation: "required", - }, - want: `if !(len(obj.strField) != 0) { -errs = append(errs, types.NewValidationError("strField must not be empty")) -} -`, - }, - { - name: "Min slice string", - args: args{ - fieldName: "strField", - fieldType: common.FieldType{BaseType: "string", ComposedType: "[]"}, - fieldValidation: "min=2", - }, - want: `if !(len(obj.strField) >= 2) { -errs = append(errs, types.NewValidationError("strField must have at least 2 elements")) -} -`, - }, - { - name: "Max slice string", - args: args{ - fieldName: "strField", - fieldType: common.FieldType{BaseType: "string", ComposedType: "[]"}, - fieldValidation: "max=5", - }, - want: `if !(len(obj.strField) <= 5) { -errs = append(errs, types.NewValidationError("strField must have at most 5 elements")) -} -`, - }, - { - name: "Len slice string", - args: args{ - fieldName: "strField", - fieldType: common.FieldType{BaseType: "string", ComposedType: "[]"}, - fieldValidation: "len=3", - }, - want: `if !(len(obj.strField) == 3) { -errs = append(errs, types.NewValidationError("strField must have exactly 3 elements")) -} -`, - }, - { - name: "In slice string", - args: args{ - fieldName: "strField", - fieldType: common.FieldType{BaseType: "string", ComposedType: "[]"}, - fieldValidation: "in=a b c", - }, - want: `if !(types.SliceOnlyContains(obj.strField, []string{"a", "b", "c"})) { -errs = append(errs, types.NewValidationError("strField elements must be one of 'a' 'b' 'c'")) -} -`, - }, - { - name: "Not in slice string", - args: args{ - fieldName: "strField", - fieldType: common.FieldType{BaseType: "string", ComposedType: "[]"}, - fieldValidation: "nin=a b c", - }, - want: `if !(types.SliceNotContains(obj.strField, []string{"a", "b", "c"})) { -errs = append(errs, types.NewValidationError("strField elements must not be one of 'a' 'b' 'c'")) -} -`, - }, - - { - name: "In array string", - args: args{ - fieldName: "strField", - fieldType: common.FieldType{BaseType: "string", ComposedType: "[N]"}, - fieldValidation: "in=a b c", - }, - want: `if !(types.SliceOnlyContains(obj.strField[:], []string{"a", "b", "c"})) { -errs = append(errs, types.NewValidationError("strField elements must be one of 'a' 'b' 'c'")) -} -`, - }, - { - name: "Not in array string", - args: args{ - fieldName: "strField", - fieldType: common.FieldType{BaseType: "string", ComposedType: "[N]"}, - fieldValidation: "nin=a b c", - }, - want: `if !(types.SliceNotContains(obj.strField[:], []string{"a", "b", "c"})) { -errs = append(errs, types.NewValidationError("strField elements must not be one of 'a' 'b' 'c'")) -} -`, - }, - { name: "inner field operation", args: args{ @@ -188,71 +41,6 @@ errs = append(errs, types.NewValidationError("field1 must be equal to field2")) want: `if !(obj.field1 == obj.Nested.field2) { errs = append(errs, types.NewValidationError("field1 must be equal to Nested.field2")) } -`, - }, - - { - name: "if code with bool", - args: args{ - fieldName: "boolField", - fieldType: common.FieldType{BaseType: "bool"}, - fieldValidation: "eq=true", - }, - want: `if !(obj.boolField == true) { -errs = append(errs, types.NewValidationError("boolField must be equal to true")) -} -`, - }, - - { - name: "if code with int", - args: args{ - fieldName: "intField", - fieldType: common.FieldType{BaseType: "int"}, - fieldValidation: "eq=5", - }, - want: `if !(obj.intField == 5) { -errs = append(errs, types.NewValidationError("intField must be equal to 5")) -} -`, - }, - - { - name: "if code with float32", - args: args{ - fieldName: "floatField", - fieldType: common.FieldType{BaseType: "float32"}, - fieldValidation: "eq=5.0", - }, - want: `if !(obj.floatField == 5.0) { -errs = append(errs, types.NewValidationError("floatField must be equal to 5.0")) -} -`, - }, - - // Map type - { - name: "if code with string map", - args: args{ - fieldName: "mapField", - fieldType: common.FieldType{BaseType: "string", ComposedType: "map"}, - fieldValidation: "len=3", - }, - want: `if !(len(obj.mapField) == 3) { -errs = append(errs, types.NewValidationError("mapField must have exactly 3 elements")) -} -`, - }, - { - name: "if code with uint8 map", - args: args{ - fieldName: "mapField", - fieldType: common.FieldType{BaseType: "uint8", ComposedType: "map"}, - fieldValidation: "len=3", - }, - want: `if !(len(obj.mapField) == 3) { -errs = append(errs, types.NewValidationError("mapField must have exactly 3 elements")) -} `, }, } @@ -273,7 +61,7 @@ errs = append(errs, types.NewValidationError("mapField must have exactly 3 eleme } } -func TestBuildValidationCodeWithNestedStructsAndSlices(t *testing.T) { +func TestBuildValidationCodeWithNestedStructs(t *testing.T) { type args struct { fieldName string fieldType common.FieldType @@ -302,30 +90,6 @@ func TestBuildValidationCodeWithNestedStructsAndSlices(t *testing.T) { }, want: "errs = append(errs, mypkg.InnerStructTypeValidate(&obj.Field)...)\n", }, - { - name: "test code with required slice of string", - args: args{ - fieldName: "Field", - fieldType: common.FieldType{BaseType: "string", ComposedType: "[]"}, - fieldValidation: "required", - }, - want: `if !(len(obj.Field) != 0) { -errs = append(errs, types.NewValidationError("Field must not be empty")) -} -`, - }, - { - name: "test code with min slice of string", - args: args{ - fieldName: "Field", - fieldType: common.FieldType{BaseType: "string", ComposedType: "[]"}, - fieldValidation: "min=2", - }, - want: `if !(len(obj.Field) >= 2) { -errs = append(errs, types.NewValidationError("Field must have at least 2 elements")) -} -`, - }, } for _, tt := range tests { @@ -351,171 +115,3 @@ errs = append(errs, types.NewValidationError("Field must have at least 2 element }) } } - -func TestBuildValidationCodeWithPointerTypes(t *testing.T) { - tests := []struct { - fieldType common.FieldType - validation string - want string - }{ - // Pointer basic types. - { - fieldType: common.FieldType{BaseType: "string", ComposedType: "*"}, - validation: "eq=abc", - want: `if !(obj.field != nil && *obj.field == "abc") { -errs = append(errs, types.NewValidationError("field must be equal to 'abc'")) -} -`, - }, - { - fieldType: common.FieldType{BaseType: "bool", ComposedType: "*"}, - validation: "eq=true", - want: `if !(obj.field != nil && *obj.field == true) { -errs = append(errs, types.NewValidationError("field must be equal to true")) -} -`, - }, - { - fieldType: common.FieldType{BaseType: "int", ComposedType: "*"}, - validation: "eq=5", - want: `if !(obj.field != nil && *obj.field == 5) { -errs = append(errs, types.NewValidationError("field must be equal to 5")) -} -`, - }, - { - fieldType: common.FieldType{BaseType: "float32", ComposedType: "*"}, - validation: "eq=5.0", - want: `if !(obj.field != nil && *obj.field == 5.0) { -errs = append(errs, types.NewValidationError("field must be equal to 5.0")) -} -`, - }, - { - fieldType: common.FieldType{BaseType: "bool", ComposedType: "*"}, - validation: "eq=true", - want: `if !(obj.field != nil && *obj.field == true) { -errs = append(errs, types.NewValidationError("field must be equal to true")) -} -`, - }, - - // Slice pointer types. - { - fieldType: common.FieldType{BaseType: "string", ComposedType: "*[]"}, - validation: "required", - want: `if !(obj.field != nil && len(*obj.field) != 0) { -errs = append(errs, types.NewValidationError("field must not be empty")) -} -`, - }, - { - fieldType: common.FieldType{BaseType: "int", ComposedType: "*[]"}, - validation: "min=10", - want: `if !(obj.field != nil && len(*obj.field) >= 10) { -errs = append(errs, types.NewValidationError("field must have at least 10 elements")) -} -`, - }, - { - fieldType: common.FieldType{BaseType: "float32", ComposedType: "*[]"}, - validation: "in=10.0 12.0 14.0", - want: `if !(obj.field != nil && types.SliceOnlyContains(*obj.field, []float32{10.0, 12.0, 14.0})) { -errs = append(errs, types.NewValidationError("field elements must be one of '10.0' '12.0' '14.0'")) -} -`, - }, - { - fieldType: common.FieldType{BaseType: "bool", ComposedType: "*[]"}, - validation: "min=3", - want: `if !(obj.field != nil && len(*obj.field) >= 3) { -errs = append(errs, types.NewValidationError("field must have at least 3 elements")) -} -`, - }, - - // Array pointer types. - { - fieldType: common.FieldType{BaseType: "string", ComposedType: "*[N]"}, - validation: "in=a b c", - want: `if !(obj.field != nil && types.SliceOnlyContains(obj.field[:], []string{"a", "b", "c"})) { -errs = append(errs, types.NewValidationError("field elements must be one of 'a' 'b' 'c'")) -} -`, - }, - { - fieldType: common.FieldType{BaseType: "int", ComposedType: "*[N]"}, - validation: "in=1 2 3", - want: `if !(obj.field != nil && types.SliceOnlyContains(obj.field[:], []int{1, 2, 3})) { -errs = append(errs, types.NewValidationError("field elements must be one of '1' '2' '3'")) -} -`, - }, - { - fieldType: common.FieldType{BaseType: "float32", ComposedType: "*[N]"}, - validation: "in=1.1 2.2 3.3", - want: `if !(obj.field != nil && types.SliceOnlyContains(obj.field[:], []float32{1.1, 2.2, 3.3})) { -errs = append(errs, types.NewValidationError("field elements must be one of '1.1' '2.2' '3.3'")) -} -`, - }, - { - fieldType: common.FieldType{BaseType: "bool", ComposedType: "*[N]"}, - validation: "in=true", - want: `if !(obj.field != nil && types.SliceOnlyContains(obj.field[:], []bool{true})) { -errs = append(errs, types.NewValidationError("field elements must be one of 'true'")) -} -`, - }, - - // Map pointer types. - { - fieldType: common.FieldType{BaseType: "string", ComposedType: "*map"}, - validation: "len=3", - want: `if !(obj.field != nil && len(*obj.field) == 3) { -errs = append(errs, types.NewValidationError("field must have exactly 3 elements")) -} -`, - }, - { - fieldType: common.FieldType{BaseType: "int", ComposedType: "*map"}, - validation: "len=3", - want: `if !(obj.field != nil && len(*obj.field) == 3) { -errs = append(errs, types.NewValidationError("field must have exactly 3 elements")) -} -`, - }, - { - fieldType: common.FieldType{BaseType: "float32", ComposedType: "*map"}, - validation: "len=3", - want: `if !(obj.field != nil && len(*obj.field) == 3) { -errs = append(errs, types.NewValidationError("field must have exactly 3 elements")) -} -`, - }, - { - fieldType: common.FieldType{BaseType: "bool", ComposedType: "*map"}, - validation: "len=3", - want: `if !(obj.field != nil && len(*obj.field) == 3) { -errs = append(errs, types.NewValidationError("field must have exactly 3 elements")) -} -`, - }, - } - - for _, tt := range tests { - testName := fmt.Sprintf("validation: %s with %s (%s)", tt.validation, tt.fieldType.ToGenericType(), tt.fieldType.ToNormalizedString()) - t.Run(testName, func(t *testing.T) { - gv := GenValidations{} - validation := AssertParserValidation(t, tt.validation) - got, err := gv.BuildValidationCode("field", tt.fieldType, []*analyzer.Validation{validation}) - if err != nil { - t.Errorf("BuildValidationCode() error = %v, wantErr %v", err, nil) - return - } - if got != tt.want { - t.Errorf("BuildValidationCode() = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/testgen/README.md b/testgen/README.md index b101ed1..eb85740 100644 --- a/testgen/README.md +++ b/testgen/README.md @@ -1,14 +1,14 @@ # TestGen -TestGen is the tool responsible for generating tests related to ValidGen validators and supported types. -These generated tests included unit tests, end-to-end tests, and benchmark tests. -In the case of benchmark tests, it means comparing ValidGen and GoValidator. +TestGen is a tool responsible for generating comprehensive test suites for ValidGen validators and supported types. +These generated tests include unit tests, end-to-end tests, and benchmark tests. +The benchmark tests compare ValidGen and GoValidator performance. ## Why a new tool? -First of all, it is necessary to answer the question: why a new tool to generate tests? +First, let's address the question: why create a dedicated tool for test generation? -Currently, ValidGen has 21 possible validations with support for some types: +ValidGen currently supports 21 validations across multiple data types: | Validation | Basic types | Slice | Array | Map | | - | - | - | - | - | @@ -34,86 +34,73 @@ Currently, ValidGen has 21 possible validations with support for some types: | ltefield | INT | | | | | ltfield | INT | | | | -In this table, STRING represents the string Go type, and BOOL represents the bool Go type. -But INT represents the ten integer Go types: int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64. -In the same way, FLOAT represents the 2 float go types: float32, float64. -In the same way, slice STRING is just []string, but slice INT is split between all integer Go types. -For array and map, the rule is the same. -And, an important modifier is "*" (pointer) because the code generated is different with or without a pointer. - -For each possible combination (validation x type) is necessary to have the following tests: -- Unit test in the analyzer phase -- Unit test in the code generator phase -- Benchmark test between ValidGen and GoValidator -- End-to-end test +In this table: +- **STRING** represents the `string` Go type +- **BOOL** represents the `bool` Go type +- **INT** represents all ten integer Go types: `int`, `int8`, `int16`, `int32`, `int64`, `uint`, `uint8`, `uint16`, `uint32`, `uint64` +- **FLOAT** represents both float Go types: `float32`, `float64` -As it is necessary to test all valid and invalid scenarios, it is necessary to test all validations against all types. -At this time, it means: -- Go types (14) -- Validations (21) -- Test types (4) -- Types with and without a pointer (2) -- Tests with valid and invalid inputs (2) +For slices, arrays, and maps, the same type expansion applies. For example, slice STRING is `[]string`, while slice INT expands to all integer Go types. -14 x 21 x 4 x 2 x 2 = 4.704 distinct test cases :-) +Additionally, the pointer modifier (`*`) is significant because ValidGen generates different code for pointer and non-pointer types. -With all the tests that need to be created (valid inputs, invalid inputs) for unit tests, benchmark, and end-to-end tests, creating the tests "by hand" is a tedious and error-prone task. +For each possible combination (validation × type), the following tests are required: +- Unit test for the analyzer phase +- Unit test for the code generator phase +- Benchmark test comparing ValidGen and GoValidator +- End-to-end test -Some of these necessary unit tests were created "by hand", but it is a pain to keep the code in sync when supporting new operations and types. +Since we need to test both valid and invalid scenarios, all validations must be tested against all types. +Currently, this means: +- Go types: 14 +- Validations: 21 +- Test types: 4 +- Pointer variants: 2 (with/without pointer) +- Input scenarios: 2 (valid/invalid) -At this time, ValidGen already has two generators: -- To generate benchmark tests between ValidGen and GoValidator -- To generate end-to-end tests - - to validate ValidGen with integer types - - to validate ValidGen with float types - - to validate all possible use cases in ValidGen (all validations x all types) +**14 × 21 × 4 × 2 × 2 = 4,704 distinct test cases** -But these generators do not have a common configuration, do not implement all tests for all cases, and keeping the distinct configuration files in sync is painful. +Creating and maintaining these thousands of tests manually is tedious, error-prone, and impractical. Early attempts to write these tests by hand proved difficult to maintain when adding new operations and types. -Beyond that, some new generators could be created: -- Unit tests to validate operations -- Unit tests to validate operation x type -- Unit tests to validate the "buildValidationCode" function -- Unit tests to validate the "condition table" -- Parser tests -- Almost all existing end-to-end tests could be generated -- Examples (in _examples/) could be generated +Previously, ValidGen had two separate test generators: +- Benchmark tests comparing ValidGen and GoValidator +- End-to-end tests for: + - Integer type validations + - Float type validations + - All possible use cases (all validations × all types) -And, for all cases, valid scenarios and invalid scenarios must be generated. +However, these generators lacked a common configuration, didn't implement all tests for all cases, and keeping the separate configuration files in sync was difficult. ## What TestGen does -TestGen generates the following tests: -- Benchmark tests between ValidGen and GoValidator -- End-to-end tests with all possible use cases (all validations vs all types vs valid and invalid inputs) -- Unit tests to validate operations -- Unit tests to validate operation vs type -- Unit tests to validate the "buildValidationCode" function -- Unit tests to validate "condition table" result -- Parser tests (parser_tests.go) -- Examples (in _examples/) +TestGen generates the following tests (without field operations): +- [x] Benchmark tests between ValidGen and GoValidator +- [x] End-to-end tests with all possible use cases (all validations vs all types vs valid and invalid inputs) +- [x] Unit tests to validate the "buildValidationCode" function -In some cases, valid scenarios and invalid scenarios must be generated. +High priority generators: +- [ ] Unit tests to validate the "condition table" (get_test_elements_*_test.go) +- [ ] Benchmark tests between ValidGen and GoValidator with field operations +- [ ] End-to-end tests with all possible use cases (all validations vs all types vs valid and invalid inputs) with field operations +- [ ] Unit tests to validate the "buildValidationCode" function with field operations -## How TestGen works +Low priority generators (already exist, but could be automated): +- [ ] Unit tests to validate operations (func TestOperationsIsValid) +- [ ] Unit tests to validate operation vs type (func TestOperationsIsValidByType) +- [ ] Unit tests to validate field operations (func TestOperationsIsFieldOperation) +- [ ] Unit tests to validate argument count by operation (func TestOperationsArgsCount) +- [ ] Examples (in _examples/) could be generated -To be possible to generate all these tests, TestGen must have a configuration with: -- All valid operations -- All valid go types -- All valid operation x type -- Valid input cases for each operation x type -- Invalid input cases for each operation x type -- Equivalent GoValidator tag +Where applicable, TestGen generates both valid and invalid test scenarios. -## Steps to generate the tests +## Usage -The steps to generate the tests are: +To generate the tests: ```bash -# Enter in the project root folder +# Navigate to the project root folder cd validgen # Run testgen make testgen ``` - diff --git a/tests/cmpbenchtests/generate_cmp_benchtests_main.go b/tests/cmpbenchtests/generate_cmp_benchtests_main.go deleted file mode 100644 index f84b914..0000000 --- a/tests/cmpbenchtests/generate_cmp_benchtests_main.go +++ /dev/null @@ -1,134 +0,0 @@ -package main - -import ( - "bytes" - "fmt" - "log" - "os" - "text/template" -) - -type BenchTests struct { - Tests []Test -} - -type Test struct { - TestType string - FieldType string - ValidGenValidate string - ValidatorValidate string - ValidInput string -} - -func main() { - log.Println("Generating benchtest files") - - benchTests := BenchTests{} - benchTests.Tests = []Test{ - { - TestType: "StringRequired", - FieldType: "string", - ValidGenValidate: "required", - ValidatorValidate: "required", - ValidInput: "xpto", - }, - { - TestType: "StringEq", - FieldType: "string", - ValidGenValidate: "eq=abc", - ValidatorValidate: "eq=abc", - ValidInput: "abc", - }, - { - TestType: "StringEqIC", - FieldType: "string", - ValidGenValidate: "eq_ignore_case=abc", - ValidatorValidate: "eq_ignore_case=abc", - ValidInput: "AbC", - }, - { - TestType: "StringNeq", - FieldType: "string", - ValidGenValidate: "neq=abc", - ValidatorValidate: "ne=abc", - ValidInput: "123", - }, - { - TestType: "StringNeqIC", - FieldType: "string", - ValidGenValidate: "neq_ignore_case=abc", - ValidatorValidate: "ne_ignore_case=abc", - ValidInput: "123", - }, - { - TestType: "StringLen", - FieldType: "string", - ValidGenValidate: "len=5", - ValidatorValidate: "len=5", - ValidInput: "abcde", - }, - { - TestType: "StringMax", - FieldType: "string", - ValidGenValidate: "max=5", - ValidatorValidate: "max=5", - ValidInput: "abcde", - }, - { - TestType: "StringMin", - FieldType: "string", - ValidGenValidate: "min=3", - ValidatorValidate: "min=3", - ValidInput: "abcd", - }, - { - TestType: "StringIn", - FieldType: "string", - ValidGenValidate: "in=ab cd ef", - ValidatorValidate: "oneof=ab cd ef", - ValidInput: "ef", - }, - { - TestType: "StringEmail", - FieldType: "string", - ValidGenValidate: "email", - ValidatorValidate: "email", - ValidInput: "aaa@example.com", - }, - - // nin - } - - if err := benchTests.GenerateFile("types.tpl", "./generated_tests/types.go"); err != nil { - log.Fatalf("error generation types file %s", err) - } - - if err := benchTests.GenerateFile("validgen_vs_validator_test.tpl", "./generated_tests/validgen_vs_validator_test.go"); err != nil { - log.Fatalf("error generation test file %s", err) - } - - log.Println("Generating done") -} - -func (bt *BenchTests) GenerateFile(tplFile, output string) error { - tpl, err := os.ReadFile(tplFile) - if err != nil { - return fmt.Errorf("error reading %s: %s", tplFile, err) - } - - tmpl, err := template.New("BenchTest").Parse(string(tpl)) - if err != nil { - return fmt.Errorf("error parsing template %s: %s", tplFile, err) - } - - code := new(bytes.Buffer) - if err := tmpl.Execute(code, bt); err != nil { - return err - } - - if err := os.WriteFile(output, code.Bytes(), 0644); err != nil { - return err - } - - return nil -} diff --git a/tests/cmpbenchtests/generated_tests/generated_cmp_perf_no_pointer_test.go b/tests/cmpbenchtests/generated_cmp_perf_no_pointer_test.go similarity index 100% rename from tests/cmpbenchtests/generated_tests/generated_cmp_perf_no_pointer_test.go rename to tests/cmpbenchtests/generated_cmp_perf_no_pointer_test.go diff --git a/tests/cmpbenchtests/generated_tests/generated_cmp_perf_pointer_test.go b/tests/cmpbenchtests/generated_cmp_perf_pointer_test.go similarity index 100% rename from tests/cmpbenchtests/generated_tests/generated_cmp_perf_pointer_test.go rename to tests/cmpbenchtests/generated_cmp_perf_pointer_test.go diff --git a/tests/cmpbenchtests/generated_tests/types.go b/tests/cmpbenchtests/generated_tests/types.go deleted file mode 100644 index 27871d7..0000000 --- a/tests/cmpbenchtests/generated_tests/types.go +++ /dev/null @@ -1,83 +0,0 @@ -// Code generated by BenchTestGenerator. DO NOT EDIT. - -package benchtests - -type ValidGenStringRequiredStruct struct { - Field string `valid:"required"` -} - -type ValidatorStringRequiredStruct struct { - Field string `validate:"required"` -} - -type ValidGenStringEqStruct struct { - Field string `valid:"eq=abc"` -} - -type ValidatorStringEqStruct struct { - Field string `validate:"eq=abc"` -} - -type ValidGenStringEqICStruct struct { - Field string `valid:"eq_ignore_case=abc"` -} - -type ValidatorStringEqICStruct struct { - Field string `validate:"eq_ignore_case=abc"` -} - -type ValidGenStringNeqStruct struct { - Field string `valid:"neq=abc"` -} - -type ValidatorStringNeqStruct struct { - Field string `validate:"ne=abc"` -} - -type ValidGenStringNeqICStruct struct { - Field string `valid:"neq_ignore_case=abc"` -} - -type ValidatorStringNeqICStruct struct { - Field string `validate:"ne_ignore_case=abc"` -} - -type ValidGenStringLenStruct struct { - Field string `valid:"len=5"` -} - -type ValidatorStringLenStruct struct { - Field string `validate:"len=5"` -} - -type ValidGenStringMaxStruct struct { - Field string `valid:"max=5"` -} - -type ValidatorStringMaxStruct struct { - Field string `validate:"max=5"` -} - -type ValidGenStringMinStruct struct { - Field string `valid:"min=3"` -} - -type ValidatorStringMinStruct struct { - Field string `validate:"min=3"` -} - -type ValidGenStringInStruct struct { - Field string `valid:"in=ab cd ef"` -} - -type ValidatorStringInStruct struct { - Field string `validate:"oneof=ab cd ef"` -} - -type ValidGenStringEmailStruct struct { - Field string `valid:"email"` -} - -type ValidatorStringEmailStruct struct { - Field string `validate:"email"` -} diff --git a/tests/cmpbenchtests/generated_tests/validgen_vs_validator_test.go b/tests/cmpbenchtests/generated_tests/validgen_vs_validator_test.go deleted file mode 100644 index 52a3c9d..0000000 --- a/tests/cmpbenchtests/generated_tests/validgen_vs_validator_test.go +++ /dev/null @@ -1,279 +0,0 @@ -// Code generated by BenchTestGenerator. DO NOT EDIT. - -package benchtests - -import ( - "testing" - - "github.com/go-playground/validator/v10" -) - -func BenchmarkValidGenStringRequired(b *testing.B) { - data := &ValidGenStringRequiredStruct{ - Field: "xpto", - } - - for b.Loop() { - if err := ValidGenStringRequiredStructValidate(data); len(err) > 0 { - b.FailNow() - } - } -} - -func BenchmarkValidatorStringRequired(b *testing.B) { - var validate *validator.Validate - - validate = validator.New(validator.WithRequiredStructEnabled()) - data := &ValidatorStringRequiredStruct{ - Field: "xpto", - } - - for b.Loop() { - if err := validate.Struct(data); err != nil { - b.FailNow() - } - } -} - -func BenchmarkValidGenStringEq(b *testing.B) { - data := &ValidGenStringEqStruct{ - Field: "abc", - } - - for b.Loop() { - if err := ValidGenStringEqStructValidate(data); len(err) > 0 { - b.FailNow() - } - } -} - -func BenchmarkValidatorStringEq(b *testing.B) { - var validate *validator.Validate - - validate = validator.New(validator.WithRequiredStructEnabled()) - data := &ValidatorStringEqStruct{ - Field: "abc", - } - - for b.Loop() { - if err := validate.Struct(data); err != nil { - b.FailNow() - } - } -} - -func BenchmarkValidGenStringEqIC(b *testing.B) { - data := &ValidGenStringEqICStruct{ - Field: "AbC", - } - - for b.Loop() { - if err := ValidGenStringEqICStructValidate(data); len(err) > 0 { - b.FailNow() - } - } -} - -func BenchmarkValidatorStringEqIC(b *testing.B) { - var validate *validator.Validate - - validate = validator.New(validator.WithRequiredStructEnabled()) - data := &ValidatorStringEqICStruct{ - Field: "AbC", - } - - for b.Loop() { - if err := validate.Struct(data); err != nil { - b.FailNow() - } - } -} - -func BenchmarkValidGenStringNeq(b *testing.B) { - data := &ValidGenStringNeqStruct{ - Field: "123", - } - - for b.Loop() { - if err := ValidGenStringNeqStructValidate(data); len(err) > 0 { - b.FailNow() - } - } -} - -func BenchmarkValidatorStringNeq(b *testing.B) { - var validate *validator.Validate - - validate = validator.New(validator.WithRequiredStructEnabled()) - data := &ValidatorStringNeqStruct{ - Field: "123", - } - - for b.Loop() { - if err := validate.Struct(data); err != nil { - b.FailNow() - } - } -} - -func BenchmarkValidGenStringNeqIC(b *testing.B) { - data := &ValidGenStringNeqICStruct{ - Field: "123", - } - - for b.Loop() { - if err := ValidGenStringNeqICStructValidate(data); len(err) > 0 { - b.FailNow() - } - } -} - -func BenchmarkValidatorStringNeqIC(b *testing.B) { - var validate *validator.Validate - - validate = validator.New(validator.WithRequiredStructEnabled()) - data := &ValidatorStringNeqICStruct{ - Field: "123", - } - - for b.Loop() { - if err := validate.Struct(data); err != nil { - b.FailNow() - } - } -} - -func BenchmarkValidGenStringLen(b *testing.B) { - data := &ValidGenStringLenStruct{ - Field: "abcde", - } - - for b.Loop() { - if err := ValidGenStringLenStructValidate(data); len(err) > 0 { - b.FailNow() - } - } -} - -func BenchmarkValidatorStringLen(b *testing.B) { - var validate *validator.Validate - - validate = validator.New(validator.WithRequiredStructEnabled()) - data := &ValidatorStringLenStruct{ - Field: "abcde", - } - - for b.Loop() { - if err := validate.Struct(data); err != nil { - b.FailNow() - } - } -} - -func BenchmarkValidGenStringMax(b *testing.B) { - data := &ValidGenStringMaxStruct{ - Field: "abcde", - } - - for b.Loop() { - if err := ValidGenStringMaxStructValidate(data); len(err) > 0 { - b.FailNow() - } - } -} - -func BenchmarkValidatorStringMax(b *testing.B) { - var validate *validator.Validate - - validate = validator.New(validator.WithRequiredStructEnabled()) - data := &ValidatorStringMaxStruct{ - Field: "abcde", - } - - for b.Loop() { - if err := validate.Struct(data); err != nil { - b.FailNow() - } - } -} - -func BenchmarkValidGenStringMin(b *testing.B) { - data := &ValidGenStringMinStruct{ - Field: "abcd", - } - - for b.Loop() { - if err := ValidGenStringMinStructValidate(data); len(err) > 0 { - b.FailNow() - } - } -} - -func BenchmarkValidatorStringMin(b *testing.B) { - var validate *validator.Validate - - validate = validator.New(validator.WithRequiredStructEnabled()) - data := &ValidatorStringMinStruct{ - Field: "abcd", - } - - for b.Loop() { - if err := validate.Struct(data); err != nil { - b.FailNow() - } - } -} - -func BenchmarkValidGenStringIn(b *testing.B) { - data := &ValidGenStringInStruct{ - Field: "ef", - } - - for b.Loop() { - if err := ValidGenStringInStructValidate(data); len(err) > 0 { - b.FailNow() - } - } -} - -func BenchmarkValidatorStringIn(b *testing.B) { - var validate *validator.Validate - - validate = validator.New(validator.WithRequiredStructEnabled()) - data := &ValidatorStringInStruct{ - Field: "ef", - } - - for b.Loop() { - if err := validate.Struct(data); err != nil { - b.FailNow() - } - } -} - -func BenchmarkValidGenStringEmail(b *testing.B) { - data := &ValidGenStringEmailStruct{ - Field: "aaa@example.com", - } - - for b.Loop() { - if err := ValidGenStringEmailStructValidate(data); len(err) > 0 { - b.FailNow() - } - } -} - -func BenchmarkValidatorStringEmail(b *testing.B) { - var validate *validator.Validate - - validate = validator.New(validator.WithRequiredStructEnabled()) - data := &ValidatorStringEmailStruct{ - Field: "aaa@example.com", - } - - for b.Loop() { - if err := validate.Struct(data); err != nil { - b.FailNow() - } - } -} diff --git a/tests/cmpbenchtests/types.tpl b/tests/cmpbenchtests/types.tpl deleted file mode 100644 index de97fc9..0000000 --- a/tests/cmpbenchtests/types.tpl +++ /dev/null @@ -1,12 +0,0 @@ -// Code generated by BenchTestGenerator. DO NOT EDIT. - -package benchtests -{{range .Tests}} -type ValidGen{{.TestType}}Struct struct { - Field {{.FieldType}} `valid:"{{.ValidGenValidate}}"` -} - -type Validator{{.TestType}}Struct struct { - Field {{.FieldType}} `validate:"{{.ValidatorValidate}}"` -} -{{end}} \ No newline at end of file diff --git a/tests/cmpbenchtests/generated_tests/validator__.go b/tests/cmpbenchtests/validator__.go similarity index 97% rename from tests/cmpbenchtests/generated_tests/validator__.go rename to tests/cmpbenchtests/validator__.go index 5684e0a..0a8fffb 100755 --- a/tests/cmpbenchtests/generated_tests/validator__.go +++ b/tests/cmpbenchtests/validator__.go @@ -3170,73 +3170,3 @@ func ValidGenRequiredUintStructValidate(obj *ValidGenRequiredUintStruct) []error } return errs } -func ValidGenStringEmailStructValidate(obj *ValidGenStringEmailStruct) []error { - var errs []error - if !(types.IsValidEmail(obj.Field)) { - errs = append(errs, types.NewValidationError("Field must be a valid email")) - } - return errs -} -func ValidGenStringEqICStructValidate(obj *ValidGenStringEqICStruct) []error { - var errs []error - if !(types.EqualFold(obj.Field, "abc")) { - errs = append(errs, types.NewValidationError("Field must be equal to 'abc'")) - } - return errs -} -func ValidGenStringEqStructValidate(obj *ValidGenStringEqStruct) []error { - var errs []error - if !(obj.Field == "abc") { - errs = append(errs, types.NewValidationError("Field must be equal to 'abc'")) - } - return errs -} -func ValidGenStringInStructValidate(obj *ValidGenStringInStruct) []error { - var errs []error - if !(obj.Field == "ab" || obj.Field == "cd" || obj.Field == "ef") { - errs = append(errs, types.NewValidationError("Field must be one of 'ab' 'cd' 'ef'")) - } - return errs -} -func ValidGenStringLenStructValidate(obj *ValidGenStringLenStruct) []error { - var errs []error - if !(len(obj.Field) == 5) { - errs = append(errs, types.NewValidationError("Field length must be 5")) - } - return errs -} -func ValidGenStringMaxStructValidate(obj *ValidGenStringMaxStruct) []error { - var errs []error - if !(len(obj.Field) <= 5) { - errs = append(errs, types.NewValidationError("Field length must be <= 5")) - } - return errs -} -func ValidGenStringMinStructValidate(obj *ValidGenStringMinStruct) []error { - var errs []error - if !(len(obj.Field) >= 3) { - errs = append(errs, types.NewValidationError("Field length must be >= 3")) - } - return errs -} -func ValidGenStringNeqICStructValidate(obj *ValidGenStringNeqICStruct) []error { - var errs []error - if !(!types.EqualFold(obj.Field, "abc")) { - errs = append(errs, types.NewValidationError("Field must not be equal to 'abc'")) - } - return errs -} -func ValidGenStringNeqStructValidate(obj *ValidGenStringNeqStruct) []error { - var errs []error - if !(obj.Field != "abc") { - errs = append(errs, types.NewValidationError("Field must not be equal to 'abc'")) - } - return errs -} -func ValidGenStringRequiredStructValidate(obj *ValidGenStringRequiredStruct) []error { - var errs []error - if !(obj.Field != "") { - errs = append(errs, types.NewValidationError("Field is required")) - } - return errs -} diff --git a/tests/cmpbenchtests/validgen_vs_validator_test.tpl b/tests/cmpbenchtests/validgen_vs_validator_test.tpl deleted file mode 100644 index 472e253..0000000 --- a/tests/cmpbenchtests/validgen_vs_validator_test.tpl +++ /dev/null @@ -1,37 +0,0 @@ -// Code generated by BenchTestGenerator. DO NOT EDIT. - -package benchtests - -import ( - "testing" - - "github.com/go-playground/validator/v10" -) -{{range .Tests}} -func BenchmarkValidGen{{.TestType}}(b *testing.B) { - data := &ValidGen{{.TestType}}Struct{ - Field: "{{.ValidInput}}", - } - - for b.Loop() { - if err := ValidGen{{.TestType}}StructValidate(data); len(err) > 0 { - b.FailNow() - } - } -} - -func BenchmarkValidator{{.TestType}}(b *testing.B) { - var validate *validator.Validate - - validate = validator.New(validator.WithRequiredStructEnabled()) - data := &Validator{{.TestType}}Struct{ - Field: "{{.ValidInput}}", - } - - for b.Loop() { - if err := validate.Struct(data); err != nil { - b.FailNow() - } - } -} -{{end}} \ No newline at end of file diff --git a/tests/endtoend/array_string.go b/tests/endtoend/array_string.go deleted file mode 100644 index ad076a4..0000000 --- a/tests/endtoend/array_string.go +++ /dev/null @@ -1,44 +0,0 @@ -package main - -import ( - "log" -) - -type ArrayString struct { - TypesIn [8]string `valid:"in=a b c"` - TypesNotIn [8]string `valid:"nin=a b c"` -} - -func arrayStringTests() { - log.Println("starting array string tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &ArrayString{ - TypesIn: [8]string{"d"}, - TypesNotIn: [8]string{"a", "b", "c", "d", "e", "f", "g", "h"}, - } - expectedMsgErrors = []string{ - "TypesIn elements must be one of 'a' 'b' 'c'", - "TypesNotIn elements must not be one of 'a' 'b' 'c'", - } - errs = ArrayStringValidate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &ArrayString{ - TypesIn: [8]string{"a", "b", "c", "a", "b", "c", "a", "b"}, - TypesNotIn: [8]string{"d", "e", "f", "d", "e", "f", "d", "e"}, - } - expectedMsgErrors = nil - errs = ArrayStringValidate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("array string tests ok") -} diff --git a/tests/endtoend/generate_tests/generate_numeric_tests.go b/tests/endtoend/generate_tests/generate_numeric_tests.go deleted file mode 100644 index 6c10b1c..0000000 --- a/tests/endtoend/generate_tests/generate_numeric_tests.go +++ /dev/null @@ -1,64 +0,0 @@ -package main - -import ( - "bytes" - "fmt" - "log" - "os" - "text/template" - - "github.com/opencodeco/validgen/internal/common" - "golang.org/x/text/cases" - "golang.org/x/text/language" -) - -type NumericTests struct { - FieldTypes []string -} - -func generateNumericTests() { - log.Println("Generating numeric test files") - - numericTests := NumericTests{} - numericTests.FieldTypes = common.HelperFromNormalizedToBasicTypes("") - - if err := numericTests.GenerateFile("numeric_int.tpl", "./generated_numeric_int_tests.go"); err != nil { - log.Fatalf("error generating numeric int file %s", err) - } - - numericTests = NumericTests{} - numericTests.FieldTypes = common.HelperFromNormalizedToBasicTypes("") - - if err := numericTests.GenerateFile("numeric_float.tpl", "./generated_numeric_float_tests.go"); err != nil { - log.Fatalf("error generating numeric float file %s", err) - } - - log.Println("Generating done") -} - -func (bt *NumericTests) GenerateFile(tplFile, output string) error { - tpl, err := os.ReadFile(tplFile) - if err != nil { - return fmt.Errorf("error reading %s: %s", tplFile, err) - } - - funcMap := template.FuncMap{ - "title": cases.Title(language.Und).String, - } - - tmpl, err := template.New("NumericTest").Funcs(funcMap).Parse(string(tpl)) - if err != nil { - return err - } - - code := new(bytes.Buffer) - if err := tmpl.Execute(code, bt); err != nil { - return err - } - - if err := os.WriteFile(output, code.Bytes(), 0644); err != nil { - return err - } - - return nil -} diff --git a/tests/endtoend/generate_tests/generate_tests.go b/tests/endtoend/generate_tests/generate_tests.go deleted file mode 100644 index da1d2c0..0000000 --- a/tests/endtoend/generate_tests/generate_tests.go +++ /dev/null @@ -1,14 +0,0 @@ -package main - -import ( - "log" -) - -func main() { - log.Println("Generating tests files") - - generateNumericTests() - generateTestCases() - - log.Println("Generating done") -} diff --git a/tests/endtoend/generate_tests/generate_usecase_tests.go b/tests/endtoend/generate_tests/generate_usecase_tests.go deleted file mode 100644 index 16a63e2..0000000 --- a/tests/endtoend/generate_tests/generate_usecase_tests.go +++ /dev/null @@ -1,947 +0,0 @@ -package main - -import ( - "bytes" - "fmt" - "go/format" - "log" - "os" - "strings" - "text/template" - - "github.com/opencodeco/validgen/internal/common" - "golang.org/x/text/cases" - "golang.org/x/text/language" -) - -type testCase struct { - validation string - normalizedType string - validCase string - invalidCase string - errorMessage string - generateOnly string -} - -var testCases = []struct { - operation string - testCases []testCase -}{ - // email operations - { - operation: "email", - testCases: []testCase{ - { - // email: "" - validation: `email`, - normalizedType: ``, - validCase: `"abcde@example.com"`, - invalidCase: `"abcde@example"`, - errorMessage: `{{.FieldName}} must be a valid email`, - }, - }, - }, - - // required operations - { - operation: "required", - testCases: []testCase{ - // required: "", "", "", "" - { - validation: `required`, - normalizedType: ``, - validCase: `"abcde"`, - invalidCase: `""`, - errorMessage: `{{.FieldName}} is required`, - }, - { - validation: `required`, - normalizedType: ``, - validCase: `32`, - invalidCase: `0`, - errorMessage: `{{.FieldName}} is required`, - }, - { - validation: `required`, - normalizedType: ``, - validCase: `12.34`, - invalidCase: `0`, - errorMessage: `{{.FieldName}} is required`, - }, - { - validation: `required`, - normalizedType: ``, - validCase: `true`, - invalidCase: `false`, - errorMessage: `{{.FieldName}} is required`, - }, - - // required: "[]", "[]", "[]", "[]" - { - validation: `required`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{"abcde"}`, - invalidCase: `{{.BasicType}}{}`, - errorMessage: `{{.FieldName}} must not be empty`, - }, - { - validation: `required`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{32}`, - invalidCase: `{{.BasicType}}{}`, - errorMessage: `{{.FieldName}} must not be empty`, - }, - { - validation: `required`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{12.34}`, - invalidCase: `{{.BasicType}}{}`, - errorMessage: `{{.FieldName}} must not be empty`, - }, - { - validation: `required`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{true}`, - invalidCase: `{{.BasicType}}{}`, - errorMessage: `{{.FieldName}} must not be empty`, - }, - - // required: "[N]", "[N]", "[N]", "[N]" - { - validation: `required`, - normalizedType: `[N]`, - validCase: `{{.BasicType}}{"abcde"}`, - invalidCase: `--`, - errorMessage: `{{.FieldName}} must not be empty`, - generateOnly: "pointer", - }, - { - validation: `required`, - normalizedType: `[N]`, - validCase: `{{.BasicType}}{32}`, - invalidCase: `--`, - errorMessage: `{{.FieldName}} must not be empty`, - generateOnly: "pointer", - }, - { - validation: `required`, - normalizedType: `[N]`, - validCase: `{{.BasicType}}{12.34}`, - invalidCase: `--`, - errorMessage: `{{.FieldName}} must not be empty`, - generateOnly: "pointer", - }, - { - validation: `required`, - normalizedType: `[N]`, - validCase: `{{.BasicType}}{true}`, - invalidCase: `--`, - errorMessage: `{{.FieldName}} must not be empty`, - generateOnly: "pointer", - }, - - // required: "map[]", "map[]", "map[]", "map[]" - { - validation: `required`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{"abcde":"value"}`, - invalidCase: `{{.BasicType}}{}`, - errorMessage: `{{.FieldName}} must not be empty`, - }, - { - validation: `required`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{32:64}`, - invalidCase: `{{.BasicType}}{}`, - errorMessage: `{{.FieldName}} must not be empty`, - }, - { - validation: `required`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{12.34:56.78}`, - invalidCase: `{{.BasicType}}{}`, - errorMessage: `{{.FieldName}} must not be empty`, - }, - { - validation: `required`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{true:true}`, - invalidCase: `{{.BasicType}}{}`, - errorMessage: `{{.FieldName}} must not be empty`, - }, - }, - }, - - // eq operations - { - operation: "eq", - testCases: []testCase{ - // eq: "", "", "", "" - { - validation: `eq=abcde`, - normalizedType: ``, - validCase: `"abcde"`, - invalidCase: `"fghij"`, - errorMessage: `{{.FieldName}} must be equal to 'abcde'`, - }, - { - validation: `eq=32`, - normalizedType: ``, - validCase: `32`, - invalidCase: `64`, - errorMessage: `{{.FieldName}} must be equal to 32`, - }, - { - validation: `eq=12.34`, - normalizedType: ``, - validCase: `12.34`, - invalidCase: `34.56`, - errorMessage: `{{.FieldName}} must be equal to 12.34`, - }, - { - validation: `eq=true`, - normalizedType: ``, - validCase: `true`, - invalidCase: `false`, - errorMessage: `{{.FieldName}} must be equal to true`, - }, - }, - }, - - // neq operations - { - operation: "neq", - testCases: []testCase{ - // neq: "", "", "", "" - { - validation: `neq=abcde`, - normalizedType: ``, - validCase: `"fghij"`, - invalidCase: `"abcde"`, - errorMessage: `{{.FieldName}} must not be equal to 'abcde'`, - }, - { - validation: `neq=32`, - normalizedType: ``, - validCase: `64`, - invalidCase: `32`, - errorMessage: `{{.FieldName}} must not be equal to 32`, - }, - { - validation: `neq=12.34`, - normalizedType: ``, - validCase: `34.56`, - invalidCase: `12.34`, - errorMessage: `{{.FieldName}} must not be equal to 12.34`, - }, - { - validation: `neq=true`, - normalizedType: ``, - validCase: `false`, - invalidCase: `true`, - errorMessage: `{{.FieldName}} must not be equal to true`, - }, - }, - }, - - // gt operations - { - operation: "gt", - testCases: []testCase{ - // gt: "", "" - { - validation: `gt=32`, - normalizedType: ``, - validCase: `33`, - invalidCase: `31`, - errorMessage: `{{.FieldName}} must be > 32`, - }, - { - validation: `gt=12.34`, - normalizedType: ``, - validCase: `12.35`, - invalidCase: `12.34`, - errorMessage: `{{.FieldName}} must be > 12.34`, - }, - }, - }, - - // gte operations - { - operation: "gte", - testCases: []testCase{ - // gte: "", "" - { - validation: `gte=32`, - normalizedType: ``, - validCase: `32`, - invalidCase: `31`, - errorMessage: `{{.FieldName}} must be >= 32`, - }, - { - validation: `gte=12.34`, - normalizedType: ``, - validCase: `12.34`, - invalidCase: `12.33`, - errorMessage: `{{.FieldName}} must be >= 12.34`, - }, - }, - }, - - // lt operations - { - operation: "lt", - testCases: []testCase{ - // lt: "", "" - { - validation: `lt=32`, - normalizedType: ``, - validCase: `31`, - invalidCase: `33`, - errorMessage: `{{.FieldName}} must be < 32`, - }, - { - validation: `lt=12.34`, - normalizedType: ``, - validCase: `12.33`, - invalidCase: `12.35`, - errorMessage: `{{.FieldName}} must be < 12.34`, - }, - }, - }, - - // lte operations - { - operation: "lte", - testCases: []testCase{ - // lte: "", "" - { - validation: `lte=32`, - normalizedType: ``, - validCase: `32`, - invalidCase: `33`, - errorMessage: `{{.FieldName}} must be <= 32`, - }, - { - validation: `lte=12.34`, - normalizedType: ``, - validCase: `12.34`, - invalidCase: `12.35`, - errorMessage: `{{.FieldName}} must be <= 12.34`, - }, - }, - }, - - // min operations - { - operation: "min", - testCases: []testCase{ - // min: "" - { - validation: `min=5`, - normalizedType: ``, - validCase: `"abcde"`, - invalidCase: `"abc"`, - errorMessage: `{{.FieldName}} length must be >= 5`, - }, - - // min: "[]", "[]", "[]", "[]" - { - validation: `min=2`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{"abc", "def"}`, - invalidCase: `{{.BasicType}}{"abc"}`, - errorMessage: `{{.FieldName}} must have at least 2 elements`, - }, - { - validation: `min=2`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{65, 67}`, - invalidCase: `{{.BasicType}}{65}`, - errorMessage: `{{.FieldName}} must have at least 2 elements`, - }, - { - validation: `min=2`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{65.65, 67.67}`, - invalidCase: `{{.BasicType}}{65.65}`, - errorMessage: `{{.FieldName}} must have at least 2 elements`, - }, - { - validation: `min=2`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{true, false}`, - invalidCase: `{{.BasicType}}{true}`, - errorMessage: `{{.FieldName}} must have at least 2 elements`, - }, - - // min: "map[]", "map[]", "map[]", "map[]" - { - validation: `min=2`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{"a": "1", "b": "2"}`, - invalidCase: `{{.BasicType}}{"a": "1"}`, - errorMessage: `{{.FieldName}} must have at least 2 elements`, - }, - { - validation: `min=2`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{1: 65, 2: 67}`, - invalidCase: `{{.BasicType}}{1: 65}`, - errorMessage: `{{.FieldName}} must have at least 2 elements`, - }, - { - validation: `min=2`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{1: 65.65, 2: 67.67}`, - invalidCase: `{{.BasicType}}{1: 65.65}`, - errorMessage: `{{.FieldName}} must have at least 2 elements`, - }, - { - validation: `min=2`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{true: true, false: false}`, - invalidCase: `{{.BasicType}}{true: true}`, - errorMessage: `{{.FieldName}} must have at least 2 elements`, - }, - }, - }, - - // max operations - { - operation: "max", - testCases: []testCase{ - // max: "" - { - validation: `max=3`, - normalizedType: ``, - validCase: `"abc"`, - invalidCase: `"abcde"`, - errorMessage: `{{.FieldName}} length must be <= 3`, - }, - - // max: "[]", "[]", "[]", "[]" - { - validation: `max=2`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{"abc", "def"}`, - invalidCase: `{{.BasicType}}{"abc", "def", "ghi"}`, - errorMessage: `{{.FieldName}} must have at most 2 elements`, - }, - { - validation: `max=2`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{65, 67}`, - invalidCase: `{{.BasicType}}{65, 66, 67}`, - errorMessage: `{{.FieldName}} must have at most 2 elements`, - }, - { - validation: `max=2`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{65.65, 67.67}`, - invalidCase: `{{.BasicType}}{65.65, 66.66, 67.67}`, - errorMessage: `{{.FieldName}} must have at most 2 elements`, - }, - { - validation: `max=2`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{true, false}`, - invalidCase: `{{.BasicType}}{true, false, true}`, - errorMessage: `{{.FieldName}} must have at most 2 elements`, - }, - - // max: "map[]", "map[]", "map[]", "map[]" - { - validation: `max=2`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{"a": "1", "b": "2"}`, - invalidCase: `{{.BasicType}}{"a": "1", "b": "2", "c": "3"}`, - errorMessage: `{{.FieldName}} must have at most 2 elements`, - }, - { - validation: `max=2`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{1: 65, 2: 67}`, - invalidCase: `{{.BasicType}}{1: 65, 2: 67, 3: 68}`, - errorMessage: `{{.FieldName}} must have at most 2 elements`, - }, - { - validation: `max=2`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{1: 65.65, 2: 67.67}`, - invalidCase: `{{.BasicType}}{1: 65.65, 2: 66.66, 3: 67.67}`, - errorMessage: `{{.FieldName}} must have at most 2 elements`, - }, - { - validation: `max=1`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{true: true}`, - invalidCase: `{{.BasicType}}{true: true, false: false}`, - errorMessage: `{{.FieldName}} must have at most 1 elements`, - }, - }, - }, - - // eq_ignore_case operations - { - operation: "eq_ignore_case", - testCases: []testCase{ - // eq_ignore_case: "" - { - validation: `eq_ignore_case=abcde`, - normalizedType: ``, - validCase: `"AbCdE"`, - invalidCase: `"a1b2c3"`, - errorMessage: `{{.FieldName}} must be equal to 'abcde'`, - }, - }, - }, - - // neq_ignore_case operations - { - operation: "neq_ignore_case", - testCases: []testCase{ - // neq_ignore_case: "" - { - validation: `neq_ignore_case=abcde`, - normalizedType: ``, - validCase: `"a1b2c3"`, - invalidCase: `"AbCdE"`, - errorMessage: `{{.FieldName}} must not be equal to 'abcde'`, - }, - }, - }, - - // len operations - { - operation: "len", - testCases: []testCase{ - // len: "" - { - validation: `len=2`, - normalizedType: ``, - validCase: `"ab"`, - invalidCase: `"abcde"`, - errorMessage: `{{.FieldName}} length must be 2`, - }, - - // len: "[]", "[]", "[]", "[]" - { - validation: `len=2`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{"abc", "def"}`, - invalidCase: `{{.BasicType}}{"abc", "def", "ghi"}`, - errorMessage: `{{.FieldName}} must have exactly 2 elements`, - }, - { - validation: `len=2`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{65, 67}`, - invalidCase: `{{.BasicType}}{65, 66, 67}`, - errorMessage: `{{.FieldName}} must have exactly 2 elements`, - }, - { - validation: `len=2`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{65.65, 67.67}`, - invalidCase: `{{.BasicType}}{65.65, 66.66, 67.67}`, - errorMessage: `{{.FieldName}} must have exactly 2 elements`, - }, - { - validation: `len=2`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{true, false}`, - invalidCase: `{{.BasicType}}{true, false, true}`, - errorMessage: `{{.FieldName}} must have exactly 2 elements`, - }, - - // len: "map[]", "map[]", "map[]", "map[]" - { - validation: `len=2`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{"a": "1", "b": "2"}`, - invalidCase: `{{.BasicType}}{"a": "1", "b": "2", "c": "3"}`, - errorMessage: `{{.FieldName}} must have exactly 2 elements`, - }, - { - validation: `len=2`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{1: 65, 2: 67}`, - invalidCase: `{{.BasicType}}{1: 65, 2: 67, 3: 68}`, - errorMessage: `{{.FieldName}} must have exactly 2 elements`, - }, - { - validation: `len=2`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{1: 65.65, 2: 67.67}`, - invalidCase: `{{.BasicType}}{1: 65.65, 2: 66.66, 3: 67.67}`, - errorMessage: `{{.FieldName}} must have exactly 2 elements`, - }, - { - validation: `len=2`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{true: true, false: false}`, - invalidCase: `{{.BasicType}}{true: true}`, - errorMessage: `{{.FieldName}} must have exactly 2 elements`, - }, - }, - }, - - // in operations - { - operation: "in", - testCases: []testCase{ - // in: "", "", "", "" - { - validation: `in=ab cd ef`, - normalizedType: ``, - validCase: `"cd"`, - invalidCase: `"fg"`, - errorMessage: `{{.FieldName}} must be one of 'ab' 'cd' 'ef'`, - }, - { - validation: `in=12 34 56`, - normalizedType: ``, - validCase: `34`, - invalidCase: `78`, - errorMessage: `{{.FieldName}} must be one of '12' '34' '56'`, - }, - { - validation: `in=11.11 22.22 33.33`, - normalizedType: ``, - validCase: `22.22`, - invalidCase: `44.44`, - errorMessage: `{{.FieldName}} must be one of '11.11' '22.22' '33.33'`, - }, - { - validation: `in=true`, - normalizedType: ``, - validCase: `true`, - invalidCase: `false`, - errorMessage: `{{.FieldName}} must be one of 'true'`, - }, - - // in: "[]", "[]", "[]", "[]" - { - validation: `in=ab cd ef`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{"ab", "ef"}`, - invalidCase: `{{.BasicType}}{"ab", "gh", "ef"}`, - errorMessage: `{{.FieldName}} elements must be one of 'ab' 'cd' 'ef'`, - }, - { - validation: `in=12 34 56`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{12, 56}`, - invalidCase: `{{.BasicType}}{12, 78, 56}`, - errorMessage: `{{.FieldName}} elements must be one of '12' '34' '56'`, - }, - { - validation: `in=11.11 22.22 33.33`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{11.11, 22.22}`, - invalidCase: `{{.BasicType}}{11.11, 44.44, 33.33}`, - errorMessage: `{{.FieldName}} elements must be one of '11.11' '22.22' '33.33'`, - }, - { - validation: `in=true`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{true, true}`, - invalidCase: `{{.BasicType}}{true, false, true}`, - errorMessage: `{{.FieldName}} elements must be one of 'true'`, - }, - - // in: "[]", "[]", "[]", "[]" - { - validation: `in=ab cd ef`, - normalizedType: `[N]`, - validCase: `{{.BasicType}}{"ab", "ef", "ab"}`, - invalidCase: `{{.BasicType}}{"ab", "gh", "ef"}`, - errorMessage: `{{.FieldName}} elements must be one of 'ab' 'cd' 'ef'`, - }, - { - validation: `in=12 34 56`, - normalizedType: `[N]`, - validCase: `{{.BasicType}}{12, 56, 12}`, - invalidCase: `{{.BasicType}}{12, 78, 56}`, - errorMessage: `{{.FieldName}} elements must be one of '12' '34' '56'`, - }, - { - validation: `in=11.11 22.22 33.33`, - normalizedType: `[N]`, - validCase: `{{.BasicType}}{11.11, 22.22, 11.11}`, - invalidCase: `{{.BasicType}}{11.11, 44.44, 33.33}`, - errorMessage: `{{.FieldName}} elements must be one of '11.11' '22.22' '33.33'`, - }, - { - validation: `in=true`, - normalizedType: `[N]`, - validCase: `{{.BasicType}}{true, true, true}`, - invalidCase: `{{.BasicType}}{true, false, true}`, - errorMessage: `{{.FieldName}} elements must be one of 'true'`, - }, - - // in: "map[]", "map[]", "map[]", "map[]" - { - validation: `in=a b c`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{"a": "1", "b": "2", "c": "3"}`, - invalidCase: `{{.BasicType}}{"a": "1", "d": "9", "c": "3"}`, - errorMessage: `{{.FieldName}} elements must be one of 'a' 'b' 'c'`, - }, - { - validation: `in=1 2 3`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{1: 65, 2: 67, 3: 68}`, - invalidCase: `{{.BasicType}}{1: 65, 4: 69, 3: 68}`, - errorMessage: `{{.FieldName}} elements must be one of '1' '2' '3'`, - }, - { - validation: `in=11.11 22.22 33.33`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{11.11: 11.11, 22.22: 22.22, 33.33: 33.33}`, - invalidCase: `{{.BasicType}}{11.11: 11.11, 44.44: 44.44, 33.33: 33.33}`, - errorMessage: `{{.FieldName}} elements must be one of '11.11' '22.22' '33.33'`, - }, - { - validation: `in=false`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{false: false}`, - invalidCase: `{{.BasicType}}{true: true, false: false}`, - errorMessage: `{{.FieldName}} elements must be one of 'false'`, - }, - }, - }, - - // nin operations - { - operation: "nin", - testCases: []testCase{ - // nin: "[]", "[]", "[]", "[]" - { - validation: `nin=ab cd ef`, - normalizedType: ``, - validCase: `"fg"`, - invalidCase: `"cd"`, - errorMessage: `{{.FieldName}} must not be one of 'ab' 'cd' 'ef'`, - }, - { - validation: `nin=12 34 56`, - normalizedType: ``, - validCase: `78`, - invalidCase: `34`, - errorMessage: `{{.FieldName}} must not be one of '12' '34' '56'`, - }, - { - validation: `nin=11.11 22.22 33.33`, - normalizedType: ``, - validCase: `44.44`, - invalidCase: `22.22`, - errorMessage: `{{.FieldName}} must not be one of '11.11' '22.22' '33.33'`, - }, - { - validation: `nin=true`, - normalizedType: ``, - validCase: `false`, - invalidCase: `true`, - errorMessage: `{{.FieldName}} must not be one of 'true'`, - }, - - // nin: "[]", "[]", "[]", "[]" - { - validation: `nin=ab cd ef`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{"gh", "ij", "kl"}`, - invalidCase: `{{.BasicType}}{"ab", "ef"}`, - errorMessage: `{{.FieldName}} elements must not be one of 'ab' 'cd' 'ef'`, - }, - { - validation: `nin=12 34 56`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{78, 91}`, - invalidCase: `{{.BasicType}}{12, 78, 56}`, - errorMessage: `{{.FieldName}} elements must not be one of '12' '34' '56'`, - }, - { - validation: `nin=11.11 22.22 33.33`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{44.44, 55.55, 66.66}`, - invalidCase: `{{.BasicType}}{11.11, 44.44, 33.33}`, - errorMessage: `{{.FieldName}} elements must not be one of '11.11' '22.22' '33.33'`, - }, - { - validation: `nin=true`, - normalizedType: `[]`, - validCase: `{{.BasicType}}{false, false, false}`, - invalidCase: `{{.BasicType}}{true, false, true}`, - errorMessage: `{{.FieldName}} elements must not be one of 'true'`, - }, - - // nin: "[]", "[]", "[]", "[]" - { - validation: `nin=ab cd ef`, - normalizedType: `[N]`, - validCase: `{{.BasicType}}{"gh", "ij", "kl"}`, - invalidCase: `{{.BasicType}}{"ab", "gh", "ef"}`, - errorMessage: `{{.FieldName}} elements must not be one of 'ab' 'cd' 'ef'`, - }, - { - validation: `nin=12 34 56`, - normalizedType: `[N]`, - validCase: `{{.BasicType}}{78, 91, 23}`, - invalidCase: `{{.BasicType}}{12, 78, 56}`, - errorMessage: `{{.FieldName}} elements must not be one of '12' '34' '56'`, - }, - { - validation: `nin=11.11 22.22 33.33`, - normalizedType: `[N]`, - validCase: `{{.BasicType}}{44.44, 55.55, 66.66}`, - invalidCase: `{{.BasicType}}{11.11, 44.44, 33.33}`, - errorMessage: `{{.FieldName}} elements must not be one of '11.11' '22.22' '33.33'`, - }, - { - validation: `nin=true`, - normalizedType: `[N]`, - validCase: `{{.BasicType}}{false, false, false}`, - invalidCase: `{{.BasicType}}{true, false, true}`, - errorMessage: `{{.FieldName}} elements must not be one of 'true'`, - }, - - // nin: "map[]", "map[]", "map[]", "map[]" - { - validation: `nin=a b c`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{"d": "1", "e": "2", "f": "3"}`, - invalidCase: `{{.BasicType}}{"a": "1", "d": "9", "c": "3"}`, - errorMessage: `{{.FieldName}} elements must not be one of 'a' 'b' 'c'`, - }, - { - validation: `nin=1 2 3`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{5: 55, 6: 66, 7: 77}`, - invalidCase: `{{.BasicType}}{1: 11, 4: 44, 3: 33}`, - errorMessage: `{{.FieldName}} elements must not be one of '1' '2' '3'`, - }, - { - validation: `nin=11.11 22.22 33.33`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{44.44: 44.44, 55.55: 55.55, 66.66: 66.66}`, - invalidCase: `{{.BasicType}}{11.11: 11.11, 44.44: 44.44, 33.33: 33.33}`, - errorMessage: `{{.FieldName}} elements must not be one of '11.11' '22.22' '33.33'`, - }, - { - validation: `nin=false`, - normalizedType: `map[]`, - validCase: `{{.BasicType}}{true: true}`, - invalidCase: `{{.BasicType}}{true: true, false: false}`, - errorMessage: `{{.FieldName}} elements must not be one of 'false'`, - }, - }, - }, -} - -type AllTestCasesToGenerate struct { - TestCases []TestCaseToGenerate -} - -type TestCaseToGenerate struct { - StructName string - Tests []TestCase -} - -type TestCase struct { - FieldName string - Validation string - FieldType string - BasicType string - ValidCase string - InvalidCase string - ErrorMessage string -} - -func generateTestCases() { - generateTestCasesFile("no_pointer_tests.tpl", "generated_no_pointer_tests.go", false) - generateTestCasesFile("pointer_tests.tpl", "generated_pointer_tests.go", true) -} - -func generateTestCasesFile(tpl, dest string, pointer bool) { - log.Printf("Generating test cases file: tpl[%s] dest[%s] pointer[%v]\n", tpl, dest, pointer) - - allTestsToGenerate := AllTestCasesToGenerate{} - - for _, testCase := range testCases { - structName := testCase.operation + "StructFields" - if pointer { - structName += "Pointer" - } - allTestsToGenerate.TestCases = append(allTestsToGenerate.TestCases, TestCaseToGenerate{ - StructName: structName, - }) - for _, toGenerate := range testCase.testCases { - // Default ("") gen no pointer and pointer test. - if toGenerate.generateOnly != "" { - if toGenerate.generateOnly == "pointer" && !pointer { - continue - } - if toGenerate.generateOnly == "nopointer" && pointer { - continue - } - } - normalizedType := toGenerate.normalizedType - if pointer { - normalizedType = "*" + normalizedType - } - fTypes := common.HelperFromNormalizedToBasicTypes(normalizedType) - sNames := common.HelperFromNormalizedToStringNames(normalizedType) - for i := range fTypes { - op, _, _ := strings.Cut(toGenerate.validation, "=") - fieldName := "Field" + cases.Title(language.Und).String(op) + sNames[i] - basicType, _ := strings.CutPrefix(fTypes[i], "*") - allTestsToGenerate.TestCases[len(allTestsToGenerate.TestCases)-1].Tests = append(allTestsToGenerate.TestCases[len(allTestsToGenerate.TestCases)-1].Tests, TestCase{ - FieldName: fieldName, - Validation: toGenerate.validation, - FieldType: fTypes[i], - BasicType: basicType, - ValidCase: strings.ReplaceAll(toGenerate.validCase, "{{.BasicType}}", basicType), - InvalidCase: strings.ReplaceAll(toGenerate.invalidCase, "{{.BasicType}}", basicType), - ErrorMessage: strings.ReplaceAll(toGenerate.errorMessage, "{{.FieldName}}", fieldName), - }) - } - } - } - - if err := allTestsToGenerate.GenerateFile(tpl, dest); err != nil { - log.Fatalf("error generation usecases file %s", err) - - } - - log.Printf("Generating %s done\n", dest) -} - -func (at *AllTestCasesToGenerate) GenerateFile(tplFile, output string) error { - tpl, err := os.ReadFile(tplFile) - if err != nil { - return fmt.Errorf("error reading %s: %s", tplFile, err) - } - - tmpl, err := template.New("UsecaseTests").Parse(string(tpl)) - if err != nil { - return err - } - - code := new(bytes.Buffer) - if err := tmpl.Execute(code, at); err != nil { - return err - } - - formattedCode, err := format.Source(code.Bytes()) - if err != nil { - return err - } - - if err := os.WriteFile(output, formattedCode, 0644); err != nil { - return err - } - - return nil -} diff --git a/tests/endtoend/generate_tests/no_pointer_tests.tpl b/tests/endtoend/generate_tests/no_pointer_tests.tpl deleted file mode 100644 index 8badfdf..0000000 --- a/tests/endtoend/generate_tests/no_pointer_tests.tpl +++ /dev/null @@ -1,50 +0,0 @@ -// Code generated by TestGenerator. DO NOT EDIT. - -package main - -import ( - "log" -) - -func noPointerTests() { -{{range .TestCases}}{{.StructName}}Tests() -{{end}} -} - -{{range .TestCases}} - -type {{.StructName}} struct { - {{range .Tests}}{{.FieldName}} {{.FieldType}} `valid:"{{.Validation}}"` - {{end}} -} - -func {{.StructName}}Tests() { - log.Println("starting {{.StructName}} types tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios (invalid cases) - v := &{{.StructName}}{} - expectedMsgErrors = []string{ - {{range .Tests}}"{{.ErrorMessage}}", - {{end}} - } - - {{range .Tests}}{{ if ne .InvalidCase "--" }}v.{{.FieldName}} = {{.InvalidCase}}{{end}} - {{end}} - errs = {{.StructName}}Validate(v) - assertExpectedErrorMsgs("testcase 1", errs, expectedMsgErrors) - - // Test case 2: All valid cases - v = &{{.StructName}}{} - {{range .Tests}}v.{{.FieldName}} = {{.ValidCase}} - {{end}} - expectedMsgErrors = nil - errs = {{.StructName}}Validate(v) - assertExpectedErrorMsgs("testcase 2", errs, expectedMsgErrors) - - log.Println("{{.StructName}} types tests ok") -} - -{{end}} \ No newline at end of file diff --git a/tests/endtoend/generate_tests/numeric_float.tpl b/tests/endtoend/generate_tests/numeric_float.tpl deleted file mode 100644 index ff3d884..0000000 --- a/tests/endtoend/generate_tests/numeric_float.tpl +++ /dev/null @@ -1,81 +0,0 @@ -// Code generated by TestGenerator. DO NOT EDIT. - -package main - -import "log" - -func numericFloatTypeTests() { - log.Println("starting numeric float tests") - - {{range .FieldTypes}}numeric{{. | title }}Tests() - {{end}} - log.Println("numeric float tests ok") -} -{{range .FieldTypes}} -type NumericType{{. | title }} struct { - FieldReq {{.}} `valid:"required"` - FieldEq {{.}} `valid:"eq=5.9"` - FieldNeq {{.}} `valid:"neq=5.9"` - FieldGt {{.}} `valid:"gt=10.1"` - FieldGte {{.}} `valid:"gte=10.1"` - FieldLt {{.}} `valid:"lt=9.9"` - FieldLte {{.}} `valid:"lte=9.9"` - FieldIn {{.}} `valid:"in=5.1 6.2 7.3"` - FieldNotIn {{.}} `valid:"nin=8.5 9.6 10.7"` -} - -func numeric{{. | title }}Tests() { - log.Println("starting numeric {{.}} tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &NumericType{{. | title }}{ - FieldReq: 0, - FieldEq: 1.2, - FieldNeq: 5.9, - FieldGt: 8.1, - FieldGte: 8.1, - FieldLt: 12.2, - FieldLte: 12.2, - FieldIn: 3.123, - FieldNotIn: 9.6, - } - expectedMsgErrors = []string{ - "FieldReq is required", - "FieldEq must be equal to 5.9", - "FieldNeq must not be equal to 5.9", - "FieldGt must be > 10.1", - "FieldGte must be >= 10.1", - "FieldLt must be < 9.9", - "FieldLte must be <= 9.9", - "FieldIn must be one of '5.1' '6.2' '7.3'", - "FieldNotIn must not be one of '8.5' '9.6' '10.7'", - } - errs = NumericType{{. | title }}Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &NumericType{{. | title }}{ - FieldReq: 123, - FieldEq: 5.9, - FieldNeq: 2.4, - FieldGt: 11.1, - FieldGte: 12.1, - FieldLt: 9.1, - FieldLte: 8.1, - FieldIn: 6.2, - FieldNotIn: 12.4, - } - expectedMsgErrors = nil - errs = NumericType{{. | title }}Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("numeric {{.}} tests ok") -} -{{end}} \ No newline at end of file diff --git a/tests/endtoend/generate_tests/numeric_int.tpl b/tests/endtoend/generate_tests/numeric_int.tpl deleted file mode 100644 index cc28723..0000000 --- a/tests/endtoend/generate_tests/numeric_int.tpl +++ /dev/null @@ -1,81 +0,0 @@ -// Code generated by TestGenerator. DO NOT EDIT. - -package main - -import "log" - -func numericIntTypeTests() { - log.Println("starting numeric int tests") - - {{range .FieldTypes}}numeric{{. | title }}Tests() - {{end}} - log.Println("numeric int tests ok") -} -{{range .FieldTypes}} -type NumericType{{. | title }} struct { - FieldReq {{.}} `valid:"required"` - FieldEq {{.}} `valid:"eq=5"` - FieldNeq {{.}} `valid:"neq=5"` - FieldGt {{.}} `valid:"gt=10"` - FieldGte {{.}} `valid:"gte=10"` - FieldLt {{.}} `valid:"lt=10"` - FieldLte {{.}} `valid:"lte=10"` - FieldIn {{.}} `valid:"in=5 6 7"` - FieldNotIn {{.}} `valid:"nin=8 9 10"` -} - -func numeric{{. | title }}Tests() { - log.Println("starting numeric {{.}} tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &NumericType{{. | title }}{ - FieldReq: 0, - FieldEq: 0, - FieldNeq: 5, - FieldGt: 8, - FieldGte: 8, - FieldLt: 12, - FieldLte: 12, - FieldIn: 3, - FieldNotIn: 9, - } - expectedMsgErrors = []string{ - "FieldReq is required", - "FieldEq must be equal to 5", - "FieldNeq must not be equal to 5", - "FieldGt must be > 10", - "FieldGte must be >= 10", - "FieldLt must be < 10", - "FieldLte must be <= 10", - "FieldIn must be one of '5' '6' '7'", - "FieldNotIn must not be one of '8' '9' '10'", - } - errs = NumericType{{. | title }}Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &NumericType{{. | title }}{ - FieldReq: 123, - FieldEq: 5, - FieldNeq: 2, - FieldGt: 11, - FieldGte: 12, - FieldLt: 9, - FieldLte: 8, - FieldIn: 6, - FieldNotIn: 12, - } - expectedMsgErrors = nil - errs = NumericType{{. | title }}Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("numeric {{.}} tests ok") -} -{{end}} \ No newline at end of file diff --git a/tests/endtoend/generate_tests/pointer_tests.tpl b/tests/endtoend/generate_tests/pointer_tests.tpl deleted file mode 100644 index 6528758..0000000 --- a/tests/endtoend/generate_tests/pointer_tests.tpl +++ /dev/null @@ -1,58 +0,0 @@ -// Code generated by TestGenerator. DO NOT EDIT. - -package main - -import ( - "log" -) - -func pointerTests() { -{{range .TestCases}}{{.StructName}}Tests() -{{end}} -} - -{{range .TestCases}} - -type {{.StructName}} struct { - {{range .Tests}}{{.FieldName}} {{.FieldType}} `valid:"{{.Validation}}"` - {{end}} -} - -func {{.StructName}}Tests() { - log.Println("starting {{.StructName}} types tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios (nil values) - v := &{{.StructName}}{} - expectedMsgErrors = []string{ - {{range .Tests}}"{{.ErrorMessage}}", - {{end}} - } - errs = {{.StructName}}Validate(v) - assertExpectedErrorMsgs("testcase 1", errs, expectedMsgErrors) - - // Test case 2: All failure scenarios (invalid cases) - {{range .Tests}}{{ if ne .InvalidCase "--" }}var Invalid{{.FieldName}} {{.BasicType}} = {{.InvalidCase}}{{end}} - {{end}} - v = &{{.StructName}}{} - {{range .Tests}}{{ if ne .InvalidCase "--" }}v.{{.FieldName}} = &Invalid{{.FieldName}}{{end}} - {{end}} - errs = {{.StructName}}Validate(v) - assertExpectedErrorMsgs("testcase 2", errs, expectedMsgErrors) - - // Test case 3: All valid cases - {{range .Tests}}var Valid{{.FieldName}} {{.BasicType}} = {{.ValidCase}} - {{end}} - v = &{{.StructName}}{} - {{range .Tests}}v.{{.FieldName}} = &Valid{{.FieldName}} - {{end}} - expectedMsgErrors = nil - errs = {{.StructName}}Validate(v) - assertExpectedErrorMsgs("testcase 3", errs, expectedMsgErrors) - - log.Println("{{.StructName}} types tests ok") -} - -{{end}} \ No newline at end of file diff --git a/tests/endtoend/generated_numeric_float_tests.go b/tests/endtoend/generated_numeric_float_tests.go deleted file mode 100644 index 1d45479..0000000 --- a/tests/endtoend/generated_numeric_float_tests.go +++ /dev/null @@ -1,148 +0,0 @@ -// Code generated by TestGenerator. DO NOT EDIT. - -package main - -import "log" - -func numericFloatTypeTests() { - log.Println("starting numeric float tests") - - numericFloat32Tests() - numericFloat64Tests() - - log.Println("numeric float tests ok") -} - -type NumericTypeFloat32 struct { - FieldReq float32 `valid:"required"` - FieldEq float32 `valid:"eq=5.9"` - FieldNeq float32 `valid:"neq=5.9"` - FieldGt float32 `valid:"gt=10.1"` - FieldGte float32 `valid:"gte=10.1"` - FieldLt float32 `valid:"lt=9.9"` - FieldLte float32 `valid:"lte=9.9"` - FieldIn float32 `valid:"in=5.1 6.2 7.3"` - FieldNotIn float32 `valid:"nin=8.5 9.6 10.7"` -} - -func numericFloat32Tests() { - log.Println("starting numeric float32 tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &NumericTypeFloat32{ - FieldReq: 0, - FieldEq: 1.2, - FieldNeq: 5.9, - FieldGt: 8.1, - FieldGte: 8.1, - FieldLt: 12.2, - FieldLte: 12.2, - FieldIn: 3.123, - FieldNotIn: 9.6, - } - expectedMsgErrors = []string{ - "FieldReq is required", - "FieldEq must be equal to 5.9", - "FieldNeq must not be equal to 5.9", - "FieldGt must be > 10.1", - "FieldGte must be >= 10.1", - "FieldLt must be < 9.9", - "FieldLte must be <= 9.9", - "FieldIn must be one of '5.1' '6.2' '7.3'", - "FieldNotIn must not be one of '8.5' '9.6' '10.7'", - } - errs = NumericTypeFloat32Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &NumericTypeFloat32{ - FieldReq: 123, - FieldEq: 5.9, - FieldNeq: 2.4, - FieldGt: 11.1, - FieldGte: 12.1, - FieldLt: 9.1, - FieldLte: 8.1, - FieldIn: 6.2, - FieldNotIn: 12.4, - } - expectedMsgErrors = nil - errs = NumericTypeFloat32Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("numeric float32 tests ok") -} - -type NumericTypeFloat64 struct { - FieldReq float64 `valid:"required"` - FieldEq float64 `valid:"eq=5.9"` - FieldNeq float64 `valid:"neq=5.9"` - FieldGt float64 `valid:"gt=10.1"` - FieldGte float64 `valid:"gte=10.1"` - FieldLt float64 `valid:"lt=9.9"` - FieldLte float64 `valid:"lte=9.9"` - FieldIn float64 `valid:"in=5.1 6.2 7.3"` - FieldNotIn float64 `valid:"nin=8.5 9.6 10.7"` -} - -func numericFloat64Tests() { - log.Println("starting numeric float64 tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &NumericTypeFloat64{ - FieldReq: 0, - FieldEq: 1.2, - FieldNeq: 5.9, - FieldGt: 8.1, - FieldGte: 8.1, - FieldLt: 12.2, - FieldLte: 12.2, - FieldIn: 3.123, - FieldNotIn: 9.6, - } - expectedMsgErrors = []string{ - "FieldReq is required", - "FieldEq must be equal to 5.9", - "FieldNeq must not be equal to 5.9", - "FieldGt must be > 10.1", - "FieldGte must be >= 10.1", - "FieldLt must be < 9.9", - "FieldLte must be <= 9.9", - "FieldIn must be one of '5.1' '6.2' '7.3'", - "FieldNotIn must not be one of '8.5' '9.6' '10.7'", - } - errs = NumericTypeFloat64Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &NumericTypeFloat64{ - FieldReq: 123, - FieldEq: 5.9, - FieldNeq: 2.4, - FieldGt: 11.1, - FieldGte: 12.1, - FieldLt: 9.1, - FieldLte: 8.1, - FieldIn: 6.2, - FieldNotIn: 12.4, - } - expectedMsgErrors = nil - errs = NumericTypeFloat64Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("numeric float64 tests ok") -} diff --git a/tests/endtoend/generated_numeric_int_tests.go b/tests/endtoend/generated_numeric_int_tests.go deleted file mode 100644 index 81d62c7..0000000 --- a/tests/endtoend/generated_numeric_int_tests.go +++ /dev/null @@ -1,692 +0,0 @@ -// Code generated by TestGenerator. DO NOT EDIT. - -package main - -import "log" - -func numericIntTypeTests() { - log.Println("starting numeric int tests") - - numericIntTests() - numericInt8Tests() - numericInt16Tests() - numericInt32Tests() - numericInt64Tests() - numericUintTests() - numericUint8Tests() - numericUint16Tests() - numericUint32Tests() - numericUint64Tests() - - log.Println("numeric int tests ok") -} - -type NumericTypeInt struct { - FieldReq int `valid:"required"` - FieldEq int `valid:"eq=5"` - FieldNeq int `valid:"neq=5"` - FieldGt int `valid:"gt=10"` - FieldGte int `valid:"gte=10"` - FieldLt int `valid:"lt=10"` - FieldLte int `valid:"lte=10"` - FieldIn int `valid:"in=5 6 7"` - FieldNotIn int `valid:"nin=8 9 10"` -} - -func numericIntTests() { - log.Println("starting numeric int tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &NumericTypeInt{ - FieldReq: 0, - FieldEq: 0, - FieldNeq: 5, - FieldGt: 8, - FieldGte: 8, - FieldLt: 12, - FieldLte: 12, - FieldIn: 3, - FieldNotIn: 9, - } - expectedMsgErrors = []string{ - "FieldReq is required", - "FieldEq must be equal to 5", - "FieldNeq must not be equal to 5", - "FieldGt must be > 10", - "FieldGte must be >= 10", - "FieldLt must be < 10", - "FieldLte must be <= 10", - "FieldIn must be one of '5' '6' '7'", - "FieldNotIn must not be one of '8' '9' '10'", - } - errs = NumericTypeIntValidate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &NumericTypeInt{ - FieldReq: 123, - FieldEq: 5, - FieldNeq: 2, - FieldGt: 11, - FieldGte: 12, - FieldLt: 9, - FieldLte: 8, - FieldIn: 6, - FieldNotIn: 12, - } - expectedMsgErrors = nil - errs = NumericTypeIntValidate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("numeric int tests ok") -} - -type NumericTypeInt8 struct { - FieldReq int8 `valid:"required"` - FieldEq int8 `valid:"eq=5"` - FieldNeq int8 `valid:"neq=5"` - FieldGt int8 `valid:"gt=10"` - FieldGte int8 `valid:"gte=10"` - FieldLt int8 `valid:"lt=10"` - FieldLte int8 `valid:"lte=10"` - FieldIn int8 `valid:"in=5 6 7"` - FieldNotIn int8 `valid:"nin=8 9 10"` -} - -func numericInt8Tests() { - log.Println("starting numeric int8 tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &NumericTypeInt8{ - FieldReq: 0, - FieldEq: 0, - FieldNeq: 5, - FieldGt: 8, - FieldGte: 8, - FieldLt: 12, - FieldLte: 12, - FieldIn: 3, - FieldNotIn: 9, - } - expectedMsgErrors = []string{ - "FieldReq is required", - "FieldEq must be equal to 5", - "FieldNeq must not be equal to 5", - "FieldGt must be > 10", - "FieldGte must be >= 10", - "FieldLt must be < 10", - "FieldLte must be <= 10", - "FieldIn must be one of '5' '6' '7'", - "FieldNotIn must not be one of '8' '9' '10'", - } - errs = NumericTypeInt8Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &NumericTypeInt8{ - FieldReq: 123, - FieldEq: 5, - FieldNeq: 2, - FieldGt: 11, - FieldGte: 12, - FieldLt: 9, - FieldLte: 8, - FieldIn: 6, - FieldNotIn: 12, - } - expectedMsgErrors = nil - errs = NumericTypeInt8Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("numeric int8 tests ok") -} - -type NumericTypeInt16 struct { - FieldReq int16 `valid:"required"` - FieldEq int16 `valid:"eq=5"` - FieldNeq int16 `valid:"neq=5"` - FieldGt int16 `valid:"gt=10"` - FieldGte int16 `valid:"gte=10"` - FieldLt int16 `valid:"lt=10"` - FieldLte int16 `valid:"lte=10"` - FieldIn int16 `valid:"in=5 6 7"` - FieldNotIn int16 `valid:"nin=8 9 10"` -} - -func numericInt16Tests() { - log.Println("starting numeric int16 tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &NumericTypeInt16{ - FieldReq: 0, - FieldEq: 0, - FieldNeq: 5, - FieldGt: 8, - FieldGte: 8, - FieldLt: 12, - FieldLte: 12, - FieldIn: 3, - FieldNotIn: 9, - } - expectedMsgErrors = []string{ - "FieldReq is required", - "FieldEq must be equal to 5", - "FieldNeq must not be equal to 5", - "FieldGt must be > 10", - "FieldGte must be >= 10", - "FieldLt must be < 10", - "FieldLte must be <= 10", - "FieldIn must be one of '5' '6' '7'", - "FieldNotIn must not be one of '8' '9' '10'", - } - errs = NumericTypeInt16Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &NumericTypeInt16{ - FieldReq: 123, - FieldEq: 5, - FieldNeq: 2, - FieldGt: 11, - FieldGte: 12, - FieldLt: 9, - FieldLte: 8, - FieldIn: 6, - FieldNotIn: 12, - } - expectedMsgErrors = nil - errs = NumericTypeInt16Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("numeric int16 tests ok") -} - -type NumericTypeInt32 struct { - FieldReq int32 `valid:"required"` - FieldEq int32 `valid:"eq=5"` - FieldNeq int32 `valid:"neq=5"` - FieldGt int32 `valid:"gt=10"` - FieldGte int32 `valid:"gte=10"` - FieldLt int32 `valid:"lt=10"` - FieldLte int32 `valid:"lte=10"` - FieldIn int32 `valid:"in=5 6 7"` - FieldNotIn int32 `valid:"nin=8 9 10"` -} - -func numericInt32Tests() { - log.Println("starting numeric int32 tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &NumericTypeInt32{ - FieldReq: 0, - FieldEq: 0, - FieldNeq: 5, - FieldGt: 8, - FieldGte: 8, - FieldLt: 12, - FieldLte: 12, - FieldIn: 3, - FieldNotIn: 9, - } - expectedMsgErrors = []string{ - "FieldReq is required", - "FieldEq must be equal to 5", - "FieldNeq must not be equal to 5", - "FieldGt must be > 10", - "FieldGte must be >= 10", - "FieldLt must be < 10", - "FieldLte must be <= 10", - "FieldIn must be one of '5' '6' '7'", - "FieldNotIn must not be one of '8' '9' '10'", - } - errs = NumericTypeInt32Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &NumericTypeInt32{ - FieldReq: 123, - FieldEq: 5, - FieldNeq: 2, - FieldGt: 11, - FieldGte: 12, - FieldLt: 9, - FieldLte: 8, - FieldIn: 6, - FieldNotIn: 12, - } - expectedMsgErrors = nil - errs = NumericTypeInt32Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("numeric int32 tests ok") -} - -type NumericTypeInt64 struct { - FieldReq int64 `valid:"required"` - FieldEq int64 `valid:"eq=5"` - FieldNeq int64 `valid:"neq=5"` - FieldGt int64 `valid:"gt=10"` - FieldGte int64 `valid:"gte=10"` - FieldLt int64 `valid:"lt=10"` - FieldLte int64 `valid:"lte=10"` - FieldIn int64 `valid:"in=5 6 7"` - FieldNotIn int64 `valid:"nin=8 9 10"` -} - -func numericInt64Tests() { - log.Println("starting numeric int64 tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &NumericTypeInt64{ - FieldReq: 0, - FieldEq: 0, - FieldNeq: 5, - FieldGt: 8, - FieldGte: 8, - FieldLt: 12, - FieldLte: 12, - FieldIn: 3, - FieldNotIn: 9, - } - expectedMsgErrors = []string{ - "FieldReq is required", - "FieldEq must be equal to 5", - "FieldNeq must not be equal to 5", - "FieldGt must be > 10", - "FieldGte must be >= 10", - "FieldLt must be < 10", - "FieldLte must be <= 10", - "FieldIn must be one of '5' '6' '7'", - "FieldNotIn must not be one of '8' '9' '10'", - } - errs = NumericTypeInt64Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &NumericTypeInt64{ - FieldReq: 123, - FieldEq: 5, - FieldNeq: 2, - FieldGt: 11, - FieldGte: 12, - FieldLt: 9, - FieldLte: 8, - FieldIn: 6, - FieldNotIn: 12, - } - expectedMsgErrors = nil - errs = NumericTypeInt64Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("numeric int64 tests ok") -} - -type NumericTypeUint struct { - FieldReq uint `valid:"required"` - FieldEq uint `valid:"eq=5"` - FieldNeq uint `valid:"neq=5"` - FieldGt uint `valid:"gt=10"` - FieldGte uint `valid:"gte=10"` - FieldLt uint `valid:"lt=10"` - FieldLte uint `valid:"lte=10"` - FieldIn uint `valid:"in=5 6 7"` - FieldNotIn uint `valid:"nin=8 9 10"` -} - -func numericUintTests() { - log.Println("starting numeric uint tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &NumericTypeUint{ - FieldReq: 0, - FieldEq: 0, - FieldNeq: 5, - FieldGt: 8, - FieldGte: 8, - FieldLt: 12, - FieldLte: 12, - FieldIn: 3, - FieldNotIn: 9, - } - expectedMsgErrors = []string{ - "FieldReq is required", - "FieldEq must be equal to 5", - "FieldNeq must not be equal to 5", - "FieldGt must be > 10", - "FieldGte must be >= 10", - "FieldLt must be < 10", - "FieldLte must be <= 10", - "FieldIn must be one of '5' '6' '7'", - "FieldNotIn must not be one of '8' '9' '10'", - } - errs = NumericTypeUintValidate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &NumericTypeUint{ - FieldReq: 123, - FieldEq: 5, - FieldNeq: 2, - FieldGt: 11, - FieldGte: 12, - FieldLt: 9, - FieldLte: 8, - FieldIn: 6, - FieldNotIn: 12, - } - expectedMsgErrors = nil - errs = NumericTypeUintValidate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("numeric uint tests ok") -} - -type NumericTypeUint8 struct { - FieldReq uint8 `valid:"required"` - FieldEq uint8 `valid:"eq=5"` - FieldNeq uint8 `valid:"neq=5"` - FieldGt uint8 `valid:"gt=10"` - FieldGte uint8 `valid:"gte=10"` - FieldLt uint8 `valid:"lt=10"` - FieldLte uint8 `valid:"lte=10"` - FieldIn uint8 `valid:"in=5 6 7"` - FieldNotIn uint8 `valid:"nin=8 9 10"` -} - -func numericUint8Tests() { - log.Println("starting numeric uint8 tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &NumericTypeUint8{ - FieldReq: 0, - FieldEq: 0, - FieldNeq: 5, - FieldGt: 8, - FieldGte: 8, - FieldLt: 12, - FieldLte: 12, - FieldIn: 3, - FieldNotIn: 9, - } - expectedMsgErrors = []string{ - "FieldReq is required", - "FieldEq must be equal to 5", - "FieldNeq must not be equal to 5", - "FieldGt must be > 10", - "FieldGte must be >= 10", - "FieldLt must be < 10", - "FieldLte must be <= 10", - "FieldIn must be one of '5' '6' '7'", - "FieldNotIn must not be one of '8' '9' '10'", - } - errs = NumericTypeUint8Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &NumericTypeUint8{ - FieldReq: 123, - FieldEq: 5, - FieldNeq: 2, - FieldGt: 11, - FieldGte: 12, - FieldLt: 9, - FieldLte: 8, - FieldIn: 6, - FieldNotIn: 12, - } - expectedMsgErrors = nil - errs = NumericTypeUint8Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("numeric uint8 tests ok") -} - -type NumericTypeUint16 struct { - FieldReq uint16 `valid:"required"` - FieldEq uint16 `valid:"eq=5"` - FieldNeq uint16 `valid:"neq=5"` - FieldGt uint16 `valid:"gt=10"` - FieldGte uint16 `valid:"gte=10"` - FieldLt uint16 `valid:"lt=10"` - FieldLte uint16 `valid:"lte=10"` - FieldIn uint16 `valid:"in=5 6 7"` - FieldNotIn uint16 `valid:"nin=8 9 10"` -} - -func numericUint16Tests() { - log.Println("starting numeric uint16 tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &NumericTypeUint16{ - FieldReq: 0, - FieldEq: 0, - FieldNeq: 5, - FieldGt: 8, - FieldGte: 8, - FieldLt: 12, - FieldLte: 12, - FieldIn: 3, - FieldNotIn: 9, - } - expectedMsgErrors = []string{ - "FieldReq is required", - "FieldEq must be equal to 5", - "FieldNeq must not be equal to 5", - "FieldGt must be > 10", - "FieldGte must be >= 10", - "FieldLt must be < 10", - "FieldLte must be <= 10", - "FieldIn must be one of '5' '6' '7'", - "FieldNotIn must not be one of '8' '9' '10'", - } - errs = NumericTypeUint16Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &NumericTypeUint16{ - FieldReq: 123, - FieldEq: 5, - FieldNeq: 2, - FieldGt: 11, - FieldGte: 12, - FieldLt: 9, - FieldLte: 8, - FieldIn: 6, - FieldNotIn: 12, - } - expectedMsgErrors = nil - errs = NumericTypeUint16Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("numeric uint16 tests ok") -} - -type NumericTypeUint32 struct { - FieldReq uint32 `valid:"required"` - FieldEq uint32 `valid:"eq=5"` - FieldNeq uint32 `valid:"neq=5"` - FieldGt uint32 `valid:"gt=10"` - FieldGte uint32 `valid:"gte=10"` - FieldLt uint32 `valid:"lt=10"` - FieldLte uint32 `valid:"lte=10"` - FieldIn uint32 `valid:"in=5 6 7"` - FieldNotIn uint32 `valid:"nin=8 9 10"` -} - -func numericUint32Tests() { - log.Println("starting numeric uint32 tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &NumericTypeUint32{ - FieldReq: 0, - FieldEq: 0, - FieldNeq: 5, - FieldGt: 8, - FieldGte: 8, - FieldLt: 12, - FieldLte: 12, - FieldIn: 3, - FieldNotIn: 9, - } - expectedMsgErrors = []string{ - "FieldReq is required", - "FieldEq must be equal to 5", - "FieldNeq must not be equal to 5", - "FieldGt must be > 10", - "FieldGte must be >= 10", - "FieldLt must be < 10", - "FieldLte must be <= 10", - "FieldIn must be one of '5' '6' '7'", - "FieldNotIn must not be one of '8' '9' '10'", - } - errs = NumericTypeUint32Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &NumericTypeUint32{ - FieldReq: 123, - FieldEq: 5, - FieldNeq: 2, - FieldGt: 11, - FieldGte: 12, - FieldLt: 9, - FieldLte: 8, - FieldIn: 6, - FieldNotIn: 12, - } - expectedMsgErrors = nil - errs = NumericTypeUint32Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("numeric uint32 tests ok") -} - -type NumericTypeUint64 struct { - FieldReq uint64 `valid:"required"` - FieldEq uint64 `valid:"eq=5"` - FieldNeq uint64 `valid:"neq=5"` - FieldGt uint64 `valid:"gt=10"` - FieldGte uint64 `valid:"gte=10"` - FieldLt uint64 `valid:"lt=10"` - FieldLte uint64 `valid:"lte=10"` - FieldIn uint64 `valid:"in=5 6 7"` - FieldNotIn uint64 `valid:"nin=8 9 10"` -} - -func numericUint64Tests() { - log.Println("starting numeric uint64 tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &NumericTypeUint64{ - FieldReq: 0, - FieldEq: 0, - FieldNeq: 5, - FieldGt: 8, - FieldGte: 8, - FieldLt: 12, - FieldLte: 12, - FieldIn: 3, - FieldNotIn: 9, - } - expectedMsgErrors = []string{ - "FieldReq is required", - "FieldEq must be equal to 5", - "FieldNeq must not be equal to 5", - "FieldGt must be > 10", - "FieldGte must be >= 10", - "FieldLt must be < 10", - "FieldLte must be <= 10", - "FieldIn must be one of '5' '6' '7'", - "FieldNotIn must not be one of '8' '9' '10'", - } - errs = NumericTypeUint64Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &NumericTypeUint64{ - FieldReq: 123, - FieldEq: 5, - FieldNeq: 2, - FieldGt: 11, - FieldGte: 12, - FieldLt: 9, - FieldLte: 8, - FieldIn: 6, - FieldNotIn: 12, - } - expectedMsgErrors = nil - errs = NumericTypeUint64Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("numeric uint64 tests ok") -} diff --git a/tests/endtoend/main.go b/tests/endtoend/main.go index 8982f92..b906cce 100644 --- a/tests/endtoend/main.go +++ b/tests/endtoend/main.go @@ -33,106 +33,17 @@ type NoValidateInfo struct { func main() { log.Println("starting tests") - allTypes1Tests() - allTypes2Tests() structInPkgTests() - stringTests() nestedStructTests() - sliceStringTests() - sliceIntegerTests() - arrayStringTests() cmpBetweenInnerFieldsTests() cmpBetweenNestedFieldsTests() boolTests() - mapStringTests() - mapUint8Tests() - numericIntTypeTests() - numericFloatTypeTests() pointerTests() noPointerTests() log.Println("finishing tests") } -func allTypes1Tests() { - var expectedMsgErrors []string - var errs []error - - v1 := &AllTypes1{} - expectedMsgErrors = []string{ - "FirstName is required", - "LastName is required", - "Age is required", - } - errs = AllTypes1Validate(v1) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - v2 := &AllTypes1{ - FirstName: "First", - LastName: "Last", - Age: 18, - } - expectedMsgErrors = nil - errs = AllTypes1Validate(v2) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("all_types1 tests ok") -} - -func allTypes2Tests() { - var expectedMsgErrors []string - var errs []error - - v1 := &AllTypes2{ - FirstName: "", - LastName: "", - Age: 135, - UserName: "abc", - } - expectedMsgErrors = []string{ - "FirstName is required", - "LastName is required", - "Age must be <= 130", - "UserName length must be >= 5", - } - errs = AllTypes2Validate(v1) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - v2 := &AllTypes2{ - FirstName: "First", - LastName: "Last", - Age: 49, - UserName: "mylongusername", - } - expectedMsgErrors = []string{ - "UserName length must be <= 10", - } - errs = AllTypes2Validate(v2) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - v3 := &AllTypes2{ - FirstName: "First", - LastName: "Last", - Age: 49, - UserName: "myusername", - } - expectedMsgErrors = nil - errs = AllTypes2Validate(v3) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("all_types2 tests ok") -} - func structInPkgTests() { var expectedMsgErrors []string var errs []error diff --git a/tests/endtoend/map_numeric.go b/tests/endtoend/map_numeric.go deleted file mode 100644 index 06f9adf..0000000 --- a/tests/endtoend/map_numeric.go +++ /dev/null @@ -1,60 +0,0 @@ -package main - -import ( - "log" -) - -type MapUint8 struct { - FieldRequired map[uint8]string `valid:"required"` - FieldMin map[uint8]string `valid:"min=2"` - FieldMax map[uint8]string `valid:"max=5"` - FieldLen map[uint8]string `valid:"len=3"` - FieldIn map[uint8]string `valid:"in=1 2 3"` - FieldNotIn map[uint8]string `valid:"nin=1 2 3"` -} - -func mapUint8Tests() { - log.Println("starting map uint8 tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &MapUint8{ - FieldRequired: map[uint8]string{}, - FieldMin: map[uint8]string{1: "1"}, - FieldMax: map[uint8]string{1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6"}, - FieldLen: map[uint8]string{1: "1", 2: "2"}, - FieldIn: map[uint8]string{4: "d"}, - FieldNotIn: map[uint8]string{1: "a", 2: "b"}, - } - expectedMsgErrors = []string{ - "FieldRequired must not be empty", - "FieldMin must have at least 2 elements", - "FieldMax must have at most 5 elements", - "FieldLen must have exactly 3 elements", - "FieldIn elements must be one of '1' '2' '3'", - "FieldNotIn elements must not be one of '1' '2' '3'", - } - errs = MapUint8Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &MapUint8{ - FieldRequired: map[uint8]string{1: "1", 2: "2"}, - FieldMin: map[uint8]string{1: "1", 2: "2"}, - FieldMax: map[uint8]string{1: "1", 2: "2", 3: "3", 4: "4"}, - FieldLen: map[uint8]string{1: "1", 2: "2", 3: "3"}, - FieldIn: map[uint8]string{1: "1", 2: "2", 3: "3"}, - FieldNotIn: map[uint8]string{4: "d", 5: "e", 6: "f"}, - } - expectedMsgErrors = nil - errs = MapUint8Validate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("map uint8 tests ok") -} diff --git a/tests/endtoend/map_string.go b/tests/endtoend/map_string.go deleted file mode 100644 index b5307e1..0000000 --- a/tests/endtoend/map_string.go +++ /dev/null @@ -1,60 +0,0 @@ -package main - -import ( - "log" -) - -type MapString struct { - FieldRequired map[string]string `valid:"required"` - FieldMin map[string]string `valid:"min=2"` - FieldMax map[string]string `valid:"max=5"` - FieldLen map[string]string `valid:"len=3"` - FieldIn map[string]string `valid:"in=a b c"` - FieldNotIn map[string]string `valid:"nin=a b c"` -} - -func mapStringTests() { - log.Println("starting map string tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &MapString{ - FieldRequired: map[string]string{}, - FieldMin: map[string]string{"1": "1"}, - FieldMax: map[string]string{"1": "1", "2": "2", "3": "3", "4": "4", "5": "5", "6": "6"}, - FieldLen: map[string]string{"1": "1", "2": "2"}, - FieldIn: map[string]string{"d": "d"}, - FieldNotIn: map[string]string{"a": "a", "b": "b"}, - } - expectedMsgErrors = []string{ - "FieldRequired must not be empty", - "FieldMin must have at least 2 elements", - "FieldMax must have at most 5 elements", - "FieldLen must have exactly 3 elements", - "FieldIn elements must be one of 'a' 'b' 'c'", - "FieldNotIn elements must not be one of 'a' 'b' 'c'", - } - errs = MapStringValidate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &MapString{ - FieldRequired: map[string]string{"1": "1", "2": "2"}, - FieldMin: map[string]string{"1": "1", "2": "2"}, - FieldMax: map[string]string{"1": "1", "2": "2", "3": "3", "4": "4"}, - FieldLen: map[string]string{"1": "1", "2": "2", "3": "3"}, - FieldIn: map[string]string{"a": "a", "b": "b", "c": "c"}, - FieldNotIn: map[string]string{"d": "d", "e": "e", "f": "f"}, - } - expectedMsgErrors = nil - errs = MapStringValidate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("map string tests ok") -} diff --git a/tests/endtoend/slice_integer.go b/tests/endtoend/slice_integer.go deleted file mode 100644 index 45ce1f0..0000000 --- a/tests/endtoend/slice_integer.go +++ /dev/null @@ -1,60 +0,0 @@ -package main - -import ( - "log" -) - -type SliceInteger struct { - TypesRequired []int `valid:"required"` - TypesMin []int `valid:"min=2"` - TypesMax []int `valid:"max=5"` - TypesLen []int `valid:"len=3"` - TypesIn []int `valid:"in=1 2 3"` - TypesNotIn []int `valid:"nin=1 2 3"` -} - -func sliceIntegerTests() { - log.Println("starting slice integer tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &SliceInteger{ - TypesRequired: []int{}, - TypesMin: []int{1}, - TypesMax: []int{1, 2, 3, 4, 5, 6}, - TypesLen: []int{1, 2}, - TypesIn: []int{4}, - TypesNotIn: []int{2, 3}, - } - expectedMsgErrors = []string{ - "TypesRequired must not be empty", - "TypesMin must have at least 2 elements", - "TypesMax must have at most 5 elements", - "TypesLen must have exactly 3 elements", - "TypesIn elements must be one of '1' '2' '3'", - "TypesNotIn elements must not be one of '1' '2' '3'", - } - errs = SliceIntegerValidate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &SliceInteger{ - TypesRequired: []int{1, 2}, - TypesMin: []int{1, 2}, - TypesMax: []int{1, 2, 3, 4}, - TypesLen: []int{1, 2, 3}, - TypesIn: []int{1, 2, 3, 1, 2, 3}, - TypesNotIn: []int{4, 5, 6}, - } - expectedMsgErrors = nil - errs = SliceIntegerValidate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("slice integer tests ok") -} diff --git a/tests/endtoend/slice_string.go b/tests/endtoend/slice_string.go deleted file mode 100644 index 2a7d426..0000000 --- a/tests/endtoend/slice_string.go +++ /dev/null @@ -1,60 +0,0 @@ -package main - -import ( - "log" -) - -type SliceString struct { - TypesRequired []string `valid:"required"` - TypesMin []string `valid:"min=2"` - TypesMax []string `valid:"max=5"` - TypesLen []string `valid:"len=3"` - TypesIn []string `valid:"in=a b c"` - TypesNotIn []string `valid:"nin=a b c"` -} - -func sliceStringTests() { - log.Println("starting slice string tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &SliceString{ - TypesRequired: []string{}, - TypesMin: []string{"1"}, - TypesMax: []string{"1", "2", "3", "4", "5", "6"}, - TypesLen: []string{"1", "2"}, - TypesIn: []string{"d"}, - TypesNotIn: []string{"d", "b"}, - } - expectedMsgErrors = []string{ - "TypesRequired must not be empty", - "TypesMin must have at least 2 elements", - "TypesMax must have at most 5 elements", - "TypesLen must have exactly 3 elements", - "TypesIn elements must be one of 'a' 'b' 'c'", - "TypesNotIn elements must not be one of 'a' 'b' 'c'", - } - errs = SliceStringValidate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &SliceString{ - TypesRequired: []string{"Type1", "Type2"}, - TypesMin: []string{"1", "2"}, - TypesMax: []string{"1", "2", "3", "4"}, - TypesLen: []string{"1", "2", "3"}, - TypesIn: []string{"a", "b", "c", "b", "a"}, - TypesNotIn: []string{"d", "e", "f"}, - } - expectedMsgErrors = nil - errs = SliceStringValidate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("slice string tests ok") -} diff --git a/tests/endtoend/string.go b/tests/endtoend/string.go deleted file mode 100644 index e6c2167..0000000 --- a/tests/endtoend/string.go +++ /dev/null @@ -1,73 +0,0 @@ -package main - -import "log" - -type StringType struct { - FieldReq string `valid:"required"` - FieldEq string `valid:"eq=aabbcc"` - FieldEqIC string `valid:"eq_ignore_case=yes"` - FieldMinMax string `valid:"min=5,max=10"` - FieldLen string `valid:"len=8"` - FieldNeq string `valid:"neq=cba"` - FieldNeqIC string `valid:"neq_ignore_case=yes"` - FieldIn string `valid:"in=ab bc cd"` - FieldNotIn string `valid:"nin=xx yy zz"` - EmailReq string `valid:"required,email"` -} - -func stringTests() { - log.Println("starting string tests") - - var expectedMsgErrors []string - var errs []error - - // Test case 1: All failure scenarios - v := &StringType{ - FieldEq: "123", - FieldEqIC: "abc", - FieldMinMax: "1", - FieldLen: "abcde", - FieldNeq: "cba", - FieldNeqIC: "yes", - FieldIn: "abc", - FieldNotIn: "zz", - EmailReq: "invalid.email.format", // Invalid required email - } - expectedMsgErrors = []string{ - "FieldReq is required", - "FieldEq must be equal to 'aabbcc'", - "FieldEqIC must be equal to 'yes'", - "FieldMinMax length must be >= 5", - "FieldLen length must be 8", - "FieldNeq must not be equal to 'cba'", - "FieldNeqIC must not be equal to 'yes'", - "FieldIn must be one of 'ab' 'bc' 'cd'", - "FieldNotIn must not be one of 'xx' 'yy' 'zz'", - "EmailReq must be a valid email", - } - errs = StringTypeValidate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - // Test case 2: All valid input - v = &StringType{ - FieldReq: "123", - FieldEq: "aabbcc", - FieldEqIC: "yEs", - FieldMinMax: "12345678", - FieldLen: "abcdefgh", - FieldNeq: "ops", - FieldNeqIC: "No", - FieldIn: "bc", - FieldNotIn: "xy", - EmailReq: "user@example.com", // Valid required email - } - expectedMsgErrors = nil - errs = StringTypeValidate(v) - if !expectedMsgErrorsOk(errs, expectedMsgErrors) { - log.Fatalf("error = %v, wantErr %v", errs, expectedMsgErrors) - } - - log.Println("string tests ok") -} diff --git a/tests/endtoend/validator__.go b/tests/endtoend/validator__.go index 89a7bbb..9f2f443 100755 --- a/tests/endtoend/validator__.go +++ b/tests/endtoend/validator__.go @@ -52,16 +52,6 @@ func AllTypes2Validate(obj *AllTypes2) []error { } return errs } -func ArrayStringValidate(obj *ArrayString) []error { - var errs []error - if !(types.SliceOnlyContains(obj.TypesIn[:], []string{"a", "b", "c"})) { - errs = append(errs, types.NewValidationError("TypesIn elements must be one of 'a' 'b' 'c'")) - } - if !(types.SliceNotContains(obj.TypesNotIn[:], []string{"a", "b", "c"})) { - errs = append(errs, types.NewValidationError("TypesNotIn elements must not be one of 'a' 'b' 'c'")) - } - return errs -} func BoolTypeValidate(obj *BoolType) []error { var errs []error if !(obj.FieldEqTrue == true) { @@ -152,506 +142,6 @@ func CmpNestedUint8FieldsValidate(obj *CmpNestedUint8Fields) []error { } return errs } -func MapStringValidate(obj *MapString) []error { - var errs []error - if !(len(obj.FieldRequired) != 0) { - errs = append(errs, types.NewValidationError("FieldRequired must not be empty")) - } - if !(len(obj.FieldMin) >= 2) { - errs = append(errs, types.NewValidationError("FieldMin must have at least 2 elements")) - } - if !(len(obj.FieldMax) <= 5) { - errs = append(errs, types.NewValidationError("FieldMax must have at most 5 elements")) - } - if !(len(obj.FieldLen) == 3) { - errs = append(errs, types.NewValidationError("FieldLen must have exactly 3 elements")) - } - if !(types.MapOnlyContains(obj.FieldIn, []string{"a", "b", "c"})) { - errs = append(errs, types.NewValidationError("FieldIn elements must be one of 'a' 'b' 'c'")) - } - if !(types.MapNotContains(obj.FieldNotIn, []string{"a", "b", "c"})) { - errs = append(errs, types.NewValidationError("FieldNotIn elements must not be one of 'a' 'b' 'c'")) - } - return errs -} -func MapUint8Validate(obj *MapUint8) []error { - var errs []error - if !(len(obj.FieldRequired) != 0) { - errs = append(errs, types.NewValidationError("FieldRequired must not be empty")) - } - if !(len(obj.FieldMin) >= 2) { - errs = append(errs, types.NewValidationError("FieldMin must have at least 2 elements")) - } - if !(len(obj.FieldMax) <= 5) { - errs = append(errs, types.NewValidationError("FieldMax must have at most 5 elements")) - } - if !(len(obj.FieldLen) == 3) { - errs = append(errs, types.NewValidationError("FieldLen must have exactly 3 elements")) - } - if !(types.MapOnlyContains(obj.FieldIn, []uint8{1, 2, 3})) { - errs = append(errs, types.NewValidationError("FieldIn elements must be one of '1' '2' '3'")) - } - if !(types.MapNotContains(obj.FieldNotIn, []uint8{1, 2, 3})) { - errs = append(errs, types.NewValidationError("FieldNotIn elements must not be one of '1' '2' '3'")) - } - return errs -} -func NumericTypeFloat32Validate(obj *NumericTypeFloat32) []error { - var errs []error - if !(obj.FieldReq != 0) { - errs = append(errs, types.NewValidationError("FieldReq is required")) - } - if !(obj.FieldEq == 5.9) { - errs = append(errs, types.NewValidationError("FieldEq must be equal to 5.9")) - } - if !(obj.FieldNeq != 5.9) { - errs = append(errs, types.NewValidationError("FieldNeq must not be equal to 5.9")) - } - if !(obj.FieldGt > 10.1) { - errs = append(errs, types.NewValidationError("FieldGt must be > 10.1")) - } - if !(obj.FieldGte >= 10.1) { - errs = append(errs, types.NewValidationError("FieldGte must be >= 10.1")) - } - if !(obj.FieldLt < 9.9) { - errs = append(errs, types.NewValidationError("FieldLt must be < 9.9")) - } - if !(obj.FieldLte <= 9.9) { - errs = append(errs, types.NewValidationError("FieldLte must be <= 9.9")) - } - if !(obj.FieldIn == 5.1 || obj.FieldIn == 6.2 || obj.FieldIn == 7.3) { - errs = append(errs, types.NewValidationError("FieldIn must be one of '5.1' '6.2' '7.3'")) - } - if !(obj.FieldNotIn != 8.5 && obj.FieldNotIn != 9.6 && obj.FieldNotIn != 10.7) { - errs = append(errs, types.NewValidationError("FieldNotIn must not be one of '8.5' '9.6' '10.7'")) - } - return errs -} -func NumericTypeFloat64Validate(obj *NumericTypeFloat64) []error { - var errs []error - if !(obj.FieldReq != 0) { - errs = append(errs, types.NewValidationError("FieldReq is required")) - } - if !(obj.FieldEq == 5.9) { - errs = append(errs, types.NewValidationError("FieldEq must be equal to 5.9")) - } - if !(obj.FieldNeq != 5.9) { - errs = append(errs, types.NewValidationError("FieldNeq must not be equal to 5.9")) - } - if !(obj.FieldGt > 10.1) { - errs = append(errs, types.NewValidationError("FieldGt must be > 10.1")) - } - if !(obj.FieldGte >= 10.1) { - errs = append(errs, types.NewValidationError("FieldGte must be >= 10.1")) - } - if !(obj.FieldLt < 9.9) { - errs = append(errs, types.NewValidationError("FieldLt must be < 9.9")) - } - if !(obj.FieldLte <= 9.9) { - errs = append(errs, types.NewValidationError("FieldLte must be <= 9.9")) - } - if !(obj.FieldIn == 5.1 || obj.FieldIn == 6.2 || obj.FieldIn == 7.3) { - errs = append(errs, types.NewValidationError("FieldIn must be one of '5.1' '6.2' '7.3'")) - } - if !(obj.FieldNotIn != 8.5 && obj.FieldNotIn != 9.6 && obj.FieldNotIn != 10.7) { - errs = append(errs, types.NewValidationError("FieldNotIn must not be one of '8.5' '9.6' '10.7'")) - } - return errs -} -func NumericTypeIntValidate(obj *NumericTypeInt) []error { - var errs []error - if !(obj.FieldReq != 0) { - errs = append(errs, types.NewValidationError("FieldReq is required")) - } - if !(obj.FieldEq == 5) { - errs = append(errs, types.NewValidationError("FieldEq must be equal to 5")) - } - if !(obj.FieldNeq != 5) { - errs = append(errs, types.NewValidationError("FieldNeq must not be equal to 5")) - } - if !(obj.FieldGt > 10) { - errs = append(errs, types.NewValidationError("FieldGt must be > 10")) - } - if !(obj.FieldGte >= 10) { - errs = append(errs, types.NewValidationError("FieldGte must be >= 10")) - } - if !(obj.FieldLt < 10) { - errs = append(errs, types.NewValidationError("FieldLt must be < 10")) - } - if !(obj.FieldLte <= 10) { - errs = append(errs, types.NewValidationError("FieldLte must be <= 10")) - } - if !(obj.FieldIn == 5 || obj.FieldIn == 6 || obj.FieldIn == 7) { - errs = append(errs, types.NewValidationError("FieldIn must be one of '5' '6' '7'")) - } - if !(obj.FieldNotIn != 8 && obj.FieldNotIn != 9 && obj.FieldNotIn != 10) { - errs = append(errs, types.NewValidationError("FieldNotIn must not be one of '8' '9' '10'")) - } - return errs -} -func NumericTypeInt16Validate(obj *NumericTypeInt16) []error { - var errs []error - if !(obj.FieldReq != 0) { - errs = append(errs, types.NewValidationError("FieldReq is required")) - } - if !(obj.FieldEq == 5) { - errs = append(errs, types.NewValidationError("FieldEq must be equal to 5")) - } - if !(obj.FieldNeq != 5) { - errs = append(errs, types.NewValidationError("FieldNeq must not be equal to 5")) - } - if !(obj.FieldGt > 10) { - errs = append(errs, types.NewValidationError("FieldGt must be > 10")) - } - if !(obj.FieldGte >= 10) { - errs = append(errs, types.NewValidationError("FieldGte must be >= 10")) - } - if !(obj.FieldLt < 10) { - errs = append(errs, types.NewValidationError("FieldLt must be < 10")) - } - if !(obj.FieldLte <= 10) { - errs = append(errs, types.NewValidationError("FieldLte must be <= 10")) - } - if !(obj.FieldIn == 5 || obj.FieldIn == 6 || obj.FieldIn == 7) { - errs = append(errs, types.NewValidationError("FieldIn must be one of '5' '6' '7'")) - } - if !(obj.FieldNotIn != 8 && obj.FieldNotIn != 9 && obj.FieldNotIn != 10) { - errs = append(errs, types.NewValidationError("FieldNotIn must not be one of '8' '9' '10'")) - } - return errs -} -func NumericTypeInt32Validate(obj *NumericTypeInt32) []error { - var errs []error - if !(obj.FieldReq != 0) { - errs = append(errs, types.NewValidationError("FieldReq is required")) - } - if !(obj.FieldEq == 5) { - errs = append(errs, types.NewValidationError("FieldEq must be equal to 5")) - } - if !(obj.FieldNeq != 5) { - errs = append(errs, types.NewValidationError("FieldNeq must not be equal to 5")) - } - if !(obj.FieldGt > 10) { - errs = append(errs, types.NewValidationError("FieldGt must be > 10")) - } - if !(obj.FieldGte >= 10) { - errs = append(errs, types.NewValidationError("FieldGte must be >= 10")) - } - if !(obj.FieldLt < 10) { - errs = append(errs, types.NewValidationError("FieldLt must be < 10")) - } - if !(obj.FieldLte <= 10) { - errs = append(errs, types.NewValidationError("FieldLte must be <= 10")) - } - if !(obj.FieldIn == 5 || obj.FieldIn == 6 || obj.FieldIn == 7) { - errs = append(errs, types.NewValidationError("FieldIn must be one of '5' '6' '7'")) - } - if !(obj.FieldNotIn != 8 && obj.FieldNotIn != 9 && obj.FieldNotIn != 10) { - errs = append(errs, types.NewValidationError("FieldNotIn must not be one of '8' '9' '10'")) - } - return errs -} -func NumericTypeInt64Validate(obj *NumericTypeInt64) []error { - var errs []error - if !(obj.FieldReq != 0) { - errs = append(errs, types.NewValidationError("FieldReq is required")) - } - if !(obj.FieldEq == 5) { - errs = append(errs, types.NewValidationError("FieldEq must be equal to 5")) - } - if !(obj.FieldNeq != 5) { - errs = append(errs, types.NewValidationError("FieldNeq must not be equal to 5")) - } - if !(obj.FieldGt > 10) { - errs = append(errs, types.NewValidationError("FieldGt must be > 10")) - } - if !(obj.FieldGte >= 10) { - errs = append(errs, types.NewValidationError("FieldGte must be >= 10")) - } - if !(obj.FieldLt < 10) { - errs = append(errs, types.NewValidationError("FieldLt must be < 10")) - } - if !(obj.FieldLte <= 10) { - errs = append(errs, types.NewValidationError("FieldLte must be <= 10")) - } - if !(obj.FieldIn == 5 || obj.FieldIn == 6 || obj.FieldIn == 7) { - errs = append(errs, types.NewValidationError("FieldIn must be one of '5' '6' '7'")) - } - if !(obj.FieldNotIn != 8 && obj.FieldNotIn != 9 && obj.FieldNotIn != 10) { - errs = append(errs, types.NewValidationError("FieldNotIn must not be one of '8' '9' '10'")) - } - return errs -} -func NumericTypeInt8Validate(obj *NumericTypeInt8) []error { - var errs []error - if !(obj.FieldReq != 0) { - errs = append(errs, types.NewValidationError("FieldReq is required")) - } - if !(obj.FieldEq == 5) { - errs = append(errs, types.NewValidationError("FieldEq must be equal to 5")) - } - if !(obj.FieldNeq != 5) { - errs = append(errs, types.NewValidationError("FieldNeq must not be equal to 5")) - } - if !(obj.FieldGt > 10) { - errs = append(errs, types.NewValidationError("FieldGt must be > 10")) - } - if !(obj.FieldGte >= 10) { - errs = append(errs, types.NewValidationError("FieldGte must be >= 10")) - } - if !(obj.FieldLt < 10) { - errs = append(errs, types.NewValidationError("FieldLt must be < 10")) - } - if !(obj.FieldLte <= 10) { - errs = append(errs, types.NewValidationError("FieldLte must be <= 10")) - } - if !(obj.FieldIn == 5 || obj.FieldIn == 6 || obj.FieldIn == 7) { - errs = append(errs, types.NewValidationError("FieldIn must be one of '5' '6' '7'")) - } - if !(obj.FieldNotIn != 8 && obj.FieldNotIn != 9 && obj.FieldNotIn != 10) { - errs = append(errs, types.NewValidationError("FieldNotIn must not be one of '8' '9' '10'")) - } - return errs -} -func NumericTypeUintValidate(obj *NumericTypeUint) []error { - var errs []error - if !(obj.FieldReq != 0) { - errs = append(errs, types.NewValidationError("FieldReq is required")) - } - if !(obj.FieldEq == 5) { - errs = append(errs, types.NewValidationError("FieldEq must be equal to 5")) - } - if !(obj.FieldNeq != 5) { - errs = append(errs, types.NewValidationError("FieldNeq must not be equal to 5")) - } - if !(obj.FieldGt > 10) { - errs = append(errs, types.NewValidationError("FieldGt must be > 10")) - } - if !(obj.FieldGte >= 10) { - errs = append(errs, types.NewValidationError("FieldGte must be >= 10")) - } - if !(obj.FieldLt < 10) { - errs = append(errs, types.NewValidationError("FieldLt must be < 10")) - } - if !(obj.FieldLte <= 10) { - errs = append(errs, types.NewValidationError("FieldLte must be <= 10")) - } - if !(obj.FieldIn == 5 || obj.FieldIn == 6 || obj.FieldIn == 7) { - errs = append(errs, types.NewValidationError("FieldIn must be one of '5' '6' '7'")) - } - if !(obj.FieldNotIn != 8 && obj.FieldNotIn != 9 && obj.FieldNotIn != 10) { - errs = append(errs, types.NewValidationError("FieldNotIn must not be one of '8' '9' '10'")) - } - return errs -} -func NumericTypeUint16Validate(obj *NumericTypeUint16) []error { - var errs []error - if !(obj.FieldReq != 0) { - errs = append(errs, types.NewValidationError("FieldReq is required")) - } - if !(obj.FieldEq == 5) { - errs = append(errs, types.NewValidationError("FieldEq must be equal to 5")) - } - if !(obj.FieldNeq != 5) { - errs = append(errs, types.NewValidationError("FieldNeq must not be equal to 5")) - } - if !(obj.FieldGt > 10) { - errs = append(errs, types.NewValidationError("FieldGt must be > 10")) - } - if !(obj.FieldGte >= 10) { - errs = append(errs, types.NewValidationError("FieldGte must be >= 10")) - } - if !(obj.FieldLt < 10) { - errs = append(errs, types.NewValidationError("FieldLt must be < 10")) - } - if !(obj.FieldLte <= 10) { - errs = append(errs, types.NewValidationError("FieldLte must be <= 10")) - } - if !(obj.FieldIn == 5 || obj.FieldIn == 6 || obj.FieldIn == 7) { - errs = append(errs, types.NewValidationError("FieldIn must be one of '5' '6' '7'")) - } - if !(obj.FieldNotIn != 8 && obj.FieldNotIn != 9 && obj.FieldNotIn != 10) { - errs = append(errs, types.NewValidationError("FieldNotIn must not be one of '8' '9' '10'")) - } - return errs -} -func NumericTypeUint32Validate(obj *NumericTypeUint32) []error { - var errs []error - if !(obj.FieldReq != 0) { - errs = append(errs, types.NewValidationError("FieldReq is required")) - } - if !(obj.FieldEq == 5) { - errs = append(errs, types.NewValidationError("FieldEq must be equal to 5")) - } - if !(obj.FieldNeq != 5) { - errs = append(errs, types.NewValidationError("FieldNeq must not be equal to 5")) - } - if !(obj.FieldGt > 10) { - errs = append(errs, types.NewValidationError("FieldGt must be > 10")) - } - if !(obj.FieldGte >= 10) { - errs = append(errs, types.NewValidationError("FieldGte must be >= 10")) - } - if !(obj.FieldLt < 10) { - errs = append(errs, types.NewValidationError("FieldLt must be < 10")) - } - if !(obj.FieldLte <= 10) { - errs = append(errs, types.NewValidationError("FieldLte must be <= 10")) - } - if !(obj.FieldIn == 5 || obj.FieldIn == 6 || obj.FieldIn == 7) { - errs = append(errs, types.NewValidationError("FieldIn must be one of '5' '6' '7'")) - } - if !(obj.FieldNotIn != 8 && obj.FieldNotIn != 9 && obj.FieldNotIn != 10) { - errs = append(errs, types.NewValidationError("FieldNotIn must not be one of '8' '9' '10'")) - } - return errs -} -func NumericTypeUint64Validate(obj *NumericTypeUint64) []error { - var errs []error - if !(obj.FieldReq != 0) { - errs = append(errs, types.NewValidationError("FieldReq is required")) - } - if !(obj.FieldEq == 5) { - errs = append(errs, types.NewValidationError("FieldEq must be equal to 5")) - } - if !(obj.FieldNeq != 5) { - errs = append(errs, types.NewValidationError("FieldNeq must not be equal to 5")) - } - if !(obj.FieldGt > 10) { - errs = append(errs, types.NewValidationError("FieldGt must be > 10")) - } - if !(obj.FieldGte >= 10) { - errs = append(errs, types.NewValidationError("FieldGte must be >= 10")) - } - if !(obj.FieldLt < 10) { - errs = append(errs, types.NewValidationError("FieldLt must be < 10")) - } - if !(obj.FieldLte <= 10) { - errs = append(errs, types.NewValidationError("FieldLte must be <= 10")) - } - if !(obj.FieldIn == 5 || obj.FieldIn == 6 || obj.FieldIn == 7) { - errs = append(errs, types.NewValidationError("FieldIn must be one of '5' '6' '7'")) - } - if !(obj.FieldNotIn != 8 && obj.FieldNotIn != 9 && obj.FieldNotIn != 10) { - errs = append(errs, types.NewValidationError("FieldNotIn must not be one of '8' '9' '10'")) - } - return errs -} -func NumericTypeUint8Validate(obj *NumericTypeUint8) []error { - var errs []error - if !(obj.FieldReq != 0) { - errs = append(errs, types.NewValidationError("FieldReq is required")) - } - if !(obj.FieldEq == 5) { - errs = append(errs, types.NewValidationError("FieldEq must be equal to 5")) - } - if !(obj.FieldNeq != 5) { - errs = append(errs, types.NewValidationError("FieldNeq must not be equal to 5")) - } - if !(obj.FieldGt > 10) { - errs = append(errs, types.NewValidationError("FieldGt must be > 10")) - } - if !(obj.FieldGte >= 10) { - errs = append(errs, types.NewValidationError("FieldGte must be >= 10")) - } - if !(obj.FieldLt < 10) { - errs = append(errs, types.NewValidationError("FieldLt must be < 10")) - } - if !(obj.FieldLte <= 10) { - errs = append(errs, types.NewValidationError("FieldLte must be <= 10")) - } - if !(obj.FieldIn == 5 || obj.FieldIn == 6 || obj.FieldIn == 7) { - errs = append(errs, types.NewValidationError("FieldIn must be one of '5' '6' '7'")) - } - if !(obj.FieldNotIn != 8 && obj.FieldNotIn != 9 && obj.FieldNotIn != 10) { - errs = append(errs, types.NewValidationError("FieldNotIn must not be one of '8' '9' '10'")) - } - return errs -} -func SliceIntegerValidate(obj *SliceInteger) []error { - var errs []error - if !(len(obj.TypesRequired) != 0) { - errs = append(errs, types.NewValidationError("TypesRequired must not be empty")) - } - if !(len(obj.TypesMin) >= 2) { - errs = append(errs, types.NewValidationError("TypesMin must have at least 2 elements")) - } - if !(len(obj.TypesMax) <= 5) { - errs = append(errs, types.NewValidationError("TypesMax must have at most 5 elements")) - } - if !(len(obj.TypesLen) == 3) { - errs = append(errs, types.NewValidationError("TypesLen must have exactly 3 elements")) - } - if !(types.SliceOnlyContains(obj.TypesIn, []int{1, 2, 3})) { - errs = append(errs, types.NewValidationError("TypesIn elements must be one of '1' '2' '3'")) - } - if !(types.SliceNotContains(obj.TypesNotIn, []int{1, 2, 3})) { - errs = append(errs, types.NewValidationError("TypesNotIn elements must not be one of '1' '2' '3'")) - } - return errs -} -func SliceStringValidate(obj *SliceString) []error { - var errs []error - if !(len(obj.TypesRequired) != 0) { - errs = append(errs, types.NewValidationError("TypesRequired must not be empty")) - } - if !(len(obj.TypesMin) >= 2) { - errs = append(errs, types.NewValidationError("TypesMin must have at least 2 elements")) - } - if !(len(obj.TypesMax) <= 5) { - errs = append(errs, types.NewValidationError("TypesMax must have at most 5 elements")) - } - if !(len(obj.TypesLen) == 3) { - errs = append(errs, types.NewValidationError("TypesLen must have exactly 3 elements")) - } - if !(types.SliceOnlyContains(obj.TypesIn, []string{"a", "b", "c"})) { - errs = append(errs, types.NewValidationError("TypesIn elements must be one of 'a' 'b' 'c'")) - } - if !(types.SliceNotContains(obj.TypesNotIn, []string{"a", "b", "c"})) { - errs = append(errs, types.NewValidationError("TypesNotIn elements must not be one of 'a' 'b' 'c'")) - } - return errs -} -func StringTypeValidate(obj *StringType) []error { - var errs []error - if !(obj.FieldReq != "") { - errs = append(errs, types.NewValidationError("FieldReq is required")) - } - if !(obj.FieldEq == "aabbcc") { - errs = append(errs, types.NewValidationError("FieldEq must be equal to 'aabbcc'")) - } - if !(types.EqualFold(obj.FieldEqIC, "yes")) { - errs = append(errs, types.NewValidationError("FieldEqIC must be equal to 'yes'")) - } - if !(len(obj.FieldMinMax) >= 5) { - errs = append(errs, types.NewValidationError("FieldMinMax length must be >= 5")) - } - if !(len(obj.FieldMinMax) <= 10) { - errs = append(errs, types.NewValidationError("FieldMinMax length must be <= 10")) - } - if !(len(obj.FieldLen) == 8) { - errs = append(errs, types.NewValidationError("FieldLen length must be 8")) - } - if !(obj.FieldNeq != "cba") { - errs = append(errs, types.NewValidationError("FieldNeq must not be equal to 'cba'")) - } - if !(!types.EqualFold(obj.FieldNeqIC, "yes")) { - errs = append(errs, types.NewValidationError("FieldNeqIC must not be equal to 'yes'")) - } - if !(obj.FieldIn == "ab" || obj.FieldIn == "bc" || obj.FieldIn == "cd") { - errs = append(errs, types.NewValidationError("FieldIn must be one of 'ab' 'bc' 'cd'")) - } - if !(obj.FieldNotIn != "xx" && obj.FieldNotIn != "yy" && obj.FieldNotIn != "zz") { - errs = append(errs, types.NewValidationError("FieldNotIn must not be one of 'xx' 'yy' 'zz'")) - } - if !(obj.EmailReq != "") { - errs = append(errs, types.NewValidationError("EmailReq is required")) - } - if !(types.IsValidEmail(obj.EmailReq)) { - errs = append(errs, types.NewValidationError("EmailReq must be a valid email")) - } - return errs -} func UserValidate(obj *User) []error { var errs []error if !(obj.FirstName != "") {