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:
owner
relationship to a Document based on the req.user.id
beforeValidate
and afterRead
hooksbeforeValidate
hookslug
with beforeValidate
beforeChange
hookAll field types provide the following hooks:
beforeValidate
beforeChange
afterChange
afterRead
Example field configuration:
{name: 'name',label: 'Name',type: 'text',hooks: {beforeValidate: [(args) => {...}],beforeChange: [(args) => {...}],afterChange: [(args) => {...}],afterRead: [(args) => {...}],}}
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:
Option | Description |
---|---|
value | The value of the field, before any updating. Will return undefined within create operations. |
data | The data passed to update the field with in create and update operations. |
originalDoc | The full original document in update or read operations. |
operation | A string relating to which operation the field type is currently executing within. |
req | The Express request object. It is mocked for Local API operations. |
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.