Skip to content

JsonSchema

@tsed/schema

Usage

typescript
import { JsonSchema } from "@tsed/schema";

See /packages/specs/schema/src/types/domain/JsonSchema.ts.

Overview

ts
class JsonSchema extends Map<string, any> implements NestedGenerics {
    readonly $kind: string;
    readonly $isJsonDocument = true;
    readonly $hooks: Hooks;
    readonly $required: Set<string>;
    readonly $allow: any[];
    $selfRequired: boolean;
    $forwardGroups: boolean;
    $ignore: boolean | IgnoreCallback;
    isDiscriminatorKey: boolean;
    isDiscriminator: boolean;
    constructor(obj?: JsonSchema | Partial<JsonSchemaObject>);
    get alias(): AliasMap;
    get nestedGenerics(): any[];
    set nestedGenerics(value: any[]);
    get genericLabels(): string[];
    set genericLabels(value: string[]);
    get isClass(): boolean;
    get isCollection(): boolean;
    get isGeneric(): boolean;
    get discriminatorAncestor(): any;
    get hasGenerics(): boolean;
    get genericType(): string;
    get class(): any;
    get canRef(): boolean;
    get isNullable(): boolean;
    get isReadOnly(): any;
    get isWriteOnly(): any;
    get hasDiscriminator(): boolean;
    static from(obj?: Partial<JsonSchemaObject>): JsonSchema;
    nullable(value: boolean): void;
    itemSchema(obj?: AnyJsonSchema): JsonSchema;
    getAliasOf(property: AliasType): AliasType | undefined;
    addAlias(property: AliasType, alias: AliasType): this;
    removeAlias(property: AliasType): this;
    $id($id: string): this;
    $ref($ref: string): this;
    $schema($schema: JSONSchema6Version): this;
    label(name: string): this;
    name(name: string): this;
    ignore(cb: boolean | IgnoreCallback): this;
    default(value: JSONSchema6Type): this;
    const(value: JSONSchema6Type): this;
    description(description: string): this;
    discriminator(): Discriminator;
    discriminatorKey(propertyName: string): this;
    discriminatorValue(...values: string[]): this;
    additionalItems(additionalItems: boolean | AnyJsonSchema): this;
    contains(contains: JSONSchema6Definition): this;
    examples(examples: JSONSchema6Type[]): this;
    example(...examples: JSONSchema6Type[]): this;
    items(items: AnyJsonSchema | AnyJsonSchema[]): this;
    maxItems(maxItems: number): this;
    minItems(minItems: number): this;
    uniqueItems(uniqueItems: boolean): this;
    maxProperties(maxProperties: number): this;
    minProperties(minProperties: number): this;
    allow(...allow: any[]): this;
    required(required?: boolean | string[]): JsonSchema;
    addRequired(property: string): this;
    removeRequired(property: string): this;
    isRequired(property: string): boolean;
    properties(properties: AnyJsonSchema | Record<string, AnyJsonSchema>): this;
    addProperty(key: string, schema: AnyJsonSchema): this;
    patternProperties(patternProperties: Record<string, AnyJsonSchema>): this;
    additionalProperties(additionalProperties: boolean | AnyJsonSchema): this;
    unknown(unknown?: boolean): this;
    dependencies(dependencies: {
        [p: string]: JSONSchema6Definition | JsonSchema | string[];
    }): this;
    propertyNames(propertyNames: JSONSchema6Definition | JsonSchema): this;
    enum(...enumValues: any[]): this;
    enum(enumSchema: JsonSchema): this;
    definitions(definitions: Record<string, AnyJsonSchema>): this;
    allOf(allOf: AnyJsonSchema[]): this;
    anyOf(anyOf: AnyJsonSchema[]): this;
    oneOf(oneOf: AnyJsonSchema[]): this;
    not(not: AnyJsonSchema): this;
    multipleOf(multipleOf: number): this;
    maximum(maximum: number): this;
    exclusiveMaximum(exclusiveMaximum: number): this;
    minimum(minimum: number): this;
    exclusiveMinimum(exclusiveMinimum: number): this;
    maxLength(maxLength: number): this;
    minLength(minLength: number): this;
    pattern(pattern: string | RegExp): this;
    format(format: JsonFormatTypes | ValueOf<JsonFormatTypes>): this;
    type(type: any | JSONSchema6TypeName | JSONSchema6TypeName[]): this;
    any(...types: any[]): this;
    integer(): this;
    title(title: string): this;
    readOnly(readOnly: boolean): this;
    writeOnly(readOnly: boolean): this;
    customKey(key: string, value: any): this;
    toObject(options?: JsonSchemaOptions): any;
    toJSON(options?: JsonSchemaOptions): any;
    assign(obj?: JsonSchema | Partial<JsonSchemaObject>): this;
    set(key: string, value: any): this;
    getComputedType(): any;
    getComputedItemType(): any;
    getJsonType(): string | string[];
    getTarget(): Type<any>;
    getName(): any;
    clone(): JsonSchema;
    protected setManyOf(keyword: "oneOf" | "anyOf" | "allOf", value: AnyJsonSchema[]): this | undefined;
}

