Skip to content

Commit

Permalink
[knf/validators] Add more validators
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 14, 2024
1 parent 9d1f896 commit 2b36109
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
## Changelog

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

- `[knf/validators]` Added support of `int64`, `uint`, and `uint64` to `Less` vaildator
- `[knf/validators]` Added support of `int64`, `uint`, and `uint64` to `Greater` vaildator
- `[knf/validators]` Added validator `SizeGreater`
- `[knf/validators]` Added validator `SizeLess`

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

Expand Down
56 changes: 56 additions & 0 deletions knf/validators/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ var (
// HasSuffix returns error if property doesn't have given suffix
HasSuffix = validatorHasSuffix

// SizeLess returns an error if the property value is smaller than the given number
SizeLess = validatorSizeLess

// SizeGreater returns an error if the property value is greater than the given number
SizeGreater = validatorSizeGreater

// TypeBool returns error if property contains non-boolean value
TypeBool = validatorTypeBool

Expand Down Expand Up @@ -258,6 +264,56 @@ func validatorGreater(config knf.IConfig, prop string, value any) error {
return nil
}

func validatorSizeLess(config knf.IConfig, prop string, value any) error {
var v uint64

switch t := value.(type) {
case int:
v = uint64(t)
case int64:
v = uint64(t)
case uint:
v = uint64(t)
case uint64:
v = uint64(t)
case float64:
v = uint64(t)
default:
return getValidatorInputError("SizeLess", prop, value)
}

if config.GetSZ(prop) > v {
return fmt.Errorf("Property %s can't be greater than %d bytes", prop, v)
}

return nil
}

func validatorSizeGreater(config knf.IConfig, prop string, value any) error {
var v uint64

switch t := value.(type) {
case int:
v = uint64(t)
case int64:
v = uint64(t)
case uint:
v = uint64(t)
case uint64:
v = uint64(t)
case float64:
v = uint64(t)
default:
return getValidatorInputError("SizeGreater", prop, value)
}

if config.GetSZ(prop) < v {
return fmt.Errorf("Property %s can't be less than %d bytes", prop, v)
}

return nil
}

func validatorInRange(config knf.IConfig, prop string, value any) error {
rng, ok := value.(Range)

Expand Down
20 changes: 20 additions & 0 deletions knf/validators/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ test1: 1
test4: ABC
test5: true
[size]
test1: 3mb
[comment]
test1: 100
# test2: 100
Expand Down Expand Up @@ -156,6 +159,7 @@ func (s *ValidatorSuite) TestBasicValidators(c *check.C) {
string: test
integer: 10
float: 10.0
size: 3mb
boolean: false`)

cfg, err := knf.Read(cfgFile)
Expand All @@ -182,6 +186,22 @@ func (s *ValidatorSuite) TestBasicValidators(c *check.C) {
c.Assert(Greater(cfg, "test:float", 30.1).Error(), check.Equals, "Property test:float can't be less than 30.1")
c.Assert(Greater(cfg, "test:float", 5.1), check.IsNil)

c.Assert(SizeGreater(cfg, "test:size", 10*1024*1024).Error(), check.Equals, "Property test:size can't be less than 10485760 bytes")
c.Assert(SizeGreater(cfg, "test:size", int64(10*1024*1024)).Error(), check.Equals, "Property test:size can't be less than 10485760 bytes")
c.Assert(SizeGreater(cfg, "test:size", uint(10*1024*1024)).Error(), check.Equals, "Property test:size can't be less than 10485760 bytes")
c.Assert(SizeGreater(cfg, "test:size", uint64(10*1024*1024)).Error(), check.Equals, "Property test:size can't be less than 10485760 bytes")
c.Assert(SizeGreater(cfg, "test:size", float64(10*1024*1024)).Error(), check.Equals, "Property test:size can't be less than 10485760 bytes")
c.Assert(SizeGreater(cfg, "test:size", false).Error(), check.Equals, "Validator knf.SizeGreater doesn't support input with type <bool> for checking test:size property")
c.Assert(SizeGreater(cfg, "test:size", uint64(1*1024*1024)), check.IsNil)

c.Assert(SizeLess(cfg, "test:size", 1*1024*1024).Error(), check.Equals, "Property test:size can't be greater than 1048576 bytes")
c.Assert(SizeLess(cfg, "test:size", int64(1*1024*1024)).Error(), check.Equals, "Property test:size can't be greater than 1048576 bytes")
c.Assert(SizeLess(cfg, "test:size", uint(1*1024*1024)).Error(), check.Equals, "Property test:size can't be greater than 1048576 bytes")
c.Assert(SizeLess(cfg, "test:size", uint64(1*1024*1024)).Error(), check.Equals, "Property test:size can't be greater than 1048576 bytes")
c.Assert(SizeLess(cfg, "test:size", float64(1*1024*1024)).Error(), check.Equals, "Property test:size can't be greater than 1048576 bytes")
c.Assert(SizeLess(cfg, "test:size", false).Error(), check.Equals, "Validator knf.SizeLess doesn't support input with type <bool> for checking test:size property")
c.Assert(SizeLess(cfg, "test:size", uint64(10*1024*1024)), check.IsNil)

c.Assert(InRange(cfg, "test:integer", Range{50, 100}).Error(), check.Equals, "Property test:integer must be in range 50-100")
c.Assert(InRange(cfg, "test:integer", Range{1, 100}), check.IsNil)
c.Assert(InRange(cfg, "test:integer", Range{uint(1), uint(100)}), check.IsNil)
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.7.1"
const VERSION = "13.8.0"

0 comments on commit 2b36109

Please sign in to comment.