-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(validations): Use functor pattern to clean the validations
- Loading branch information
Kevin COMBRIAT
committed
Jan 15, 2021
1 parent
2090453
commit d309352
Showing
3 changed files
with
46 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,42 @@ | ||
include Formidable.Validations | ||
open Formidable.Validations | ||
|
||
module Label = { | ||
type t = [#required | #email | #equals] | ||
} | ||
|
||
let required = ( | ||
#name(#required), | ||
({Validator.Args.label: label, value}) => | ||
module Make = ( | ||
Values: { | ||
type t | ||
}, | ||
) => { | ||
module Description = Description.Make(Values, Label) | ||
|
||
let required = Description.make(~name=#required, ({label, value}) => | ||
switch value { | ||
| "" => #error(#error(("required", label))) | ||
| _ => #ok(value) | ||
}, | ||
) | ||
} | ||
) | ||
|
||
let emailRegEx = %re("/.+@.+/") | ||
let emailRegEx = %re("/.+@.+/") | ||
|
||
let email = ( | ||
#name(#email), | ||
({Validator.Args.label: label, value}) => | ||
if emailRegEx->Js.Re.test_(value) { | ||
#ok(value) | ||
} else { | ||
#error(#error(("email", label))) | ||
}, | ||
) | ||
let email = ( | ||
#name(#email), | ||
({Validator.Args.label: label, value}) => | ||
if emailRegEx->Js.Re.test_(value) { | ||
#ok(value) | ||
} else { | ||
#error(#error(("email", label))) | ||
}, | ||
) | ||
|
||
let equals = lens => ( | ||
#name(#equals), | ||
({Validator.Args.label: label, value, values}) => | ||
if lens.Optic.Lens.get(values) == value { | ||
#ok(value) | ||
} else { | ||
#error(#error(("equals", label))) | ||
}, | ||
) | ||
let equals = lens => ( | ||
#name(#equals), | ||
({Validator.Args.label: label, value, values}) => | ||
if lens.Optic.Lens.get(values) == value { | ||
#ok(value) | ||
} else { | ||
#error(#error(("equals", label))) | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters