Skip to content

Commit

Permalink
Merge pull request #29026 from afinch7/master
Browse files Browse the repository at this point in the history
json-schema: v6 and v7 changed to better comply with the draft definitions.
  • Loading branch information
jessetrinity committed Sep 20, 2018
2 parents 13845db + e1d83a1 commit cb75e9c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 35 deletions.
62 changes: 34 additions & 28 deletions types/json-schema/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export type JSONSchema6Type = any[] | boolean | number | null | object | string
* JSON Schema V6
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01
*/
export type JSONSchema6Definition = JSONSchema6 | boolean;
export interface JSONSchema6 {
$id?: string
$ref?: string
Expand Down Expand Up @@ -281,7 +282,7 @@ export interface JSONSchema6 {
* 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?: boolean | JSONSchema6 | JSONSchema6[]
items?: JSONSchema6Definition | JSONSchema6Definition[]

/**
* This keyword determines how child instances validate for arrays, and does not directly validate the immediate instance itself.
Expand All @@ -292,7 +293,7 @@ export interface JSONSchema6 {
* 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?: boolean | JSONSchema6
additionalItems?: JSONSchema6Definition

/**
* Must be a non-negative integer.
Expand Down Expand Up @@ -321,7 +322,7 @@ export interface JSONSchema6 {
* 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?: boolean | JSONSchema6
contains?: JSONSchema6Definition

/**
* Must be a non-negative integer.
Expand Down Expand Up @@ -356,7 +357,7 @@ export interface JSONSchema6 {
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.18
*/
properties?: {
[k: string]: boolean | JSONSchema6
[k: string]: JSONSchema6Definition
}

/**
Expand All @@ -368,7 +369,7 @@ export interface JSONSchema6 {
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.19
*/
patternProperties?: {
[k: string]: boolean | JSONSchema6
[k: string]: JSONSchema6Definition
}

/**
Expand All @@ -378,7 +379,7 @@ export interface JSONSchema6 {
* 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?: boolean | JSONSchema6
additionalProperties?: JSONSchema6Definition

/**
* This keyword specifies rules that are evaluated if the instance is an object and contains a certain property.
Expand All @@ -388,7 +389,7 @@ export interface JSONSchema6 {
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.21
*/
dependencies?: {
[k: string]: boolean | JSONSchema6 | string[]
[k: string]: JSONSchema6Definition | string[]
}

/**
Expand All @@ -397,7 +398,7 @@ export interface JSONSchema6 {
* 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?: boolean | JSONSchema6
propertyNames?: JSONSchema6Definition

/**
* This provides an enumeration of all possible values that are valid
Expand Down Expand Up @@ -425,28 +426,28 @@ export interface JSONSchema6 {
/**
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.26
*/
allOf?: JSONSchema6[]
allOf?: JSONSchema6Definition[]

/**
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.27
*/
anyOf?: JSONSchema6[]
anyOf?: JSONSchema6Definition[]

/**
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.28
*/
oneOf?: JSONSchema6[]
oneOf?: JSONSchema6Definition[]

/**
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.29
*/
not?: boolean | JSONSchema6
not?: JSONSchema6Definition

/**
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.1
*/
definitions?: {
[k: string]: boolean | JSONSchema6
[k: string]: JSONSchema6Definition
}

/**
Expand Down Expand Up @@ -508,6 +509,11 @@ export type JSONSchema7Version = 'http://json-schema.org/schema#'
| 'http://json-schema.org/draft-07/schema#'
| 'http://json-schema.org/draft-07/hyper-schema#';

/**
* JSON Schema v7
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
*/
export type JSONSchema7Definition = JSONSchema7 | boolean;
export interface JSONSchema7 {
$id?: string;
$ref?: string;
Expand Down Expand Up @@ -540,8 +546,8 @@ export interface JSONSchema7 {
/**
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4
*/
items?: JSONSchema7 | JSONSchema7[];
additionalItems?: JSONSchema7;
items?: JSONSchema7Definition | JSONSchema7Definition[];
additionalItems?: JSONSchema7Definition;
maxItems?: number;
minItems?: number;
uniqueItems?: boolean;
Expand All @@ -554,31 +560,31 @@ export interface JSONSchema7 {
minProperties?: number;
required?: string[];
properties?: {
[key: string]: JSONSchema7;
[key: string]: JSONSchema7Definition;
};
patternProperties?: {
[key: string]: JSONSchema7;
[key: string]: JSONSchema7Definition;
};
additionalProperties?: JSONSchema7;
additionalProperties?: JSONSchema7Definition;
dependencies?: {
[key: string]: JSONSchema7 | string[];
[key: string]: JSONSchema7Definition | string[];
};
propertyNames?: JSONSchema7;
propertyNames?: JSONSchema7Definition;

/**
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6
*/
if?: JSONSchema7;
then?: JSONSchema7;
else?: JSONSchema7;
if?: JSONSchema7Definition;
then?: JSONSchema7Definition;
else?: JSONSchema7Definition;

/**
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.7
*/
allOf?: JSONSchema7[];
anyOf?: JSONSchema7[];
oneOf?: JSONSchema7[];
not?: JSONSchema7;
allOf?: JSONSchema7Definition[];
anyOf?: JSONSchema7Definition[];
oneOf?: JSONSchema7Definition[];
not?: JSONSchema7Definition;

/**
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-7
Expand All @@ -595,7 +601,7 @@ export interface JSONSchema7 {
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9
*/
definitions?: {
[key: string]: JSONSchema7;
[key: string]: JSONSchema7Definition;
};

/**
Expand Down
30 changes: 23 additions & 7 deletions types/json-schema/json-schema-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
JSONSchema7,
JSONSchema7Array,
JSONSchema7Type,
JSONSchema7TypeName
JSONSchema7TypeName,
JSONSchema6Definition,
JSONSchema7Definition
} from 'json-schema'

/* JSON Schema 4 */
Expand Down Expand Up @@ -80,6 +82,10 @@ import {
}
}

// Class
class Schema4 implements JSONSchema4 {
}

/* JSON Schema 6 */

// SimpleType
Expand All @@ -96,10 +102,10 @@ import {
const c: JSONSchema6Type = [1, 2]
}

// JSONSchema6
// JSONSchema6Definition
() => {
const a: JSONSchema6 = {}
const b: JSONSchema6 = {
const a: JSONSchema6Definition = {}
const b: JSONSchema6Definition = {
$id: 'foo',
$ref: 'foo/bar',
$schema: 'http://json-schema.org/schema#',
Expand Down Expand Up @@ -149,6 +155,11 @@ import {
propertyNames: {},
format: 'date-time'
}
const c: JSONSchema6Definition = false;
}

// Class
class Schema6 implements JSONSchema6 {
}

/* JSON Schema 7 */
Expand All @@ -167,10 +178,10 @@ import {
const c: JSONSchema7Type = [1, 2]
}

// JSONSchema4
// JSONSchema7Definition
() => {
const a: JSONSchema7 = {}
const b: JSONSchema7 = {
const a: JSONSchema7Definition = {}
const b: JSONSchema7Definition = {
$id: 'foo',
$ref: 'foo/bar',
$schema: 'http://json-schema.org/schema#',
Expand Down Expand Up @@ -228,4 +239,9 @@ import {
if: {},
then: {}
}
const c: JSONSchema7Definition = false;
}

// Class
class Schema7 implements JSONSchema7 {
}

0 comments on commit cb75e9c

Please sign in to comment.