This document lists the public schema builders and utilities available in
@dep/schema.
import { s } from '@dep/schema';You can also import named exports directly:
import { number, object, string } from '@dep/schema';s.string()s.number()s.boolean()s.bigint()s.symbol()s.null()s.undefined()
s.any()s.unknown()s.never()s.void()s.nan()
s.literal(value)s.enum(values)s.templateLiteral(parts)
s.array(schema)s.object(shape)s.tuple(items)s.record(keySchema, valueSchema?)s.map(keySchema, valueSchema)s.set(schema)s.promise(schema)s.function(config)s.instanceof(constructor)s.file()s.date()s.json()s.lazy(() => schema)
s.union([...schemas])s.intersection(left, right)s.optional(schema)s.exactOptional(schema)s.nonoptional(schema)s.nullable(schema)s.default(schema, value)s.prefault(schema, value)s.catch(schema, value)s.transform(schema, fn)s.pipe(inputSchema, outputSchema)s.readonly(schema)s.success(schema)s.clone(schema)
s.email()s.url()s.httpUrl()s.uuid()s.uuidv1()s.uuidv2()s.uuidv3()s.uuidv4()s.uuidv5()s.uuidv6()s.uuidv7()s.uuidv8()s.guid()s.nanoid()s.cuid()s.cuid2()s.ulid()s.xid()s.ksuid()s.emoji()s.base64()s.base64url()s.jwt()s.ipv4()s.ipv6()s.cidrv4()s.cidrv6()s.e164()s.tel()s.stringFormat(format, pattern)
s.iso.date()s.iso.datetime()s.iso.duration()s.iso.time()
s.int()s.int32()s.uint32()s.float32()s.float64()s.numberFormat(name)
s.int64()s.uint64()s.bigintFormat(name)
s.coerce.string()s.coerce.number()s.coerce.boolean()s.coerce.bigint()s.coerce.date()
Every schema supports:
schema.parse(data)schema.parseAsync(data)schema.safeParse(data)schema.safeParseAsync(data)
import { s } from '@dep/schema';
const User = s.object({
name: s.string().min(2),
age: s.number().int().positive(),
});
const result = User.safeParse({
name: 'Ada',
age: 20,
});
if (result.success) {
console.log(result.data);
} else {
console.error(result.error.issues);
}Creates a string schema.
const Name = s.string();Common string methods:
.startsWith(value).endsWith(value).includes(value).lowercase().uppercase().regex(pattern).min(length).max(length).length(length).nonempty().trim().toLowerCase().toUpperCase()
Creates a number schema.
const Age = s.number();Common number methods:
.gt(value).gte(value).min(value).lt(value).lte(value).max(value).int().positive().nonnegative().negative().nonpositive().multipleOf(value)
Creates a boolean schema.
const Enabled = s.boolean();Creates a bigint schema.
const Id = s.bigint();Creates a symbol schema.
const Token = s.symbol();Creates a null schema.
const OnlyNull = s.null();Creates an undefined schema.
const OnlyUndefined = s.undefined();Accepts any value.
Accepts unknown values.
Rejects all values.
Validates undefined as void.
Validates NaN.
Validates a single literal value.
const Role = s.literal('admin');Validates enum-like values.
const Status = s.enum(['draft', 'published']);Builds a template-literal-based schema.
const UserId = s.templateLiteral(['user_', s.number()]);Creates an array schema.
const Tags = s.array(s.string());Creates an object schema.
const User = s.object({
name: s.string(),
age: s.number(),
});Useful object helpers may include:
.strict().strip().loose().catchall(schema).extend(shape).safeExtend(shape).partial().required().pick(keys).omit(keys).keyof()
Creates a fixed-length tuple schema.
const Point = s.tuple([s.number(), s.number()]);Creates a record schema.
const Scores = s.record(s.string(), s.number());Creates a Map schema.
const Lookup = s.map(s.string(), s.number());Creates a Set schema.
const UniqueTags = s.set(s.string());Creates a promise schema.
const AsyncName = s.promise(s.string());Creates a function schema.
const Fn = s.function({
input: [s.string()],
output: s.number(),
});Validates instances of a class.
const ErrorSchema = s.instanceof(Error);Creates a file schema.
Creates a date schema.
Validates JSON-compatible values.
Creates a lazily evaluated schema.
const Tree = s.lazy(() =>
s.object({
value: s.string(),
children: s.array(Tree).optional(),
})
);Accepts one of multiple schemas.
const Value = s.union([s.string(), s.number()]);Combines two schemas.
const Entity = s.intersection(
s.object({ id: s.number() }),
s.object({ name: s.string() }),
);Allows undefined.
Exact optional wrapper.
Removes optionality.
Allows null.
Provides a fallback when input is undefined.
const Name = s.default(s.string(), 'Anonymous');Pre-parse fallback value.
Returns a fallback value when parsing fails.
Transforms parsed output.
const Length = s.transform(s.string(), (value) => value.length);Pipes one schema into another.
const Piped = s.pipe(
s.string().transform((v) => Number(v)),
s.number().positive(),
);Marks output as readonly.
Wraps successful parsing semantics.
Clones a schema definition.
s.uuid()s.uuidv1()s.uuidv2()s.uuidv3()s.uuidv4()s.uuidv5()s.uuidv6()s.uuidv7()s.uuidv8()s.guid()s.nanoid()s.cuid()s.cuid2()s.ulid()s.xid()s.ksuid()
s.url()s.httpUrl()s.ipv4()s.ipv6()s.cidrv4()s.cidrv6()
s.email()s.e164()s.tel()
s.base64()s.base64url()s.jwt()
s.emoji()s.stringFormat(format, pattern)
Example:
const Email = s.email();
const Url = s.httpUrl();
const Token = s.jwt();Available under s.iso:
s.iso.date()s.iso.datetime()s.iso.duration()s.iso.time()
const BirthDate = s.iso.date();
const Timestamp = s.iso.datetime();Integer number schema.
32-bit signed integer schema.
32-bit unsigned integer schema.
32-bit floating point number schema.
64-bit floating point number schema.
Generic number format schema.
const Port = s.uint32();64-bit signed bigint schema.
64-bit unsigned bigint schema.
Generic bigint format schema.
const BigId = s.uint64();Available under s.coerce:
s.coerce.string()s.coerce.number()s.coerce.boolean()s.coerce.bigint()s.coerce.date()
const Age = s.coerce.number().int().positive();Validation failures throw SchemaError.
import { s, SchemaError } from '@dep/schema';
try {
s.number().parse('hello');
} catch (error) {
if (error instanceof SchemaError) {
console.log(error.issues);
}
}Each issue may include fields like:
codemessagepathexpectedreceivedformat