Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jsonpolice

Package Overview
Dependencies
Maintainers
1
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonpolice - npm Package Compare versions

Comparing version 3.0.3 to 3.1.0

227

lib/index.js

@@ -14,2 +14,3 @@ 'use strict';

exports.create = create;
exports.flatten = flatten;
exports.addVersion = addVersion;

@@ -71,2 +72,59 @@ exports.fireValidationError = fireValidationError;

}
function linkProperty(o, i, k) {
Object.defineProperty(o, k, {
get: function get() {
return i[k];
},
set: function set(v) {},
configurable: false,
enumerable: true
});
}
function pushProperty(o, i, k) {
Object.defineProperty(o, o.length, {
get: function get() {
return i[k];
},
set: function set(v) {},
configurable: false,
enumerable: true
});
}
function mergeProperties(o, i, k) {
if (o[i].allOf) {
pushProperty(o[i].allOf, i, k);
} else {
var a = [];
pushProperty(a, o, k);
pushProperty(a, i, k);
o[i] = { allOf: a };
}
}
function mergeObjects(o, i, k) {
if (!enumerableAndDefined(o, k)) {
o[k] = {};
}
for (var j in i[k]) {
if (enumerableAndDefined(o[k], j)) {
mergeProperties(o[k], i[k], j);
} else {
linkProperty(o[k], i[k], j);
}
}
}
function assignIfEnumerableAndDefined(o, i, k) {
if (enumerableAndDefined(i, k)) o[k] = i[k];
}
function assignIfEnumerableAndDefinedAndNotSet(o, i, k) {
if (enumerableAndDefined(i, k) && !enumerableAndDefined(o[k])) o[k] = i[k];
}
function assignIfEnumerableAndDefinedAndLessThan(o, i, k) {
if (enumerableAndDefined(i, k) && (!enumerableAndDefined(o[k]) || i[k] < o[k])) o[k] = i[k];
}
function assignIfEnumerableAndDefinedAndGreaterThan(o, i, k) {
if (enumerableAndDefined(i, k) && (!enumerableAndDefined(o[k]) || i[k] > o[k])) o[k] = i[k];
}
function assignIfEnumerableAndDefinedAndNot(o, i, k, v) {
if (enumerableAndDefined(i, k) && (!enumerableAndDefined(o[k]) || i[k] !== v)) o[k] = i[k];
}

