-
Notifications
You must be signed in to change notification settings - Fork 92
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
Introduce validators for building custom contracts #1970
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
yannham
changed the title
Feat/contract from validator
Introduce validators for building custom contracts
Jun 19, 2024
jneem
approved these changes
Jun 21, 2024
🎉 All dependencies have been resolved ! |
yannham
force-pushed
the
feat/contract-from-validator
branch
from
June 24, 2024 12:37
7c96286
to
3e0476e
Compare
yannham
force-pushed
the
feat/contract-from-validator
branch
from
June 24, 2024 12:38
3e0476e
to
f348ded
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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, andcustom
, 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> |]
whereErrorData
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:
Validator
%contract/from_validator%
, as well as the corresponding stdlib wrapperstd.contract.from_validator