Schema.build
An object containing functions that create Builder instances for specific types.
const build: Readonly<{
any: () => Builder<any>;
string: (error?: StringConvertible) => StringBuilder;
number: (error?: StringConvertible) => NumberBuilder;
int: (error?: StringConvertible) => NumberBuilder;
boolean: (error?: StringConvertible) => BooleanBuilder;
date: (error?: StringConvertible) => DateBuilder;
array: <T extends Builder>(item: T | Schema<T>) => ArrayBuilder<Infer<T>>;
object: <T extends Record<string, Builder | Schema<any>>>(
schema: T,
) => ObjectBuilder<{ [K in keyof T]: Infer<T[K]> }>;
literal: <T extends string | number | boolean>(
value: T | T[],
) => LiteralBuilder<T>;
union: <T extends (Builder | Schema<any>)[]>(
...types: T
) => Builder<Infer<T[number]>>;
optional: <T>(b: Builder<T>) => Builder<T | undefined>;
nullable: <T>(b: Builder<T>) => Builder<T | null>;
coerce: {
string: (error?: StringConvertible) => StringBuilder;
number: (error?: StringConvertible) => NumberBuilder;
boolean: (error?: StringConvertible) => BooleanBuilder;
date: (error?: StringConvertible) => DateBuilder;
};
}>;Examples
const schema = new Schema((f) => {
// f is the build object:
return f.object({
// each method creates an object with fluent interface:
name: f.string().required(),
age: f.number(),
active: f.boolean()
});
});
Static members
Schema.build.any static
Creates a validation builder that accepts any value type.Schema.build.string static
Creates a validation builder that accepts string values.Schema.build.number static
Creates a validation builder that accepts number values.Schema.build.int static
Creates a validation builder that accepts integer values.Schema.build.boolean static
Creates a validation builder that accepts boolean values.Schema.build.date static
Creates a validation builder that accepts Date values.Schema.build.array static
Creates a validation builder that accepts an array of values.Schema.build.object static
Creates a validation builder that accepts an object with specific properties.Schema.build.literal static
Creates a validation builder that only accepts a specific value.Schema.build.union static
Creates a validation builder that accepts any of the provided types.Schema.build.optional static
Shortcut for calling optional() on the provided validation builder.Schema.build.nullable static
Shortcut for calling nullable() on the provided validation builder.Schema.build.coerce static
Validation builder methods that coerce the input value to the target type.
Related
class Schema
A class that represents a validation schema for input values, objects, and arrays.
