Predicates
Predicates are functions that return a boolean value based on the context of a Feathers service call. They can be used to conditionally execute hooks or logic based on the state of the request, such as whether the user is authenticated, if the data is an array, or any other custom condition you define.
Hooks that are meant to be used with predicates:
- iff: Executes a hook if the predicate returns true.
- iffElse: Executes one hook if the predicate returns true, and another if it returns false.
- unless: Executes a series of hooks if the predicate returns false.
- skippable: Allows you to skip the execution of a hook based on a predicate.
- throwIf: Throws an error if the predicate returns true.
everyReturn the and of a series of sync or async predicate functions.
isContextCheck if the context matches the given options.
isMultiutil to check if a hook is a multi hook:
- find: true
- get: false
- create:
context.datais an array - update: false
- patch:
context.id == null - remove:
context.id == null
isPaginatedutil to check if a hook is a paginated hook using getPaginate
isProviderCheck which transport provided the service call.
notNegate a predicate function.
shouldSkipUtil to detect if a hook should be skipped
Checks the params.skipHooks array for the hook name, type, or 'all'.
someReturn the or of a series of sync or async predicate functions.
Custom Predicates
You can easily create custom predicates like:
const isSingleData = (context) => !Array.isArray(context.data);or
const isAuthenticated = (context) => !!context.params?.authenticated;