Signed-off-by: Émile Ré <nemile.re@gmail.com>
This commit is contained in:
Émile Ré
2025-12-11 16:58:42 +01:00
parent 5eddc95703
commit cab9d6f2ed
5 changed files with 33 additions and 0 deletions

View File

@@ -225,6 +225,25 @@ func OneOfSlice[T any](allowed []T) ValidatorFunc {
}
}
// NotOneOfSlice validates that a value is not one of the values in the slice.
// Accepts a slice of any type. Compares by value first, then by string representation.
func NotOneOfSlice[T any](disallowed []T) ValidatorFunc {
oneOfSlice := OneOfSlice(disallowed)
return func(value any) *ValidationError {
err := oneOfSlice(disallowed)
if err == nil {
return newValidationError(
ErrorCodeInvalidEnum,
"must not be one of the disallowed values",
)
}
return nil
}
}
// OneOf validates that a value is one of the allowed values.
// Accepts strings or types that implement fmt.Stringer as variadic arguments.
func OneOf(allowed ...any) ValidatorFunc {