Skip to main content

Writing schema rules

The types of a schema check single values: a number is a number, a string is not too long or one of a fixed list of values (enum). Rules check everything else, e.g. that one property is smaller than another, that a value never decreases, or that a file is an image. This guide continues the Place schema of defining a schema and adds a rule to it.

Rules are Expr Lang expressions. Like schemas, they need a token with the capability schema.manage.

Creating a rule

A rule has a name, an expression which returns true if the write is fine, and an optional description. The following rule makes sure that the minimum temperature of a place is not above its maximum. It is added to the existing schema with PATCH /schema/<type>, here PATCH /schema/Place:

{
"rules": [
{
"name": "temperature-range",
"expression": "document.climate?.temperatureMin == nil || document.climate?.temperatureMax == nil || document.climate.temperatureMin <= document.climate.temperatureMax ? true : \"The minimum temperature must not be above the maximum temperature.\"",
"description": "The minimum temperature is not above the maximum temperature."
}
]
}

The expression reads the element as it will be after the write from the variable document. As climate and its temperatures are optional, the rule first checks that they are present: nil stands for a missing value. Instead of true, a rule can also return a string, which then becomes the error message. All variables, e.g. previous for the values before an update, are listed in the rules reference.

Rules are part of the schema, so they can also be written together with the properties with PUT /schema/<type>. They apply immediately, no migration is needed.

Note: PATCH /schema/<type> is a JSON merge patch, which replaces lists as a whole. Sending rules therefore replaces all existing rules of the schema; include the rules which should be kept.

Seeing a rule fail

A place whose minimum temperature is above its maximum is now rejected:

{
"type": "Place",
"data": {
"name": "Frostville",
"location": "POINT (10 54)",
"climate": {
"temperatureMin": 12.5,
"temperatureMax": 3
}
}
}

The response names the rule and contains its message:

{
"type": "http://api/error/400/bad-content",
"title": "Bad content",
"status": 400,
"instance": "urn:uuid:02IiNrFOD0xe3guYePp7Q56rkmY64xrAqxNEJ",
"detail": "The rule 'temperature-range' of the schema 'Place' rejected the request: The minimum temperature must not be above the maximum temperature."
}

Debugging rules

Rules are easiest to develop outside the API:

  1. Open the Expr Lang playground.
  2. Paste the variables of a write as the environment, e.g. {"operation": "create", "document": {...}, "previous": {}}, with the properties of an element in document.
  3. Write the expression and change the environment until every case returns what you expect: valid elements true, invalid ones false or a message.
  4. Copy the expression into the schema.

Typical mistakes:

  • Missing values: accessing a property which an element does not have returns nil, and comparing nil with a number fails. Check optional properties with != nil first, as in the rule above.
  • Wrong result type: only exactly true accepts a write. An expression which returns e.g. 1 or a list rejects every write.
  • Typos: an expression which can not be compiled is rejected when the schema is saved, with an error which names the rule. An expression which compiles but fails at runtime, e.g. because it uses an operator on a value of the wrong kind, rejects the writes it fails for. Fix the schema, schema changes are never blocked by rules.
  • Updates: document contains all properties after the write, not only the changed ones. Use changes to react to the properties which a request sets, and previous to compare with the values before.

Keep rules small and fast: they run on every write of the type, inside the API process, see performance and safety.