Libraries
    Preparing search index...
    • Converts a @cleverbrush/schema builder to a JSON Schema object.

      Parameters

      Returns Record<string, unknown>

      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:

      • Custom validators added via addValidator (no JSON Schema equivalent)
      • Preprocessors added via addPreprocessor
      • JSDoc comments on schema properties
      • exclusiveMinimum/exclusiveMaximum constraints from fromJsonSchema (stored as custom validators — not introspectable; only positive()/ negative() extension-based exclusives are emitted)
      • IP format with both v4 and v6 allowed simultaneously (no single standard JSON Schema format covers both; the 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"]
      // }
      // Embed in an OpenAPI spec (strip the $schema header)
      toJsonSchema(schema, { $schema: false });

      // Use JSON Schema Draft 07
      toJsonSchema(schema, { draft: '07' });