Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

zwilias
Copy link

@zwilias zwilias commented Feb 7, 2018

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 with preMap 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.

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`)
@gyzerok
Copy link

gyzerok commented Feb 7, 2018

This issue comes from the discussion in Slack.

In my project we are using elm-validate and after recent update it appeared to be impossible to use it in a samish way. I will post here our use-case, so you will have more information for API design.

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.

@gyzerok
Copy link

gyzerok commented Feb 7, 2018

Since original discussion in Slack, one more naming idea popped up in my head - narrow. Example:

validateModel =
    Validate.all [ Validate.narrow .foo validateFoo ]

@andys8
Copy link

andys8 commented Jul 19, 2018

With this PR one could compose validatorA and validatorB to create validatorModel, right?

type alias Model = { a: A, b: B }

validatorA: Validator a A
validatorB: Validator a B
validatorModel : Validator a Model

I need this! 😃 👍

@rtfeldman
Copy link
Owner

rtfeldman commented Jul 19, 2018

How about by as a name?

userValidator : Validator String User
userValidator =
    Validate.by .username usernameValidator

cc @gyzerok @zwilias

@andys8
Copy link

andys8 commented Jul 19, 2018

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

@andys8
Copy link

andys8 commented Nov 21, 2019

Still missing this feature. Going to look for other libraries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants