regex
Accepts and returns strings that match the given regular expression.Type Signature
Parameters
- regex:
RegExp- The regular expression pattern to test against - msg:
string- The error message to display when validation fails
Usage
Examples
Phone Number
Semantic Version
Username
Implementation
src/strings.ts:35-37
Built-in Regex Decoders
Many string decoders are built usingregex():
- email - Email addresses
- uuid - UUIDs
- identifier - Programming identifiers
- decimal - Decimal digit strings
- hexadecimal - Hexadecimal digit strings
- nonEmptyString - Non-empty strings
startsWith
Accepts and returns strings that start with the given prefix.Type Signature
Parameters
- prefix:
string- The required prefix
Usage
Examples
Implementation
src/strings.ts:42-47
endsWith
Accepts and returns strings that end with the given suffix.Type Signature
Parameters
- suffix:
string- The required suffix
Usage
Examples
Implementation
src/strings.ts:52-57
nonEmptyString
Likestring, but will reject the empty string or strings containing only whitespace.
Type Signature
Validation Pattern
Must contain at least one non-whitespace character. Regex pattern:Valid Inputs
Invalid Inputs
Error Message
When validation fails:"Must be non-empty string"
Implementation
src/strings.ts:30
nanoid
Accepts and returns nanoid string values. Assumes the default nanoid alphabet. If you’re using a custom alphabet, useregex() instead.
Type Signature
Parameters
- options:
SizeOptions(optional) - Size constraintssize?: number- Exact size (default: 21)min?: number- Minimum sizemax?: number- Maximum size
Validation Pattern
Regex pattern:Usage
Valid Inputs
Invalid Inputs
Implementation
src/strings.ts:108-110
decimal
Accepts and returns strings with decimal digits only (base-10). To convert these to numbers, use the numeric decoder.Type Signature
Validation Pattern
Regex pattern:Valid Inputs
Invalid Inputs
Error Message
When validation fails:"Must only contain digits"
Implementation
src/strings.ts:144
Related Decoders
- numeric - Parse decimal strings as numbers
- hexadecimal - Validate hexadecimal strings
hexadecimal
Accepts and returns strings with hexadecimal digits only (base-16).Type Signature
Validation Pattern
Regex pattern:Valid Inputs
Invalid Inputs
Error Message
When validation fails:"Must only contain hexadecimal digits"
Implementation
src/strings.ts:149-152
Related Decoders
numeric
Accepts valid numerical strings (in base-10) and returns them as a number. To only accept numerical strings and keep them as string values, use the decimal decoder.Type Signature
Validation
First validates the string contains only decimal digits using decimal, then transforms to a number.Valid Inputs
Invalid Inputs
Error Message
When validation fails:"Must only contain digits"
Implementation
src/strings.ts:159
Related Decoders
- decimal - Validate decimal strings (returns string)
- number - Accept actual number values (not strings)
