Add missing functions to json-schema definition (#40924)

* Update index.d.ts

Add missing functions to definition

* Add missing validation functions to definition and tests.

* Update json-schema-tests.ts

Fix spaces

* * -> **

Co-authored-by: Daniel Rosenwasser <DanielRosenwasser@users.noreply.github.com>
This commit is contained in:
Paul van Brenk
2019-12-23 14:05:34 -06:00
committed by Andrew Branch
co-authored by Daniel Rosenwasser
parent ca9907198e
commit 68be545f66
2 changed files with 158 additions and 120 deletions
+31 -1
View File
@@ -522,7 +522,7 @@ export type JSONSchema7Type = JSONSchema7Array[] | boolean | number | null | obj
// Workaround for infinite type recursion
// https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
export interface JSONSchema7Array extends Array<JSONSchema7Type> {}
export interface JSONSchema7Array extends Array<JSONSchema7Type> { }
/**
* Meta schema
@@ -642,3 +642,33 @@ export interface JSONSchema7 {
writeOnly?: boolean;
examples?: JSONSchema7Type;
}
export interface ValidationResult {
valid: boolean;
errors: ValidationError[];
}
export interface ValidationError {
property: string;
message: string;
}
/**
* To use the validator call JSONSchema.validate with an instance object and an optional schema object.
* If a schema is provided, it will be used to validate. If the instance object refers to a schema (self-validating),
* that schema will be used to validate and the schema parameter is not necessary (if both exist,
* both validations will occur).
*/
export function validate(instance: {}, schema: JSONSchema4 | JSONSchema6 | JSONSchema7): ValidationResult;
/**
* The checkPropertyChange method will check to see if an value can legally be in property with the given schema
* This is slightly different than the validate method in that it will fail if the schema is readonly and it will
* not check for self-validation, it is assumed that the passed in value is already internally valid.
*/
export function checkPropertyChange(value: any, schema: JSONSchema4 | JSONSchema6 | JSONSchema7, property: string): ValidationResult;
/**
* This checks to ensure that the result is valid and will throw an appropriate error message if it is not.
*/
export function mustBeValid(result: ValidationResult): void;
+127 -119
View File
@@ -10,76 +10,79 @@ import {
JSONSchema7Type,
JSONSchema7TypeName,
JSONSchema6Definition,
JSONSchema7Definition
JSONSchema7Definition,
validate,
mustBeValid,
checkPropertyChange
} from 'json-schema'
/* JSON Schema 4 */
// SimpleType
() => {
const a: JSONSchema4TypeName = 'string'
const b: JSONSchema4TypeName = 'null'
const c: JSONSchema4TypeName = 'any'
const a: JSONSchema4TypeName = 'string'
const b: JSONSchema4TypeName = 'null'
const c: JSONSchema4TypeName = 'any'
}
// Type
() => {
const a: JSONSchema4Type = 'foo'
const b: JSONSchema4Type = null
const c: JSONSchema4Type = [1, 2]
const a: JSONSchema4Type = 'foo'
const b: JSONSchema4Type = null
const c: JSONSchema4Type = [1, 2]
}
// JSONSchema4
() => {
const a: JSONSchema4 = {}
const b: JSONSchema4 = {
id: 'foo',
$ref: 'foo/bar',
$schema: 'http://json-schema.org/schema#',
title: 'foo',
description: 'bar',
default: 42,
multipleOf: 3,
maximum: 4,
exclusiveMaximum: true,
minimum: 5,
exclusiveMinimum: false,
maxLength: 6,
minLength: 7,
pattern: 'baz',
additionalItems: true,
items: [
{ items: [{ minLength: 4 }] }
],
maxItems: 4,
minItems: 5,
uniqueItems: true,
maxProperties: 10,
minProperties: 11,
required: ['foo', 'bar'],
additionalProperties: false,
definitions: {
foo: { type: 'string' }
},
properties: {
bar: { type: 'boolean' }
},
patternProperties: {
foo: { type: 'integer' }
},
dependencies: {
baz: { type: 'integer' }
},
enum: ['foo', 42],
type: 'string',
allOf: [{}],
anyOf: [{}],
oneOf: [{}],
not: {},
extends: 'foo',
bar: 4,
format: 'date-time'
}
const a: JSONSchema4 = {}
const b: JSONSchema4 = {
id: 'foo',
$ref: 'foo/bar',
$schema: 'http://json-schema.org/schema#',
title: 'foo',
description: 'bar',
default: 42,
multipleOf: 3,
maximum: 4,
exclusiveMaximum: true,
minimum: 5,
exclusiveMinimum: false,
maxLength: 6,
minLength: 7,
pattern: 'baz',
additionalItems: true,
items: [
{ items: [{ minLength: 4 }] }
],
maxItems: 4,
minItems: 5,
uniqueItems: true,
maxProperties: 10,
minProperties: 11,
required: ['foo', 'bar'],
additionalProperties: false,
definitions: {
foo: { type: 'string' }
},
properties: {
bar: { type: 'boolean' }
},
patternProperties: {
foo: { type: 'integer' }
},
dependencies: {
baz: { type: 'integer' }
},
enum: ['foo', 42],
type: 'string',
allOf: [{}],
anyOf: [{}],
oneOf: [{}],
not: {},
extends: 'foo',
bar: 4,
format: 'date-time'
}
}
// Class
@@ -90,72 +93,72 @@ class Schema4 implements JSONSchema4 {
// SimpleType
() => {
const a: JSONSchema6TypeName = 'string'
const b: JSONSchema6TypeName = 'null'
const c: JSONSchema6TypeName = 'any'
const a: JSONSchema6TypeName = 'string'
const b: JSONSchema6TypeName = 'null'
const c: JSONSchema6TypeName = 'any'
}
// Type
() => {
const a: JSONSchema6Type = 'foo'
const b: JSONSchema6Type = null
const c: JSONSchema6Type = [1, 2]
const a: JSONSchema6Type = 'foo'
const b: JSONSchema6Type = null
const c: JSONSchema6Type = [1, 2]
}
// JSONSchema6Definition
() => {
const a: JSONSchema6Definition = {}
const b: JSONSchema6Definition = {
$id: 'foo',
$ref: 'foo/bar',
$schema: 'http://json-schema.org/schema#',
title: 'foo',
description: 'bar',
default: 42,
multipleOf: 3,
maximum: 4,
exclusiveMaximum: 4,
minimum: 5,
exclusiveMinimum: 5,
maxLength: 6,
minLength: 7,
pattern: 'baz',
additionalItems: true,
items: [
{ items: [{ minLength: 4 }] }
],
maxItems: 4,
minItems: 5,
uniqueItems: true,
maxProperties: 10,
minProperties: 11,
required: ['foo', 'bar'],
additionalProperties: false,
definitions: {
foo: { type: 'string' }
},
properties: {
bar: { type: 'boolean' }
},
patternProperties: {
foo: { type: 'integer' }
},
dependencies: {
baz: { type: 'integer' }
},
enum: ['foo', 42],
type: 'string',
allOf: [{}],
anyOf: [{}],
oneOf: [{}],
not: {},
const: 'foo',
contains: {},
examples: [{}],
propertyNames: {},
format: 'date-time'
}
const c: JSONSchema6Definition = false;
const a: JSONSchema6Definition = {}
const b: JSONSchema6Definition = {
$id: 'foo',
$ref: 'foo/bar',
$schema: 'http://json-schema.org/schema#',
title: 'foo',
description: 'bar',
default: 42,
multipleOf: 3,
maximum: 4,
exclusiveMaximum: 4,
minimum: 5,
exclusiveMinimum: 5,
maxLength: 6,
minLength: 7,
pattern: 'baz',
additionalItems: true,
items: [
{ items: [{ minLength: 4 }] }
],
maxItems: 4,
minItems: 5,
uniqueItems: true,
maxProperties: 10,
minProperties: 11,
required: ['foo', 'bar'],
additionalProperties: false,
definitions: {
foo: { type: 'string' }
},
properties: {
bar: { type: 'boolean' }
},
patternProperties: {
foo: { type: 'integer' }
},
dependencies: {
baz: { type: 'integer' }
},
enum: ['foo', 42],
type: 'string',
allOf: [{}],
anyOf: [{}],
oneOf: [{}],
not: {},
const: 'foo',
contains: {},
examples: [{}],
propertyNames: {},
format: 'date-time'
}
const c: JSONSchema6Definition = false;
}
// Class
@@ -199,7 +202,7 @@ class Schema6 implements JSONSchema6 {
pattern: 'baz',
additionalItems: {},
items: [
{items: [{minLength: 4}]}
{ items: [{ minLength: 4 }] }
],
maxItems: 4,
minItems: 5,
@@ -209,16 +212,16 @@ class Schema6 implements JSONSchema6 {
required: ['foo', 'bar'],
additionalProperties: {},
definitions: {
foo: {type: 'string'}
foo: { type: 'string' }
},
properties: {
bar: {type: 'boolean'}
bar: { type: 'boolean' }
},
patternProperties: {
foo: {type: 'integer'}
foo: { type: 'integer' }
},
dependencies: {
baz: {type: 'integer'}
baz: { type: 'integer' }
},
enum: ['foo', 42],
type: 'string',
@@ -245,3 +248,8 @@ class Schema6 implements JSONSchema6 {
// Class
class Schema7 implements JSONSchema7 {
}
let result = validate({}, { $id: 'schema-id' });
mustBeValid(result);
result = checkPropertyChange({ foo: 'bar' }, { $id: 'schema-id' }, 'foo');