shakacode / re-formality

Form validation tool for reason-react
https://re-formality.now.sh
MIT License
244 stars 35 forks source link

`result` type #38

Closed alex35mil closed 5 years ago

alex35mil commented 5 years ago

Before final 1.0.0 I want to discuss one more thing: result type.

type result('message) =
  | Valid
  | Invalid('message)
  | Optional;

From README:

You want to return Optional when optional field receives no value (e.g. value == ""). Valid and Optional states are explicitly differentiated since there's no reason to show success message/icon in UI when no value is provided.

Now it's not perfectly clear that both Valid & Optional are OK and only Invalid is show-stopper. It's also possible to mess something up by misusing Optional constructor. My proposal is to leverage Result type to make things perfectly clear:

type ok =
  | Valid
  | NoValue;

type validate = 'state => Belt.Result.t(ok, 'message);

let validate = state => switch (state.optional) {
  | "" => Ok(NoValue)
  | _ when !someCondition => Error("Nope")
  | _ => Ok(Valid)
};

Thoughts?

baransu commented 5 years ago

For me, it would be a great change. Using Belt.Result module with functionality like map/isError/isOk gives great foundations for userland abstractions over re-formality.

alex35mil commented 5 years ago

💯 agree, doing this.

alex35mil commented 5 years ago

Released in 1.0.0-beta.2

baransu commented 5 years ago

I'll upgrade my project to the latest version when I'll have some spare time. Probably weekend. Will create an issue in case of some problems. Thank you!