Skip to main content

each

$each

Applies a processor to each element of an array. The processor can be any valid processor specification (RegExp, function, keyword, or parameterized processor). If any element fails validation, the entire array is rejected.

This operator is useful for applying consistent validation or transformation rules across all array elements without requiring explicit array element schemas.

Parameters

  • processor (any valid processor spec, required): The processor to apply to each element. Can be a RegExp, function, string keyword (e.g., '$numeric'), or parameterized processor object.

Example

// Trim whitespace from every string in an array
new Schema('array').transformer({$each: '$trim'})

// Validate that every element is numeric
new Schema('array').validator({$each: '$numeric'})

// Validate that each element is within a range
new Schema('array').validator({$each: {$range: {min: 0, max: 100}}})

// Normalize every tag: trim and lowercase
new Schema('object', {
tags: new Schema('array').transformer({$each: ['$trim', '$lowercase']}),
})