tweag / nickel

Better configuration for less
https://nickel-lang.org/
MIT License
2.23k stars 85 forks source link

Introduce validators for building custom contracts #1970

Closed yannham closed 1 week ago

yannham commented 2 weeks ago

Depends on #1964. Closes #1957.

Motivation

There's currently two constructors for custom contracts: from_predicate, which takes a boolean predicate and turn it into a contract, and custom, which takes a general partial identity.

The former is nice because it's simple to write (and will support boolean operations). However, it can't customize error messages, which is important for developer experience.

The latter is the most general, but it's not very natural to write, as we need to call blame and use label helpers to attach error data, and because we need to return the original value instead of just "ok" or "not ok".

This PR introduces a third form, in the middle, called validators. Validators leverage the enum variants of Nickel to cover the case of eager contracts (like predicates) but with all the customization power of custom. Basically, instead of returning a boolean, a validator returns [| 'Ok, 'Error <ErrorData> |] where ErrorData mimics the structure of a label, but as a normal Nickel record. Because it's eager, it's trivially convertible to a predicate, so validators will be usable with boolean operators.

The hope is that many concrete cases are actually validator, which are simpler to write than general custom contracts, and have much better properties (they can be negated, or-ed, etc.).

Content

This PR:

dpulls[bot] commented 1 week ago

:tada: All dependencies have been resolved !