Skip to main content

Function: matches()

matches(pattern, errorMessage): Validator<string, string, string>

Defined in: validation/validators/index.ts:110

Validates string matches a regex pattern.

Parameters

pattern

RegExp

Regex pattern to match

errorMessage

string

Custom error message

Returns

Validator<string, string, string>

Validator that checks regex match

Example

matches(/^[A-Z]/, 'Must start with uppercase')('John') // => Valid('John')
matches(/^[A-Z]/, 'Must start with uppercase')('john') // => Invalid(['Must start with uppercase'])

// Username validation
matches(/^[a-zA-Z0-9_]+$/, 'Only letters, numbers, and underscores')