json_schema.ts

Writes a schema as JSON Schema, draft 2020-12, in the shapes zod's toJSONSchema writes where the two libraries express the same schema.

What a schema does that JSON Schema can't say is unrepresentable: a bigint, a check, a transform on the output side, a literal of undefined, a bigint, or a number past the finite ones, an action outside a pipe or of another type than its step, a step after a transform on the input side, a step but a uuid after a to_lower_case on the input side and before one on the output side, a step that asserts something of the string before a to_integer on the output side, a regex with flags beyond d and u or whose source is no pattern under u or means something else there, a bound that isn't finite, a length that isn't whole, a default that isn't JSON, a variant option whose discriminator is no literal entry, and a kind defined outside the library. By default it throws, naming the kind and where in the document, and with unrepresentable: 'any' it writes {} there, or leaves the keyword out, or keeps the variant option, under anyOf. Some things are refused in both modes, as they are the schema's author's mistakes: an entry named like a member of Object.prototype, a schema that comes back to itself without reading into the value, metadata that isn't JSON, and a metadata key that is a keyword of JSON Schema other than an annotation.

It is a module of its own, so a bundle that writes no JSON Schema carries none of it. It reads schemas and makes none, and calls nothing but a lazy's function, once each.

view source

Declarations
#

4 declarations

JsonSchema
#

json_schema.ts view source

A JSON Schema document or subschema, typed by the keywords to_json_schema writes. Metadata adds any other key.

import type {JsonSchema} from '@fuzdev/fuz_schema/json_schema.ts';

members

$schema
?: string
#
$ref
?: string
#
$defs
?: Record<string, JsonSchema>
#
const
?: unknown
#
enum
?: unknown[]
#
#
anyOf
?: JsonSchema[]
#
oneOf
?: JsonSchema[]
#
allOf
?: JsonSchema[]
#
properties
?: Record<string, JsonSchema>
#
required
?: string[]
#
additionalProperties
?: JsonSchema | false
#
propertyNames
?: JsonSchema
#
items
?: JsonSchema
#
minLength
?: number
#
maxLength
?: number
#
minItems
?: number
#
maxItems
?: number
#
minimum
?: number
#
maximum
?: number
#
format
?: string
#
pattern
?: string
#
default
?: unknown
#
[key: string]
: unknown

JsonSchemaOptions
#

json_schema.ts view source

Options of to_json_schema.

import type {JsonSchemaOptions} from '@fuzdev/fuz_schema/json_schema.ts';

members

io
?: "input" | "output" = 'output'
#

Which side of a parse to describe: 'output', what it returns, or 'input', what it accepts. They differ where a default fills a key in, which is required of the output and optional in the input, where strip_object drops unknown keys, where a transform runs, which has no output to write and whose input is the steps before it, where a to_lower_case runs, whose steps before it are of the input and those after it of the output, and where a to_integer reads a string, an integer of the output and a pattern of the input.

unrepresentable
?: "throw" | "any" = 'throw'
#

What to do with what JSON Schema can't say: 'throw', or 'any', which writes {} for it, or leaves out the keyword for a step of a pipe.

JsonSchemaType
#

json_schema.ts view source

type "string" | "number" | "integer" | "boolean" | "null" | "object" | "array"

The values of the keyword type.

import type {JsonSchemaType} from '@fuzdev/fuz_schema/json_schema.ts';

to_json_schema
#

json_schema.ts view source

to_json_schema(schema: AnySchema, options?: JsonSchemaOptions): JsonSchema

Writes schema as a JSON Schema document, with $schema naming draft 2020-12.

  • An object schema is {type: 'object', properties, required}: object closes it with additionalProperties: false, strip_object too on the output side, loose_object writes {} there, and object_with_rest its rest. A key is optional where the parse can do without it: on the input side where a missing key passes, and on the output side where the parse can leave it out, read as the library runs the entry on a missing key, through a nullable, a lazy, a pipe and its steps, and a union's options in the order it tries them.
  • A record is {type: 'object', propertyNames, additionalProperties}, and a keyed_record the same with its key schema's node as propertyNames.
  • A nullable and a union are anyOf, folded into a type array where every option is a bare type, and a variant is oneOf of the options it can pick, or anyOf where a discriminator is written as other than a const.
  • The actions of a pipe are keywords of its first schema's node: lengths, bounds, integer, formats, and patterns, the tightest bound winning. A to_lower_case writes none, and the keyword of a step that reads another string than the document's, after it on the input side or before it on the output side, is unrepresentable, but a uuid's, which holds of both.
  • A to_integer is an integer within the bounds after it on the output side, with nothing of the string, its metadata included, and on the input side a pattern of exactly the strings it reads within them, not: {} where none is. On the output side a step before it that asserts something of the string is unrepresentable, as the output can't be narrowed by it, and on the input side its default is left out, as for a transform.
  • A with_default writes default, unless its schema transforms on the input side. A with_default_fn writes none, as its make is never called, and drops one of the schema it holds.
  • A lazy is written in place, unless the schema comes back to itself, when it is in $defs and referred to, or is # when it is the root.
  • The metadata of every layer is copied into the node it is written in, the outer layer winning, a with_default's default included, and undefined clearing a key. A key that is a keyword of JSON Schema, but an annotation, is refused, as it would change what the document accepts.
import {to_json_schema} from '@fuzdev/fuz_schema/json_schema.ts';

parameters

schema
: AnySchema
#

the schema to write

#

which side of a parse, and what to do with what can't be written

throws

  • Error - naming the kind and the place in the document of what can't be written, or is refused

Depends on
#