Skip to content

Commit

Permalink
Merge pull request #512 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 13.8.0
  • Loading branch information
andyone authored Oct 14, 2024
2 parents 37713aa + 2b36109 commit fcf8714
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## Changelog

### [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)

- `[fmtutil]` Added support of `kk`, `kkk`, `kib`, `mib`, `gib`, and `tib` in `ParseSize`
Expand Down
86 changes: 86 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 @@ -197,6 +203,21 @@ func validatorLess(config knf.IConfig, prop string, value any) error {
return fmt.Errorf("Property %s can't be greater than %d", prop, t)
}

case int64:
if config.GetI64(prop) > t {
return fmt.Errorf("Property %s can't be greater than %d", prop, t)
}

case uint:
if config.GetU(prop) > t {
return fmt.Errorf("Property %s can't be greater than %d", prop, t)
}

case uint64:
if config.GetU64(prop) > t {
return fmt.Errorf("Property %s can't be greater than %d", prop, t)
}

case float64:
if config.GetF(prop) > t {
return fmt.Errorf("Property %s can't be greater than %g", prop, t)
Expand All @@ -216,6 +237,21 @@ func validatorGreater(config knf.IConfig, prop string, value any) error {
return fmt.Errorf("Property %s can't be less than %d", prop, t)
}

case int64:
if config.GetI64(prop) < t {
return fmt.Errorf("Property %s can't be less than %d", prop, t)
}

case uint:
if config.GetU(prop) < t {
return fmt.Errorf("Property %s can't be less than %d", prop, t)
}

case uint64:
if config.GetU64(prop) < t {
return fmt.Errorf("Property %s can't be less than %d", prop, t)
}

case float64:
if config.GetF(prop) < t {
return fmt.Errorf("Property %s can't be less than %g", prop, t)
Expand All @@ -228,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
26 changes: 26 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 @@ -167,15 +171,37 @@ func (s *ValidatorSuite) TestBasicValidators(c *check.C) {
c.Assert(Set(cfg, "test:string", nil), check.IsNil)

c.Assert(Less(cfg, "test:integer", 5).Error(), check.Equals, "Property test:integer can't be greater than 5")
c.Assert(Less(cfg, "test:integer", int64(5)).Error(), check.Equals, "Property test:integer can't be greater than 5")
c.Assert(Less(cfg, "test:integer", uint(5)).Error(), check.Equals, "Property test:integer can't be greater than 5")
c.Assert(Less(cfg, "test:integer", uint64(5)).Error(), check.Equals, "Property test:integer can't be greater than 5")
c.Assert(Less(cfg, "test:integer", 30), check.IsNil)
c.Assert(Less(cfg, "test:float", 5.1).Error(), check.Equals, "Property test:float can't be greater than 5.1")
c.Assert(Less(cfg, "test:float", 30.1), check.IsNil)

c.Assert(Greater(cfg, "test:integer", 30).Error(), check.Equals, "Property test:integer can't be less than 30")
c.Assert(Greater(cfg, "test:integer", int64(30)).Error(), check.Equals, "Property test:integer can't be less than 30")
c.Assert(Greater(cfg, "test:integer", uint(30)).Error(), check.Equals, "Property test:integer can't be less than 30")
c.Assert(Greater(cfg, "test:integer", uint64(30)).Error(), check.Equals, "Property test:integer can't be less than 30")
c.Assert(Greater(cfg, "test:integer", 5), check.IsNil)
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.0"
const VERSION = "13.8.0"

0 comments on commit fcf8714

Please sign in to comment.