New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow reusing validators #17
base: master
Are you sure you want to change the base?
Conversation
Some sort of contravariant map would allow reusing validators in different contexts. Alternative names proposed in Slack by norpan and gyzerok: - `comap` - `contraMap` - `select` (i.e. `Validate.select .name nameValidator`)
This issue comes from the discussion in Slack. In my project we are using On our forms we need to be able to do validation dynamically. It means that we don't show all the errors once user clicks submit, but show them as user goes through the form. With previous implementation we had following code validateFoo : foo -> List ValidationError
...
validateModel : Model -> List ValidationError
validateModel =
Validate.all [ .foo >> validateFoo ] This way we are able use validator both for validating specific field and for validating entire model. With the new API it is impossible to do so anyhow. |
Since original discussion in Slack, one more naming idea popped up in my head - validateModel =
Validate.all [ Validate.narrow .foo validateFoo ] |
With this PR one could compose type alias Model = { a: A, b: B }
validatorA: Validator a A
validatorB: Validator a B
validatorModel : Validator a Model I need this! 😃 👍 |
For the naming, the functionality can probably be compared to lenses (focus / optics), but only one direction is necessary. Or transformation. But naming is different in Elm, I know. I think you will make a good decision and I'm looking forward for the feature. https://www.schoolofhaskell.com/school/to-infinity-and-beyond/pick-of-the-week/basic-lensing https://en.m.wikipedia.org/wiki/Bidirectional_transformation |
Still missing this feature. Going to look for other libraries. |
Some sort of contravariant map would allow reusing validators in different
contexts.
Possible names, with input from @norpan and @gyzerok on slack:
preMap
comap
contraMap
(not used in Elm, so far)select
(i.e.Validate.select .name nameValidator
)I sort of like
select
, though I'd already typed it out withpreMap
and figured a PR was a better place for discussion.One alternative is to encourage users to write
Sometype -> Validator Error Sometype
style functions instead. I feel like that sort of defeats the purpose of providing a nice type for a validator, though.