-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added base fake data generator * Added array generator * Added valdiator * created fake data prototype * Fixed tests * Added validator tests * Updated sonar properties * Added tests for validator * Fixed seeding * Added test for handler * Added options tests * Added test for response * Improve tets * Added tests for validator * Added tests for fake data list * Updated response validator * Updated fake node validator * Added docs generator * Updated tests * Cleanup data * Added numbers * Added groups to the generator * Configurare base data * Added full correct options to generate data * Cleanup data
- Loading branch information
Showing
31 changed files
with
4,281 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,5 @@ coverage.out | |
.DS_Store | ||
uncors | ||
!uncors/ | ||
tools/fakedata/scheme.json | ||
tools/fakedata/docs.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package base | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gobuffalo/validate" | ||
) | ||
|
||
type StringEnumValidator struct { | ||
Field string | ||
Value string | ||
Options []string | ||
} | ||
|
||
func (f *StringEnumValidator) IsValid(errors *validate.Errors) { | ||
for _, option := range f.Options { | ||
if f.Value == option { | ||
return | ||
} | ||
} | ||
|
||
errors.Add(f.Field, fmt.Sprintf("'%s' is not a valid option", f.Value)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package base_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/evg4b/uncors/internal/config/validators/base" | ||
"github.com/gobuffalo/validate" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
option1 = "option-1" | ||
option2 = "option-2" | ||
) | ||
|
||
func TestStringEnumValidator(t *testing.T) { | ||
t.Run("valid option", func(t *testing.T) { | ||
errors := validate.Validate(&base.StringEnumValidator{ | ||
Field: "field", | ||
Value: option1, | ||
Options: []string{ | ||
option1, | ||
option2, | ||
}, | ||
}) | ||
|
||
assert.False(t, errors.HasAny()) | ||
}) | ||
|
||
t.Run("valid option", func(t *testing.T) { | ||
errors := validate.Validate(&base.StringEnumValidator{ | ||
Field: "field", | ||
Value: "option-x", | ||
Options: []string{ | ||
option1, | ||
option2, | ||
}, | ||
}) | ||
|
||
require.EqualError(t, errors, "'option-x' is not a valid option") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package validators | ||
|
||
import ( | ||
"github.com/evg4b/uncors/internal/config/validators/base" | ||
"github.com/evg4b/uncors/pkg/fakedata" | ||
"github.com/gobuffalo/validate" | ||
) | ||
|
||
type FakedataNodeValidator struct { | ||
Field string | ||
Value *fakedata.Node | ||
Root bool | ||
} | ||
|
||
func (c *FakedataNodeValidator) IsValid(errors *validate.Errors) { | ||
errors.Append(validate.Validate(&base.StringEnumValidator{ | ||
Field: joinPath(c.Field, "type"), | ||
Value: c.Value.Type, | ||
Options: fakedata.GetTypes(), | ||
})) | ||
|
||
if !c.Root && c.Value.Seed != uint64(0) { | ||
errors.Add(joinPath(c.Field, "seed"), "property 'seed' is not allowed in nested nodes") | ||
} | ||
|
||
if c.Value.Type == "object" { | ||
c.validateAsObject(errors) | ||
} | ||
|
||
if c.Value.Type == "array" { | ||
c.validateAsArray(errors) | ||
} | ||
} | ||
|
||
func (c *FakedataNodeValidator) validateAsArray(errors *validate.Errors) { | ||
if c.Value.Properties != nil { | ||
errors.Add(joinPath(c.Field, "properties"), "property 'properties' is not allowed for array nodes") | ||
} | ||
|
||
if c.Value.Item == nil { | ||
errors.Add(joinPath(c.Field, "item"), "property 'item' is required for array nodes") | ||
} else { | ||
errors.Append(validate.Validate(&FakedataNodeValidator{ | ||
Field: joinPath(c.Field, "item"), | ||
Value: c.Value.Item, | ||
})) | ||
} | ||
|
||
if c.Value.Count < 0 { | ||
errors.Add(joinPath(c.Field, "count"), "property 'count' must be greater than or equal to 0") | ||
} | ||
} | ||
|
||
func (c *FakedataNodeValidator) validateAsObject(errors *validate.Errors) { | ||
if c.Value.Count != 0 { | ||
errors.Add(joinPath(c.Field, "count"), "property 'count' is not allowed for object nodes") | ||
} | ||
|
||
if c.Value.Item != nil { | ||
errors.Add(joinPath(c.Field, "item"), "property 'item' is not allowed for object nodes") | ||
} | ||
|
||
if c.Value.Options != nil { | ||
errors.Add(joinPath(c.Field, "options"), "property 'options' is not allowed for object nodes") | ||
} | ||
|
||
for key, node := range c.Value.Properties { | ||
errors.Append(validate.Validate(&FakedataNodeValidator{ | ||
Field: joinPath(c.Field, key), | ||
Value: &node, | ||
})) | ||
} | ||
} |
Oops, something went wrong.