From 0d7ab87136520634baf79ac5f4d43293966901ab Mon Sep 17 00:00:00 2001 From: andy Date: Wed, 19 Sep 2018 16:01:02 -0400 Subject: [PATCH] Refactored my changes to avoid breaking classes that implement JSONSchema6/7 and added test to avoid breaking this in the future. --- types/json-schema/index.d.ts | 64 +++++++++++++------------- types/json-schema/json-schema-tests.ts | 32 +++++++++---- 2 files changed, 55 insertions(+), 41 deletions(-) diff --git a/types/json-schema/index.d.ts b/types/json-schema/index.d.ts index b5d9aedc83..156a86c9a5 100644 --- a/types/json-schema/index.d.ts +++ b/types/json-schema/index.d.ts @@ -214,8 +214,8 @@ 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 JSONSchema6 = JSONSchema6Object | boolean; -export interface JSONSchema6Object { +export type JSONSchema6Definition = JSONSchema6 | boolean; +export interface JSONSchema6 { $id?: string $ref?: string $schema?: 'http://json-schema.org/schema#' | 'http://json-schema.org/hyper-schema#' | @@ -282,7 +282,7 @@ export interface JSONSchema6Object { * 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?: JSONSchema6 | JSONSchema6[] + items?: JSONSchema6Definition | JSONSchema6Definition[] /** * This keyword determines how child instances validate for arrays, and does not directly validate the immediate instance itself. @@ -293,7 +293,7 @@ export interface JSONSchema6Object { * 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?: JSONSchema6 + additionalItems?: JSONSchema6Definition /** * Must be a non-negative integer. @@ -322,7 +322,7 @@ export interface JSONSchema6Object { * 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?: JSONSchema6 + contains?: JSONSchema6Definition /** * Must be a non-negative integer. @@ -357,7 +357,7 @@ export interface JSONSchema6Object { * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.18 */ properties?: { - [k: string]: JSONSchema6 + [k: string]: JSONSchema6Definition } /** @@ -369,7 +369,7 @@ export interface JSONSchema6Object { * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.19 */ patternProperties?: { - [k: string]: JSONSchema6 + [k: string]: JSONSchema6Definition } /** @@ -379,7 +379,7 @@ export interface JSONSchema6Object { * 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?: JSONSchema6 + additionalProperties?: JSONSchema6Definition /** * This keyword specifies rules that are evaluated if the instance is an object and contains a certain property. @@ -389,7 +389,7 @@ export interface JSONSchema6Object { * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.21 */ dependencies?: { - [k: string]: JSONSchema6 | string[] + [k: string]: JSONSchema6Definition | string[] } /** @@ -398,7 +398,7 @@ export interface JSONSchema6Object { * 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?: JSONSchema6 + propertyNames?: JSONSchema6Definition /** * This provides an enumeration of all possible values that are valid @@ -426,28 +426,28 @@ export interface JSONSchema6Object { /** * @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?: JSONSchema6 + not?: JSONSchema6Definition /** * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.1 */ definitions?: { - [k: string]: JSONSchema6 + [k: string]: JSONSchema6Definition } /** @@ -513,8 +513,8 @@ export type JSONSchema7Version = 'http://json-schema.org/schema#' * JSON Schema v7 * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01 */ -export type JSONSchema7 = JSONSchema7Object | boolean; -export interface JSONSchema7Object { +export type JSONSchema7Definition = JSONSchema7 | boolean; +export interface JSONSchema7 { $id?: string; $ref?: string; $schema?: JSONSchema7Version; @@ -546,8 +546,8 @@ export interface JSONSchema7Object { /** * @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; @@ -560,31 +560,31 @@ export interface JSONSchema7Object { 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 @@ -601,7 +601,7 @@ export interface JSONSchema7Object { * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9 */ definitions?: { - [key: string]: JSONSchema7; + [key: string]: JSONSchema7Definition; }; /** diff --git a/types/json-schema/json-schema-tests.ts b/types/json-schema/json-schema-tests.ts index 8d43385aea..ae750271b5 100644 --- a/types/json-schema/json-schema-tests.ts +++ b/types/json-schema/json-schema-tests.ts @@ -8,7 +8,9 @@ import { JSONSchema7, JSONSchema7Array, JSONSchema7Type, - JSONSchema7TypeName + JSONSchema7TypeName, + JSONSchema6Definition, + JSONSchema7Definition } from 'json-schema' /* JSON Schema 4 */ @@ -80,6 +82,10 @@ import { } } +// Class +class Schema4 implements JSONSchema4 { +} + /* JSON Schema 6 */ // SimpleType @@ -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#', @@ -149,7 +155,11 @@ import { propertyNames: {}, format: 'date-time' } - const c: JSONSchema7 = false; + const c: JSONSchema6Definition = false; +} + +// Class +class Schema6 implements JSONSchema6 { } /* JSON Schema 7 */ @@ -168,10 +178,10 @@ import { const c: JSONSchema7Type = [1, 2] } -// JSONSchema7 +// JSONSchema7Definition () => { - const a: JSONSchema7 = {} - const b: JSONSchema7 = { + const a: JSONSchema7Definition = {} + const b: JSONSchema7Definition = { $id: 'foo', $ref: 'foo/bar', $schema: 'http://json-schema.org/schema#', @@ -229,5 +239,9 @@ import { if: {}, then: {} } - const c: JSONSchema7 = false; + const c: JSONSchema7Definition = false; +} + +// Class +class Schema7 implements JSONSchema7 { }