readonly $kind

ts
readonly $kind: string;

readonly $isJsonDocument

ts
readonly $isJsonDocument = true;

readonly $hooks

ts
readonly $hooks: Hooks;

readonly $required

ts
readonly $required: Set<string>;

readonly $allow

ts
readonly $allow: any[];

$selfRequired

ts
$selfRequired: boolean;

$forwardGroups

ts
$forwardGroups: boolean;

$ignore

ts
$ignore: boolean | IgnoreCallback;

isDiscriminatorKey

ts
isDiscriminatorKey: boolean;

isDiscriminator

ts
isDiscriminator: boolean;

get alias

ts
get alias(): AliasMap;

get nestedGenerics

ts
get nestedGenerics(): any[];

set nestedGenerics

ts
set nestedGenerics(value: any[]);

get genericLabels

ts
get genericLabels(): string[];

set genericLabels

ts
set genericLabels(value: string[]);

get isClass

ts
get isClass(): boolean;

get isCollection

ts
get isCollection(): boolean;

Current schema is a collection

get isGeneric

ts
get isGeneric(): boolean;

Current schema is a generic

get discriminatorAncestor

ts
get discriminatorAncestor(): any;

get hasGenerics

ts
get hasGenerics(): boolean;

Current schema has generics items

get genericType

ts
get genericType(): string;

get class

ts
get class(): any;

get canRef

ts
get canRef(): boolean;

get isNullable

ts
get isNullable(): boolean;

get isReadOnly

ts
get isReadOnly(): any;

get isWriteOnly

ts
get isWriteOnly(): any;

get hasDiscriminator

ts
get hasDiscriminator(): boolean;

static from

ts
static from(obj?: Partial<JsonSchemaObject>): JsonSchema;

nullable

ts
nullable(value: boolean): void;

itemSchema

ts
itemSchema(obj?: AnyJsonSchema): JsonSchema;

getAliasOf

ts
getAliasOf(property: AliasType): AliasType | undefined;

addAlias

ts
addAlias(property: AliasType, alias: AliasType): this;

removeAlias

ts
removeAlias(property: AliasType): this;

$id

ts
$id($id: string): this;

$ref

ts
$ref($ref: string): this;

$schema

ts
$schema($schema: JSONSchema6Version): this;

label

ts
label(name: string): this;

Create a ref and use name to sharing schema

name

ts
name(name: string): this;

ignore

ts
ignore(cb: boolean | IgnoreCallback): this;

default

ts
default(value: JSONSchema6Type): this;

This keyword can be used to supply a default JSON value associated with a particular schema. It is RECOMMENDED that a default value be valid against the associated schema.

const

ts
const(value: JSONSchema6Type): this;

More readible form of a one-element "enum"

description

ts
description(description: string): this;

This attribute is a string that provides a full description of the of purpose the instance property.

discriminator

ts
discriminator(): Discriminator;

discriminatorKey

ts
discriminatorKey(propertyName: string): this;

discriminatorValue

ts
discriminatorValue(...values: string[]): this;

additionalItems

ts
additionalItems(additionalItems: boolean | AnyJsonSchema): this;

