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.
Built-in Predicates
| Predicate | Description |
|---|---|
every | Returns a predicate that is |
isContext | Returns a predicate that checks whether the hook context matches the given criteria.
You can filter by |
isMulti | Checks if the current hook context represents a multi operation.
Returns |
isPaginated | Checks if the current |
isProvider | Returns a predicate that checks the transport provider of the service call.
Matches against |
not | Negates a sync or async predicate function, inverting its boolean result. Useful for composing conditions like "not external" or "not multi". |
shouldSkip | Returns a predicate that checks |
some | Returns a predicate that is |
Hooks that are meant to be used with predicates
| Hook | Description |
|---|---|
iff | Conditionally executes a series of hooks when the predicate is truthy.
The predicate can be a boolean value or a sync/async function.
Supports an |
iffElse | Executes one array of hooks when the predicate is truthy, or another array when it is falsy.
The predicate can be a boolean or a sync/async function.
Unlike |
skippable | Wraps a hook so it can be conditionally skipped based on a predicate.
When the predicate returns |
throwIf | Throws a |
unless | Executes a series of hooks when the predicate is falsy --- the inverse of |
Custom Predicates
You can easily create custom predicates like:
const isSingleData = (context) => !Array.isArray(context.data);or
const isAuthenticated = (context) => !!context.params?.authenticated;