@doubter/json-schema
Version:
Converts Doubter shapes from and to JSON schemas.
55 lines (54 loc) • 1.72 kB
TypeScript
import { AnyShape } from 'doubter';
import { Dict, JSONSchema } from './types';
export interface JSONSchemaOptions {
/**
* The mapping from the definition name to a shape that is converted to the JSON schema.
*/
definitions?: Dict<AnyShape>;
/**
* The key under which the definitions are stored.
*
* @default "definitions"
*/
definitionsKey?: string;
/**
* The schema base path.
*
* @default "#"
*/
basePath?: string;
/**
* The schema dialect placed in {@linkcode JSONSchema.$schema}. By default, no dialect is added.
*
* @example
* "https://json-schema.org/draft/2020-12/schema"
*/
dialect?: string;
/**
* If `true` then definitions from {@linkcode definitions} that aren't referenced, are still rendered under
* {@linkcode definitionsKey}. Otherwise, those definitions aren't rendered.
*/
unusedDefinitions?: boolean;
/**
* If `true` then {@link JSONSchema.const} is rendered as {@link JSONSchema.enum} with a single element.
*/
constAsEnum?: boolean;
/**
* Called for each schema after it is generated.
*/
postprocess?: (shape: AnyShape, schema: JSONSchema) => void;
}
/**
* Converts the shape to a JSON schema.
*
* @param shape The shape to convert.
* @param options The JSON schema options.
*/
export declare function toJSONSchema(shape: AnyShape, options?: JSONSchemaOptions): JSONSchema;
/**
* Converts definitions to a JSON schema.
*
* @param shapes The mapping from a shape name to shape.
* @param options The JSON schema options.
*/
export declare function toJSONSchema(shapes: Dict<AnyShape>, options?: JSONSchemaOptions): JSONSchema;