-
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 14, 2021
1 parent
7f5e2bb
commit 7b2d000
Showing
4 changed files
with
38 additions
and
32 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,32 +1,38 @@ | ||
include Formidable.Validations | ||
|
||
let required = { | ||
Description.names: list{"required"}, | ||
validator: ({label, value}) => | ||
switch value { | ||
| "" => #error(#error(("required", label))) | ||
| _ => #ok(value) | ||
}, | ||
module type Values = { | ||
type t | ||
} | ||
|
||
let emailRegEx = %re("/.+@.+/") | ||
module Make = (Values: Values) => { | ||
include Formidable.Validations | ||
|
||
let email = { | ||
Description.names: list{"email"}, | ||
validator: ({label, value}) => | ||
if emailRegEx->Js.Re.test_(value) { | ||
#ok(value) | ||
} else { | ||
#error(#error(("email", label))) | ||
}, | ||
} | ||
let required: Description.t<Values.t, _, _> = { | ||
names: ["required"], | ||
validator: ({label, value}) => | ||
switch value { | ||
| "" => #error(#error(("required", label))) | ||
| _ => #ok(value) | ||
}, | ||
} | ||
|
||
let emailRegEx = %re("/.+@.+/") | ||
|
||
let email: Description.t<Values.t, _, _> = { | ||
names: ["email"], | ||
validator: ({label, value}) => | ||
if emailRegEx->Js.Re.test_(value) { | ||
#ok(value) | ||
} else { | ||
#error(#error(("email", label))) | ||
}, | ||
} | ||
|
||
let equals = lens => { | ||
Description.names: list{"equals"}, | ||
validator: ({label, value, values}) => | ||
if lens.Optic.Lens.get(values) == value { | ||
#ok(value) | ||
} else { | ||
#error(#error(("equals", label))) | ||
}, | ||
let equals = lens => { | ||
Description.names: ["equals"], | ||
validator: ({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
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