@@ -134,7 +192,8 @@ var SchemaError = function (_Error) {

var Schema = function () {
function Schema(data, scope) {
function Schema(data, opts) {
_classCallCheck(this, Schema);
this.data = data;
this.scope = refs.scope(data) || data.id || scope || '#';
this.opts = opts;
this.scope = refs.scope(data) || data.id || opts.scope || '#';
}

@@ -295,3 +354,3 @@

key: 'create',
value: function create(data, scope) {
value: function create(data, opts) {
var schema;

@@ -310,28 +369,28 @@ if (defined(data)) {

});
schema = new Schema(_data, scope);
schema = new Schema(_data, opts);
} else {
switch (data.type) {
case 'array':
schema = new ArraySchema(data, scope);
schema = new ArraySchema(data, opts);
break;
case 'boolean':
schema = new BooleanSchema(data, scope);
schema = new BooleanSchema(data, opts);
break;
case 'integer':
schema = new IntegerSchema(data, scope);
schema = new IntegerSchema(data, opts);
break;
case 'number':
schema = new NumberSchema(data, scope);
schema = new NumberSchema(data, opts);
break;
case 'null':
schema = new NullSchema(data, scope);
schema = new NullSchema(data, opts);
break;
case 'object':
schema = new ObjectSchema(data, scope);
schema = new ObjectSchema(data, opts);
break;
case 'string':
schema = new StringSchema(data, scope);
schema = new StringSchema(data, opts);
break;
default:
throw new SchemaError(scope, 'type', data.type);
throw new SchemaError(opts.scope, 'type', data.type);
break;

@@ -341,3 +400,3 @@ }

} else {
schema = new Schema(data, scope);
schema = new Schema(data, opts);
}

@@ -349,5 +408,103 @@ data[__schema] = schema;

} else {
throw new SchemaError(scope, 'no_data');
throw new SchemaError(opts.scope, 'no_data');
}
}
}, {
key: 'flatten',
value: function flatten(data) {
if (!enumerableAndDefined(data, 'allOf')) {
return data;
} else {
var out = {};
// Init the data that must be taken from the outer schema
if (enumerableAndDefined(data, 'id')) out.id = data.id;
if (enumerableAndDefined(data, '$schema')) out.$schema = data.$schema;
return _lodash2.default.reduce(data.allOf.concat(data), function (o, i, k) {
if (i !== data) {
i = Schema.flatten(i);
}
assignIfEnumerableAndDefined(o, i, 'title');
assignIfEnumerableAndDefined(o, i, 'description');
assignIfEnumerableAndDefinedAndNotSet(o, i, 'default');
assignIfEnumerableAndDefined(o, i, 'multipleOf');
assignIfEnumerableAndDefinedAndLessThan(o, i, 'maximum');
assignIfEnumerableAndDefinedAndNot(o, i, 'exclusiveMaximum', true);
assignIfEnumerableAndDefinedAndGreaterThan(o, i, 'minimum');
assignIfEnumerableAndDefinedAndNot(o, i, 'exclusiveMinimum', true);
assignIfEnumerableAndDefinedAndLessThan(o, i, 'maxLength');
assignIfEnumerableAndDefinedAndGreaterThan(o, i, 'minLength');
assignIfEnumerableAndDefined(o, i, 'pattern');
if (enumerableAndDefined(i, 'additionalItems')) {
if (enumerableAndDefined(o, 'additionalItems')) {
if (_typeof(i.additionalItems) === 'object') {
if (_typeof(o.additionalItems) === 'object') {
mergeProperties(o, i, 'additionalItems');
} else if (o.additionalItems !== false) {
linkProperty(o, i, 'additionalItems');
}
} else if (i.additionalItems === false) {
o.additionalItems = false;
}
} else {
linkProperty(o, i, 'additionalItems');
}
}
if (enumerableAndDefined(i, 'items')) {
mergeProperties(o, i, 'items');
}
assignIfEnumerableAndDefinedAndLessThan(o, i, 'maxItems');
assignIfEnumerableAndDefinedAndGreaterThan(o, i, 'minItems');
assignIfEnumerableAndDefinedAndNot(o, i, 'uniqueItems', true);
assignIfEnumerableAndDefinedAndLessThan(o, i, 'maxProperties');
assignIfEnumerableAndDefinedAndGreaterThan(o, i, 'minProperties');
if (enumerableAndDefined(i, 'required')) {
o.required = _lodash2.default.uniq((o.required || []).concat(i.required));
}
if (enumerableAndDefined(i, 'additionalProperties')) {
if (enumerableAndDefined(o, 'additionalProperties')) {
if (_typeof(i.additionalProperties) === 'object') {
if (_typeof(o.additionalProperties) === 'object') {
mergeProperties(o, i, 'additionalProperties');
} else if (o.additionalProperties !== false) {
linkProperty(o, i, 'additionalProperties');
}
} else if (i.additionalProperties === false) {
o.additionalProperties = false;
}
} else {
linkProperty(o, i, 'additionalProperties');
}
}
if (enumerableAndDefined(i, 'definitions')) {
mergeObjects(o, i, 'definitions');
}
if (enumerableAndDefined(i, 'properties')) {
mergeObjects(o, i, 'properties');
}
if (enumerableAndDefined(i, 'patternProperties')) {
mergeObjects(o, i, 'patternProperties');
}
if (enumerableAndDefined(i, 'dependencies')) {
if (enumerableAndDefined(o, 'dependencies')) {
if (Array.isArray(o.dependencies) && Array.isArray(i.dependencies)) {
o.dependencies = _lodash2.default.uniq((o.dependencies || []).concat(i.dependencies));
} else if (_typeof(o.dependencies) === 'object' && _typeof(i.dependencies) === 'object') {
mergeProperties(o, i, 'dependencies');
} else {
linkProperty(o, i, 'dependencies');
}
} else {
linkProperty(o, i, 'dependencies');
}
}
assignIfEnumerableAndDefinedAndNotSet(o, i, 'enum');
assignIfEnumerableAndDefinedAndNotSet(o, i, 'type');
assignIfEnumerableAndDefined(o, i, 'anyOf');
assignIfEnumerableAndDefined(o, i, 'oneOf');
assignIfEnumerableAndDefined(o, i, 'not');
return o;
}, out);
}
}
}]);

