A JSON Schema literal. Pass with as const for precise
TypeScript type inference on the returned builder.
A @cleverbrush/schema builder whose static type mirrors the
structure described by the JSON Schema node.
as const is required for precise TypeScript inference. Without it
TypeScript widens string literals to string and the inferred builder
type collapses to SchemaBuilder<unknown>. Always pass the schema literal
(or the variable holding it) with as const.
Supported JSON Schema keywords
| Keyword | Builder equivalent |
|---|---|
type: 'string' |
string() |
type: 'number' |
number() |
type: 'integer' |
number() (integer flag) |
type: 'boolean' |
boolean() |
type: 'null' |
SchemaBuilder<null> |
type: 'array' + items |
array(itemBuilder) |
type: 'object' + properties |
object({ … }) |
required: […] |
required / optional per-prop |
additionalProperties: true |
.acceptUnknownProps() |
const |
string/number/boolean eq |
enum |
union(…) |
anyOf |
union(…) |
minLength / maxLength |
.minLength() / .maxLength() |
pattern |
.matches(regex) (invalid patterns silently ignored) |
minimum / maximum |
.min() / .max() |
exclusiveMinimum / exclusiveMaximum |
custom validator (not round-trippable via toJsonSchema) |
multipleOf |
.multipleOf() |
minItems / maxItems |
.minLength() / .maxLength() |
format: 'email' |
.email() extension |
format: 'uuid' |
.uuid() extension |
format: 'uri' / 'url' |
.url() extension |
format: 'ipv4' |
.ip({ version: 'v4' }) |
format: 'ipv6' |
.ip({ version: 'v6' }) |
format: 'date-time' |
.matches(iso8601 regex) |
Keywords not supported: allOf (falls back to any()), $ref,
$defs, if/then/else, not, contains, unevaluatedProperties,
contentEncoding.
import { fromJsonSchema } from '@cleverbrush/schema-json';
const schema = fromJsonSchema({
type: 'object',
properties: {
name: { type: 'string', minLength: 1 },
score: { type: 'number', minimum: 0, maximum: 100 },
},
required: ['name'],
} as const);
schema.parse({ name: 'Alice', score: 95 });
// TypeScript infers: { name: string; score?: number }
// Union types via `enum`
const statusSchema = fromJsonSchema({
enum: ['active', 'inactive', 'pending']
} as const);
statusSchema.parse('active'); // valid — 'active' | 'inactive' | 'pending'
// Use InferFromJsonSchema to derive the TypeScript type statically
import type { InferFromJsonSchema } from '@cleverbrush/schema-json';
const S = { type: 'object', properties: { id: { type: 'integer' } }, required: ['id'] } as const;
type Payload = InferFromJsonSchema<typeof S>; // { id: number }
const payloadSchema = fromJsonSchema(S);
Converts a JSON Schema object into a
@cleverbrush/schemabuilder.