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 1.0.3 to 1.0.4

src/index.js.save

204

index.js

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

exports.create = create;
exports.safeCreate = safeCreate;
exports.register = register;

@@ -37,2 +38,3 @@ exports.meta = meta;

_this.name = 'DataError';
_this.path = path;

@@ -45,10 +47,28 @@ return _this;

var InitError = function (_Error2) {
_inherits(InitError, _Error2);
function InitError(value, errors) {
_classCallCheck(this, InitError);
var _this2 = _possibleConstructorReturn(this, Object.getPrototypeOf(InitError).call(this));
_this2.name = 'InitError';
_this2.value = value;
_this2.errors = errors;
return _this2;
}
return InitError;
}(Error);
var Property = function () {
function Property(config, value) {
function Property(config, value, parent, key, errors) {
_classCallCheck(this, Property);
this.config = Property.getConfig(config);
try {
this.setValue(defined(value) || this.config.default);
} catch (e) {}
if (defined(parent) && defined(key)) {
this.attach(parent, key);
}
this.setValue(defined(value) ? value : this.config.default, errors);
}

@@ -81,10 +101,24 @@

}, {
key: 'dataError',
value: function dataError(type, sink) {
var e = new DataError(type, this.getPath());
if (sink) {
sink.push(e);
} else {
throw e;
}
}
}, {
key: 'check',
value: function check(v) {
if (!defined(v) && this.config.required) throw new DataError('required', this.getPath());
if (this.config.validator && !this.config.validator(v)) throw new DataError('validator', this.getPath());
if (!defined(v) && this.config.required) {
this.dataError('required');
}
if (this.config.validator && !this.config.validator(v, this)) {
this.dataError('validator');
}
}
}, {
key: 'init',
value: function init(v) {
value: function init(v, errors) {
this.check(v);

@@ -114,4 +148,12 @@ }

key: 'setValue',
value: function setValue(v) {
this.init(v);
value: function setValue(v, errors) {
try {
this.init(v, errors);
} catch (e) {
if (errors) {
errors.push(e);
} else {
throw e;
}
}
return this.value = v;

@@ -160,3 +202,5 @@ }

}
if (!config) throw new Error('bad_config');
if (!config) {
throw new Error('bad_config');
}
return config;

@@ -166,3 +210,3 @@ }

key: 'create',
value: function create(typeOrConfig, value) {
value: function create(typeOrConfig, value, parent, key, errors) {
var factories = {

@@ -178,4 +222,6 @@ "string": StringProperty,

var config = Property.getConfig(typeOrConfig);
if (!factories[config.type]) throw new Error('bad_type');
return new factories[config.type](config, value);
if (!factories[config.type]) {
throw new Error('bad_type');
}
return new factories[config.type](config, value, parent, key, errors);
}

@@ -190,15 +236,15 @@ }]);

function StringProperty(config, value) {
function StringProperty(config, value, parent, key, errors) {
_classCallCheck(this, StringProperty);
var _this2 = _possibleConstructorReturn(this, Object.getPrototypeOf(StringProperty).call(this, config, value));
var _this3 = _possibleConstructorReturn(this, Object.getPrototypeOf(StringProperty).call(this, config, value, parent, key, errors));
if (defined(_this2.config.pattern)) {
if (typeof _this2.config.pattern === 'string') {
_this2.config.pattern = new RegExp(_this2.config.pattern);
} else if (!(_this2.config.patten instanceof RegExp)) {
if (defined(_this3.config.pattern)) {
if (typeof _this3.config.pattern === 'string') {
_this3.config.pattern = new RegExp(_this3.config.pattern);
} else if (!(_this3.config.patten instanceof RegExp)) {
throw new Error('bad_pattern');
}
}
return _this2;
return _this3;
}

@@ -209,8 +255,16 @@

value: function check(v) {
if (defined(v) && typeof v !== 'string') throw new DataError('type', this.getPath());
if (defined(v) && typeof v !== 'string') {
this.dataError('type');
}
_get(Object.getPrototypeOf(StringProperty.prototype), 'check', this).call(this, v);
if (defined(v)) {
if (defined(this.config.minLength) && v.length < +this.config.minLength) throw new DataError('minLength', this.getPath());
if (defined(this.config.maxLength) && v.length > +this.config.maxLength) throw new DataError('maxLength', this.getPath());
if (defined(this.config.pattern) && !this.config.pattern.test(v)) throw new DataError('pattern', this.getPath());
if (defined(this.config.minLength) && v.length < +this.config.minLength) {
this.dataError('minLength');
}
if (defined(this.config.maxLength) && v.length > +this.config.maxLength) {
this.dataError('maxLength');
}
if (defined(this.config.pattern) && !this.config.pattern.test(v)) {
this.dataError('pattern');
}
}

@@ -226,6 +280,6 @@ }

function NumberProperty(config, value) {
function NumberProperty(config, value, parent, key, errors) {
_classCallCheck(this, NumberProperty);
return _possibleConstructorReturn(this, Object.getPrototypeOf(NumberProperty).call(this, config, value));
return _possibleConstructorReturn(this, Object.getPrototypeOf(NumberProperty).call(this, config, value, parent, key, errors));
}

@@ -236,7 +290,13 @@

value: function check(v) {
if (defined(v) && typeof v !== 'number') throw new DataError('type', this.getPath());
if (defined(v) && typeof v !== 'number') {
this.dataError('type');
}
_get(Object.getPrototypeOf(NumberProperty.prototype), 'check', this).call(this, v);
if (defined(v)) {
if (defined(this.config.min) && !(v >= +this.config.min)) throw new DataError('min', this.getPath());
if (defined(this.config.max) && !(v <= +this.config.max)) throw new DataError('max', this.getPath());
if (defined(this.config.min) && !(v >= +this.config.min)) {
this.dataError('min');
}
if (defined(this.config.max) && !(v <= +this.config.max)) {
this.dataError('max');
}
}

@@ -252,6 +312,6 @@ }

function BooleanProperty(config, value) {
function BooleanProperty(config, value, parent, key, errors) {
_classCallCheck(this, BooleanProperty);
return _possibleConstructorReturn(this, Object.getPrototypeOf(BooleanProperty).call(this, config, value));
return _possibleConstructorReturn(this, Object.getPrototypeOf(BooleanProperty).call(this, config, value, parent, key, errors));
}

@@ -262,3 +322,5 @@

value: function check(v) {
if (defined(v) && typeof v !== 'boolean') throw new DataError('type', this.getPath());
if (defined(v) && typeof v !== 'boolean') {
this.dataError('type');
}
_get(Object.getPrototypeOf(BooleanProperty.prototype), 'check', this).call(this, v);

@@ -274,9 +336,9 @@ }

function ArrayProperty(config, value) {
function ArrayProperty(config, value, parent, key, errors) {
_classCallCheck(this, ArrayProperty);
var _this5 = _possibleConstructorReturn(this, Object.getPrototypeOf(ArrayProperty).call(this, config, value));
var _this6 = _possibleConstructorReturn(this, Object.getPrototypeOf(ArrayProperty).call(this, config, value, parent, key, errors));
_this5.config.value = Property.getConfig(_this5.config.value);
return _this5;
_this6.config.value = Property.getConfig(_this6.config.value);
return _this6;
}

@@ -286,10 +348,10 @@

key: 'init',
value: function init(v) {
var _this6 = this;
value: function init(v, errors) {
var _this7 = this;
this.check(v);
if (!defined(v) || v[__sym] === this) return;
v[__sym] = this;
for (var i = 0, p; i < v.length; i++) {
p = Property.create(this.config.value, v[i]);
if (p) p.attach(v, i);
p = Property.create(this.config.value, v[i], v, i, errors);
}

@@ -302,7 +364,5 @@ v.push = function () {

for (var i = 0; i < args.length; i++) {
p = Property.create(_this6.config.value, args[i]);
if (p) p.attach(v, v.length);
p = Property.create(_this7.config.value, args[i], v, v.length);
}
};
v[__sym] = this;
}

@@ -312,7 +372,13 @@ }, {

value: function check(v) {
if (defined(v) && (typeof v === 'undefined' ? 'undefined' : _typeof(v)) !== 'object' && !(v instanceof Array)) throw new DataError('type', this.getPath());
if (defined(v) && (typeof v === 'undefined' ? 'undefined' : _typeof(v)) !== 'object' && !(v instanceof Array)) {
this.dataError('type');
}
_get(Object.getPrototypeOf(ArrayProperty.prototype), 'check', this).call(this, v);
if (defined(v)) {
if (defined(this.config.minLength) && v.length < +this.config.minLength) throw new DataError('minLength', this.getPath());
if (defined(this.config.maxLength) && v.length > +this.config.maxLength) throw new DataError('maxLength', this.getPath());
if (defined(this.config.minLength) && v.length < +this.config.minLength) {
this.dataError('minLength');
}
if (defined(this.config.maxLength) && v.length > +this.config.maxLength) {
this.dataError('maxLength');
}
}

@@ -328,6 +394,6 @@ }

function DateProperty(config, value) {
function DateProperty(config, value, parent, key, errors) {
_classCallCheck(this, DateProperty);
return _possibleConstructorReturn(this, Object.getPrototypeOf(DateProperty).call(this, config, value));
return _possibleConstructorReturn(this, Object.getPrototypeOf(DateProperty).call(this, config, value, parent, key, errors));
}

@@ -350,9 +416,11 @@

function SelectProperty(config, value) {
function SelectProperty(config, value, parent, key, errors) {
_classCallCheck(this, SelectProperty);
var _this8 = _possibleConstructorReturn(this, Object.getPrototypeOf(SelectProperty).call(this, config, value));
var _this9 = _possibleConstructorReturn(this, Object.getPrototypeOf(SelectProperty).call(this, config, value, parent, key, errors));
if (!(_this8.config.value instanceof Array) || !_this8.config.value.length) throw new Error('bad_value');
return _this8;
if (!(_this9.config.value instanceof Array) || !_this9.config.value.length) {
throw new Error('bad_value');
}
return _this9;
}

@@ -364,3 +432,5 @@

_get(Object.getPrototypeOf(SelectProperty.prototype), 'check', this).call(this, v);
if (this.config.value.indexOf(v) === -1) throw new DataError('value', this.getPath());
if (this.config.value.indexOf(v) === -1) {
this.dataError('value');
}
}

@@ -375,6 +445,6 @@ }]);

function ObjectProperty(config, value) {
function ObjectProperty(config, value, parent, key, errors) {
_classCallCheck(this, ObjectProperty);
return _possibleConstructorReturn(this, Object.getPrototypeOf(ObjectProperty).call(this, config, value));
return _possibleConstructorReturn(this, Object.getPrototypeOf(ObjectProperty).call(this, config, value, parent, key, errors));
}

@@ -384,5 +454,6 @@

key: 'init',
value: function init(v) {
value: function init(v, errors) {
this.check(v);
if (!defined(v) || v[__sym] === this) return;
v[__sym] = this;
var k,

@@ -393,6 +464,4 @@ p,

sub[k] = Property.getConfig(sub[k]);
p = Property.create(sub[k], v[k]);
if (p) p.attach(v, k);
p = Property.create(sub[k], v[k], v, k, errors);
}
v[__sym] = this;
}

@@ -402,3 +471,5 @@ }, {

value: function check(v) {
if (defined(v) && (typeof v === 'undefined' ? 'undefined' : _typeof(v)) !== 'object') throw new DataError('type', this.getPath());
if (defined(v) && (typeof v === 'undefined' ? 'undefined' : _typeof(v)) !== 'object') {
this.dataError('type');
}
_get(Object.getPrototypeOf(ObjectProperty.prototype), 'check', this).call(this, v);

@@ -411,5 +482,14 @@ }

function create(typeOrConfig, value) {
return Property.create(typeOrConfig, value || {}).value;
function create(typeOrConfig, value, parent, key) {
return Property.create(typeOrConfig, value || {}, parent, key).value;
}
function safeCreate(typeOrConfig, value, parent, key) {
var errors = [];
var value = Property.create(typeOrConfig, value || {}, parent, key, errors).value;
if (errors.length) {
throw new InitError(value, errors);
} else {
return value;
}
}
function register(type, config) {

@@ -416,0 +496,0 @@ return Property.registerType(type, config);

{
"name": "jsonpolice",
"version": "1.0.3",
"version": "1.0.4",
"description": "Javascript constraints enforcer",
"main": "index.js",
"scripts": {
"prepublish": "babel --presets es2015 --source-maps --out-file index.js src/index.js "
"prepublish": "babel --presets es2015 --source-maps --out-file index.js src/index.js"
},

@@ -9,0 +9,0 @@ "repository": {

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

super(name);
this.name = 'DataError';
this.path = path;

@@ -17,8 +18,18 @@ }

class InitError extends Error {
constructor(value, errors) {
super();
this.name = 'InitError';
this.value = value;
this.errors = errors;
}
}
class Property {
constructor(config, value) {
constructor(config, value, parent, key, errors) {
this.config = Property.getConfig(config);
try {
this.setValue(defined(value) || this.config.default);
} catch(e) {}
if (defined(parent) && defined(key)) {
this.attach(parent, key);
}
this.setValue(defined(value) ? value : this.config.default, errors);
}

@@ -45,7 +56,19 @@ attach(parent, key) {

}
dataError(type, sink) {
var e = new DataError(type, this.getPath());
if (sink) {
sink.push(e);
} else {
throw e;
}
}
check(v) {
if (!defined(v) && this.config.required) throw new DataError('required', this.getPath());
if (this.config.validator && !this.config.validator(v)) throw new DataError('validator', this.getPath());
if (!defined(v) && this.config.required) {
this.dataError('required');
}
if (this.config.validator && !this.config.validator(v, this)) {
this.dataError('validator');
}
}
init(v) {
init(v, errors) {
this.check(v);

@@ -67,4 +90,12 @@ }

}
setValue(v) {
this.init(v);
setValue(v, errors) {
try {
this.init(v, errors);
} catch(e) {
if (errors) {
errors.push(e);
} else {
throw e;
}
}
return this.value = v;

@@ -105,6 +136,8 @@ }

}
if (!config) throw new Error('bad_config');
if (!config) {
throw new Error('bad_config');
}
return config;
}
static create(typeOrConfig, value) {
static create(typeOrConfig, value, parent, key, errors) {
var factories = {

@@ -120,4 +153,6 @@ "string": StringProperty,

var config = Property.getConfig(typeOrConfig);
if (!factories[config.type]) throw new Error('bad_type');
return new factories[config.type](config, value);
if (!factories[config.type]) {
throw new Error('bad_type');
}
return new factories[config.type](config, value, parent, key, errors);
}

@@ -127,4 +162,4 @@ }

class StringProperty extends Property {
constructor(config, value) {
super(config, value);
constructor(config, value, parent, key, errors) {
super(config, value, parent, key, errors);
if (defined(this.config.pattern)) {

@@ -139,8 +174,16 @@ if (typeof this.config.pattern === 'string') {

check(v) {
if (defined(v) && typeof v !== 'string') throw new DataError('type', this.getPath());
if (defined(v) && typeof v !== 'string') {
this.dataError('type');
}
super.check(v);
if (defined(v)) {
if (defined(this.config.minLength) && v.length < +this.config.minLength) throw new DataError('minLength', this.getPath());
if (defined(this.config.maxLength) && v.length > +this.config.maxLength) throw new DataError('maxLength', this.getPath());
if (defined(this.config.pattern) && !this.config.pattern.test(v)) throw new DataError('pattern', this.getPath());
if (defined(this.config.minLength) && v.length < +this.config.minLength) {
this.dataError('minLength');
}
if (defined(this.config.maxLength) && v.length > +this.config.maxLength) {
this.dataError('maxLength');
}
if (defined(this.config.pattern) && !this.config.pattern.test(v)) {
this.dataError('pattern');
}
}

@@ -151,11 +194,17 @@ }

class NumberProperty extends Property {
constructor(config, value) {
super(config, value);
constructor(config, value, parent, key, errors) {
super(config, value, parent, key, errors);
}
check(v) {
if (defined(v) && typeof v !== 'number') throw new DataError('type', this.getPath());
if (defined(v) && typeof v !== 'number') {
this.dataError('type');
}
super.check(v);
if (defined(v)) {
if (defined(this.config.min) && !(v >= +this.config.min)) throw new DataError('min', this.getPath());
if (defined(this.config.max) && !(v <= +this.config.max)) throw new DataError('max', this.getPath());
if (defined(this.config.min) && !(v >= +this.config.min)) {
this.dataError('min');
}
if (defined(this.config.max) && !(v <= +this.config.max)) {
this.dataError('max');
}
}

@@ -166,7 +215,9 @@ }

class BooleanProperty extends Property {
constructor(config, value) {
super(config, value);
constructor(config, value, parent, key, errors) {
super(config, value, parent, key, errors);
}
check(v) {
if (defined(v) && typeof v !== 'boolean') throw new DataError('type', this.getPath());
if (defined(v) && typeof v !== 'boolean') {
this.dataError('type');
}
super.check(v);

@@ -177,27 +228,31 @@ }

class ArrayProperty extends Property {
constructor(config, value) {
super(config, value);
constructor(config, value, parent, key, errors) {
super(config, value, parent, key, errors);
this.config.value = Property.getConfig(this.config.value);
}
init(v) {
init(v, errors) {
this.check(v);
if (!defined(v) || v[__sym] === this) return;
v[__sym] = this;
for (var i = 0, p ; i < v.length ; i++) {
p = Property.create(this.config.value, v[i]);
if (p) p.attach(v, i);
p = Property.create(this.config.value, v[i], v, i, errors);
}
v.push = (...args) => {
for (var i = 0 ; i < args.length ; i++) {
p = Property.create(this.config.value, args[i]);
if (p) p.attach(v, v.length);
p = Property.create(this.config.value, args[i], v, v.length);
}
}
v[__sym] = this;
}
check(v) {
if (defined(v) && typeof v !== 'object' && !(v instanceof Array)) throw new DataError('type', this.getPath());
if (defined(v) && typeof v !== 'object' && !(v instanceof Array)) {
this.dataError('type');
}
super.check(v);
if (defined(v)) {
if (defined(this.config.minLength) && v.length < +this.config.minLength) throw new DataError('minLength', this.getPath());
if (defined(this.config.maxLength) && v.length > +this.config.maxLength) throw new DataError('maxLength', this.getPath());
if (defined(this.config.minLength) && v.length < +this.config.minLength) {
this.dataError('minLength');
}
if (defined(this.config.maxLength) && v.length > +this.config.maxLength) {
this.dataError('maxLength');
}
}

@@ -208,4 +263,4 @@ }

class DateProperty extends Property {
constructor(config, value) {
super(config, value);
constructor(config, value, parent, key, errors) {
super(config, value, parent, key, errors);
}

@@ -220,9 +275,13 @@ setValue(v) {

class SelectProperty extends Property {
constructor(config, value) {
super(config, value);
if (!(this.config.value instanceof Array) || !this.config.value.length) throw new Error('bad_value');
constructor(config, value, parent, key, errors) {
super(config, value, parent, key, errors);
if (!(this.config.value instanceof Array) || !this.config.value.length) {
throw new Error('bad_value');
}
}
check(v) {
super.check(v);
if (this.config.value.indexOf(v) === -1) throw new DataError('value', this.getPath());
if (this.config.value.indexOf(v) === -1) {
this.dataError('value');
}
}

@@ -232,18 +291,19 @@ }

class ObjectProperty extends Property {
constructor(config, value) {
super(config, value);
constructor(config, value, parent, key, errors) {
super(config, value, parent, key, errors);
}
init(v) {
init(v, errors) {
this.check(v);
if (!defined(v) || v[__sym] === this) return;
v[__sym] = this;
var k, p, sub = this.config.value || {};
for (k in sub) {
sub[k] = Property.getConfig(sub[k]);
p = Property.create(sub[k], v[k]);
if (p) p.attach(v, k);
p = Property.create(sub[k], v[k], v, k, errors);
}
v[__sym] = this;
}
check(v) {
if (defined(v) && typeof v !== 'object') throw new DataError('type', this.getPath());
if (defined(v) && typeof v !== 'object') {
this.dataError('type');
}
super.check(v);

@@ -253,5 +313,14 @@ }

export function create(typeOrConfig, value) {
return Property.create(typeOrConfig, value || {}).value;
export function create(typeOrConfig, value, parent, key) {
return Property.create(typeOrConfig, value || {}, parent, key).value;
}
export function safeCreate(typeOrConfig, value, parent, key) {
var errors = [];
var value = Property.create(typeOrConfig, value || {}, parent, key, errors).value;
if (errors.length) {
throw new InitError(value, errors);
} else {
return value;
}
}
export function register(type, config) {

@@ -258,0 +327,0 @@ return Property.registerType(type, config);

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