Skip to main content

Class: JsonSchemaHelper

A helper for JSON schemas.

Constructors

Constructor

new JsonSchemaHelper(): JsonSchemaHelper

Returns

JsonSchemaHelper

Properties

SCHEMA_VERSION

readonly static SCHEMA_VERSION: "https://json-schema.org/draft/2020-12/schema" = "https://json-schema.org/draft/2020-12/schema"

The schema version 2020 (default).


SCHEMA_VERSION_2019

readonly static SCHEMA_VERSION_2019: "https://json-schema.org/draft/2019-09/schema" = "https://json-schema.org/draft/2019-09/schema"

The schema version 2019.

Methods

setLoggers()

static setLoggers(loggers?): void

Set the loggers used during schema loading.

Parameters

loggers?

Optional loggers for schema loading, useful when you have a lot of references in your schema and want to track the loading process.

loadingSchema?

(uri) => Promise<void>

Called when a schema is being loaded.

schemaLoaded?

(uri) => Promise<void>

Called when a schema has been successfully loaded.

schemaLoadFailed?

(uri, error) => Promise<void>

Called when a schema fails to load.

Returns

void


validate()

static validate<T>(schema, data, additionalTypes?): Promise<IValidationFailure[]>

Validates data against the schema.

Type Parameters

T

T = unknown

Parameters

schema

SchemaObject

The schema to validate the data with.

data

T

The data to be validated.

additionalTypes?

Additional types to add for reference, not already in DataTypeHandlerFactory.

Returns

Promise<IValidationFailure[]>

Result containing errors if there are any.


getPropertyType()

static getPropertyType(schema, propertyName): string | undefined

Get the property type from a schema.

Parameters

schema

SchemaObject

The schema to extract the types from.

propertyName

string

The name of the property to get the type for.

Returns

string | undefined

The types of the property.


entitySchemaToJsonSchema()

static entitySchemaToJsonSchema(entitySchema, baseDomain?): SchemaObject

Convert an entity schema to JSON schema e.g https://example.com/schemas/.

Parameters

entitySchema

IEntitySchema<unknown> | undefined

The entity schema to convert.

baseDomain?

string

The base domain for local schemas e.g. https://example.com/

Returns

SchemaObject

The JSON schema for the entity.