-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add some extensions methods for validation #518
base: master
Are you sure you want to change the base?
Changes from 3 commits
b8ca198
8ea0b26
cedb1b2
3762d16
d997501
42fc644
22e5f28
d6a9cec
0a69dc5
9868e74
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
namespace FSharpPlus | ||
|
||
[<RequireQualifiedAccess>] | ||
module Validations = | ||
rodriguestiago0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
open System | ||
open FSharpPlus.Data | ||
|
||
let inline validate errorMessage f v = | ||
if f v then | ||
Success v | ||
else | ||
Failure [ errorMessage ] | ||
|
||
let inline requireString propName = | ||
let errorMessage = | ||
sprintf "%s cannot be null, empty or whitespace." propName | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my experience using string as errors doesn't scale well, unless they are at the boundaries with the user. Having said that, we could add an additional parameter which is a function that creates the error, so the user is free to supply whatever structure he uses. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know to let the user define the Error to be added. It can be a string, int or whatever the developer wants. |
||
|
||
validate errorMessage (String.IsNullOrWhiteSpace >> not) | ||
|
||
let inline requireGreaterThan propName min = | ||
let errorMessage = | ||
sprintf "%s have to be greater or equal to '%d'." propName min | ||
|
||
validate errorMessage (flip (>) min) | ||
|
||
let inline requireGreaterOrEqualThan propName min = | ||
let errorMessage = | ||
sprintf "%s have to be greater or equal to '%d'." propName min | ||
|
||
validate errorMessage (flip (>=) min) | ||
|
||
let inline requireEmail propName = | ||
let errorMessage = | ||
sprintf "%s is not a valid email address." propName | ||
|
||
let check (v: string) = | ||
try | ||
let _ = Net.Mail.MailAddress(v) | ||
true | ||
with | ||
| ex -> false | ||
|
||
validate errorMessage check | ||
|
||
let inline requireGuid propName = | ||
validate (sprintf "%s is required" propName) (fun v -> v <> Guid.Empty) | ||
|
||
let inline requireObject propName = | ||
let check value = box value <> null | ||
validate (sprintf "%s is required" propName) check | ||
|
||
let inline requireWhenSome value checkWhenSome = | ||
match value with | ||
| Some v -> checkWhenSome v |> Validation.map Some | ||
| _ -> Success None | ||
|
||
let inline requireArrayValues values check = | ||
let validated : Validation<_,_> [] = | ||
values | ||
|> Array.map check | ||
validated | ||
|> sequence | ||
|> Validation.map Seq.toArray | ||
|
||
let inline requireListValues values check = | ||
let validated : List<Validation<_,_>> = | ||
values | ||
|> List.map check | ||
validated | ||
|> sequence | ||
|> Validation.map Seq.toArray | ||
|
||
let inline requireAtLeastOne propName = | ||
let check values = | ||
Seq.isEmpty values |> not | ||
|
||
let errorMessage = | ||
sprintf "%s should have at least one element'." propName | ||
|
||
validate errorMessage check |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this is just a special case of the
applicative
computation expression, closed toValidation<list<string>, 'T>
.I mean, right now you can just define it like:
let validator<'T> = applicative<Validation<list<string>, 'T>>
and you'll have the same methods and more available.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually the
bind
is not part of theapplicative
CE, butValidation
is not a monad, as it doesn't obey monad rules. However in order to get the short-circuit behavior you can uselet validator<'T> = monad'<Result<'T, List<string>>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe an alias then?