JsonSchema
Usage
import { JsonSchema } from "@tsed/schema";
Overview
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
readonly $kind: string;
readonly $isJsonDocument
readonly $isJsonDocument = true;
readonly $hooks
readonly $hooks: Hooks;
readonly $required
readonly $required: Set<string>;
readonly $allow
readonly $allow: any[];
$selfRequired
$selfRequired: boolean;
$forwardGroups
$forwardGroups: boolean;
$ignore
$ignore: boolean | IgnoreCallback;
isDiscriminatorKey
isDiscriminatorKey: boolean;
isDiscriminator
isDiscriminator: boolean;
get alias
get alias(): AliasMap;
get nestedGenerics
get nestedGenerics(): any[];
set nestedGenerics
set nestedGenerics(value: any[]);
get genericLabels
get genericLabels(): string[];
set genericLabels
set genericLabels(value: string[]);
get isClass
get isClass(): boolean;
get isCollection
get isCollection(): boolean;
Current schema is a collection
get isGeneric
get isGeneric(): boolean;
Current schema is a generic
get discriminatorAncestor
get discriminatorAncestor(): any;
get hasGenerics
get hasGenerics(): boolean;
Current schema has generics items
get genericType
get genericType(): string;
get class
get class(): any;
get canRef
get canRef(): boolean;
get isNullable
get isNullable(): boolean;
get isReadOnly
get isReadOnly(): any;
get isWriteOnly
get isWriteOnly(): any;
get hasDiscriminator
get hasDiscriminator(): boolean;
static from
static from(obj?: Partial<JsonSchemaObject>): JsonSchema;
nullable
nullable(value: boolean): void;
itemSchema
itemSchema(obj?: AnyJsonSchema): JsonSchema;
getAliasOf
getAliasOf(property: AliasType): AliasType | undefined;
addAlias
addAlias(property: AliasType, alias: AliasType): this;
removeAlias
removeAlias(property: AliasType): this;
$id
$id($id: string): this;
$ref
$ref($ref: string): this;
$schema
$schema($schema: JSONSchema6Version): this;
label
label(name: string): this;
Create a ref and use name to sharing schema
name
name(name: string): this;
ignore
ignore(cb: boolean | IgnoreCallback): this;
default
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
const(value: JSONSchema6Type): this;
More readible form of a one-element "enum"
description
description(description: string): this;
This attribute is a string that provides a full description of the of purpose the instance property.
discriminator
discriminator(): Discriminator;
discriminatorKey
discriminatorKey(propertyName: string): this;
discriminatorValue
discriminatorValue(...values: string[]): this;
additionalItems
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
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
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
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
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
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
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
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
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
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
allow(...allow: any[]): this;
required
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
addRequired(property: string): this;
removeRequired
removeRequired(property: string): this;
isRequired
isRequired(property: string): boolean;
properties
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
addProperty(key: string, schema: AnyJsonSchema): this;
patternProperties
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
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
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
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
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
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
enum(enumSchema: JsonSchema): this;
definitions
definitions(definitions: Record<string, AnyJsonSchema>): this;
allOf
allOf(allOf: AnyJsonSchema[]): this;
anyOf
anyOf(anyOf: AnyJsonSchema[]): this;
oneOf
oneOf(oneOf: AnyJsonSchema[]): this;
not
not(not: AnyJsonSchema): this;
multipleOf
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
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
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
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
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
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
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
pattern(pattern: string | RegExp): this;
Should be a valid regular expression, according to the ECMA 262 regular expression dialect.
format
format(format: JsonFormatTypes | ValueOf<JsonFormatTypes>): this;
type
type(type: any | JSONSchema6TypeName | JSONSchema6TypeName[]): this;
A single type, or a union of simple types
any
any(...types: any[]): this;
integer
integer(): this;
title
title(title: string): this;
This attribute is a string that provides a short description of the instance property.
readOnly
readOnly(readOnly: boolean): this;
writeOnly
writeOnly(readOnly: boolean): this;
customKey
customKey(key: string, value: any): this;
toObject
toObject(options?: JsonSchemaOptions): any;
toJSON
toJSON(options?: JsonSchemaOptions): any;
assign
assign(obj?: JsonSchema | Partial<JsonSchemaObject>): this;
set
set(key: string, value: any): this;
getComputedType
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
getComputedItemType(): any;
getJsonType
getJsonType(): string | string[];
Return the Json type as string
getTarget
getTarget(): Type<any>;
getName
getName(): any;
Get the symbolic name of the entity
clone
clone(): JsonSchema;
protected setManyOf
protected setManyOf(keyword: "oneOf" | "anyOf" | "allOf", value: AnyJsonSchema[]): this | undefined;