diff --git a/example/App.res b/example/App.res index 546cde0..9f4f280 100644 --- a/example/App.res +++ b/example/App.res @@ -27,6 +27,8 @@ let makeForm = Formidable.make(~values=module(Values), ~error=module(I18n.Error) // In many cases, it's not needed, especially thanks to the Consumer component module Form = unpack(makeForm(~onSubmit=_values => (), ~onSubmitError=(_values, _errors) => (), ())) +module Validations = Validations.Make(Values) + 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 23d214e..9061e6e 100644 --- a/example/Validations.res +++ b/example/Validations.res @@ -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 = { + names: ["required"], + validator: ({label, value}) => + switch value { + | "" => #error(#error(("required", label))) + | _ => #ok(value) + }, + } + + let emailRegEx = %re("/.+@.+/") + + let email: Description.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))) + }, + } } diff --git a/src/Formidable.res b/src/Formidable.res index 6d457dd..02ac329 100644 --- a/src/Formidable.res +++ b/src/Formidable.res @@ -393,9 +393,7 @@ module Make = ( let validationNames = props["validations"]->Option.mapWithDefault([], validations => - validations->ArrayExtra.flatMap(validation => - validation->Validations.getNames->List.toArray - ) + validations->ArrayExtra.flatMap(Validations.getNames) ) let hasValidation = name => validationNames->Js.Array2.includes(name) diff --git a/src/FormidableValidations.res b/src/FormidableValidations.res index f580b52..94a02cc 100644 --- a/src/FormidableValidations.res +++ b/src/FormidableValidations.res @@ -28,7 +28,7 @@ module Validator = { module Description = { type t<'values, 'value, 'error> = { - names: list, + names: array, validator: Validator.t<'values, 'value, 'error>, } } @@ -43,7 +43,7 @@ let compose = ( {Description.names: names, validator}, {Description.names: names', validator: validator'}, ) => { - Description.names: names->List.concat(names'), + Description.names: names->Js.Array2.concat(names'), validator: field => switch validator(field) { | #ok(_) => validator'(field)