Field Hooks

Field-level hooks offer incredible potential for encapsulating your logic. They help to isolate concerns and package up functionalities to be easily reusable across your projects.

Example use cases include:

  • Automatically add an owner relationship to a Document based on the
  • Encrypt / decrypt a sensitive field using beforeValidate and afterRead hooks
  • Auto-generate field data using a beforeValidate hook
  • Format incoming data such as kebab-casing a document slug with beforeValidate
  • Restrict updating a document to only once every X hours using the beforeChange hook

All field types provide the following hooks:

  • beforeValidate
  • beforeChange
  • afterChange
  • afterRead


Example field configuration:

name: 'name',
type: 'text',
hooks: {
beforeValidate: [(args) => {...}],
beforeChange: [(args) => {...}],
afterChange: [(args) => {...}],
afterRead: [(args) => {...}],

Arguments and return values

All field-level hooks are formatted to accept the same arguments, although some arguments may be undefined based on which field hook you are utilizing.


Field Hooks receive one args argument that contains the following properties:

valueThe value of the field.
dataThe data passed to update the field with in create and update operations, and the full document itself in the afterRead hook.
originalDocThe full original document in update operations.
operationA string relating to which operation the field type is currently executing within. Useful within beforeValidate, beforeChange, and afterChange hooks to differentiate between create and update operations.
reqThe Express request object. It is mocked for Local API operations.

Return value

All field hooks can optionally modify the return value of the field before the operation continues. Field Hooks may optionally return the value that should be used within the field.


Global Hooks