Skip to main content

matches

$matches

Constraint that tests the string representation of the input against a RegExp. Throws a ConstraintError if the input does not match. Returns the original value unchanged.

This is the explicit constraint form; bare RegExp values in processor specs are no longer treated as implicit constraints and must be wrapped with $matches.

Parameters

  • pattern (RegExp, required): The pattern to test against.

Example

// Validate a semantic version string
new Schema('string').validator({$matches: /^\d+\.\d+\.\d+$/})

// Validate a slug (lowercase letters, numbers, hyphens only)
new Schema('string').validator({$matches: /^[a-z0-9]+(?:-[a-z0-9]+)*$/})

// Validate a hex color code
new Schema('object', {
color: new Schema('string').validator({$matches: /^#[0-9a-fA-F]{6}$/}),
})