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

Improve validation creation #6

Closed
gaku-sei opened this issue Aug 7, 2020 · 0 comments · Fixed by #30
Closed

Improve validation creation #6

gaku-sei opened this issue Aug 7, 2020 · 0 comments · Fixed by #30

Comments

@gaku-sei
Copy link
Contributor

gaku-sei commented Aug 7, 2020

The current way to create a validation is, for instance:

let required =
  ( [ "required" ],
    fun { label; value } ->
      if String.isEmpty value then `error (`error ("required", label))
      else `ok value )

Which comes with 2 downsides:

  • Verbose and bizarre: why do we need the [] around the "required" name?
  • Unsafe: what if one just creates a validation with no name, or many names? (These could be an actual use case one day, but not for now)

There are several solutions to this problem:

  • Use the NonEmptyList type under the hood
  • Then create a function/operator to make validation creation simpler:
let required =
  "required" // fun { label; value } ->
    if String.isEmpty value then `error (`error ("required", label))
    else `ok value )

The above solution solves all the issues so far but it doesn't compile (the polymorphic type values is said "weak").

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