Skip to content

How to apply contracts to record field names #1458

Answered by vkleen
DrRuhe asked this question in Q&A
Discussion options

You must be logged in to vote

The from_predicate function doesn't give you a way of customizing error messages. You can write the contract explicitly like this:

let FruitsAreValid = fun label ingredients =>
  let check =
    ingredients
    |> std.record.fields
    |> std.array.fold_right
      (
        fun ingredient acc =>
          if !std.record.has_field ingredient fruits then
            { error = true, offending_field = ingredient }
          else
            acc
      )
      { error = false, offending_field = "" }
  in
  if check.error then
    std.contract.blame_with_message "the field '%{check.offending_field}' is invalid" label
  else
    ingredients

Unfortunately, I don't know of a stable way of making …

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@vkleen
Comment options

@DrRuhe
Comment options

@DrRuhe
Comment options

Answer selected by DrRuhe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants