Schema types
The complete reference for every schema type dynz supports — string, number, boolean, date, object, array, options, file, enum, literal, expression, and discriminated union.
Every dynz schema starts with a type builder. All builders share the same property setters (setRequired, setMutable, setIncluded, setPrivate, setDefault, setCoerce) and can be composed freely.
d.string()
Validates a Javascript string.
import * as d from 'dynz';
const schema = d.string();
// With rules
const username = d.string().min(3).max(20).regex('^[a-z0-9_]+$');
const email = d.string().email();
const code = d.string().isNumeric();Available rules: min, max, email, regex, equals, oneOf, includes, notIncludes, isNumeric, custom, when
d.number()
Validates a JavaScript number.
const age = d.number().min(0).max(120);
const price = d.number().min(0).maxPrecision(2); // max 2 decimal places
const quantity = d.number().min(1);Available rules: min, max, maxPrecision, equals, custom, when
d.boolean()
Validates a true or false value.
const agreed = d.boolean();
const active = d.boolean().setDefault(false);Available rules: equals, custom, when
d.date()
Validates a Date object. Rules accept either a static Date or a d.ref() to another date field.
const birthDate = d.date().before(new Date());
const checkIn = d.date().after(new Date());
const checkOut = d.date().after(d.ref('checkIn'), 'Check-out must be after check-in');
const eventDate = d.date().minDate(new Date('2024-01-01'))
.maxDate(new Date('2024-12-31'));Available rules: before, after, minDate, maxDate, custom, when
before and after are exclusive (strict). Use minDate / maxDate for inclusive bounds.
d.object(fields)
Validates a plain object. Each value in fields is itself a schema.
const userSchema = d.object({
id: d.string(),
profile: d.object({
name: d.string().min(1),
bio: d.string().optional(),
}),
tags: d.array(d.string()),
});Object schemas nest arbitrarily. Validation descends into nested objects and collects all errors before returning.
Available rules: none (use inner field schemas for rules)
d.array(itemSchema)
Validates an array where every element matches itemSchema.
const tags = d.array(d.string().min(1));
const items = d.array(d.object({ id: d.string(), qty: d.number() }));
// Constrain the array length
const photos = d.array(d.file()).min(1).max(10);Available rules: min (min items), max (max items), custom, when
To constrain what can change in an existing array, use setMutable on the inner schema:
// Elements cannot be edited in place, but the array itself can grow/shrink
const readOnlyItems = d.array(d.string().setMutable(false));d.options(values)
Validates that the value is one of a fixed set of string or number literals. This is the right type for dropdowns, radio groups, and segmented controls.
const plan = d.options(['free', 'pro', 'enterprise'] as const);
const country = d.options(['US', 'GB', 'DE', 'FR'] as const);
const rating = d.options([1, 2, 3, 4, 5] as const);Options can carry a conditional enabled flag so individual choices appear or disappear based on other field values:
const shippingMethod = d.options([
'standard',
{ value: 'express', enabled: d.eq(d.ref('country'), 'US') },
{ value: 'overnight', enabled: d.and(
d.eq(d.ref('country'), 'US'),
d.eq(d.ref('plan'), 'pro'),
)
},
]);Available rules: equals, oneOf, custom, when
d.file()
Validates a File object (browser File API).
const avatar = d.file().maxSize(2 * 1024 * 1024) // 2 MB
.mimeType(['image/jpeg', 'image/png', 'image/webp']);
const resume = d.file().maxSize(5 * 1024 * 1024)
.mimeType(['application/pdf']);Available rules: maxSize, minSize, mimeType, custom, when
d.enum(values)
Validates a value against a TypeScript-style enum object. Use d.options() when you have a plain array of strings — d.enum() is for enum-shaped records.
const Status = { DRAFT: 'draft', PUBLISHED: 'published', ARCHIVED: 'archived' } as const;
const status = d.enum(Status);
// Valid values: 'draft' | 'published' | 'archived'd.literal(value)
Validates that the value is exactly a specific string, number, boolean, or null.
const trueLiteral = d.literal(true);
const versionOne = d.literal(1);
const nullField = d.literal(null);
const fixedVersion = d.literal('v2');Use a literal when a field must always be one fixed value — for example, an accepted terms field that must be true, an API version field that must be 'v2', or a constant marker in a data structure.
d.discriminatedUnion(key, members)
Validates a tagged union. The key field selects which member to validate against.
const paymentSchema = d.discriminatedUnion('method', [
{
method: 'card',
cardNumber: d.string().regex('^[0-9]{16}$'),
expiry: d.string().regex('^(0[1-9]|1[0-2])\\/[0-9]{2}$'),
cvv: d.string().min(3).max(4).setPrivate(true),
},
{
method: 'bank_transfer',
iban: d.string().min(15).max(34),
accountName: d.string().min(1),
},
{
method: 'paypal',
paypalEmail: d.string().email(),
},
]);When method === 'card', only the first member is validated. The other members are ignored.
d.expression(value)
An expression schema doesn't validate input — it computes a value. Use it to derive calculated fields that should appear in the output but aren't submitted by the user.
const orderSchema = d.object({
unitPrice: d.number().min(0),
quantity: d.number().min(1),
total: d.expression(d.multiply(d.ref('unitPrice'), d.ref('quantity'))),
});
const result = await d.validate(orderSchema, undefined, { unitPrice: 10, quantity: 3 });
// result.values.total === 30Common properties
All schema types support these property setters:
| Setter | Description |
|---|---|
.setRequired(value) | true, false, or a predicate. Defaults to true. |
.optional() | Shorthand for .setRequired(false). |
.setMutable(value) | true, false, or a predicate. Controls whether the value can change on update. |
.setIncluded(value) | true, false, or a predicate. Excluded fields must not have a value. |
.setPrivate(true) | Masks the value in the output. See private fields. |
.setDefault(value) | Substitutes this value when the field is empty. |
.setCoerce(true) | Enables automatic type coercion (e.g. "42" → 42 for number schemas). |
.setUi(config) | Attaches arbitrary UI metadata (labels, placeholders, etc.) for your renderer. |