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

Conditional validation #24

Open
andys8 opened this issue Jul 19, 2018 · 6 comments
Open

Conditional validation #24

andys8 opened this issue Jul 19, 2018 · 6 comments

Comments

@andys8
Copy link

andys8 commented Jul 19, 2018

There is ifTrue : (subject -> Bool) -> error -> Validator error subject to return a fixed error, when a predicate is not true.

I think it's a good idea to add a helper to have validation only in the case a predicate holds.

Example

  • If the age of somebody is >= 18, the person has to be this tall (made up).
  • Different rules for different types of data

A helper would look like this example.

conditionalValidator : (a -> Bool) -> Validator e a -> Validator e a
conditionalValidator pred validator =
    Validate.fromErrors <|
        \value ->
            if pred value then
                Validate.validate validator value
            else
                []
@rtfeldman
Copy link
Owner

I like to make API decisions based on situations that have come up in practice.

Do you have any before/after examples of some code you've written that would have used this if it existed?

@andys8
Copy link
Author

andys8 commented Jul 19, 2018

The code above is what I'm using right now. The examples above are comparable to the real use case: Validating form input. Depending on a dropdown field "type". If one specific value is selected a list in another field has to contain at least one value, otherwise it doesn't matter. This is one case, and there are more. One can try to model some of those cases via types, but I think the capabilities are limited in elm, without refinement types.

Perhaps there is a another, better solution? Happy to hear it :)

@rtfeldman
Copy link
Owner

Sorry, to clarify - I'm looking for something really specific - not a hypothetical or comparable example, but rather the exact use case that came up in your application. 😄

So for example:

Depending on a dropdown field "type". If one specific value is selected a list in another field has to contain at least one value, otherwise it doesn't matter

What did the dropdown contain? What was the one specific value that was selected, and what was the other field that had to contain what value?

It's very important to me to understand the particulars of the motivating use case before making an API decision on this.

@andys8
Copy link
Author

andys8 commented Jul 19, 2018

@rtfeldman Let's talk in elm slack

@k-bx
Copy link

k-bx commented Dec 27, 2018

Possibly related: #37

@andys8
Copy link
Author

andys8 commented Dec 28, 2018

Having implemented this in another project in another language and coming back to this issue: Conditional could/should be possible using any.

Example from top:

If the age of somebody is >= 18, the person has to be this tall (made up).

any(person.age < 18, person has to be this tall)

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

No branches or pull requests

3 participants