@tsed/schema
Version:
JsonSchema module for Ts.ED Framework
385 lines (384 loc) • 18.7 kB
TypeScript
import { Type, ValueOf } from "@tsed/core";
import { Hooks } from "@tsed/hooks";
import type { JSONSchema7, JSONSchema7Definition, JSONSchema7Type, JSONSchema7TypeName, JSONSchema7Version } from "json-schema";
import { VendorKeys } from "../constants/VendorKeys.js";
import { IgnoreCallback } from "../interfaces/IgnoreCallback.js";
import { JsonSchemaOptions } from "../interfaces/JsonSchemaOptions.js";
import { type GenericsMap, GenericValue } from "../utils/generics.js";
import { AliasMap, AliasType } from "./JsonAliasMap.js";
import { Discriminator } from "./JsonDiscriminator.js";
import { JsonFormatTypes } from "./JsonFormatTypes.js";
import { JsonLazyRef } from "./JsonLazyRef.js";
export interface JsonSchemaObject extends Omit<JSONSchema7, "type" | "additionalProperties" | "items"> {
type?: Type | JSONSchema7["type"] | null | (String | null | Date | Number | Object | Boolean)[];
additionalProperties?: boolean | JSONSchema7 | Type | JsonSchema;
items?: (Type | JSONSchema7Definition | JsonSchema) | (Type | JSONSchema7Definition | JsonSchema)[];
}
export type AnyJsonSchema = JsonSchemaObject | JSONSchema7 | JsonSchema | JsonLazyRef | {
label?: string;
} | Type;
export declare class JsonSchema extends Map<string, any> {
#private;
readonly $kind: string;
readonly $isJsonDocument = true;
readonly $hooks: Hooks;
readonly $allow: any[];
$selfRequired: boolean;
$ignore: boolean | IgnoreCallback;
isDiscriminatorKey: boolean;
isDiscriminator: boolean;
constructor(obj?: JsonSchema | Partial<JsonSchemaObject>);
get alias(): AliasMap;
get isClass(): boolean;
/**
* The current schema is a collection
*/
get isCollection(): boolean;
/**
* The current schema is a reference to another schema
* to link property/param to a class schema (if the reference exists).
*/
get isLocalSchema(): boolean;
/**
* Current schema is a generic
*/
get isGeneric(): boolean;
get discriminatorAncestor(): any;
/**
* Return the itemSchema computed type.
* If the type is a function used for a recursive model,
* the function will be called to get the right type.
*/
get class(): Type<any>;
/**
* Return the itemSchema computed type.
*/
get collectionClass(): Type<any>;
get canRef(): boolean;
get isNullable(): boolean;
get isReadOnly(): any;
get isWriteOnly(): any;
get hasDiscriminator(): boolean;
static from(item?: Partial<JsonSchemaObject> | Type<any> | JsonSchema): JsonSchema;
/**
* Check if the schema as key, #key or x-key
* @param key
*/
has(key: string): boolean;
/**
* Get the value of the schema by key, #key or x-key.
* @param key
* @param defaultValue
*/
get<T = any>(key: string, defaultValue: T): T;
get<T = any>(key: string, defaultValue?: T): T | undefined;
forwardGroups(bool?: boolean): this;
groups(groups?: string[], isProperty?: boolean): this;
groupsName(groupsName?: string): this;
allowedGroups(groups?: string[]): this;
/**
* Assign the Generic Labels used over a class or a schema.
* @param genericLabels
*/
genericLabels(genericLabels: string[]): this;
genericLabel(genericLabel: string): this;
/**
* Assign the types of the generics that must be used for the property
* @param generics
*/
genericOf(...generics: GenericValue[][]): this;
nullable(value: boolean): this;
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: JSONSchema7Version): this;
/**
* Create a ref and use name to sharing schema
* @param name
*/
label(name: string): this;
name(name: string): this;
ignore(cb: boolean | IgnoreCallback): 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.
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.3
*/
default(value: JSONSchema7Type | undefined | (() => JSONSchema7Type)): this;
/**
* More readible form of a one-element "enum"
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.24
*/
const(value: JSONSchema7Type): this;
/**
* This attribute is a string that provides a full description of the of purpose the instance property.
*
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.2
*/
description(description: string): this;
discriminator(): Discriminator;
discriminatorKey(propertyName: string): this;
discriminatorValue(...values: string[]): 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.
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.10
*/
additionalItems(additionalItems: boolean | AnyJsonSchema): this;
/**
* An array instance is valid against "contains" if at least one of its elements is valid against the given schema.
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.14
*/
contains(contains: JSONSchema7Definition): this;
/**
* Array of examples with no validation effect the value of "default" is usable as an example without repeating it under this keyword
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.4
*/
examples(examples: JSONSchema7Type[]): this;
/**
* Array of examples with no validation effect the value of "default" is usable as an example without repeating it under this keyword
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.4
*/
example(...examples: JSONSchema7Type[]): 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.
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.9
*/
items(items: AnyJsonSchema | AnyJsonSchema[]): 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.
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.11
*/
maxItems(maxItems: 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.
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.12
*/
minItems(minItems: number): 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.
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.13
*/
uniqueItems(uniqueItems: boolean): 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.
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.15
*/
maxProperties(maxProperties: 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.
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.16
*/
minProperties(minProperties: number): this;
allow(...allow: any[]): this;
/**
* 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.
*
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.17
*/
required(required?: boolean | string[]): JsonSchema;
addRequired(property: string): this;
removeRequired(property: string): this;
isRequired(property: string): boolean;
getRequiredFields(): string[];
/**
* 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.
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.18
*/
properties(properties: AnyJsonSchema | Record<string, AnyJsonSchema>): this;
addProperty(key: string, schema: 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.
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.19
*/
patternProperties(patternProperties: Record<string, 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.
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.20
*/
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.
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.20
* @alias additionalProperties
* @param unknown
*/
unknown(unknown?: boolean): 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.
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.21
*/
dependencies(dependencies: {
[p: string]: JSONSchema7Definition | JsonSchema | string[];
}): 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.
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.22
*/
propertyNames(propertyNames: JSONSchema7Definition | JsonSchema): 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.
*
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.23
*/
enum(...enumValues: any[]): this;
enum(enumSchema: JsonSchema): this;
/**
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.1
*/
definitions(definitions: Record<string, AnyJsonSchema>): this;
/**
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.26
*/
allOf(allOf: AnyJsonSchema[]): this;
/**
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.27
*/
anyOf(anyOf: AnyJsonSchema[]): this;
oneOf(oneOf: AnyJsonSchema[]): this;
/**
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.29
*/
not(not: AnyJsonSchema): this;
/**
* Must be strictly greater than 0.
* A numeric instance is valid only if division by this keyword's value results in an integer.
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.1
*/
multipleOf(multipleOf: 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".
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.2
*/
maximum(maximum: 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".
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.3
*/
exclusiveMaximum(exclusiveMaximum: 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".
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.4
*/
minimum(minimum: 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".
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.5
*/
exclusiveMinimum(exclusiveMinimum: 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.
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.6
*/
maxLength(maxLength: 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.
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.7
*/
minLength(minLength: number): this;
/**
* Should be a valid regular expression, according to the ECMA 262 regular expression dialect.
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.8
*/
pattern(pattern: string | RegExp): this;
/**
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-8
*/
format(format: JsonFormatTypes | ValueOf<JsonFormatTypes>): this;
/**
* A single type, or a union of simple types
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.25
*/
type(type: any | JSONSchema7TypeName | JSONSchema7TypeName[]): this;
any(...types: any[]): this;
integer(): this;
/**
* This attribute is a string that provides a short description of the instance property.
*
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.2
*/
title(title: string): this;
readOnly(readOnly: boolean): this;
writeOnly(readOnly: boolean): this;
customKey(key: string, value: any): this;
vendorKey(key: VendorKeys, value: any): this;
toObject(options?: JsonSchemaOptions): any;
toJSON(options?: JsonSchemaOptions): any;
assign(obj?: AnyJsonSchema): this;
set(key: string, value: any): this;
/**
* Return the Json type as string
*/
getJsonType(): string | string[];
getTarget(): Type<any>;
getAllowedGroups(): Set<string> | undefined;
getAllowedRequiredValues(): any[];
getGroups(): string[] | undefined;
getGroupsName(): string | undefined;
/**
* Get the symbolic name of the entity
*/
getName(): any;
clone(): JsonSchema;
getGenericLabels(): string[] | undefined;
getGenericOf(): GenericsMap | undefined;
/**
* Returns the reference schema for class-based schemas.
* Used to get the original schema definition when this schema is a local reference.
* @returns The reference schema or undefined if not applicable
*/
getRefSchema(): JsonSchema | undefined;
propertyKey(s: string): this;
getPropertyKey(): string | symbol | undefined;
target(target: any): void;
isRequiredValue(property: string, value: any): boolean;
protected setManyOf(keyword: "oneOf" | "anyOf" | "allOf", value: AnyJsonSchema[]): this | undefined;
}