This keyword determines how child instances validate for arrays, and does not directly validate the immediate instance itself. If "items" is an array of schemas, validation succeeds if every instance element at a position greater than the size of "items" validates against "additionalItems". Otherwise, "additionalItems" MUST be ignored, as the "items" schema (possibly the default value of an empty schema) is applied to all elements. Omitting this keyword has the same behavior as an empty schema.

contains

ts
contains(contains: JSONSchema6Definition): this;

An array instance is valid against "contains" if at least one of its elements is valid against the given schema.

examples

ts
examples(examples: JSONSchema6Type[]): this;

Array of examples with no validation effect the value of "default" is usable as an example without repeating it under this keyword

example

ts
example(...examples: JSONSchema6Type[]): this;

Array of examples with no validation effect the value of "default" is usable as an example without repeating it under this keyword

items

ts
items(items: AnyJsonSchema | AnyJsonSchema[]): this;

This keyword determines how child instances validate for arrays, and does not directly validate the immediate instance itself. Omitting this keyword has the same behavior as an empty schema.

maxItems

ts
maxItems(maxItems: number): this;

Must be a non-negative integer. An array instance is valid against "maxItems" if its size is less than, or equal to, the value of this keyword.

minItems

ts
minItems(minItems: number): this;

Must be a non-negative integer. An array instance is valid against "maxItems" if its size is greater than, or equal to, the value of this keyword. Omitting this keyword has the same behavior as a value of 0.

uniqueItems

ts
uniqueItems(uniqueItems: boolean): this;

If this keyword has boolean value false, the instance validates successfully. If it has boolean value true, the instance validates successfully if all of its elements are unique. Omitting this keyword has the same behavior as a value of false.

maxProperties

ts
maxProperties(maxProperties: number): this;

Must be a non-negative integer. An object instance is valid against "maxProperties" if its number of properties is less than, or equal to, the value of this keyword.

minProperties

ts
minProperties(minProperties: number): this;

Must be a non-negative integer. An object instance is valid against "maxProperties" if its number of properties is greater than, or equal to, the value of this keyword. Omitting this keyword has the same behavior as a value of 0.

allow

ts
allow(...allow: any[]): this;

required

ts
required(required?: boolean | string[]): JsonSchema;

Elements of this array must be unique. An object instance is valid against this keyword if every item in the array is the name of a property in the instance. Omitting this keyword has the same behavior as an empty array.

addRequired

ts
addRequired(property: string): this;

removeRequired

ts
removeRequired(property: string): this;

isRequired

ts
isRequired(property: string): boolean;

properties

ts
properties(properties: AnyJsonSchema | Record<string, AnyJsonSchema>): this;

This keyword determines how child instances validate for objects, and does not directly validate the immediate instance itself. Validation succeeds if, for each name that appears in both the instance and as a name within this keyword's value, the child instance for that name successfully validates against the corresponding schema. Omitting this keyword has the same behavior as an empty object.

addProperty

ts
addProperty(key: string, schema: AnyJsonSchema): this;

patternProperties

ts
patternProperties(patternProperties: Record<string, AnyJsonSchema>): this;

This attribute is an object that defines the schema for a set of property names of an object instance. The name of each property of this attribute's object is a regular expression pattern in the ECMA 262, while the value is a schema. If the pattern matches the name of a property on the instance object, the value of the instance's property MUST be valid against the pattern name's schema value. Omitting this keyword has the same behavior as an empty object.

additionalProperties

ts
additionalProperties(additionalProperties: boolean | AnyJsonSchema): this;

This attribute defines a schema for all properties that are not explicitly defined in an object type definition. If specified, the value MUST be a schema or a boolean. If false is provided, no additional properties are allowed beyond the properties defined in the schema. The default value is an empty schema which allows any value for additional properties.

unknown

ts
unknown(unknown?: boolean): this;

This attribute defines a schema for all properties that are not explicitly defined in an object type definition. If specified, the value MUST be a schema or a boolean. If false is provided, no additional properties are allowed beyond the properties defined in the schema. The default value is an empty schema which allows any value for additional properties.

dependencies

ts
dependencies(dependencies: {
     [p: string]: JSONSchema6Definition | JsonSchema | string[];
 }): this;

