gate
$gate
This is a conditional operator that evaluates a predicate for whether it returns a defined value.
If the predicate returns a defined value, $gate will return the result of invoking any provided success action with the original input. If not provided, the default success action will simply return the original input.
If the predicate returns undefined or rejects/throws, $gate will return the result of invoking the failure action on the original input. The default failure action will return undefined.
This processor can act as a constraint if the success or failure actions throw when triggered.
To pass the original input to the actions instead of the predicate results, see $if.
Parameters
predicate(processor, optional): Evaluated for defined-ness. If omitted, the input itself is tested.success(processor, optional): Invoked with the original input when predicate returns a defined value. Default: returns input as-is.failure(processor, optional): Invoked with the original input when predicate returns undefined or throws. Default: returns undefined.
Array form: {$gate: [predicate, success, failure]}
Object form: {$gate: {predicate: ..., success: ..., failure: ...}}
Example
// Only process the value through $uppercase if it was provided
new Schema('string').normalizer({
$gate: ['$defined', '$uppercase']
})
// Use the first resolvable property as a value
new Schema('object', {
host: new Schema('string'),
fallbackHost: new Schema('string'),
}).transformer({
$gate: [{$property: 'host'}, {$property: 'host'}, {$property: 'fallbackHost'}]
})