Skip to main content

clamp

$clamp

Constrains a number to a [min, max] range by returning the nearest boundary when the value falls outside. Unlike $range (which throws), $clamp transforms.

Both min and max are optional; omitting either leaves that end unclamped.

Parameters

  • min (number, optional): Lower bound.
  • max (number, optional): Upper bound.

Example

// Clamp a volume setting to [0, 100]
new Schema('number').transformer({$clamp: {min: 0, max: 100}})

// Clamp retry count to at most 10 (no lower bound)
new Schema('number').transformer({$clamp: {max: 10}})

// Ensure a timeout is at least 100ms
new Schema('number').transformer({$clamp: {min: 100}})