Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ and a conversion that doesn't fit the field doesn't compile:

| Conversion | Fields | Rules |
|---|---|---|
| `str.Of` | `string`, `*string`, named strings | `Required`, lengths in bytes and runes, `AnyOf`, `Prefix`, `Suffix`, `Contains`, `Regexp`, `Email`, `URL`, `IP`, `CIDR`, `MAC`, `Hostname`, `UUID`, `E164`, `Base64`, `JSON`, `Semver`, `HexColor`, `Datetime`, `CountryCode`, `CurrencyCode`, `CreditCard`, `Trim`... |
| `num.Of`, `num.Ptr` | integers, floats, named numbers, pointers to them | `Required`, `Min`, `Max`, `AnyOf`, `AnyOfInterval`, `Latitude`, `Longitude` |
| `str.Of` | `string`, `*string`, named strings | `Required`, lengths in bytes and runes, `AnyOf`, `Prefix`, `Suffix`, `Contains`, `Excludes`, `Alpha`, `Digits`, `Numeric`, `Hexadecimal`, `Regexp`, `Email`, `URL`, `IP`, `CIDR`, `MAC`, `Hostname`, `UUID`, `E164`, `Base64`, `JSON`, `Semver`, `HexColor`, `Datetime`, `CountryCode`, `CurrencyCode`, `CreditCard`, `Trim`... |
| `num.Of`, `num.Ptr` | integers, floats, named numbers, pointers to them | `Required`, `Min`, `Max`, `GreaterThan`, `LessThan`, `AnyOf`, `AnyOfInterval`, `Latitude`, `Longitude` |
| `timex.Of` | `time.Time`, `*time.Time` | `Required`, `Before`, `After`, `Between`, `InPast`, `InFuture` |
| `eq.Of`, `eq.Ptr` | any `comparable`: enums, bools | `Required`, `OneOf`, `NotOneOf`, `Equal`, `NotEqual` |
| `vuuid.Of` | `uuid.UUID`, `*uuid.UUID`, the separate module `github.com/insei/valigo/rules/uuid` | `Required`, `AnyOf` |
Expand Down
11 changes: 8 additions & 3 deletions docs/features/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,17 @@ Output:
| `string.max_len`, `string.max_runes` | `max` |
| `string.any_of` | `values` |
| `string.prefix`, `string.suffix`, `string.contains` | `prefix`, `suffix`, `substring` |
| `string.equal_field` | `field` |
| `string.equal_field`, `string.not_equal_field` | `field` |
| `string.excludes`, `string.not_prefix`, `string.not_suffix` | `substring`, `prefix`, `suffix` |
| `string.contains_any`, `string.excludes_any` | `chars` |
| `string.equal_fold` | `value` |
| `string.alpha`, `string.digits`, `string.numeric`, `string.hexadecimal` | — |
| `string.base64`, `string.base64url`, `string.json`, `string.semver`, `string.cidr`, `string.mac`, `string.e164`, `string.hex_color`, `string.ascii`, `string.lowercase`, `string.uppercase`, `string.country_code`, `string.currency_code`, `string.luhn`, `string.credit_card` | — |
| `string.datetime` | `layout` |
| `number.required` | — |
| `number.min`, `number.max` | `min`, `max` |
| `number.min_field`, `number.max_field` | `field` |
| `number.min_field`, `number.max_field`, `number.greater_field`, `number.less_field` | `field` |
| `number.greater_than`, `number.less_than` | `min`, `max` |
| `number.any_of` | `values` |
| `number.interval` | `min`, `max` |
| `number.latitude`, `number.longitude` | — |
Expand All @@ -99,7 +104,7 @@ Output:
| `value.required`, `value.unique` | — |
| `value.one_of`, `value.not_one_of` | `values` |
| `value.equal`, `value.not_equal` | `value` |
| `value.equal_field` | `field` |
| `value.equal_field`, `value.not_equal_field` | `field` |
| `uuid.required` | — |
| `uuid.any_of` | `values` |
| `slice.required`, `map.required` | — |
Expand Down
2 changes: 2 additions & 0 deletions docs/features/numbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Slices of numbers are converted by `num.Slice`: `b.Slice(&p.Sizes).As(num.Slice)
| `Required()` | `num.Of`: the value is zero; `num.Ptr`: the value is absent | Should be fulfilled |
| `Min(n)` | the value is less than `n` | Cannot be less than %v |
| `Max(n)` | the value is greater than `n` | Cannot be greater than %v |
| `GreaterThan(n)`, `LessThan(n)` | the value isn't greater / less than `n` | Should be greater than %v, Should be less than %v |
| `GreaterField(&m.X)`, `LessField(&m.X)` | the value isn't greater / less than the value of the field `X` | Should be greater than %v, Should be less than %v |
| `MinField(&m.X)`, `MaxField(&m.X)` | the value is less or greater than the value of the field `X`, see [Rules on other fields](fields.md) | Cannot be less than %v, Cannot be greater than %v |
| `AnyOf(values...)` | the value isn't one of `values` | Only %v values is allowed |
| `AnyOfInterval(begin, end)` | the value is outside `[begin, end]`, both ends are included | Only interval[%v - %v] is allowed |
Expand Down
4 changes: 2 additions & 2 deletions docs/features/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ Every `shared.Constraint` has the configured struct, the field, the path from th
|---|---|
| `str`: `Required` | `required`, `minLength: 1` |
| `str`: `MinRunes`, `MaxRunes`, `MaxLen` | `minLength`, `maxLength` |
| `str`: `Regexp`, `Prefix`, `Suffix`, `Contains`, `Alphanumeric`, `Semver`, `E164`, `HexColor` | `pattern` |
| `str`: `Regexp`, `Prefix`, `Suffix`, `Contains`, `Alphanumeric`, `Alpha`, `Digits`, `Numeric`, `Hexadecimal`, `Semver`, `E164`, `HexColor` | `pattern` |
| `str`: `Email`, `URL`, `IPv4`, `IPv6`, `Hostname`, `UUID` | `format`: `email`, `uri`, `ipv4`, `ipv6`, `hostname`, `uuid` |
| `str`: `Datetime(time.RFC3339)`, `Datetime(time.DateOnly)` | `format`: `date-time`, `date` |
| `str`: `Base64`, `Base64URL` | `contentEncoding` |
| `str`, `num`, `uuid`: `AnyOf`; `eq`: `OneOf` | `enum` |
| `eq`: `Equal` | `const` |
| `num`: `Min`, `Max`, `AnyOfInterval`, `Latitude`, `Longitude` | `minimum`, `maximum` |
| `num`: `Min`, `Max`, `AnyOfInterval`, `Latitude`, `Longitude`, `GreaterThan`, `LessThan` | `minimum`, `maximum`, `exclusiveMinimum`, `exclusiveMaximum` |
| `num`, `eq`, `uuid`: `Required` | `required` |
| slices: `Required`, `MinLen`, `MaxLen`, `Unique`, `Each` | `required` and `minItems: 1`, `minItems`, `maxItems`, `uniqueItems`, `items` |
| maps: `Required`, `MinLen`, `MaxLen`, `EachKey`, `EachValue` | `required` and `minProperties: 1`, `minProperties`, `maxProperties`, `propertyNames`, `additionalProperties` |
Expand Down
8 changes: 8 additions & 0 deletions docs/features/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,22 @@ b.Slice(&u.Emails).As(str.Slice).MaxLen(3).Email()
| `MaxRunes(n)` | the number of characters (runes) is greater than `n` | Cannot be longer than %d characters |
| `AnyOf(values...)` | the value isn't one of `values` | Only %s values is allowed |
| `EqualField(&m.X)` | the value isn't equal to the value of the string field `X`, see [Rules on other fields](fields.md) | Should be equal to %s |
| `NotEqualField(&m.X)` | the value is equal to the value of the string field `X` | Cannot be equal to %s |
| `EqualFold(s)` | the value isn't equal to `s` ignoring case, by Unicode case folding | Should be equal to %s ignoring case |
| `Prefix(s)`, `Suffix(s)`, `Contains(s)` | the value doesn't start with, end with or contain `s` | Should start with %s, Should end with %s, Should contain %s |
| `NotPrefix(s)`, `NotSuffix(s)`, `Excludes(s)` | the value starts with, ends with or contains `s` | Cannot start with %s, Cannot end with %s, Cannot contain %s |
| `ContainsAny(chars)`, `ExcludesAny(chars)` | the value has none / one of the characters of `chars` | Should contain any of the characters %s, Cannot contain the characters %s |
| `Regexp(re, opts...)` | the value doesn't match `re` | Doesn't match required regexp pattern |
| `Email()` | the value isn't an email address | Should be email address |
| `URL()` | the value isn't an absolute URL with a scheme and a host | Should be URL |
| `IP()`, `IPv4()`, `IPv6()` | the value isn't an IP address of the version | Should be IP address, Should be IPv4 address, Should be IPv6 address |
| `Hostname()` | the value isn't a hostname by RFC 1123 | Should be hostname |
| `UUID()` | the value isn't a UUID in the canonical form, any case | Should be UUID |
| `Alphanumeric()` | the value is empty or has characters other than ASCII letters and digits | Should contain only letters and digits |
| `Alpha()` | the value is empty or has characters other than ASCII letters | Should contain only letters |
| `Digits()` | the value is empty or has characters other than ASCII digits, `0042` | Should contain only digits |
| `Numeric()` | the value isn't a decimal number with an optional sign and fraction, `-12`, `+3.5` | Should be a number |
| `Hexadecimal()` | the value isn't a hexadecimal number with an optional `0x`, `ff`, `0x1A` | Should be hexadecimal |
| `ASCII()` | the value is empty or has non-ASCII characters | Should contain only ASCII characters |
| `Lowercase()`, `Uppercase()` | the value is empty or has upper / lower case letters | Should be lowercase, Should be uppercase |
| `Base64()`, `Base64URL()` | the value isn't base64 of RFC 4648 with the standard alphabet and padding / with the URL alphabet, padding optional | Should be base64 encoded, Should be base64url encoded |
Expand Down
1 change: 1 addition & 0 deletions docs/features/values.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ For strings and numbers [`str.Of`](strings.md) and [`num.Of`](numbers.md) have m
| `Equal(v)` | the value isn't `v` | Should be %v |
| `NotEqual(v)` | the value is `v` | Cannot be %v |
| `EqualField(&m.X)` | the value isn't equal to the value of the field `X`, see [Rules on other fields](fields.md) | Should be equal to %s |
| `NotEqualField(&m.X)` | the value is equal to the value of the field `X` | Cannot be equal to %s |
| `Custom(fn)` | `fn` returns errors | your messages, see [Custom rules](custom.md) |
| `When(fn)` | — | the next rules are applied only if `fn` returns true, see [Conditions](conditions.md) |

Expand Down
8 changes: 8 additions & 0 deletions rules/eq/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
equalLocaleKey = "validation:value:Should be %v"
notEqualLocaleKey = "validation:value:Cannot be %v"
equalFieldKey = "validation:value:Should be equal to %s"
notEqualFieldKey = "validation:value:Cannot be equal to %s"
)

// BaseConfigurator configures rules for a comparable value.
Expand Down Expand Up @@ -78,6 +79,13 @@ func (i *BaseConfigurator[T]) EqualField(other *T) *BaseConfigurator[T] {
return i
}

// NotEqualField checks if the value differs from the value of another field of the object:
// b.Field(&m.Target).As(eq.Of).NotEqualField(&m.Source).
func (i *BaseConfigurator[T]) NotEqualField(other *T) *BaseConfigurator[T] {
i.f.CheckWithMsg(other, func(v, o T) bool { return v != o }, shared.M("value.not_equal_field", notEqualFieldKey))
return i
}

// NotEqual checks if the value is not equal to forbidden.
func (i *BaseConfigurator[T]) NotEqual(forbidden T) *BaseConfigurator[T] {
i.f.CheckMsg(func(v T) bool { return v != forbidden }, shared.M("value.not_equal", notEqualLocaleKey, shared.P("value", forbidden)))
Expand Down
4 changes: 2 additions & 2 deletions rules/eq/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ func TestZeroAllocationsOnValid(t *testing.T) {
func TestCodes(t *testing.T) {
v := valigo.New()
valigo.Configure[account](v, func(c valigo.Configurator[account], m *account) {
c.Field(&m.Status).As(eq.Of).Required().OneOf(active).NotOneOf("").Equal(blocked).NotEqual("").EqualField(&m.Previous)
c.Field(&m.Status).As(eq.Of).Required().OneOf(active).NotOneOf("").Equal(blocked).NotEqual("").EqualField(&m.Previous).NotEqualField(&m.Status)
c.Slice(&m.Roles).As(eq.Slice).Unique()
})
var codes []string
for _, err := range v.ValidateTyped(context.Background(), &account{Previous: active, Roles: []status{active, active}}) {
codes = append(codes, err.Code)
}
assert.Equal(t, []string{"value.required", "value.one_of", "value.not_one_of", "value.equal", "value.not_equal", "value.equal_field", "value.unique"}, codes)
assert.Equal(t, []string{"value.required", "value.one_of", "value.not_one_of", "value.equal", "value.not_equal", "value.equal_field", "value.not_equal_field", "value.unique"}, codes)
}
30 changes: 30 additions & 0 deletions rules/num/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const (
anyOfIntervalLocalKey = "validation:num:Only interval[%v - %v] is allowed"
latitudeLocaleKey = "validation:num:Should be latitude"
longitudeLocaleKey = "validation:num:Should be longitude"
greaterLocaleKey = "validation:num:Should be greater than %v"
lessLocaleKey = "validation:num:Should be less than %v"
)

// Number is a constraint for numeric types supported by BaseConfigurator.
Expand Down Expand Up @@ -69,6 +71,34 @@ func (i *BaseConfigurator[N]) Min(minNum N) *BaseConfigurator[N] {
return i
}

// GreaterThan checks if the number is greater than n.
func (i *BaseConfigurator[N]) GreaterThan(n N) *BaseConfigurator[N] {
i.f.Schema("exclusiveMinimum", n)
i.f.CheckMsg(func(v N) bool { return v > n }, shared.M("number.greater_than", greaterLocaleKey, shared.P("min", n)))
return i
}

// LessThan checks if the number is less than n.
func (i *BaseConfigurator[N]) LessThan(n N) *BaseConfigurator[N] {
i.f.Schema("exclusiveMaximum", n)
i.f.CheckMsg(func(v N) bool { return v < n }, shared.M("number.less_than", lessLocaleKey, shared.P("max", n)))
return i
}

// GreaterField checks if the number is greater than the value of another numeric field of the object:
// b.Field(&m.Max).As(num.Of).GreaterField(&m.Min).
func (i *BaseConfigurator[N]) GreaterField(other *N) *BaseConfigurator[N] {
i.f.CheckWithMsg(other, func(v, o N) bool { return v > o }, shared.M("number.greater_field", greaterLocaleKey))
return i
}

// LessField checks if the number is less than the value of another numeric field of the object:
// b.Field(&m.Min).As(num.Of).LessField(&m.Max).
func (i *BaseConfigurator[N]) LessField(other *N) *BaseConfigurator[N] {
i.f.CheckWithMsg(other, func(v, o N) bool { return v < o }, shared.M("number.less_field", lessLocaleKey))
return i
}

// MinField checks if the number is not less than the value of another numeric field of the object:
// b.Field(&m.Price).As(num.Of).MinField(&m.MinPrice).
func (i *BaseConfigurator[N]) MinField(other *N) *BaseConfigurator[N] {
Expand Down
5 changes: 3 additions & 2 deletions rules/num/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@ func TestCodes(t *testing.T) {
v := valigo.New()
valigo.Configure[numbers](v, func(c valigo.Configurator[numbers], m *numbers) {
c.Field(&m.Int).As(num.Of).Required().Min(5).Max(-1).MinField(&m.Min).MaxField(&m.Max).AnyOf(7).AnyOfInterval(1, 2)
c.Field(&m.Float64).As(num.Of).Latitude().Longitude()
c.Field(&m.Float64).As(num.Of).Latitude().Longitude().GreaterThan(300).LessThan(100).GreaterField(&m.Float64).LessField(&m.Float64)
})
var codes []string
for _, err := range v.ValidateTyped(context.Background(), &numbers{Min: 1, Max: -1, Float64: 200}) {
codes = append(codes, err.Code)
}
assert.Equal(t, []string{"number.required", "number.min", "number.max", "number.min_field", "number.max_field", "number.any_of", "number.interval", "number.latitude", "number.longitude"}, codes)
assert.Equal(t, []string{"number.required", "number.min", "number.max", "number.min_field", "number.max_field", "number.any_of", "number.interval", "number.latitude", "number.longitude",
"number.greater_than", "number.less_than", "number.greater_field", "number.less_field"}, codes)
}
Loading
Loading