-
Notifications
You must be signed in to change notification settings - Fork 101
Add some extensions methods for validation #518
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
base: master
Are you sure you want to change the base?
Changes from 9 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,75 @@ | ||
namespace FSharpPlus | ||
|
||
[<RequireQualifiedAccess>] | ||
module RequiredValidation = | ||
open System | ||
open FSharpPlus.Data | ||
|
||
let inline validate error f v = | ||
if f v then | ||
Success v | ||
else | ||
Failure [ error ] | ||
|
||
let inline string error value = | ||
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'm unsure if just calling it |
||
validate (error value) (String.IsNullOrWhiteSpace >> not) value | ||
|
||
let inline greaterThan error min value = | ||
validate (error value) (flip (>) min) value | ||
|
||
let inline greaterOrEqualThan error min value = | ||
validate (error value) (flip (>=) min) value | ||
|
||
let inline email error value = | ||
let check (v: string) = | ||
try | ||
let _ = Net.Mail.MailAddress(v) | ||
true | ||
with | ||
| ex -> false | ||
|
||
validate (error value) check value | ||
|
||
let inline guid error value = | ||
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. this type of validator goes into the question if there is some more general class of nonEmpty restrictions? |
||
validate (error value) (fun v -> v <> Guid.Empty) value | ||
|
||
let inline object error value = | ||
let check value = box value <> null | ||
validate (error value) check | ||
|
||
let inline whenSome value checkWhenSome = | ||
match value with | ||
| Some v -> checkWhenSome v |> Validation.map Some | ||
| _ -> Success None | ||
|
||
let inline arrayValues values check = | ||
let validated : Validation<_,_> [] = | ||
values | ||
|> Array.map check | ||
validated | ||
|> sequence | ||
|> Validation.map Seq.toArray | ||
|
||
let inline listValues values check = | ||
let validated : List<Validation<_,_>> = | ||
values | ||
|> List.map check | ||
validated | ||
|> sequence | ||
|> Validation.map Seq.toArray | ||
|
||
let inline atLeastOne error = | ||
let check values = | ||
Seq.isEmpty values |> not | ||
|
||
validate error check | ||
|
||
let isSuccess = | ||
function | ||
| Success _ -> true | ||
| Failure _ -> false | ||
|
||
let isFailure = | ||
function | ||
| Success _ -> false | ||
| Failure _ -> true |
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.
We have this function already and it's being deprecated.
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.
#248