jsonpolice
Advanced tools
Comparing version 3.5.1 to 4.0.0
import * as refs from 'jsonref'; | ||
export declare var __schema: symbol; | ||
export declare var regexps: { | ||
export declare const __schema: symbol; | ||
export declare const regexps: { | ||
[name: string]: RegExp; | ||
@@ -5,0 +5,0 @@ }; |
@@ -13,4 +13,1 @@ import { SchemaOptions } from './global'; | ||
export declare function flatten(dataOrUri: any, opts?: SchemaOptions): Promise<any>; | ||
export declare function getVersion(dataOrUri: any, opts?: SchemaOptions): Promise<any>; | ||
export declare function addVersion(dataOrUri: any, opts?: SchemaOptions): Promise<any>; | ||
export declare function fireValidationError(dataScope: string, schemaScope: string, type: string, info?: any): never; |
"use strict"; | ||
const _ = require('lodash'); | ||
const refs = require('jsonref'); | ||
const vers = require('./versions'); | ||
const global_1 = require('./global'); | ||
@@ -24,15 +22,6 @@ const schema_1 = require('./schema'); | ||
else { | ||
return vers.parseKnown().then(function (versions) { | ||
if (!opts.scope) | ||
opts.scope = (typeof dataOrUri === 'string' ? dataOrUri : '#'); | ||
if (!opts.store) | ||
opts.store = {}; | ||
_.defaults(opts.store, versions); | ||
return refs.parse(dataOrUri, opts).then(function (data) { | ||
return vers.get(data.$schema, opts).then(function (schemaVersion) { | ||
let _schemaVersion = schema_1.Schema.create(schemaVersion, _.defaults(opts, { scope: refs.scope(schemaVersion) })); | ||
_schemaVersion.validate(data); | ||
return schema_1.Schema.create(data, opts); | ||
}); | ||
}); | ||
if (!opts.store) | ||
opts.store = {}; | ||
return refs.parse(dataOrUri, opts).then(function (data) { | ||
return schema_1.Schema.create(data, Object.assign({ store: {} }, opts)); | ||
}); | ||
@@ -48,16 +37,2 @@ } | ||
exports.flatten = flatten; | ||
function getVersion(dataOrUri, opts = {}) { | ||
return vers.get(dataOrUri, opts); | ||
} | ||
exports.getVersion = getVersion; | ||
function addVersion(dataOrUri, opts = {}) { | ||
return vers.parseKnown().then(function () { | ||
return vers.add(dataOrUri, opts); | ||
}); | ||
} | ||
exports.addVersion = addVersion; | ||
function fireValidationError(dataScope, schemaScope, type, info) { | ||
throw new global_1.ValidationError(dataScope, schemaScope, type, info); | ||
} | ||
exports.fireValidationError = fireValidationError; | ||
//# sourceMappingURL=index.js.map |
@@ -20,7 +20,7 @@ "use strict"; | ||
_.each(this.data.items, (data, i) => { | ||
schema_1.Schema.create(this.data.items[i], _.defaults(this.opts, { scope: this.scope + '/items/' + i })); | ||
schema_1.Schema.create(this.data.items[i], Object.assign({}, this.opts, { scope: this.scope + '/items/' + i })); | ||
}); | ||
} | ||
else if (typeof this.data.items === 'object') { | ||
schema_1.Schema.create(this.data.items, _.defaults(this.opts, { scope: this.scope + '/items' })); | ||
schema_1.Schema.create(this.data.items, Object.assign({}, this.opts, { scope: this.scope + '/items' })); | ||
} | ||
@@ -33,3 +33,3 @@ else { | ||
if (typeof this.data.additionalItems === 'object') { | ||
schema_1.Schema.create(this.data.additionalItems, _.defaults(this.opts, { scope: this.scope + '/additionalItems' })); | ||
schema_1.Schema.create(this.data.additionalItems, Object.assign({}, this.opts, { scope: this.scope + '/additionalItems' })); | ||
} | ||
@@ -36,0 +36,0 @@ else if (typeof this.data.additionalItems !== 'boolean') { |
"use strict"; | ||
const _ = require('lodash'); | ||
const global_1 = require('./global'); | ||
@@ -40,3 +39,3 @@ const schema_1 = require('./schema'); | ||
for (i in this.data.properties) { | ||
schema_1.Schema.create(this.data.properties[i], _.defaults(this.opts, { scope: this.scope + '/properties/' + i })); | ||
schema_1.Schema.create(this.data.properties[i], Object.assign({}, this.opts, { scope: this.scope + '/properties/' + i })); | ||
} | ||
@@ -46,3 +45,3 @@ } | ||
for (i in this.data.patternProperties) { | ||
schema_1.Schema.create(this.data.patternProperties[i], _.defaults(this.opts, { scope: this.scope + '/patternProperties/' + i })); | ||
schema_1.Schema.create(this.data.patternProperties[i], Object.assign({}, this.opts, { scope: this.scope + '/patternProperties/' + i })); | ||
} | ||
@@ -52,3 +51,3 @@ } | ||
if (typeof this.data.additionalProperties === 'object') { | ||
schema_1.Schema.create(this.data.additionalProperties, _.defaults(this.opts, { scope: this.scope + '/additionalProperties' })); | ||
schema_1.Schema.create(this.data.additionalProperties, Object.assign({}, this.opts, { scope: this.scope + '/additionalProperties' })); | ||
} | ||
@@ -62,3 +61,3 @@ else if (typeof this.data.additionalProperties !== 'boolean') { | ||
if (typeof this.data.dependencies[i] === 'object' && !Array.isArray(this.data.dependencies[i])) { | ||
schema_1.Schema.create(this.data.dependencies[i], _.defaults(this.opts, { scope: this.scope + '/dependencies/' + i })); | ||
schema_1.Schema.create(this.data.dependencies[i], Object.assign({}, this.opts, { scope: this.scope + '/dependencies/' + i })); | ||
} | ||
@@ -143,6 +142,3 @@ } | ||
else if (typeof this.data.additionalProperties === 'object') { | ||
let s = this.data.additionalProperties[global_1.__schema]; | ||
if (s) { | ||
s.validate(data[k], path + '/' + k); | ||
} | ||
this.data.additionalProperties[global_1.__schema].validate(data[k], path + '/' + k); | ||
} | ||
@@ -149,0 +145,0 @@ } |
@@ -5,3 +5,2 @@ "use strict"; | ||
const global_1 = require('./global'); | ||
let __validating = Symbol(); | ||
function linkProperty(o, i, k) { | ||
@@ -61,36 +60,23 @@ o[k] = i[k]; | ||
validate(data, path = '') { | ||
if (this[__validating]) { | ||
return data; | ||
} | ||
else { | ||
this[__validating] = true; | ||
} | ||
data = this.default(data); | ||
if (typeof data !== 'undefined') { | ||
try { | ||
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); | ||
} | ||
if (global_1.enumerableAndDefined(this.data, 'type')) { | ||
data = this.validateType(data, path); | ||
} | ||
catch (e) { | ||
delete this[__validating]; | ||
throw e; | ||
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); | ||
} | ||
} | ||
delete this[__validating]; | ||
return data; | ||
@@ -101,3 +87,3 @@ } | ||
_.each(this.data.allOf, (data, i) => { | ||
Schema.create(this.data.allOf[i], Object.assign(this.opts, { scope: this.scope + '/allOf/' + i })); | ||
Schema.create(this.data.allOf[i], Object.assign({}, this.opts, { scope: this.scope + '/allOf/' + i })); | ||
}); | ||
@@ -107,3 +93,3 @@ } | ||
_.each(this.data.anyOf, (data, i) => { | ||
Schema.create(this.data.anyOf[i], Object.assign(this.opts, { scope: this.scope + '/anyOf/' + i })); | ||
Schema.create(this.data.anyOf[i], Object.assign({}, this.opts, { scope: this.scope + '/anyOf/' + i })); | ||
}); | ||
@@ -113,7 +99,7 @@ } | ||
_.each(this.data.oneOf, (data, i) => { | ||
Schema.create(this.data.oneOf[i], Object.assign(this.opts, { scope: this.scope + '/oneOf/' + 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' })); | ||
Schema.create(this.data.not, Object.assign({}, this.opts, { scope: this.scope + '/not' })); | ||
} | ||
@@ -120,0 +106,0 @@ } |
{ | ||
"name": "jsonpolice", | ||
"version": "3.5.1", | ||
"version": "4.0.0", | ||
"description": "JSON Schema parser and validator", | ||
@@ -62,5 +62,5 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"jsonref": "^3.4.0", | ||
"jsonref": "^3.5.0", | ||
"lodash": "^4.5.1" | ||
} | ||
} |
@@ -22,4 +22,3 @@ # jsonpolice | ||
Create a new instance of schema validator. The schema itself is validated against the version of the specification | ||
the schema uses, as specified with the `$schema` property (defaults to `http://json-schema.org/draft-04/schema#`). | ||
Create a new instance of schema validator. | ||
@@ -76,56 +75,2 @@ * `dataOrUri`, the schema to parse or a fully qualified URI to pass to `retriever` to download the schema | ||
## addVersion(dataOrUri _[, options]_) | ||
Register a new version of the specification that can be later used to validate schemas against. You can use this | ||
function to register custom extensions to the basic JSON Schema specification | ||
* `dataOrUri`, the data to parse or a fully qualified URI to pass to `retriever` to download the data | ||
* `options` (optional), parsing options, the following optional properties are supported: | ||
* `scope`, the current resolution scope (base href) of URLs and paths. | ||
* `retriever`, a function accepting a URL in input and returning a promise resolved to an object | ||
representing the data downloaded for the URI. Whenever a `$ref` to a new URI is found, if the URI is not | ||
already cached in the store in use, it'll be fetched using this `retriever`. If not `retriever` is passed | ||
and a URI needs to be downloaded, a `no_retriever` exception is thrown. | ||
* `removeAdditional`, see the description of the parameter with the same name in the `create` function above. | ||
The function returns a Promise resolving to the parsed data, with all `$ref` instances resolved. | ||
### Example | ||
```javascript | ||
var jsonpolice = require('jsonpolice'); | ||
jsonpolice.addVersion({ | ||
id: 'http://vivocha.com/sampleSwaggerParameter#', | ||
allOf: [ | ||
{ $ref: 'http://json-schema.org/draft-04/schema#' }, | ||
{ | ||
type: 'object', | ||
properties: { | ||
collectionFormat: { | ||
enum: [ 'csv', 'ssv', 'tsv', 'pipes' ], | ||
default: 'csv' | ||
} | ||
} | ||
} | ||
] | ||
}).then(function() { | ||
jsonpolice.create({ | ||
$schema: 'http://vivocha.com/sampleSwaggerParameter#', | ||
type: 'object', | ||
properties: { | ||
a: { | ||
type: 'array', | ||
collectionFormat: 'ssv', | ||
items: { | ||
type: 'integer' | ||
} | ||
} | ||
} | ||
}).then(function(schema) { | ||
console.log(schema.validate({ a: "5 10 15" })); | ||
}); | ||
}); | ||
``` | ||
## Schema.validate(data) | ||
@@ -132,0 +77,0 @@ |
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
81838
33
892
149
Updatedjsonref@^3.5.0