Skip to content

Commit

Permalink
Merge pull request #35 from sheridanchris/misc-improvements
Browse files Browse the repository at this point in the history
Misc improvements / fixes
  • Loading branch information
pimbrouwers authored Jan 28, 2024
2 parents 71f7fc5 + 7dcaeb8 commit 6a28f13
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Validus/Core.fs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module ValidationMessages =
let seqEmpty field = sprintf "'%s' must be empty" field
let seqEqualsLen field len = sprintf "'%s' must be %i items" field len
let seqExists field = sprintf "'%s' must contain the specified item" field
let seqForAll field = sprintf "'%s' must only contain items that satisfy the predicate" field
let seqGreaterThanLen field min = sprintf "'%s' must be greater than %i items" field min
let seqGreaterThanOrEqualToLen field min = sprintf "'%s' must be greater than or equal to %i items" field min
let seqLessThanLen field max = sprintf "'%s' must be less than %i items" field max
Expand Down Expand Up @@ -209,9 +210,8 @@ module Validator =
(rule : 'a -> bool)
: Validator<'a, 'a> =
fun (field : string) (value : 'a) ->
let error = ValidationErrors.create field [ message field ]
if rule value then Ok value
else error |> Error
else [ message field ] |> ValidationErrors.create field |> Error

let success : Validator<'a, 'a> = fun field x -> Ok x
let fail msg : Validator<'a, 'a> = fun field x -> Error (ValidationErrors.create field [ msg field ])
Expand Down
17 changes: 11 additions & 6 deletions src/Validus/Validators.Default.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type DefaultComparisonValidator<'a when 'a
/// error message
member _.greaterThanOrEqualTo (min : 'a) (field : string) (input : 'a) =
let msg field = ValidationMessages.greaterThanOrEqualTo field min
x.greaterThan min msg field input
x.greaterThanOrEqualTo min msg field input

/// Value is less than provided max with the default error message
member _.lessThan (max : 'a) (field : string) (input : 'a) =
Expand All @@ -49,7 +49,7 @@ type DefaultComparisonValidator<'a when 'a
/// error message
member _.lessThanOrEqualTo (max : 'a) (field : string) (input : 'a) =
let msg field = ValidationMessages.lessThanOrEqualTo field max
x.lessThan max msg field input
x.lessThanOrEqualTo max msg field input

type DefaultStringValidator(x : StringValidator) =
inherit DefaultEqualityValidator<string>(x)
Expand Down Expand Up @@ -81,7 +81,7 @@ type DefaultStringValidator(x : StringValidator) =
/// value with the default error message
member _.greaterThanOrEqualToLen (min : int) (field : string) (input : string) =
let msg field = ValidationMessages.strGreaterThanOrEqualToLen field min
x.greaterThanLen min msg field input
x.greaterThanOrEqualToLen min msg field input

/// Validate string length is less than provided value with the
/// default error message
Expand All @@ -93,7 +93,7 @@ type DefaultStringValidator(x : StringValidator) =
/// with the default error message
member _.lessThanOrEqualToLen (max : int) (field : string) (input : string) =
let msg field = ValidationMessages.strLessThanOrEqualToLen field max
x.lessThanLen max msg field input
x.lessThanOrEqualToLen max msg field input

/// Validate string is not null or "" with the default error message
member _.notEmpty (field : string) (input : string) =
Expand Down Expand Up @@ -173,6 +173,11 @@ type DefaultSequenceValidator<'a, 'b when 'a : equality and 'b :> 'a seq>(x : Se
let msg field = ValidationMessages.seqExists field
x.exists predicate msg field input

/// Validate all elements in a sequence satisfiy the predicate
member _.forAll (predicate : 'a -> bool) (field: string) (input : 'b) =
let msg field = ValidationMessages.seqForAll field
x.forAll predicate msg field input

/// Validate sequence length is greater than provided value with the
/// default error message
member _.greaterThanLen (min : int) (field : string) (input : 'b) =
Expand All @@ -183,7 +188,7 @@ type DefaultSequenceValidator<'a, 'b when 'a : equality and 'b :> 'a seq>(x : Se
/// value with the default error message
member _.greaterThanOrEqualToLen (min : int) (field : string) (input : 'b) =
let msg field = ValidationMessages.seqGreaterThanOrEqualToLen field min
x.greaterThanLen min msg field input
x.greaterThanOrEqualToLen min msg field input

/// Validate sequence length is less than provided value with the
/// default error message
Expand All @@ -195,7 +200,7 @@ type DefaultSequenceValidator<'a, 'b when 'a : equality and 'b :> 'a seq>(x : Se
/// with the default error message
member _.lessThanOrEqualToLen (max : int) (field : string) (input : 'b) =
let msg field = ValidationMessages.seqLessThanOrEqualToLen field max
x.lessThanLen max msg field input
x.lessThanOrEqualToLen max msg field input

/// Validate sequence is not null or "" with the default error message
member _.notEmpty (field : string) (input : 'b) =
Expand Down
9 changes: 9 additions & 0 deletions src/Validus/Validators.fs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,15 @@ type SequenceValidator<'a, 'b when 'a : equality and 'b :> 'a seq>() =
: ValidationResult<'b> =
Validator.create message (Seq.exists predicate) field input

/// Validate all elements in a sequence satisfiy the predicate
member _.forAll
(predicate : 'a -> bool)
(message : ValidationMessage)
(field : string)
(input : 'b)
: ValidationResult<'b> =
Validator.create message (Seq.forall predicate) field input

/// Validate sequence length is greater than provided value
member _.greaterThanLen
(min : int)
Expand Down
12 changes: 12 additions & 0 deletions test/Validus.Tests/SequenceValidatorTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,15 @@ let ``(TestValidator.notEmpty) should produce Error`` () =
match TestValidator.notEmpty "Test" empty with
| Ok _ -> false
| Error _ -> true

[<Property>]
let ``(TestValidator.forAll) should produce Success`` () =
match TestValidator.forAll (fun number -> number <= 10) "Test" testSequence with
| Ok _ -> true
| Error _ -> false

[<Property>]
let ``(TestValidator.forAll) should produce Error`` () =
match TestValidator.forAll (fun number -> number % 2 = 0) "Test" testSequence with
| Ok _ -> false
| Error _ -> true

0 comments on commit 6a28f13

Please sign in to comment.