@@ -361,6 +518,6 @@

function ArraySchema(data, scope) {
function ArraySchema(data, opts) {
_classCallCheck(this, ArraySchema);
return _possibleConstructorReturn(this, Object.getPrototypeOf(ArraySchema).call(this, data, scope));
return _possibleConstructorReturn(this, Object.getPrototypeOf(ArraySchema).call(this, data, opts));
}

@@ -437,6 +594,6 @@

function BooleanSchema(data, scope) {
function BooleanSchema(data, opts) {
_classCallCheck(this, BooleanSchema);
return _possibleConstructorReturn(this, Object.getPrototypeOf(BooleanSchema).call(this, data, scope));
return _possibleConstructorReturn(this, Object.getPrototypeOf(BooleanSchema).call(this, data, opts));
}

@@ -467,6 +624,6 @@

function NumberSchema(data, scope) {
function NumberSchema(data, opts) {
_classCallCheck(this, NumberSchema);
return _possibleConstructorReturn(this, Object.getPrototypeOf(NumberSchema).call(this, data, scope));
return _possibleConstructorReturn(this, Object.getPrototypeOf(NumberSchema).call(this, data, opts));
}

@@ -499,6 +656,6 @@

function IntegerSchema(data, scope) {
function IntegerSchema(data, opts) {
_classCallCheck(this, IntegerSchema);
return _possibleConstructorReturn(this, Object.getPrototypeOf(IntegerSchema).call(this, data, scope));
return _possibleConstructorReturn(this, Object.getPrototypeOf(IntegerSchema).call(this, data, opts));
}

@@ -523,6 +680,6 @@

function NullSchema(data, scope) {
function NullSchema(data, opts) {
_classCallCheck(this, NullSchema);
return _possibleConstructorReturn(this, Object.getPrototypeOf(NullSchema).call(this, data, scope));
return _possibleConstructorReturn(this, Object.getPrototypeOf(NullSchema).call(this, data, opts));
}

@@ -546,6 +703,6 @@

function ObjectSchema(data, scope) {
function ObjectSchema(data, opts) {
_classCallCheck(this, ObjectSchema);
return _possibleConstructorReturn(this, Object.getPrototypeOf(ObjectSchema).call(this, data, scope));
return _possibleConstructorReturn(this, Object.getPrototypeOf(ObjectSchema).call(this, data, opts));
}

@@ -649,3 +806,7 @@

if (this.data.additionalProperties === false) {
throw new ValidationError(path + '/' + k, this.scope, 'property');
if (this.opts.removeAdditional) {
delete data[k];
} else {
throw new ValidationError(path + '/' + k, this.scope, 'property');
}
} else if (_typeof(this.data.additionalProperties) === 'object') {

@@ -668,6 +829,6 @@ data[k] = this.data.additionalProperties[__schema].validate(data[k], path + '/' + k);

function StringSchema(data, scope) {
function StringSchema(data, opts) {
_classCallCheck(this, StringSchema);
return _possibleConstructorReturn(this, Object.getPrototypeOf(StringSchema).call(this, data, scope));
return _possibleConstructorReturn(this, Object.getPrototypeOf(StringSchema).call(this, data, opts));
}

@@ -714,2 +875,3 @@

var _opts = opts || {};
if (!_opts.scope) _opts.scope = typeof dataOrUri === 'string' ? dataOrUri : '#';
if (!_opts.store) _opts.store = {};

@@ -721,3 +883,3 @@ _lodash2.default.defaults(_opts.store, versions);

_schemaVersion.validate(data);
return Schema.create(data, _opts.scope || (typeof dataOrUri === 'string' ? dataOrUri : '#'));
return Schema.create(data, _opts);
});

@@ -728,2 +890,7 @@ });

}
function flatten(dataOrUri, opts) {
return create(dataOrUri, opts).then(function (schema) {
return Schema.flatten(schema.data);
});
}
function addVersion(dataOrUri, opts) {

@@ -730,0 +897,0 @@ return vers.parseKnown().then(function () {

2

package.json
{
"name": "jsonpolice",
"version": "3.0.3",
"version": "3.1.0",
"description": "JSON Schema parser and validator",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -38,2 +38,5 @@ # jsonpolice

with Node.js.
* `removeAdditional`, if `true` unknown properties are filtered out. Unknown properties are properties
not passing the validation of none of `properties`, `patternProperties` and `additionalProperties`) If
omitted or set to `false`, an unknown property triggers a ValidationError.

@@ -86,2 +89,3 @@ The function returns a Promise resolving to a new instance of Schema. Once created, a schema instance can be used

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.

@@ -88,0 +92,0 @@ The function returns a Promise resolving to the parsed data, with all `$ref` instances resolved.

@@ -36,2 +36,59 @@ import _ from 'lodash';

}
function linkProperty(o, i, k) {
Object.defineProperty(o, k, {
get: () => {
return i[k];
},
set: (v) => { },
configurable: false,
enumerable: true
});
}
function pushProperty(o, i, k) {
Object.defineProperty(o, o.length, {
get: () => {
return i[k];
},
set: (v) => { },
configurable: false,
enumerable: true
});
}
function mergeProperties(o, i, k) {
if (o[i].allOf) {
pushProperty(o[i].allOf, i, k);
} else {
var a = [];
pushProperty(a, o, k);
pushProperty(a, i, k);
o[i] = { allOf: a };
}
}
function mergeObjects(o, i, k) {
if (!enumerableAndDefined(o, k)) {
o[k] = {};
}
for (var j in i[k]) {
if (enumerableAndDefined(o[k], j)) {
mergeProperties(o[k], i[k], j);
} else {
linkProperty(o[k], i[k], j);
}
}
}
function assignIfEnumerableAndDefined(o, i, k) {
if (enumerableAndDefined(i, k)) o[k] = i[k];
}
function assignIfEnumerableAndDefinedAndNotSet(o, i, k) {
if (enumerableAndDefined(i, k) && !enumerableAndDefined(o[k])) o[k] = i[k];
}
function assignIfEnumerableAndDefinedAndLessThan(o, i, k) {
if (enumerableAndDefined(i, k) && (!enumerableAndDefined(o[k]) || i[k] < o[k])) o[k] = i[k];
}
function assignIfEnumerableAndDefinedAndGreaterThan(o, i, k) {
if (enumerableAndDefined(i, k) && (!enumerableAndDefined(o[k]) || i[k] > o[k])) o[k] = i[k];
}
function assignIfEnumerableAndDefinedAndNot(o, i, k, v) {
if (enumerableAndDefined(i, k) && (!enumerableAndDefined(o[k]) || i[k] !== v)) o[k] = i[k];
}

@@ -82,5 +139,6 @@ class SchemaError extends Error {

class Schema {
constructor(data, scope) {
constructor(data, opts) {
this.data = data;
this.scope = refs.scope(data) || data.id || scope || '#';
this.opts = opts;
this.scope = refs.scope(data) || data.id || opts.scope || '#';
}

@@ -218,3 +276,3 @@ init() {

}
static create(data, scope) {
static create(data, opts) {
var schema;

@@ -233,28 +291,28 @@ if (defined(data)) {

});
schema = new Schema(_data, scope);
schema = new Schema(_data, opts);
} else {
switch (data.type) {
case 'array':
schema = new ArraySchema(data, scope);
schema = new ArraySchema(data, opts);
break;
case 'boolean':
schema = new BooleanSchema(data, scope);
schema = new BooleanSchema(data, opts);
break;
case 'integer':
schema = new IntegerSchema(data, scope);
schema = new IntegerSchema(data, opts);
break;
case 'number':
schema = new NumberSchema(data, scope);
schema = new NumberSchema(data, opts);
break;
case 'null':
schema = new NullSchema(data, scope);
schema = new NullSchema(data, opts);
break;
case 'object':
schema = new ObjectSchema(data, scope);
schema = new ObjectSchema(data, opts);
break;
case 'string':
schema = new StringSchema(data, scope);
schema = new StringSchema(data, opts);
break;
default:
throw new SchemaError(scope, 'type', data.type);
throw new SchemaError(opts.scope, 'type', data.type);
break;

@@ -264,3 +322,3 @@ }

} else {
schema = new Schema(data, scope);
schema = new Schema(data, opts);
}

@@ -272,10 +330,106 @@ data[__schema] = schema;

} else {
throw new SchemaError(scope, 'no_data');
throw new SchemaError(opts.scope, 'no_data');
}
}
static flatten(data) {
if (!enumerableAndDefined(data, 'allOf')) {
return data;
} else {
var out = {};
// Init the data that must be taken from the outer schema
if (enumerableAndDefined(data, 'id')) out.id = data.id;
if (enumerableAndDefined(data, '$schema')) out.$schema = data.$schema;
return _.reduce(data.allOf.concat(data), function(o, i, k) {
if (i !== data) {
i = Schema.flatten(i);
}
assignIfEnumerableAndDefined(o, i, 'title');
assignIfEnumerableAndDefined(o, i, 'description');
assignIfEnumerableAndDefinedAndNotSet(o, i, 'default');
assignIfEnumerableAndDefined(o, i, 'multipleOf');
assignIfEnumerableAndDefinedAndLessThan(o, i, 'maximum');
assignIfEnumerableAndDefinedAndNot(o, i, 'exclusiveMaximum', true);
assignIfEnumerableAndDefinedAndGreaterThan(o, i, 'minimum');
assignIfEnumerableAndDefinedAndNot(o, i, 'exclusiveMinimum', true);
assignIfEnumerableAndDefinedAndLessThan(o, i, 'maxLength');
assignIfEnumerableAndDefinedAndGreaterThan(o, i, 'minLength');
assignIfEnumerableAndDefined(o, i, 'pattern');
if (enumerableAndDefined(i, 'additionalItems')) {
if (enumerableAndDefined(o, 'additionalItems')) {
if (typeof i.additionalItems === 'object') {
if (typeof o.additionalItems === 'object') {
mergeProperties(o, i, 'additionalItems');
} else if (o.additionalItems !== false) {
linkProperty(o, i, 'additionalItems');
}
} else if (i.additionalItems === false) {
o.additionalItems = false;
}
} else {
linkProperty(o, i, 'additionalItems');
}
}
if (enumerableAndDefined(i, 'items')) {
mergeProperties(o, i, 'items');
}
assignIfEnumerableAndDefinedAndLessThan(o, i, 'maxItems');
assignIfEnumerableAndDefinedAndGreaterThan(o, i, 'minItems');
assignIfEnumerableAndDefinedAndNot(o, i, 'uniqueItems', true);
assignIfEnumerableAndDefinedAndLessThan(o, i, 'maxProperties');
assignIfEnumerableAndDefinedAndGreaterThan(o, i, 'minProperties');
if (enumerableAndDefined(i, 'required')) {
o.required = _.uniq((o.required || []).concat(i.required));
}
if (enumerableAndDefined(i, 'additionalProperties')) {
if (enumerableAndDefined(o, 'additionalProperties')) {
if (typeof i.additionalProperties === 'object') {
if (typeof o.additionalProperties === 'object') {
mergeProperties(o, i, 'additionalProperties');
} else if (o.additionalProperties !== false) {
linkProperty(o, i, 'additionalProperties');
}
} else if (i.additionalProperties === false) {
o.additionalProperties = false;
}
} else {
linkProperty(o, i, 'additionalProperties');
}
}
if (enumerableAndDefined(i, 'definitions')) {
mergeObjects(o, i , 'definitions');
}
if (enumerableAndDefined(i, 'properties')) {
mergeObjects(o, i , 'properties');
}
if (enumerableAndDefined(i, 'patternProperties')) {
mergeObjects(o, i , 'patternProperties');
}
if (enumerableAndDefined(i, 'dependencies')) {
if (enumerableAndDefined(o, 'dependencies')) {
if (Array.isArray(o.dependencies) && Array.isArray(i.dependencies)) {
o.dependencies = _.uniq((o.dependencies || []).concat(i.dependencies));
} else if (typeof o.dependencies === 'object' && typeof i.dependencies === 'object') {
mergeProperties(o, i, 'dependencies');
} else {
linkProperty(o, i, 'dependencies');
}
} else {
linkProperty(o, i, 'dependencies');
}
}
assignIfEnumerableAndDefinedAndNotSet(o, i, 'enum');
assignIfEnumerableAndDefinedAndNotSet(o, i, 'type');
assignIfEnumerableAndDefined(o, i, 'anyOf');
assignIfEnumerableAndDefined(o, i, 'oneOf');
assignIfEnumerableAndDefined(o, i, 'not');
return o;
}, out);
}
}
}
class ArraySchema extends Schema {
constructor(data, scope) {
super(data, scope);
constructor(data, opts) {
super(data, opts);
}

@@ -340,4 +494,4 @@ init() {

class BooleanSchema extends Schema {
constructor(data, scope) {
super(data, scope);
constructor(data, opts) {
super(data, opts);
}

@@ -360,4 +514,4 @@ validateType(data, path) {

class NumberSchema extends Schema {
constructor(data, scope) {
super(data, scope);
constructor(data, opts) {
super(data, opts);
}

@@ -382,4 +536,4 @@ validateType(data, path) {

class IntegerSchema extends NumberSchema {
constructor(data, scope) {
super(data, scope);
constructor(data, opts) {
super(data, opts);
}

@@ -396,4 +550,4 @@ validateType(data, path) {

class NullSchema extends Schema {
constructor(data, scope) {
super(data, scope);
constructor(data, opts) {
super(data, opts);
}

@@ -409,4 +563,4 @@ validateType(data, path) {

class ObjectSchema extends Schema {
constructor(data, scope) {
super(data, scope);
constructor(data, opts) {
super(data, opts);
}

@@ -503,3 +657,7 @@ init() {

if (this.data.additionalProperties === false) {
throw new ValidationError(path + '/' + k, this.scope, 'property');
if (this.opts.removeAdditional) {
delete data[k];
} else {
throw new ValidationError(path + '/' + k, this.scope, 'property');
}
} else if (typeof this.data.additionalProperties === 'object') {

@@ -517,4 +675,4 @@ data[k] = this.data.additionalProperties[__schema].validate(data[k], path + '/' + k);

class StringSchema extends Schema {
constructor(data, scope) {
super(data, scope);
constructor(data, opts) {
super(data, opts);
}

@@ -555,2 +713,3 @@ validateType(data, path) {

var _opts = opts || {};
if (!_opts.scope) _opts.scope = (typeof dataOrUri === 'string' ? dataOrUri : '#');
if (!_opts.store) _opts.store = {};

@@ -562,3 +721,3 @@ _.defaults(_opts.store, versions);

_schemaVersion.validate(data);
return Schema.create(data, _opts.scope || (typeof dataOrUri === 'string' ? dataOrUri : '#'));
return Schema.create(data, _opts);
});

@@ -569,2 +728,7 @@ });

}
export function flatten(dataOrUri, opts) {
return create(dataOrUri, opts).then(function(schema) {
return Schema.flatten(schema.data);
});
}
export function addVersion(dataOrUri, opts) {

@@ -571,0 +735,0 @@ return vers.parseKnown().then(function() {

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc