Skip to Content
@jetio/schema-builder docs are live 🎉
API ReferenceSchemaBuilder

SchemaBuilder Class

const b = new SchemaBuilder();

Type methods

Each returns a type-specific builder. Once you call one, the builder locks to that type and exposes only its constraints — see type locking.

MethodReturnsDescription
.string()StringSchemaBuilderSet type to string
.number()NumberSchemaBuilderSet type to number
.integer()NumberSchemaBuilderSet type to integer
.boolean()BooleanSchemaSet type to boolean
.null()NullSchemaSet type to null
.array()ArraySchemaBuilderSet type to array
.object()ObjectSchemaBuilderSet type to object

Metadata methods

MethodParametersDescription
.$id(id)stringSet $id (Draft 6+)
.$schema(uri)stringSet $schema
.$anchor(name)stringSet $anchor
.$dynamicAnchor(name)stringSet $dynamicAnchor
.title(title)stringSet title
.description(desc)stringSet description
.default(value)anySet default value
.examples(...values)any[]Set examples
.option(key, value)string, anySet any custom keyword

Value constraint methods

MethodParametersDescription
.enum(values)any[] or $dataSet allowed values
.const(value)anySet single allowed value

Composition methods

MethodParametersDescription
.not(schema)BuilderSchema or (b: SchemaBuilder) => SchemaBuilderNegate a schema
.anyOf(...schemas)BuilderSchema[] or ((b: SchemaBuilder) => SchemaBuilder)[]Match at least one schema
.allOf(...schemas)BuilderSchema[] or ((b: SchemaBuilder) => SchemaBuilder)[]Match all schemas
.oneOf(...schemas)BuilderSchema[] or ((b: SchemaBuilder) => SchemaBuilder)[]Match exactly one schema

BuilderSchema = SchemaDefinition | SchemaBuilder<any> | boolean

Conditional methods

MethodParametersReturnsDescription
.if(condition)BuilderSchema or (b: SchemaBuilder) => SchemaBuilderConditionBuilderStart conditional

Reference methods

MethodParametersDescription
.$ref(ref)string or RefBuilder or (r: RefBuilder) => RefBuilderSet $ref
.$dynamicRef(ref)string or RefBuilder or (r: RefBuilder) => RefBuilderSet $dynamicRef
.$defs(defs)Record<string, BuilderSchema> or Record<string, (b: SchemaBuilder) => SchemaBuilder>Set $defs (Draft 2019-09+)
.definitions(defs)Record<string, BuilderSchema> or Record<string, (b: SchemaBuilder) => SchemaBuilder>Set definitions (Draft 04-07)

Error message methods

MethodParametersDescription
.errorMessage(msg)string or Record<string, string | Record<string, string>>Set custom error message

Utility methods

MethodParametersReturnsDescription
.build()—SReturn the final schema object
.extend(schema)BuilderSchemaSchemaBuilderExtend from an existing schema
.json(schema)SchemaDefinition or stringSchemaBuilderLoad from JSON object/string
.url(url)stringPromise<SchemaBuilder>Load from URL (async)
.file(path)stringPromise<SchemaBuilder>Load from file (async)
Last updated on