From 2b3610946bd33be30c24ed0ff6fc3edd1bb61a0f Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 15 Oct 2024 01:28:39 +0300 Subject: [PATCH] [knf/validators] Add more validators --- CHANGELOG.md | 4 ++- knf/validators/validators.go | 56 +++++++++++++++++++++++++++++++ knf/validators/validators_test.go | 20 +++++++++++ version.go | 2 +- 4 files changed, 80 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f40c1c25..bd58f822 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/knf/validators/validators.go b/knf/validators/validators.go index fc96e48b..f77dd71d 100644 --- a/knf/validators/validators.go +++ b/knf/validators/validators.go @@ -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 @@ -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) diff --git a/knf/validators/validators_test.go b/knf/validators/validators_test.go index 3df987d8..330320e5 100644 --- a/knf/validators/validators_test.go +++ b/knf/validators/validators_test.go @@ -60,6 +60,9 @@ test1: 1 test4: ABC test5: true +[size] + test1: 3mb + [comment] test1: 100 # test2: 100 @@ -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) @@ -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 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 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) diff --git a/version.go b/version.go index ad082a46..2ffec021 100644 --- a/version.go +++ b/version.go @@ -8,4 +8,4 @@ package ek // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "13.7.1" +const VERSION = "13.8.0"