This keyword specifies rules that are evaluated if the instance is an object and contains a certain property. Each property specifies a dependency. If the dependency value is an array, each element in the array must be unique. Omitting this keyword has the same behavior as an empty object.

propertyNames

ts
propertyNames(propertyNames: JSONSchema6Definition | JsonSchema): this;

Takes a schema which validates the names of all properties rather than their values. Note the property name that the schema is testing will always be a string. Omitting this keyword has the same behavior as an empty schema.

enum

ts
enum(...enumValues: any[]): this;

This provides an enumeration of all possible values that are valid for the instance property. This MUST be an array, and each item in the array represents a possible value for the instance value. If this attribute is defined, the instance value MUST be one of the values in the array in order for the schema to be valid.

enum

ts
enum(enumSchema: JsonSchema): this;

definitions

ts
definitions(definitions: Record<string, AnyJsonSchema>): this;

allOf

ts
allOf(allOf: AnyJsonSchema[]): this;

anyOf

ts
anyOf(anyOf: AnyJsonSchema[]): this;

oneOf

ts
oneOf(oneOf: AnyJsonSchema[]): this;

not

ts
not(not: AnyJsonSchema): this;

multipleOf

ts
multipleOf(multipleOf: number): this;

Must be strictly greater than 0. A numeric instance is valid only if division by this keyword's value results in an integer.

maximum

ts
maximum(maximum: number): this;

Representing an inclusive upper limit for a numeric instance. This keyword validates only if the instance is less than or exactly equal to "maximum".

exclusiveMaximum

ts
exclusiveMaximum(exclusiveMaximum: number): this;

Representing an exclusive upper limit for a numeric instance. This keyword validates only if the instance is strictly less than (not equal to) to "exclusiveMaximum".

minimum

ts
minimum(minimum: number): this;

Representing an inclusive lower limit for a numeric instance. This keyword validates only if the instance is greater than or exactly equal to "minimum".

exclusiveMinimum

ts
exclusiveMinimum(exclusiveMinimum: number): this;

Representing an exclusive lower limit for a numeric instance. This keyword validates only if the instance is strictly greater than (not equal to) to "exclusiveMinimum".

maxLength

ts
maxLength(maxLength: number): this;

Must be a non-negative integer. A string instance is valid against this keyword if its length is less than, or equal to, the value of this keyword.

minLength

ts
minLength(minLength: number): this;

Must be a non-negative integer. A string instance is valid against this keyword if its length is greater than, or equal to, the value of this keyword. Omitting this keyword has the same behavior as a value of 0.

pattern

ts
pattern(pattern: string | RegExp): this;

Should be a valid regular expression, according to the ECMA 262 regular expression dialect.

format

ts
format(format: JsonFormatTypes | ValueOf<JsonFormatTypes>): this;

type

ts
type(type: any | JSONSchema6TypeName | JSONSchema6TypeName[]): this;

A single type, or a union of simple types

any

ts
any(...types: any[]): this;

integer

ts
integer(): this;

title

ts
title(title: string): this;

This attribute is a string that provides a short description of the instance property.

readOnly

ts
readOnly(readOnly: boolean): this;

writeOnly

ts
writeOnly(readOnly: boolean): this;

customKey

ts
customKey(key: string, value: any): this;

toObject

ts
toObject(options?: JsonSchemaOptions): any;

toJSON

ts
toJSON(options?: JsonSchemaOptions): any;

assign

ts
assign(obj?: JsonSchema | Partial<JsonSchemaObject>): this;

set

ts
set(key: string, value: any): this;

getComputedType

ts
getComputedType(): any;

Return the itemSchema computed type. If the type is a function used for recursive model, the function will be called to get the right type.

getComputedItemType

ts
getComputedItemType(): any;

getJsonType

ts
getJsonType(): string | string[];

Return the Json type as string

getTarget

ts
getTarget(): Type<any>;

getName

ts
getName(): any;

Get the symbolic name of the entity

clone

ts
clone(): JsonSchema;

protected setManyOf

ts
protected setManyOf(keyword: "oneOf" | "anyOf" | "allOf", value: AnyJsonSchema[]): this | undefined;

Released under the MIT License.