diff --git a/example/App.res b/example/App.res index 14b455e..568842b 100644 --- a/example/App.res +++ b/example/App.res @@ -32,6 +32,10 @@ module MakeForm = Formidable.Make(Validations.Label, I18n.Error) // all the validations and input components will just work, and the creation of a new form is trivial module Form = MakeForm(Values) +module Validations = Validations.Make(Values) + +open Formidable.Validations + open Validations // Validations can be defined in an other module, and re-used easily diff --git a/example/Validations.res b/example/Validations.res index ad05cab..efdd696 100644 --- a/example/Validations.res +++ b/example/Validations.res @@ -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))) + }, + ) +} diff --git a/src/FormidableValidations.res b/src/FormidableValidations.res index c6b82b2..ec938a0 100644 --- a/src/FormidableValidations.res +++ b/src/FormidableValidations.res @@ -38,6 +38,17 @@ The second one is used only internally when composing validations`) } type t<'values, 'value, 'error, 'label> = (kind<'label>, Validator.t<'values, 'value, 'error>) + + module Make = ( + Values: { + type t + }, + Label: { + type t + }, + ) => { + let make = (~name, validator): t => (#name(name), validator) + } } type t<'values, 'value, 'error, 'label> = (