Skip to content

Commit

Permalink
[knf] Added type 'Validators' for 'Validator' slice
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 20, 2024
1 parent 28ae902 commit 55cf6bc
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

### [13.8.2](https://kaos.sh/ek/13.8.2)

- `[knf]` Added type `Validators` for `Validator` slice

### [13.8.1](https://kaos.sh/ek/13.8.1)

- `[req]` `AutoDiscard` now doesn't affect responses with successful response status codes (`200`-`299`)
Expand Down
14 changes: 12 additions & 2 deletions knf/knf.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ type Validator struct {
Value any // Expected value
}

// Validators is a slice with validators
type Validators []*Validator

// PropertyValidator is default type of property validation function
type PropertyValidator func(config IConfig, prop string, value any) error

Expand Down Expand Up @@ -412,7 +415,7 @@ func Props(section string) []string {

// Validate executes all given validators and
// returns slice with validation errors
func Validate(validators []*Validator) []error {
func Validate(validators Validators) []error {
if global == nil {
return []error{ErrNilConfig}
}
Expand All @@ -422,6 +425,13 @@ func Validate(validators []*Validator) []error {

// ////////////////////////////////////////////////////////////////////////////////// //

// Add adds given validators and returns new slice
func (v Validators) Add(validators Validators) Validators {
return append(v, validators...)
}

// ////////////////////////////////////////////////////////////////////////////////// //

// Merge merges two configurations
func (c *Config) Merge(cfg *Config) error {
if c == nil || c.mx == nil {
Expand Down Expand Up @@ -831,7 +841,7 @@ func (c *Config) File() string {

// Validate executes all given validators and
// returns slice with validation errors
func (c *Config) Validate(validators []*Validator) []error {
func (c *Config) Validate(validators Validators) []error {
if c == nil || c.mx == nil {
return []error{ErrNilConfig}
}
Expand Down
16 changes: 11 additions & 5 deletions knf/knf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (s *KNFSuite) TestErrors(c *check.C) {
c.Assert(HasProp("test:test"), check.Equals, false)
c.Assert(Sections(), check.HasLen, 0)
c.Assert(Props("test"), check.HasLen, 0)
c.Assert(Validate([]*Validator{}), check.DeepEquals, []error{ErrNilConfig})
c.Assert(Validate(Validators{}), check.DeepEquals, []error{ErrNilConfig})
c.Assert(Alias("test:test", "test:test"), check.NotNil)
c.Assert(global.Merge(nil), check.NotNil)

Expand All @@ -253,7 +253,7 @@ func (s *KNFSuite) TestErrors(c *check.C) {
c.Assert(config.HasProp("test:test"), check.Equals, false)
c.Assert(config.Sections(), check.HasLen, 0)
c.Assert(config.Props("test"), check.HasLen, 0)
c.Assert(config.Validate([]*Validator{}), check.HasLen, 0)
c.Assert(config.Validate(Validators{}), check.HasLen, 0)
c.Assert(config.Merge(nil), check.NotNil)

c.Assert(config.Alias("", ""), check.NotNil)
Expand Down Expand Up @@ -620,7 +620,7 @@ func (s *KNFSuite) TestNil(c *check.C) {
c.Assert(err, check.NotNil)
c.Assert(err, check.DeepEquals, ErrNilConfig)

errs := nilConf.Validate([]*Validator{})
errs := nilConf.Validate(Validators{})

c.Assert(errs, check.Not(check.HasLen), 0)
c.Assert(errs, check.DeepEquals, []error{ErrNilConfig})
Expand Down Expand Up @@ -701,11 +701,17 @@ func (s *KNFSuite) TestSimpleValidator(c *check.C) {
return nil
}

errs := Validate([]*Validator{
validators := Validators{
{"string:test2", simpleValidator, nil},
}

validators = validators.Add(Validators{
{"string:test2", simpleValidator, nil},
})

c.Assert(errs, check.HasLen, 1)
errs := Validate(validators)

c.Assert(errs, check.HasLen, 2)
}

func (s *KNFSuite) TestKNFParserExceptions(c *check.C) {
Expand Down
4 changes: 2 additions & 2 deletions knf/united/united.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func GetL(name string, defvals ...[]string) []string {

// Validate executes all given validators and
// returns slice with validation errors
func Validate(validators []*knf.Validator) []error {
func Validate(validators knf.Validators) []error {
if global == nil {
return []error{knf.ErrNilConfig}
}
Expand Down Expand Up @@ -464,7 +464,7 @@ func (c *united) getProp(name string) string {
}

// validate runs validators over configuration
func validate(validators []*knf.Validator) []error {
func validate(validators knf.Validators) []error {
var result []error

for _, v := range validators {
Expand Down
9 changes: 7 additions & 2 deletions knf/validators/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (s *ValidatorSuite) TestBasicValidators(c *check.C) {

var errs []error

errs = knf.Validate([]*knf.Validator{
validators := knf.Validators{
{"integer:test1", Set, nil},
{"integer:test1", Greater, 0},
{"integer:test1", Greater, 0.5},
Expand All @@ -114,16 +114,21 @@ func (s *ValidatorSuite) TestBasicValidators(c *check.C) {
{"integer:test1", NotEquals, 10},
{"integer:test1", NotEquals, 10.1},
{"integer:test1", NotEquals, "123"},
}

validators = validators.Add(knf.Validators{
{"string:test3", HasPrefix, "45"},
{"string:test3", HasSuffix, "00"},
{"string:test1", LenGreater, 3},
{"string:test1", LenLess, 10},
{"string:test1", LenEquals, 4},
})

errs = knf.Validate(validators)

c.Assert(errs, check.HasLen, 0)

errs = knf.Validate([]*knf.Validator{
errs = knf.Validate(knf.Validators{
{"boolean:test5", Set, nil},
{"integer:test1", Greater, 10},
{"integer:test1", Less, 3},
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ package ek
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "13.8.1"
const VERSION = "13.8.2"

0 comments on commit 55cf6bc

Please sign in to comment.