jsonpolice
Advanced tools
Comparing version 4.1.0 to 5.0.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.__schema = Symbol(); | ||
@@ -3,0 +4,0 @@ exports.regexps = { |
"use strict"; | ||
const refs = require('jsonref'); | ||
const global_1 = require('./global'); | ||
const schema_1 = require('./schema'); | ||
require('./schema_array'); | ||
require('./schema_boolean'); | ||
require('./schema_integer'); | ||
require('./schema_number'); | ||
require('./schema_null'); | ||
require('./schema_object'); | ||
require('./schema_string'); | ||
var schema_2 = require('./schema'); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const refs = require("jsonref"); | ||
const global_1 = require("./global"); | ||
const schema_1 = require("./schema"); | ||
require("./schema_array"); | ||
require("./schema_boolean"); | ||
require("./schema_integer"); | ||
require("./schema_number"); | ||
require("./schema_null"); | ||
require("./schema_object"); | ||
require("./schema_string"); | ||
var schema_2 = require("./schema"); | ||
exports.Schema = schema_2.Schema; | ||
var global_2 = require('./global'); | ||
var global_2 = require("./global"); | ||
exports.SchemaError = global_2.SchemaError; | ||
@@ -16,0 +17,0 @@ exports.ValidationError = global_2.ValidationError; |
import { SchemaOptions } from './global'; | ||
import { Schema } from './schema'; | ||
export declare class ArraySchema extends Schema { | ||
import { UntypedSchema } from './schema'; | ||
export declare class ArraySchema extends UntypedSchema { | ||
constructor(data: any, opts: SchemaOptions); | ||
init(): void; | ||
validateType(data: any, path: string): any; | ||
validateType(data: any, path: string): Promise<any>; | ||
} |
"use strict"; | ||
const _ = require('lodash'); | ||
const global_1 = require('./global'); | ||
const schema_1 = require('./schema'); | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const _ = require("lodash"); | ||
const global_1 = require("./global"); | ||
const schema_1 = require("./schema"); | ||
let seps = { | ||
@@ -11,3 +20,3 @@ 'csv': ',', | ||
}; | ||
class ArraySchema extends schema_1.Schema { | ||
class ArraySchema extends schema_1.UntypedSchema { | ||
constructor(data, opts) { | ||
@@ -41,41 +50,43 @@ super(data, opts); | ||
validateType(data, path) { | ||
if (typeof data === 'string') { | ||
let sep = seps[this.data.collectionFormat || 'csv']; | ||
if (!sep) { | ||
throw new global_1.SchemaError(this.scope, 'collectionFormat', this.data.collectionFormat); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (typeof data === 'string') { | ||
let sep = seps[this.data.collectionFormat || 'csv']; | ||
if (!sep) { | ||
throw new global_1.SchemaError(this.scope, 'collectionFormat', this.data.collectionFormat); | ||
} | ||
data = data.split(sep); | ||
} | ||
data = data.split(sep); | ||
} | ||
if (!Array.isArray(data)) { | ||
throw new global_1.ValidationError(path, this.scope, 'type'); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'minItems') && data.length < this.data.minItems) { | ||
throw new global_1.ValidationError(path, this.scope, 'minItems'); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'maxItems') && data.length > this.data.maxItems) { | ||
throw new global_1.ValidationError(path, this.scope, 'maxItems'); | ||
} | ||
else if (this.data.uniqueItems === true && _.uniqWith(data, _.isEqual).length !== data.length) { | ||
throw new global_1.ValidationError(path, this.scope, 'uniqueItems'); | ||
} | ||
else if (data.length && (global_1.enumerableAndDefined(this.data, 'items') || global_1.enumerableAndDefined(this.data, 'additionalItems'))) { | ||
let itemsIsArray = Array.isArray(this.data.items); | ||
let itemsIsObject = !itemsIsArray && typeof this.data.items === 'object'; | ||
let addIsObject = typeof this.data.additionalItems === 'object'; | ||
for (let i = 0; i < data.length; i++) { | ||
if (itemsIsArray && i < this.data.items.length) { | ||
data[i] = this.data.items[i][global_1.__schema].validate(data[i], path + '/' + i); | ||
if (!Array.isArray(data)) { | ||
throw new global_1.ValidationError(path, this.scope, 'type'); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'minItems') && data.length < this.data.minItems) { | ||
throw new global_1.ValidationError(path, this.scope, 'minItems'); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'maxItems') && data.length > this.data.maxItems) { | ||
throw new global_1.ValidationError(path, this.scope, 'maxItems'); | ||
} | ||
else if (this.data.uniqueItems === true && _.uniqWith(data, _.isEqual).length !== data.length) { | ||
throw new global_1.ValidationError(path, this.scope, 'uniqueItems'); | ||
} | ||
else if (data.length && (global_1.enumerableAndDefined(this.data, 'items') || global_1.enumerableAndDefined(this.data, 'additionalItems'))) { | ||
let itemsIsArray = Array.isArray(this.data.items); | ||
let itemsIsObject = !itemsIsArray && typeof this.data.items === 'object'; | ||
let addIsObject = typeof this.data.additionalItems === 'object'; | ||
for (let i = 0; i < data.length; i++) { | ||
if (itemsIsArray && i < this.data.items.length) { | ||
data[i] = yield this.data.items[i][global_1.__schema].validate(data[i], path + '/' + i); | ||
} | ||
else if (itemsIsObject) { | ||
data[i] = yield this.data.items[global_1.__schema].validate(data[i], path + '/' + i); | ||
} | ||
else if (addIsObject) { | ||
data[i] = yield this.data.additionalItems[global_1.__schema].validate(data[i], path + '/' + i); | ||
} | ||
else if (this.data.additionalItems === false) { | ||
throw new global_1.ValidationError(path + '/' + i, this.scope, 'additionalItems'); | ||
} | ||
} | ||
else if (itemsIsObject) { | ||
data[i] = this.data.items[global_1.__schema].validate(data[i], path + '/' + i); | ||
} | ||
else if (addIsObject) { | ||
data[i] = this.data.additionalItems[global_1.__schema].validate(data[i], path + '/' + i); | ||
} | ||
else if (this.data.additionalItems === false) { | ||
throw new global_1.ValidationError(path + '/' + i, this.scope, 'additionalItems'); | ||
} | ||
} | ||
} | ||
return data; | ||
return data; | ||
}); | ||
} | ||
@@ -82,0 +93,0 @@ } |
import { SchemaOptions } from './global'; | ||
import { Schema } from './schema'; | ||
export declare class BooleanSchema extends Schema { | ||
import { UntypedSchema } from './schema'; | ||
export declare class BooleanSchema extends UntypedSchema { | ||
constructor(data: any, opts: SchemaOptions); | ||
validateType(data: any, path: string): any; | ||
validateType(data: any, path: string): Promise<any>; | ||
} |
"use strict"; | ||
const global_1 = require('./global'); | ||
const schema_1 = require('./schema'); | ||
class BooleanSchema extends schema_1.Schema { | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const global_1 = require("./global"); | ||
const schema_1 = require("./schema"); | ||
class BooleanSchema extends schema_1.UntypedSchema { | ||
constructor(data, opts) { | ||
@@ -9,14 +18,16 @@ super(data, opts); | ||
validateType(data, path) { | ||
if (typeof data === 'string') { | ||
if (data === 'true' || data === '1') { | ||
data = true; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (typeof data === 'string') { | ||
if (data === 'true' || data === '1') { | ||
data = true; | ||
} | ||
else if (data === 'false' || data === '0') { | ||
data = false; | ||
} | ||
} | ||
else if (data === 'false' || data === '0') { | ||
data = false; | ||
if (typeof data !== 'boolean') { | ||
throw new global_1.ValidationError(path, this.scope, 'type'); | ||
} | ||
} | ||
if (typeof data !== 'boolean') { | ||
throw new global_1.ValidationError(path, this.scope, 'type'); | ||
} | ||
return data; | ||
return data; | ||
}); | ||
} | ||
@@ -23,0 +34,0 @@ } |
@@ -5,3 +5,3 @@ import { SchemaOptions } from './global'; | ||
constructor(data: any, opts: SchemaOptions); | ||
validateType(data: any, path: string): any; | ||
validateType(data: any, path: string): Promise<any>; | ||
} |
"use strict"; | ||
const global_1 = require('./global'); | ||
const schema_1 = require('./schema'); | ||
const schema_number_1 = require('./schema_number'); | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const global_1 = require("./global"); | ||
const schema_1 = require("./schema"); | ||
const schema_number_1 = require("./schema_number"); | ||
class IntegerSchema extends schema_number_1.NumberSchema { | ||
@@ -10,7 +19,10 @@ constructor(data, opts) { | ||
validateType(data, path) { | ||
data = super.validateType(data, path); | ||
if (parseInt(data) !== data) { | ||
throw new global_1.ValidationError(path, this.scope, 'type'); | ||
} | ||
return data; | ||
const _super = name => super[name]; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
data = yield _super("validateType").call(this, data, path); | ||
if (parseInt(data) !== data) { | ||
throw new global_1.ValidationError(path, this.scope, 'type'); | ||
} | ||
return data; | ||
}); | ||
} | ||
@@ -17,0 +29,0 @@ } |
import { SchemaOptions } from './global'; | ||
import { Schema } from './schema'; | ||
export declare class NullSchema extends Schema { | ||
import { UntypedSchema } from './schema'; | ||
export declare class NullSchema extends UntypedSchema { | ||
constructor(data: any, opts: SchemaOptions); | ||
validateType(data: any, path: string): any; | ||
validateType(data: any, path: string): Promise<any>; | ||
} |
"use strict"; | ||
const global_1 = require('./global'); | ||
const schema_1 = require('./schema'); | ||
class NullSchema extends schema_1.Schema { | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const global_1 = require("./global"); | ||
const schema_1 = require("./schema"); | ||
class NullSchema extends schema_1.UntypedSchema { | ||
constructor(data, opts) { | ||
@@ -9,6 +18,8 @@ super(data, opts); | ||
validateType(data, path) { | ||
if (data !== null) { | ||
throw new global_1.ValidationError(path, this.scope, 'type'); | ||
} | ||
return data; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (data !== null) { | ||
throw new global_1.ValidationError(path, this.scope, 'type'); | ||
} | ||
return data; | ||
}); | ||
} | ||
@@ -15,0 +26,0 @@ } |
import { SchemaOptions } from './global'; | ||
import { Schema } from './schema'; | ||
export declare class NumberSchema extends Schema { | ||
import { UntypedSchema } from './schema'; | ||
export declare class NumberSchema extends UntypedSchema { | ||
constructor(data: any, opts: SchemaOptions); | ||
validateType(data: any, path: string): any; | ||
validateType(data: any, path: string): Promise<any>; | ||
} |
"use strict"; | ||
const global_1 = require('./global'); | ||
const schema_1 = require('./schema'); | ||
class NumberSchema extends schema_1.Schema { | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const global_1 = require("./global"); | ||
const schema_1 = require("./schema"); | ||
class NumberSchema extends schema_1.UntypedSchema { | ||
constructor(data, opts) { | ||
@@ -9,18 +18,20 @@ super(data, opts); | ||
validateType(data, path) { | ||
if (typeof data === 'string') { | ||
data = +data; | ||
} | ||
if (typeof data !== 'number' || isNaN(data)) { | ||
throw new global_1.ValidationError(path, this.scope, 'type'); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'multipleOf') && (data % this.data.multipleOf) !== 0) { | ||
throw new global_1.ValidationError(path, this.scope, 'multipleOf'); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'maximum') && (data > this.data.maximum || (this.data.exclusiveMaximum === true && data === this.data.maximum))) { | ||
throw new global_1.ValidationError(path, this.scope, 'maximum'); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'minimum') && (data < this.data.minimum || (this.data.exclusiveMinimum === true && data === this.data.minimum))) { | ||
throw new global_1.ValidationError(path, this.scope, 'minimum'); | ||
} | ||
return data; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (typeof data === 'string') { | ||
data = +data; | ||
} | ||
if (typeof data !== 'number' || isNaN(data)) { | ||
throw new global_1.ValidationError(path, this.scope, 'type'); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'multipleOf') && (data % this.data.multipleOf) !== 0) { | ||
throw new global_1.ValidationError(path, this.scope, 'multipleOf'); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'maximum') && (data > this.data.maximum || (this.data.exclusiveMaximum === true && data === this.data.maximum))) { | ||
throw new global_1.ValidationError(path, this.scope, 'maximum'); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'minimum') && (data < this.data.minimum || (this.data.exclusiveMinimum === true && data === this.data.minimum))) { | ||
throw new global_1.ValidationError(path, this.scope, 'minimum'); | ||
} | ||
return data; | ||
}); | ||
} | ||
@@ -27,0 +38,0 @@ } |
import { SchemaOptions } from './global'; | ||
import { Schema } from './schema'; | ||
export declare class ObjectSchema extends Schema { | ||
import { UntypedSchema } from './schema'; | ||
export declare class ObjectSchema extends UntypedSchema { | ||
constructor(data: any, opts: SchemaOptions); | ||
init(): void; | ||
default(data: any): any; | ||
validateType(data: any, path: string): any; | ||
validateType(data: any, path: string): Promise<any>; | ||
} |
"use strict"; | ||
const global_1 = require('./global'); | ||
const schema_1 = require('./schema'); | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const global_1 = require("./global"); | ||
const schema_1 = require("./schema"); | ||
let __salmon = Symbol(); | ||
@@ -30,3 +39,3 @@ function createDefaultProperty(schema, obj, key) { | ||
} | ||
class ObjectSchema extends schema_1.Schema { | ||
class ObjectSchema extends schema_1.UntypedSchema { | ||
constructor(data, opts) { | ||
@@ -78,70 +87,72 @@ super(data, opts); | ||
validateType(data, path) { | ||
if (data === null || typeof data !== 'object' || Array.isArray(data)) { | ||
throw new global_1.ValidationError(path, this.scope, 'type'); | ||
} | ||
let props = Object.keys(data); | ||
if (global_1.enumerableAndDefined(this.data, 'maxProperties') && props.length > this.data.maxProperties) { | ||
throw new global_1.ValidationError(path, this.scope, 'maxProperties'); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'minProperties') && props.length < this.data.minProperties) { | ||
throw new global_1.ValidationError(path, this.scope, 'minProperties'); | ||
} | ||
else { | ||
let i, j, k, found; | ||
if (global_1.enumerableAndDefined(this.data, 'required')) { | ||
for (i = 0; i < this.data.required.length; i++) { | ||
k = this.data.required[i]; | ||
if (!global_1.enumerableAndDefined(data, k)) { | ||
throw new global_1.ValidationError(path + '/' + k, this.scope, 'required'); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (data === null || typeof data !== 'object' || Array.isArray(data)) { | ||
throw new global_1.ValidationError(path, this.scope, 'type'); | ||
} | ||
let props = Object.keys(data); | ||
if (global_1.enumerableAndDefined(this.data, 'maxProperties') && props.length > this.data.maxProperties) { | ||
throw new global_1.ValidationError(path, this.scope, 'maxProperties'); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'minProperties') && props.length < this.data.minProperties) { | ||
throw new global_1.ValidationError(path, this.scope, 'minProperties'); | ||
} | ||
else { | ||
let i, j, k, found; | ||
if (global_1.enumerableAndDefined(this.data, 'required')) { | ||
for (i = 0; i < this.data.required.length; i++) { | ||
k = this.data.required[i]; | ||
if (!global_1.enumerableAndDefined(data, k)) { | ||
throw new global_1.ValidationError(path + '/' + k, this.scope, 'required'); | ||
} | ||
} | ||
} | ||
} | ||
if (global_1.defined(data) && global_1.enumerableAndDefined(this.data, 'dependencies')) { | ||
for (k in this.data.dependencies) { | ||
if (global_1.enumerableAndDefined(data, k)) { | ||
if (Array.isArray(this.data.dependencies[k])) { | ||
for (j = 0; j < this.data.dependencies[k].length; j++) { | ||
if (!global_1.enumerableAndDefined(data, this.data.dependencies[k][j])) { | ||
throw new global_1.ValidationError(path + '/' + k, this.scope, 'dependencies', this.data.dependencies[k][j]); | ||
if (global_1.defined(data) && global_1.enumerableAndDefined(this.data, 'dependencies')) { | ||
for (k in this.data.dependencies) { | ||
if (global_1.enumerableAndDefined(data, k)) { | ||
if (Array.isArray(this.data.dependencies[k])) { | ||
for (j = 0; j < this.data.dependencies[k].length; j++) { | ||
if (!global_1.enumerableAndDefined(data, this.data.dependencies[k][j])) { | ||
throw new global_1.ValidationError(path + '/' + k, this.scope, 'dependencies', this.data.dependencies[k][j]); | ||
} | ||
} | ||
} | ||
else { | ||
data[k] = yield this.data.dependencies[k][global_1.__schema].validate(data[k], path + '/' + k); | ||
} | ||
} | ||
else { | ||
data[k] = this.data.dependencies[k][global_1.__schema].validate(data[k], path + '/' + k); | ||
} | ||
} | ||
} | ||
} | ||
if (global_1.enumerableAndDefined(this.data, 'properties') || global_1.enumerableAndDefined(this.data, 'additionalProperties') || global_1.enumerableAndDefined(this.data, 'patternProperties')) { | ||
while (k = props.shift()) { | ||
found = false; | ||
if (global_1.enumerableAndDefined(this.data, 'properties') && this.data.properties[k]) { | ||
found = true; | ||
data[k] = this.data.properties[k][global_1.__schema].validate(data[k], path + '/' + k); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'patternProperties')) { | ||
for (i in this.data.patternProperties) { | ||
if (global_1.testRegExp(i, k)) { | ||
found = true; | ||
data[k] = this.data.patternProperties[i][global_1.__schema].validate(data[k], path + '/' + k); | ||
if (global_1.enumerableAndDefined(this.data, 'properties') || global_1.enumerableAndDefined(this.data, 'additionalProperties') || global_1.enumerableAndDefined(this.data, 'patternProperties')) { | ||
while (k = props.shift()) { | ||
found = false; | ||
if (global_1.enumerableAndDefined(this.data, 'properties') && this.data.properties[k]) { | ||
found = true; | ||
data[k] = yield this.data.properties[k][global_1.__schema].validate(data[k], path + '/' + k); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'patternProperties')) { | ||
for (i in this.data.patternProperties) { | ||
if (global_1.testRegExp(i, k)) { | ||
found = true; | ||
data[k] = yield this.data.patternProperties[i][global_1.__schema].validate(data[k], path + '/' + k); | ||
} | ||
} | ||
} | ||
} | ||
if (!found) { | ||
if (this.data.additionalProperties === false) { | ||
if (this.opts.removeAdditional) { | ||
delete data[k]; | ||
if (!found) { | ||
if (this.data.additionalProperties === false) { | ||
if (this.opts.removeAdditional) { | ||
delete data[k]; | ||
} | ||
else { | ||
throw new global_1.ValidationError(path + '/' + k, this.scope, 'property'); | ||
} | ||
} | ||
else { | ||
throw new global_1.ValidationError(path + '/' + k, this.scope, 'property'); | ||
else if (typeof this.data.additionalProperties === 'object') { | ||
yield this.data.additionalProperties[global_1.__schema].validate(data[k], path + '/' + k); | ||
} | ||
} | ||
else if (typeof this.data.additionalProperties === 'object') { | ||
this.data.additionalProperties[global_1.__schema].validate(data[k], path + '/' + k); | ||
} | ||
} | ||
} | ||
return data; | ||
} | ||
return data; | ||
} | ||
}); | ||
} | ||
@@ -148,0 +159,0 @@ } |
import { SchemaOptions } from './global'; | ||
import { Schema } from './schema'; | ||
export declare class StringSchema extends Schema { | ||
import { UntypedSchema } from './schema'; | ||
export declare class StringSchema extends UntypedSchema { | ||
constructor(data: any, opts: SchemaOptions); | ||
validateType(data: any, path: string): any; | ||
validateType(data: any, path: string): Promise<any>; | ||
} |
"use strict"; | ||
const global_1 = require('./global'); | ||
const schema_1 = require('./schema'); | ||
class StringSchema extends schema_1.Schema { | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const global_1 = require("./global"); | ||
const schema_1 = require("./schema"); | ||
class StringSchema extends schema_1.UntypedSchema { | ||
constructor(data, opts) { | ||
@@ -9,33 +18,35 @@ super(data, opts); | ||
validateType(data, path) { | ||
if (this.data.format === 'date-time') { | ||
if (!(data instanceof Date)) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this.data.format === 'date-time') { | ||
if (!(data instanceof Date)) { | ||
if (typeof data !== 'string') { | ||
throw new global_1.ValidationError(path, this.scope, 'type'); | ||
} | ||
else if (!global_1.testRegExp(this.data.format, data)) { | ||
throw new global_1.ValidationError(path, this.scope, 'format'); | ||
} | ||
else { | ||
data = new Date(data); | ||
} | ||
} | ||
} | ||
else { | ||
if (typeof data !== 'string') { | ||
throw new global_1.ValidationError(path, this.scope, 'type'); | ||
} | ||
else if (!global_1.testRegExp(this.data.format, data)) { | ||
else if (global_1.enumerableAndDefined(this.data, 'minLength') && data.length < this.data.minLength) { | ||
throw new global_1.ValidationError(path, this.scope, 'minLength'); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'maxLength') && data.length > this.data.maxLength) { | ||
throw new global_1.ValidationError(path, this.scope, 'maxLength'); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'pattern') && !global_1.testRegExp(this.data.pattern, data)) { | ||
throw new global_1.ValidationError(path, this.scope, 'pattern'); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'format') && global_1.defined(global_1.regexps[this.data.format]) && !global_1.testRegExp(this.data.format, data)) { | ||
throw new global_1.ValidationError(path, this.scope, 'format'); | ||
} | ||
else { | ||
data = new Date(data); | ||
} | ||
} | ||
} | ||
else { | ||
if (typeof data !== 'string') { | ||
throw new global_1.ValidationError(path, this.scope, 'type'); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'minLength') && data.length < this.data.minLength) { | ||
throw new global_1.ValidationError(path, this.scope, 'minLength'); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'maxLength') && data.length > this.data.maxLength) { | ||
throw new global_1.ValidationError(path, this.scope, 'maxLength'); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'pattern') && !global_1.testRegExp(this.data.pattern, data)) { | ||
throw new global_1.ValidationError(path, this.scope, 'pattern'); | ||
} | ||
else if (global_1.enumerableAndDefined(this.data, 'format') && global_1.defined(global_1.regexps[this.data.format]) && !global_1.testRegExp(this.data.format, data)) { | ||
throw new global_1.ValidationError(path, this.scope, 'format'); | ||
} | ||
} | ||
return data; | ||
return data; | ||
}); | ||
} | ||
@@ -42,0 +53,0 @@ } |
@@ -5,16 +5,7 @@ import { SchemaOptions } from './global'; | ||
} | ||
export declare class Schema { | ||
protected data: any; | ||
export declare abstract class Schema { | ||
readonly scope: string; | ||
protected opts: SchemaOptions; | ||
readonly scope: string; | ||
constructor(data: any, opts: SchemaOptions); | ||
validate(data: any, path?: string): any; | ||
protected init(): void; | ||
protected default(data: any): any; | ||
protected validateType(data: any, path: string): any; | ||
protected validateEnum(data: any, path: string): any; | ||
protected validateAllOf(data: any, path: string): any; | ||
protected validateAnyOf(data: any, path: string): any; | ||
protected validateOneOf(data: any, path: string): any; | ||
protected validateNot(data: any, path: string): any; | ||
constructor(scope: string, opts: SchemaOptions); | ||
abstract validate(data: any, path: string): Promise<any>; | ||
static factories: { | ||
@@ -27,1 +18,15 @@ [type: string]: SchemaFactory; | ||
} | ||
export declare class UntypedSchema extends Schema { | ||
protected data: any; | ||
protected opts: SchemaOptions; | ||
constructor(data: any, opts: SchemaOptions); | ||
protected init(): void; | ||
protected default(data: any): any; | ||
validate(data: any, path?: string): Promise<any>; | ||
protected validateType(data: any, path: string): Promise<any>; | ||
protected validateEnum(data: any, path: string): Promise<any>; | ||
protected validateAllOf(data: any, path: string): Promise<any>; | ||
protected validateAnyOf(data: any, path: string): Promise<any>; | ||
protected validateOneOf(data: any, path: string): Promise<any>; | ||
protected validateNot(data: any, path: string): Promise<any>; | ||
} |
"use strict"; | ||
const _ = require('lodash'); | ||
const refs = require('jsonref'); | ||
const global_1 = require('./global'); | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const _ = require("lodash"); | ||
const refs = require("jsonref"); | ||
const global_1 = require("./global"); | ||
function linkProperty(o, i, k) { | ||
@@ -53,132 +62,6 @@ o[k] = i[k]; | ||
class Schema { | ||
constructor(data, opts) { | ||
this.data = data; | ||
constructor(scope, opts) { | ||
this.scope = scope; | ||
this.opts = opts; | ||
this.scope = refs.scope(data) || data.id || opts.scope || '#'; | ||
} | ||
validate(data, path = '') { | ||
data = this.default(data); | ||
if (typeof data !== 'undefined') { | ||
if (global_1.enumerableAndDefined(this.data, 'type')) { | ||
data = this.validateType(data, path); | ||
} | ||
if (global_1.enumerableAndDefined(this.data, 'enum')) { | ||
data = this.validateEnum(data, path); | ||
} | ||
if (global_1.enumerableAndDefined(this.data, 'allOf')) { | ||
data = this.validateAllOf(data, path); | ||
} | ||
if (global_1.enumerableAndDefined(this.data, 'anyOf')) { | ||
data = this.validateAnyOf(data, path); | ||
} | ||
if (global_1.enumerableAndDefined(this.data, 'oneOf')) { | ||
data = this.validateOneOf(data, path); | ||
} | ||
if (global_1.enumerableAndDefined(this.data, 'not')) { | ||
data = this.validateNot(data, path); | ||
} | ||
} | ||
return data; | ||
} | ||
init() { | ||
if (global_1.enumerableAndDefined(this.data, 'allOf')) { | ||
_.each(this.data.allOf, (data, i) => { | ||
Schema.create(this.data.allOf[i], Object.assign({}, this.opts, { scope: this.scope + '/allOf/' + i })); | ||
}); | ||
} | ||
if (global_1.enumerableAndDefined(this.data, 'anyOf')) { | ||
_.each(this.data.anyOf, (data, i) => { | ||
Schema.create(this.data.anyOf[i], Object.assign({}, this.opts, { scope: this.scope + '/anyOf/' + i })); | ||
}); | ||
} | ||
if (global_1.enumerableAndDefined(this.data, 'oneOf')) { | ||
_.each(this.data.oneOf, (data, i) => { | ||
Schema.create(this.data.oneOf[i], Object.assign({}, this.opts, { scope: this.scope + '/oneOf/' + i })); | ||
}); | ||
} | ||
if (global_1.enumerableAndDefined(this.data, 'not')) { | ||
Schema.create(this.data.not, Object.assign({}, this.opts, { scope: this.scope + '/not' })); | ||
} | ||
} | ||
default(data) { | ||
let def; | ||
if (global_1.defined(data)) { | ||
def = data; | ||
} | ||
if (!global_1.defined(def) && global_1.enumerableAndDefined(this.data, 'allOf')) { | ||
for (let i = 0; !def && i < this.data.allOf.length; i++) { | ||
def = this.data.allOf[i][global_1.__schema].default(); | ||
} | ||
} | ||
if (!global_1.defined(def) && global_1.enumerableAndDefined(this.data, 'anyOf')) { | ||
for (let i = 0; !def && i < this.data.anyOf.length; i++) { | ||
def = this.data.anyOf[i][global_1.__schema].default(); | ||
} | ||
} | ||
if (!global_1.defined(def) && global_1.enumerableAndDefined(this.data, 'oneOf')) { | ||
for (let i = 0; !def && i < this.data.oneOf.length; i++) { | ||
def = this.data.oneOf[i][global_1.__schema].default(); | ||
} | ||
} | ||
if (!global_1.defined(def) && global_1.enumerableAndDefined(this.data, 'default')) { | ||
def = _.cloneDeep(this.data.default); | ||
} | ||
return def; | ||
} | ||
validateType(data, path) { | ||
throw new global_1.SchemaError(this.scope, 'type', data.type); | ||
} | ||
validateEnum(data, path) { | ||
let found = false; | ||
for (let i = 0; !found && i < this.data.enum.length; i++) { | ||
found = _.isEqual(data, this.data.enum[i]); | ||
} | ||
if (!found) { | ||
throw new global_1.ValidationError(path, this.scope, 'enum'); | ||
} | ||
return data; | ||
} | ||
validateAllOf(data, path) { | ||
for (let i = 0; i < this.data.allOf.length; i++) { | ||
data = this.data.allOf[i][global_1.__schema].validate(data, path); | ||
} | ||
return data; | ||
} | ||
validateAnyOf(data, path) { | ||
let found = false, _data; | ||
for (let i = 0; !found && i < this.data.anyOf.length; i++) { | ||
try { | ||
_data = this.data.anyOf[i][global_1.__schema].validate(data, path); | ||
found = true; | ||
} | ||
catch (e) { } | ||
} | ||
if (!found) { | ||
throw new global_1.ValidationError(path, this.scope, 'anyOf'); | ||
} | ||
return _data; | ||
} | ||
validateOneOf(data, path) { | ||
let count = 0, _data; | ||
for (let i = 0; count < 2 && i < this.data.oneOf.length; i++) { | ||
try { | ||
_data = this.data.oneOf[i][global_1.__schema].validate(data, path); | ||
count++; | ||
} | ||
catch (e) { } | ||
} | ||
if (count !== 1) { | ||
throw new global_1.ValidationError(path, this.scope, 'oneOf'); | ||
} | ||
return _data; | ||
} | ||
validateNot(data, path) { | ||
try { | ||
this.data.not[global_1.__schema].validate(data, path); | ||
} | ||
catch (e) { | ||
return data; | ||
} | ||
throw new global_1.ValidationError(path, this.scope, 'not'); | ||
} | ||
static registerFactory(type, factory) { | ||
@@ -202,3 +85,3 @@ Schema.factories[type] = factory; | ||
}); | ||
schema = new Schema(_data, opts); | ||
schema = new UntypedSchema(_data, opts); | ||
} | ||
@@ -213,3 +96,3 @@ else if (Schema.factories[data.type]) { | ||
else { | ||
schema = new Schema(data, opts); | ||
schema = new UntypedSchema(data, opts); | ||
} | ||
@@ -351,2 +234,147 @@ data[global_1.__schema] = schema; | ||
exports.Schema = Schema; | ||
class UntypedSchema extends Schema { | ||
constructor(data, opts) { | ||
super(refs.scope(data) || data.id || opts.scope || '#', opts); | ||
this.data = data; | ||
this.opts = opts; | ||
} | ||
init() { | ||
if (global_1.enumerableAndDefined(this.data, 'allOf')) { | ||
_.each(this.data.allOf, (data, i) => { | ||
Schema.create(this.data.allOf[i], Object.assign({}, this.opts, { scope: this.scope + '/allOf/' + i })); | ||
}); | ||
} | ||
if (global_1.enumerableAndDefined(this.data, 'anyOf')) { | ||
_.each(this.data.anyOf, (data, i) => { | ||
Schema.create(this.data.anyOf[i], Object.assign({}, this.opts, { scope: this.scope + '/anyOf/' + i })); | ||
}); | ||
} | ||
if (global_1.enumerableAndDefined(this.data, 'oneOf')) { | ||
_.each(this.data.oneOf, (data, i) => { | ||
Schema.create(this.data.oneOf[i], Object.assign({}, this.opts, { scope: this.scope + '/oneOf/' + i })); | ||
}); | ||
} | ||
if (global_1.enumerableAndDefined(this.data, 'not')) { | ||
Schema.create(this.data.not, Object.assign({}, this.opts, { scope: this.scope + '/not' })); | ||
} | ||
} | ||
default(data) { | ||
let def; | ||
if (global_1.defined(data)) { | ||
def = data; | ||
} | ||
if (!global_1.defined(def) && global_1.enumerableAndDefined(this.data, 'allOf')) { | ||
for (let i = 0; !def && i < this.data.allOf.length; i++) { | ||
def = this.data.allOf[i][global_1.__schema].default(); | ||
} | ||
} | ||
if (!global_1.defined(def) && global_1.enumerableAndDefined(this.data, 'anyOf')) { | ||
for (let i = 0; !def && i < this.data.anyOf.length; i++) { | ||
def = this.data.anyOf[i][global_1.__schema].default(); | ||
} | ||
} | ||
if (!global_1.defined(def) && global_1.enumerableAndDefined(this.data, 'oneOf')) { | ||
for (let i = 0; !def && i < this.data.oneOf.length; i++) { | ||
def = this.data.oneOf[i][global_1.__schema].default(); | ||
} | ||
} | ||
if (!global_1.defined(def) && global_1.enumerableAndDefined(this.data, 'default')) { | ||
def = _.cloneDeep(this.data.default); | ||
} | ||
return def; | ||
} | ||
validate(data, path = '') { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
data = this.default(data); | ||
if (global_1.enumerableAndDefined(this.data, 'type')) { | ||
data = yield this.validateType(data, path); | ||
} | ||
if (global_1.enumerableAndDefined(this.data, 'enum')) { | ||
data = yield this.validateEnum(data, path); | ||
} | ||
if (global_1.enumerableAndDefined(this.data, 'allOf')) { | ||
data = yield this.validateAllOf(data, path); | ||
} | ||
if (global_1.enumerableAndDefined(this.data, 'anyOf')) { | ||
data = yield this.validateAnyOf(data, path); | ||
} | ||
if (global_1.enumerableAndDefined(this.data, 'oneOf')) { | ||
data = yield this.validateOneOf(data, path); | ||
} | ||
if (global_1.enumerableAndDefined(this.data, 'not')) { | ||
data = yield this.validateNot(data, path); | ||
} | ||
return data; | ||
}); | ||
} | ||
validateType(data, path) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
throw new global_1.SchemaError(this.scope, 'type', data.type); | ||
}); | ||
} | ||
validateEnum(data, path) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let found = false; | ||
for (let i = 0; !found && i < this.data.enum.length; i++) { | ||
found = _.isEqual(data, this.data.enum[i]); | ||
} | ||
if (!found) { | ||
throw new global_1.ValidationError(path, this.scope, 'enum'); | ||
} | ||
return data; | ||
}); | ||
} | ||
validateAllOf(data, path) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
for (let i = 0; i < this.data.allOf.length; i++) { | ||
data = yield this.data.allOf[i][global_1.__schema].validate(data, path); | ||
} | ||
return data; | ||
}); | ||
} | ||
validateAnyOf(data, path) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let found = false, _data; | ||
for (let i = 0; !found && i < this.data.anyOf.length; i++) { | ||
try { | ||
_data = yield this.data.anyOf[i][global_1.__schema].validate(data, path); | ||
found = true; | ||
} | ||
catch (e) { } | ||
} | ||
if (!found) { | ||
throw new global_1.ValidationError(path, this.scope, 'anyOf'); | ||
} | ||
return _data; | ||
}); | ||
} | ||
validateOneOf(data, path) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let count = 0, _data; | ||
for (let i = 0; count < 2 && i < this.data.oneOf.length; i++) { | ||
try { | ||
_data = yield this.data.oneOf[i][global_1.__schema].validate(data, path); | ||
count++; | ||
} | ||
catch (e) { } | ||
} | ||
if (count !== 1) { | ||
throw new global_1.ValidationError(path, this.scope, 'oneOf'); | ||
} | ||
return _data; | ||
}); | ||
} | ||
validateNot(data, path) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
yield this.data.not[global_1.__schema].validate(data, path); | ||
} | ||
catch (e) { | ||
return data; | ||
} | ||
throw new global_1.ValidationError(path, this.scope, 'not'); | ||
}); | ||
} | ||
} | ||
exports.UntypedSchema = UntypedSchema; | ||
//# sourceMappingURL=schema.js.map |
{ | ||
"name": "jsonpolice", | ||
"version": "4.1.0", | ||
"version": "5.0.0", | ||
"description": "JSON Schema parser and validator", | ||
@@ -8,4 +8,6 @@ "main": "dist/index.js", | ||
"scripts": { | ||
"clean": "rimraf dist coverage .nyc_output node_modules", | ||
"prebuild": "rimraf dist", | ||
"build": "tsc", | ||
"precommit": "npm run build && npm run cover && npm run check-coverage", | ||
"commit": "git-cz", | ||
@@ -40,15 +42,15 @@ "check-coverage": "nyc check-coverage --statements 100 --branches 100 --functions 100 --lines 100", | ||
"devDependencies": { | ||
"@types/lodash": "4.14.37", | ||
"@types/lodash": "4.14.62", | ||
"chai": "3.5.0", | ||
"chai-as-promised": "5.3.0", | ||
"chai-as-promised": "6.0.0", | ||
"chai-spies": "0.7.1", | ||
"codecov.io": "0.1.6", | ||
"commitizen": "2.8.6", | ||
"cz-conventional-changelog": "1.2.0", | ||
"ghooks": "1.3.2", | ||
"mocha": "3.0.2", | ||
"nyc": "8.3.0", | ||
"rimraf": "2.5.4", | ||
"semantic-release": "^4.3.5", | ||
"typescript": "2.0.3" | ||
"commitizen": "2.9.6", | ||
"coveralls": "2.13.0", | ||
"cz-conventional-changelog": "2.0.0", | ||
"husky": "0.13.3", | ||
"mocha": "3.2.0", | ||
"nyc": "10.2.0", | ||
"rimraf": "2.6.1", | ||
"semantic-release": "^6.3.2", | ||
"typescript": "2.2.2" | ||
}, | ||
@@ -58,11 +60,8 @@ "config": { | ||
"path": "node_modules/cz-conventional-changelog" | ||
}, | ||
"ghooks": { | ||
"pre-commit": "npm run build && npm run cover && npm run check-coverage" | ||
} | ||
}, | ||
"dependencies": { | ||
"jsonref": "^3.5.0", | ||
"lodash": "^4.5.1" | ||
"jsonref": "^3.5.2", | ||
"lodash": "^4.17.4" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
90037
1009
Updatedjsonref@^3.5.2
Updatedlodash@^4.17.4