Any @cleverbrush/schema builder instance.
Optionalopts: ToJsonSchemaOptionsOptional output configuration (see ToJsonSchemaOptions).
A plain JSON-serialisable object representing the schema.
What round-trips cleanly: all declarative constraints — type, format, minLength/maxLength, minimum/maximum, multipleOf, pattern, required/optional per property, additionalProperties, items, enum/const literals, anyOf/union.
What is silently omitted:
addValidator (no JSON Schema equivalent)addPreprocessorexclusiveMinimum/exclusiveMaximum constraints from fromJsonSchema
(stored as custom validators — not introspectable; only positive()/
negative() extension-based exclusives are emitted)format keyword is omitted
in that case)By default the output includes a $schema header for JSON Schema Draft
2020-12. Pass { $schema: false } when embedding the result in an OpenAPI
specification, or { draft: '07' } for Draft 07 compatibility.
import { toJsonSchema } from '@cleverbrush/schema-json';
import { object, string, number } from '@cleverbrush/schema';
const UserSchema = object({
name: string().minLength(1),
email: string().email(),
age: number().optional(),
});
const spec = toJsonSchema(UserSchema);
// {
// "$schema": "https://json-schema.org/draft/2020-12/schema",
// "type": "object",
// "properties": {
// "name": { "type": "string", "minLength": 1 },
// "email": { "type": "string", "format": "email" },
// "age": { "type": "number" }
// },
// "required": ["name", "email"]
// }
Converts a
@cleverbrush/schemabuilder to a JSON Schema object.