Skip to content

Function: fromJsonSchema() ​

fromJsonSchema(schema, options?): Schema

Converts a JSON Schema (drafts 04 to 2020-12, or an OpenAPI 3.0/3.1 schema object) into a schema, which validates like JSON Schema: without conversion, the unknown keys as additionalProperties says, the formats like the full formats of ajv-formats, and the keywords next to a $ref applied (like ajv). The default values are applied.

Parameters ​

schema ​

boolean | JSONSchema

The JSON Schema, or a boolean schema (true accepts every value, false none).

options? ​

FromJsonSchemaOptions

The version of the JSON Schema, and where its references are resolved.

Returns ​

Schema

The schema, to validate with DTO.validate() or to use in a DTO.

Throws ​

When the JSON Schema is not an object nor a boolean, a reference cannot be resolved, or a pattern is not a valid regular expression.

Example ​

typescript
import {DTO, type Schema} from 'lakutata'
import {fromJsonSchema} from 'lakutata/dtos'

const schema: Schema = fromJsonSchema({
    type: 'object',
    properties: {
        name: {type: 'string'},
        age: {type: 'integer', minimum: 0, default: 18}
    },
    required: ['name']
})
console.log(DTO.validate({name: 'Ada'}, schema)) //{name: 'Ada', age: 18}

Released under the MIT License.