Function: toJsonSchema()
toJsonSchema(
source,options?):JSONSchema
Converts a schema (or a DTO class) into a JSON Schema, for an API document or another validator. The JSON Schema describes the values the schema accepts without conversion: what cannot be expressed (custom rules, references to other values, conversions) is dropped, so the JSON Schema may accept more values, never less. The formats are named after the rules (their implementations differ on edge cases), and the numbers beyond the safe integers are accepted.
Parameters
source
The schema, or the DTO class, to convert.
options?
The JSON Schema version and the validation options the schema is used with.
Returns
A new JSON Schema.
Throws
When the source is not a schema nor a DTO class, or the target is unknown.
Example
typescript
import {DTO, type JSONSchema} from 'lakutata'
import {toJsonSchema} from 'lakutata/dtos'
const schema: JSONSchema = toJsonSchema(DTO.Object({
name: DTO.String().max(64).required(),
age: DTO.Number().integer().min(0)
}), {target: 'openapi-3.1'})
//{type: 'object', properties: {name: {type: 'string', maxLength: 64, …}, age: {type: 'integer', minimum: 0}}, required: ['name'], …}
console.log(schema)