Hey, me and @rauan were discussing today should validators in re-formality
be wrapped in abstractions like Min
, Max
, Email
, Required
etc.
Right now validator is implemented as simple function which should return validation result. E.g.
validate: (value, _state) =>
switch value {
| "" => {
valid: false,
message: Some("Uh oh, it's required!"),
meta: None
}
| _ when String.length(value) < minLength => {
valid: false,
message: Some("Too short"),
meta: None
}
| _ => {
valid: true,
message: Some("Nice!"),
meta: None
}
}
To provide some default validators I can make validate
a list
. Something like this:
validate: [
Required({ message: Some("Required field"), meta: None }),
Min(7, { message: Some("Must be at least 7"), meta: None }),
Custom((value, _state) =>
switch value {
| "invalid" => {
valid: false,
message: Some("That's invalid!"),
meta: None
}
| _ => {
valid: true,
message: Some("Nice!"),
meta: None
}
})
]
The question is: should I? Do default validators worth one more abstraction?
The main reason why I havenāt implemented it yet is b/c every time I need some more or less complex validation like email or credit card, I always end up using something custom but not standart regexes. Email: b/c it must use exactly the same regex as my server, credit card: b/c I use vendorās helpers (e.g. Stripeās). And Required
or Min
is so simple that Iām not sure if itās worth it.
But maybe Iām wrong and people actively use those?