1 // Type definitions for json-schema 4.0, 6.0 and 7.0
2 // Project: https://github.com/kriszyp/json-schema
3 // Definitions by: Boris Cherny <https://github.com/bcherny>
4 // Cyrille Tuzi <https://github.com/cyrilletuzi>
5 // Lucian Buzzo <https://github.com/lucianbuzzo>
6 // Roland Groza <https://github.com/rolandjitsu>
7 // Jason Kwok <https://github.com/JasonHK>
8 // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
9 // TypeScript Version: 2.2
11 //==================================================================================================
12 // JSON Schema Draft 04
13 //==================================================================================================
16 * @see https://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.1
18 export type JSONSchema4TypeName =
29 * @see https://tools.ietf.org/html/draft-zyp-json-schema-04#section-3.5
31 export type JSONSchema4Type =
39 // Workaround for infinite type recursion
40 export interface JSONSchema4Object {
41 [key: string]: JSONSchema4Type;
44 // Workaround for infinite type recursion
45 // https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
46 export interface JSONSchema4Array extends Array<JSONSchema4Type> {}
52 * - 'http://json-schema.org/schema#'
53 * - 'http://json-schema.org/hyper-schema#'
54 * - 'http://json-schema.org/draft-04/schema#'
55 * - 'http://json-schema.org/draft-04/hyper-schema#'
56 * - 'http://json-schema.org/draft-03/schema#'
57 * - 'http://json-schema.org/draft-03/hyper-schema#'
59 * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
61 export type JSONSchema4Version = string;
65 * @see https://tools.ietf.org/html/draft-zyp-json-schema-04
67 export interface JSONSchema4 {
70 $schema?: JSONSchema4Version
73 * This attribute is a string that provides a short description of the
76 * @see https://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.21
81 * This attribute is a string that provides a full description of the of
82 * purpose the instance property.
84 * @see https://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.22
88 default?: JSONSchema4Type
91 exclusiveMaximum?: boolean
93 exclusiveMinimum?: boolean
99 * May only be defined when "items" is defined, and is a tuple of JSONSchemas.
101 * This provides a definition for additional items in an array instance
102 * when tuple definitions of the items is provided. This can be false
103 * to indicate additional items in the array are not allowed, or it can
104 * be a schema that defines the schema of the additional items.
106 * @see https://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.6
108 additionalItems?: boolean | JSONSchema4
111 * This attribute defines the allowed items in an instance array, and
112 * MUST be a schema or an array of schemas. The default value is an
113 * empty schema which allows any value for items in the instance array.
115 * When this attribute value is a schema and the instance value is an
116 * array, then all the items in the array MUST be valid according to the
119 * When this attribute value is an array of schemas and the instance
120 * value is an array, each position in the instance array MUST conform
121 * to the schema in the corresponding position for this array. This
122 * called tuple typing. When tuple typing is used, additional items are
123 * allowed, disallowed, or constrained by the "additionalItems"
124 * (Section 5.6) attribute using the same rules as
125 * "additionalProperties" (Section 5.4) for objects.
127 * @see https://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.5
129 items?: JSONSchema4 | JSONSchema4[]
133 uniqueItems?: boolean
134 maxProperties?: number
135 minProperties?: number
138 * This attribute indicates if the instance must have a value, and not
139 * be undefined. This is false by default, making the instance
142 * @see https://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.7
144 required?: false | string[]
147 * This attribute defines a schema for all properties that are not
148 * explicitly defined in an object type definition. If specified, the
149 * value MUST be a schema or a boolean. If false is provided, no
150 * additional properties are allowed beyond the properties defined in
151 * the schema. The default value is an empty schema which allows any
152 * value for additional properties.
154 * @see https://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.4
156 additionalProperties?: boolean | JSONSchema4
159 [k: string]: JSONSchema4
163 * This attribute is an object with property definitions that define the
164 * valid values of instance object property values. When the instance
165 * value is an object, the property values of the instance object MUST
166 * conform to the property definitions in this object. In this object,
167 * each property definition's value MUST be a schema, and the property's
168 * name MUST be the name of the instance property that it defines. The
169 * instance property value MUST be valid according to the schema from
170 * the property definition. Properties are considered unordered, the
171 * order of the instance properties MAY be in any order.
173 * @see https://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.2
176 [k: string]: JSONSchema4
180 * This attribute is an object that defines the schema for a set of
181 * property names of an object instance. The name of each property of
182 * this attribute's object is a regular expression pattern in the ECMA
183 * 262/Perl 5 format, while the value is a schema. If the pattern
184 * matches the name of a property on the instance object, the value of
185 * the instance's property MUST be valid against the pattern name's
188 * @see https://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.3
190 patternProperties?: {
191 [k: string]: JSONSchema4
194 [k: string]: JSONSchema4 | string[]
198 * This provides an enumeration of all possible values that are valid
199 * for the instance property. This MUST be an array, and each item in
200 * the array represents a possible value for the instance value. If
201 * this attribute is defined, the instance value MUST be one of the
202 * values in the array in order for the schema to be valid.
204 * @see https://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.19
206 enum?: JSONSchema4Type[]
209 * A single type, or a union of simple types
211 type?: JSONSchema4TypeName | JSONSchema4TypeName[]
213 allOf?: JSONSchema4[]
214 anyOf?: JSONSchema4[]
215 oneOf?: JSONSchema4[]
219 * The value of this property MUST be another schema which will provide
220 * a base schema which the current schema will inherit from. The
221 * inheritance rules are such that any instance that is valid according
222 * to the current schema MUST be valid according to the referenced
223 * schema. This MAY also be an array, in which case, the instance MUST
224 * be valid for all the schemas in the array. A schema that extends
225 * another schema MAY define additional attributes, constrain existing
226 * attributes, or add other constraints.
228 * Conceptually, the behavior of extends can be seen as validating an
229 * instance against all constraints in the extending schema as well as
230 * the extended schema(s).
232 * @see https://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.26
234 extends?: string | string[]
237 * @see https://tools.ietf.org/html/draft-zyp-json-schema-04#section-5.6
244 //==================================================================================================
245 // JSON Schema Draft 06
246 //==================================================================================================
248 export type JSONSchema6TypeName =
258 export type JSONSchema6Type =
266 // Workaround for infinite type recursion
267 export interface JSONSchema6Object {
268 [key: string]: JSONSchema6Type;
271 // Workaround for infinite type recursion
272 // https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
273 export interface JSONSchema6Array extends Array<JSONSchema6Type> {}
278 * Recommended values:
279 * - 'http://json-schema.org/schema#'
280 * - 'http://json-schema.org/hyper-schema#'
281 * - 'http://json-schema.org/draft-06/schema#'
282 * - 'http://json-schema.org/draft-06/hyper-schema#'
284 * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
286 export type JSONSchema6Version = string;
290 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01
292 export type JSONSchema6Definition = JSONSchema6 | boolean;
293 export interface JSONSchema6 {
296 $schema?: JSONSchema6Version
299 * Must be strictly greater than 0.
300 * A numeric instance is valid only if division by this keyword's value results in an integer.
301 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.1
306 * Representing an inclusive upper limit for a numeric instance.
307 * This keyword validates only if the instance is less than or exactly equal to "maximum".
308 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.2
313 * Representing an exclusive upper limit for a numeric instance.
314 * This keyword validates only if the instance is strictly less than (not equal to) to "exclusiveMaximum".
315 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.3
317 exclusiveMaximum?: number
320 * Representing an inclusive lower limit for a numeric instance.
321 * This keyword validates only if the instance is greater than or exactly equal to "minimum".
322 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.4
327 * Representing an exclusive lower limit for a numeric instance.
328 * This keyword validates only if the instance is strictly greater than (not equal to) to "exclusiveMinimum".
329 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.5
331 exclusiveMinimum?: number
334 * Must be a non-negative integer.
335 * A string instance is valid against this keyword if its length is less than, or equal to, the value of this keyword.
336 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.6
341 * Must be a non-negative integer.
342 * A string instance is valid against this keyword if its length is greater than, or equal to, the value of this keyword.
343 * Omitting this keyword has the same behavior as a value of 0.
344 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.7
349 * Should be a valid regular expression, according to the ECMA 262 regular expression dialect.
350 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.8
355 * This keyword determines how child instances validate for arrays, and does not directly validate the immediate instance itself.
356 * Omitting this keyword has the same behavior as an empty schema.
357 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.9
359 items?: JSONSchema6Definition | JSONSchema6Definition[]
362 * This keyword determines how child instances validate for arrays, and does not directly validate the immediate instance itself.
363 * If "items" is an array of schemas, validation succeeds if every instance element
364 * at a position greater than the size of "items" validates against "additionalItems".
365 * Otherwise, "additionalItems" MUST be ignored, as the "items" schema
366 * (possibly the default value of an empty schema) is applied to all elements.
367 * Omitting this keyword has the same behavior as an empty schema.
368 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.10
370 additionalItems?: JSONSchema6Definition
373 * Must be a non-negative integer.
374 * An array instance is valid against "maxItems" if its size is less than, or equal to, the value of this keyword.
375 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.11
380 * Must be a non-negative integer.
381 * An array instance is valid against "maxItems" if its size is greater than, or equal to, the value of this keyword.
382 * Omitting this keyword has the same behavior as a value of 0.
383 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.12
388 * If this keyword has boolean value false, the instance validates successfully.
389 * If it has boolean value true, the instance validates successfully if all of its elements are unique.
390 * Omitting this keyword has the same behavior as a value of false.
391 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.13
393 uniqueItems?: boolean
396 * An array instance is valid against "contains" if at least one of its elements is valid against the given schema.
397 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.14
399 contains?: JSONSchema6Definition
402 * Must be a non-negative integer.
403 * An object instance is valid against "maxProperties" if its number of properties is less than, or equal to, the value of this keyword.
404 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.15
406 maxProperties?: number
409 * Must be a non-negative integer.
410 * An object instance is valid against "maxProperties" if its number of properties is greater than,
411 * or equal to, the value of this keyword.
412 * Omitting this keyword has the same behavior as a value of 0.
413 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.16
415 minProperties?: number
418 * Elements of this array must be unique.
419 * An object instance is valid against this keyword if every item in the array is the name of a property in the instance.
420 * Omitting this keyword has the same behavior as an empty array.
422 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.17
427 * This keyword determines how child instances validate for objects, and does not directly validate the immediate instance itself.
428 * Validation succeeds if, for each name that appears in both the instance and as a name within this keyword's value,
429 * the child instance for that name successfully validates against the corresponding schema.
430 * Omitting this keyword has the same behavior as an empty object.
431 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.18
434 [k: string]: JSONSchema6Definition
438 * This attribute is an object that defines the schema for a set of property names of an object instance.
439 * 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.
440 * If the pattern matches the name of a property on the instance object, the value of the instance's property
441 * MUST be valid against the pattern name's schema value.
442 * Omitting this keyword has the same behavior as an empty object.
443 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.19
445 patternProperties?: {
446 [k: string]: JSONSchema6Definition
450 * This attribute defines a schema for all properties that are not explicitly defined in an object type definition.
451 * If specified, the value MUST be a schema or a boolean.
452 * If false is provided, no additional properties are allowed beyond the properties defined in the schema.
453 * The default value is an empty schema which allows any value for additional properties.
454 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.20
456 additionalProperties?: JSONSchema6Definition
459 * This keyword specifies rules that are evaluated if the instance is an object and contains a certain property.
460 * Each property specifies a dependency.
461 * If the dependency value is an array, each element in the array must be unique.
462 * Omitting this keyword has the same behavior as an empty object.
463 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.21
466 [k: string]: JSONSchema6Definition | string[]
470 * Takes a schema which validates the names of all properties rather than their values.
471 * Note the property name that the schema is testing will always be a string.
472 * Omitting this keyword has the same behavior as an empty schema.
473 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.22
475 propertyNames?: JSONSchema6Definition
478 * This provides an enumeration of all possible values that are valid
479 * for the instance property. This MUST be an array, and each item in
480 * the array represents a possible value for the instance value. If
481 * this attribute is defined, the instance value MUST be one of the
482 * values in the array in order for the schema to be valid.
484 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.23
486 enum?: JSONSchema6Type[]
489 * More readable form of a one-element "enum"
490 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.24
492 const?: JSONSchema6Type
495 * A single type, or a union of simple types
496 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.25
498 type?: JSONSchema6TypeName | JSONSchema6TypeName[]
501 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.26
503 allOf?: JSONSchema6Definition[]
506 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.27
508 anyOf?: JSONSchema6Definition[]
511 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.28
513 oneOf?: JSONSchema6Definition[]
516 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.29
518 not?: JSONSchema6Definition
521 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.1
524 [k: string]: JSONSchema6Definition
528 * This attribute is a string that provides a short description of the instance property.
530 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.2
535 * This attribute is a string that provides a full description of the of purpose the instance property.
537 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.2
542 * This keyword can be used to supply a default JSON value associated with a particular schema.
543 * It is RECOMMENDED that a default value be valid against the associated schema.
544 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.3
546 default?: JSONSchema6Type
549 * Array of examples with no validation effect the value of "default" is usable as an example without repeating it under this keyword
550 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.4
552 examples?: JSONSchema6Type[]
555 * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-8
560 //==================================================================================================
561 // JSON Schema Draft 07
562 //==================================================================================================
563 // https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
564 //--------------------------------------------------------------------------------------------------
568 * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
570 export type JSONSchema7TypeName =
581 * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
583 export type JSONSchema7Type =
591 // Workaround for infinite type recursion
592 export interface JSONSchema7Object {
593 [key: string]: JSONSchema7Type;
596 // Workaround for infinite type recursion
597 // https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
598 export interface JSONSchema7Array extends Array<JSONSchema7Type> {}
603 * Recommended values:
604 * - 'http://json-schema.org/schema#'
605 * - 'http://json-schema.org/hyper-schema#'
606 * - 'http://json-schema.org/draft-07/schema#'
607 * - 'http://json-schema.org/draft-07/hyper-schema#'
609 * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
611 export type JSONSchema7Version = string;
615 * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
617 export type JSONSchema7Definition = JSONSchema7 | boolean;
618 export interface JSONSchema7 {
621 $schema?: JSONSchema7Version;
625 * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1
627 type?: JSONSchema7TypeName | JSONSchema7TypeName[];
628 enum?: JSONSchema7Type[];
629 const?: JSONSchema7Type;
632 * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.2
636 exclusiveMaximum?: number;
638 exclusiveMinimum?: number;
641 * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.3
648 * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4
650 items?: JSONSchema7Definition | JSONSchema7Definition[];
651 additionalItems?: JSONSchema7Definition;
654 uniqueItems?: boolean;
655 contains?: JSONSchema7;
658 * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5
660 maxProperties?: number;
661 minProperties?: number;
664 [key: string]: JSONSchema7Definition;
666 patternProperties?: {
667 [key: string]: JSONSchema7Definition;
669 additionalProperties?: JSONSchema7Definition;
671 [key: string]: JSONSchema7Definition | string[];
673 propertyNames?: JSONSchema7Definition;
676 * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6
678 if?: JSONSchema7Definition;
679 then?: JSONSchema7Definition;
680 else?: JSONSchema7Definition;
683 * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.7
685 allOf?: JSONSchema7Definition[];
686 anyOf?: JSONSchema7Definition[];
687 oneOf?: JSONSchema7Definition[];
688 not?: JSONSchema7Definition;
691 * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-7
696 * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-8
698 contentMediaType?: string;
699 contentEncoding?: string;
702 * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9
705 [key: string]: JSONSchema7Definition;
709 * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10
712 description?: string;
713 default?: JSONSchema7Type;
716 examples?: JSONSchema7Type;
719 export interface ValidationResult {
721 errors: ValidationError[];
724 export interface ValidationError {
730 * To use the validator call JSONSchema.validate with an instance object and an optional schema object.
731 * If a schema is provided, it will be used to validate. If the instance object refers to a schema (self-validating),
732 * that schema will be used to validate and the schema parameter is not necessary (if both exist,
733 * both validations will occur).
735 export function validate(instance: {}, schema: JSONSchema4 | JSONSchema6 | JSONSchema7): ValidationResult;
738 * The checkPropertyChange method will check to see if an value can legally be in property with the given schema
739 * This is slightly different than the validate method in that it will fail if the schema is readonly and it will
740 * not check for self-validation, it is assumed that the passed in value is already internally valid.
742 export function checkPropertyChange(value: any, schema: JSONSchema4 | JSONSchema6 | JSONSchema7, property: string): ValidationResult;
745 * This checks to ensure that the result is valid and will throw an appropriate error message if it is not.
747 export function mustBeValid(result: ValidationResult): void;