Primitives
String, number, boolean, and other primitive type schemas
Primitives
valrs provides schemas for all JavaScript primitive types with chainable validation methods.
String
Create string schemas with v.string():
String Methods
| Method | Description |
|---|---|
.min(n) | Minimum length |
.max(n) | Maximum length |
.length(n) | Exact length |
.email() | Valid email format |
.url() | Valid URL format |
.uuid() | Valid UUID format |
.cuid() | Valid CUID format |
.cuid2() | Valid CUID2 format |
.ulid() | Valid ULID format |
.regex(re) | Match regular expression |
.includes(str) | Contains substring |
.startsWith(str) | Starts with prefix |
.endsWith(str) | Ends with suffix |
.datetime() | ISO 8601 datetime |
.ip() | Valid IP address (v4 or v6) |
.trim() | Trim whitespace (transform) |
.toLowerCase() | Convert to lowercase (transform) |
.toUpperCase() | Convert to uppercase (transform) |
String Examples
Custom Error Messages
Number
Create number schemas with v.number():
Number Methods
| Method | Description |
|---|---|
.gt(n) | Greater than |
.gte(n) or .min(n) | Greater than or equal |
.lt(n) | Less than |
.lte(n) or .max(n) | Less than or equal |
.int() | Must be integer |
.positive() | Must be positive |
.nonnegative() | Must be non-negative |
.negative() | Must be negative |
.nonpositive() | Must be non-positive |
.multipleOf(n) or .step(n) | Must be multiple of n |
.finite() | Must be finite (not Infinity) |
.safe() | Must be safe integer |
Number Examples
BigInt
Create bigint schemas with v.bigint():
Boolean
Create boolean schemas with v.boolean():
Date
Create date schemas with v.date():
Null, Undefined, Void
Any, Unknown, Never
Coercion
Use v.coerce to automatically convert values before validation:
Coercion Types
| Coercer | Description |
|---|---|
v.coerce.string() | Converts to string using String() |
v.coerce.number() | Converts to number using Number(), fails on NaN |
v.coerce.boolean() | Converts to boolean using Boolean() |
v.coerce.bigint() | Converts to bigint using BigInt() |
v.coerce.date() | Converts to Date using new Date() |
Coercion with Validation
Coercion happens before validation methods are applied:
Integer Types
valrs provides specialized integer schemas for specific bit widths:
Literals
Create schemas for literal values:
Next Steps
- Objects - Object schemas and methods
- Collections - Arrays, tuples, records
- Unions - Union types and enums