Socket
Socket
Sign inDemoInstall

@orion-js/schema

Package Overview
Dependencies
Maintainers
2
Versions
110
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@orion-js/schema - npm Package Compare versions

Comparing version 0.0.18 to 0.0.21

112

lib/clean/index.js

@@ -47,20 +47,24 @@ 'use strict';

if (!((0, _isArray2.default)(type) && (0, _isArray2.default)(value))) {
_context.next = 17;
if (!((0, _isArray2.default)(type) && !(0, _isNil2.default)(value))) {
_context.next = 18;
break;
}
if (!(0, _isArray2.default)(value)) {
value = [value];
}
items = [];
i = 0;
case 4:
case 5:
if (!(i < value.length)) {
_context.next = 12;
_context.next = 13;
break;
}
_context.next = 7;
_context.next = 8;
return clean.apply(undefined, [type[0], type[0], value[i], info].concat(args));
case 7:
case 8:
newValue = _context.sent;

@@ -72,17 +76,17 @@

case 9:
case 10:
i++;
_context.next = 4;
_context.next = 5;
break;
case 12:
_context.next = 14;
case 13:
_context.next = 15;
return _cleanType2.default.apply(undefined, ['array', fieldSchema, items, info].concat(args));
case 14:
case 15:
return _context.abrupt('return', _context.sent);
case 17:
case 18:
if (!((0, _isPlainObject2.default)(type) && (0, _isPlainObject2.default)(value))) {
_context.next = 59;
_context.next = 66;
break;

@@ -98,8 +102,8 @@ }

_iteratorError = undefined;
_context.prev = 23;
_context.prev = 24;
_iterator = keys[Symbol.iterator]();
case 25:
case 26:
if (_iteratorNormalCompletion = (_step = _iterator.next()).done) {
_context.next = 40;
_context.next = 41;
break;

@@ -109,7 +113,7 @@ }

key = _step.value;
_context.prev = 27;
_context.next = 30;
_context.prev = 28;
_context.next = 31;
return clean.apply(undefined, [type[key].type, type[key], value[key], info].concat(args));
case 30:
case 31:
_newValue = _context.sent;

@@ -120,28 +124,28 @@

}
_context.next = 37;
_context.next = 38;
break;
case 34:
_context.prev = 34;
_context.t0 = _context['catch'](27);
case 35:
_context.prev = 35;
_context.t0 = _context['catch'](28);
throw new Error('Error cleaning field ' + key + ', error: ' + _context.t0.message);
case 37:
case 38:
_iteratorNormalCompletion = true;
_context.next = 25;
_context.next = 26;
break;
case 40:
_context.next = 46;
case 41:
_context.next = 47;
break;
case 42:
_context.prev = 42;
_context.t1 = _context['catch'](23);
case 43:
_context.prev = 43;
_context.t1 = _context['catch'](24);
_didIteratorError = true;
_iteratorError = _context.t1;
case 46:
_context.prev = 46;
case 47:
_context.prev = 47;
_context.prev = 48;

@@ -152,7 +156,7 @@ if (!_iteratorNormalCompletion && _iterator.return) {

case 49:
_context.prev = 49;
case 50:
_context.prev = 50;
if (!_didIteratorError) {
_context.next = 52;
_context.next = 53;
break;

@@ -163,23 +167,39 @@ }

case 52:
return _context.finish(49);
case 53:
return _context.finish(46);
return _context.finish(50);
case 54:
_context.next = 56;
return _context.finish(47);
case 55:
if (!(typeof type.__clean === 'function')) {
_context.next = 61;
break;
}
_context.next = 58;
return type.__clean.apply(type, [value, info].concat(args));
case 58:
return _context.abrupt('return', _context.sent);
case 61:
_context.next = 63;
return _cleanType2.default.apply(undefined, ['plainObject', fieldSchema, fields, info].concat(args));
case 56:
case 63:
return _context.abrupt('return', _context.sent);
case 59:
_context.next = 61;
case 64:
_context.next = 69;
break;
case 66:
_context.next = 68;
return _cleanType2.default.apply(undefined, [type, fieldSchema, value, info].concat(args));
case 61:
case 68:
return _context.abrupt('return', _context.sent);
case 62:
case 69:
case 'end':

@@ -189,3 +209,3 @@ return _context.stop();

}
}, _callee, this, [[23, 42, 46, 54], [27, 34], [47,, 49, 53]]);
}, _callee, this, [[24, 43, 47, 55], [28, 35], [48,, 50, 54]]);
}));

@@ -192,0 +212,0 @@

@@ -378,2 +378,117 @@ 'use strict';

}, _callee9, undefined);
})));
test('perform custom cleaning', _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee11() {
var person, schema, cleaned;
return regeneratorRuntime.wrap(function _callee11$(_context11) {
while (1) {
switch (_context11.prev = _context11.next) {
case 0:
person = {
name: {
type: String
},
__clean: function __clean(value) {
var _this = this;
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee10() {
return regeneratorRuntime.wrap(function _callee10$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
if (!(value.name === 'Joaquin')) {
_context10.next = 4;
break;
}
return _context10.abrupt('return', { name: 'Roberto' });
case 4:
return _context10.abrupt('return', value);
case 5:
case 'end':
return _context10.stop();
}
}
}, _callee10, _this);
}))();
}
};
schema = {
persons: {
type: [person]
}
};
_context11.next = 4;
return (0, _index2.default)(schema, {
persons: [{ name: 'Nicolás' }, { name: 'Joaquin' }]
});
case 4:
cleaned = _context11.sent;
expect(cleaned).toEqual({
persons: [{ name: 'Nicolás' }, { name: 'Roberto' }]
});
case 6:
case 'end':
return _context11.stop();
}
}
}, _callee11, undefined);
})));
test('perform non deep custom cleaning', _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee13() {
var schema, cleaned;
return regeneratorRuntime.wrap(function _callee13$(_context13) {
while (1) {
switch (_context13.prev = _context13.next) {
case 0:
schema = {
name: {
type: String
},
__clean: function __clean(value) {
var _this2 = this;
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee12() {
return regeneratorRuntime.wrap(function _callee12$(_context12) {
while (1) {
switch (_context12.prev = _context12.next) {
case 0:
if (!(value.name === 'Joaquin')) {
_context12.next = 4;
break;
}
return _context12.abrupt('return', { name: 'Roberto' });
case 4:
return _context12.abrupt('return', value);
case 5:
case 'end':
return _context12.stop();
}
}
}, _callee12, _this2);
}))();
}
};
_context13.next = 3;
return (0, _index2.default)(schema, { name: 'Joaquin' });
case 3:
cleaned = _context13.sent;
expect(cleaned).toEqual({ name: 'Roberto' });
case 5:
case 'end':
return _context13.stop();
}
}
}, _callee13, undefined);
})));

@@ -67,31 +67,39 @@ 'use strict';

_context2.next = 6;
return (0, _cleanKey2.default)(schema, 'car.brand', 'Nissan');
return (0, _cleanKey2.default)(schema, 'car.tags', { name: 12 });
case 6:
_context2.t1 = _context2.sent;
(0, _context2.t0)(_context2.t1).toBe('Nissan');
_context2.t2 = expect;
_context2.next = 11;
return (0, _cleanKey2.default)(schema, 'car.tags', 'Nice');
_context2.t2 = [{ name: '12' }];
(0, _context2.t0)(_context2.t1).toEqual(_context2.t2);
_context2.t3 = expect;
_context2.next = 12;
return (0, _cleanKey2.default)(schema, 'car.brand', 'Nissan');
case 11:
_context2.t3 = _context2.sent;
_context2.t4 = ['Nice'];
(0, _context2.t2)(_context2.t3).toEqual(_context2.t4);
case 12:
_context2.t4 = _context2.sent;
(0, _context2.t3)(_context2.t4).toBe('Nissan');
_context2.t5 = expect;
_context2.next = 17;
return (0, _cleanKey2.default)(schema, 'car.tags.$.name', 12);
return (0, _cleanKey2.default)(schema, 'car.tags', 'Nice');
case 17:
_context2.t6 = _context2.sent;
(0, _context2.t5)(_context2.t6).toBe('12');
_context2.t7 = expect;
_context2.next = 22;
_context2.t7 = ['Nice'];
(0, _context2.t5)(_context2.t6).toEqual(_context2.t7);
_context2.t8 = expect;
_context2.next = 23;
return (0, _cleanKey2.default)(schema, 'car.tags.$.name', 12);
case 23:
_context2.t9 = _context2.sent;
(0, _context2.t8)(_context2.t9).toBe('12');
_context2.t10 = expect;
_context2.next = 28;
return (0, _cleanKey2.default)(schema, 'car.tags.100.name', 12);
case 22:
_context2.t8 = _context2.sent;
(0, _context2.t7)(_context2.t8).toBe('12');
case 28:
_context2.t11 = _context2.sent;
(0, _context2.t10)(_context2.t11).toBe('12');
case 24:
case 30:
case 'end':

@@ -129,2 +137,42 @@ return _context2.stop();

}, _callee3, undefined);
})));
test('clean key with custom clean function', _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
var calls, schema;
return regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
calls = 0;
schema = {
services: {
type: {
password: { type: String },
__clean: function __clean(_ref5) {
var password = _ref5.password;
calls++;
console.log('running custom cleaning');
return { password: password.slice(0, 2) };
}
}
}
};
_context4.t0 = expect;
_context4.next = 5;
return (0, _cleanKey2.default)(schema, 'services', { password: '123456' });
case 5:
_context4.t1 = _context4.sent;
_context4.t2 = { password: '12' };
(0, _context4.t0)(_context4.t1).toEqual(_context4.t2);
expect(calls).toBe(1);
case 9:
case 'end':
return _context4.stop();
}
}
}, _callee4, undefined);
})));

@@ -7,2 +7,4 @@ 'use strict';

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _getError = require('./getError');

@@ -52,3 +54,3 @@

var error, schemaKeys, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, key, itemSchema, itemValue, keyItemKeys, documentKeys, notInSchemaKeys, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, _key, _keyItemKeys, _itemSchema, i, _itemValue, _keyItemKeys2;
var info, error, schemaKeys, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, key, itemSchema, itemValue, keyItemKeys, documentKeys, notInSchemaKeys, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, _key, _keyItemKeys, _itemSchema, i, _itemValue, _keyItemKeys2;

@@ -59,10 +61,11 @@ return regeneratorRuntime.wrap(function _callee$(_context) {

case 0:
_context.next = 2;
return (0, _getError2.default)({ schema: schema, doc: doc, value: value, currentSchema: currentSchema, keys: keys, options: options, args: args });
info = { schema: schema, doc: doc, value: value, currentSchema: currentSchema, keys: keys, options: options, args: args, addError: addError };
_context.next = 3;
return (0, _getError2.default)(info);
case 2:
case 3:
error = _context.sent;
if (!error) {
_context.next = 6;
_context.next = 7;
break;

@@ -74,5 +77,5 @@ }

case 6:
case 7:
if (!(0, _isNil2.default)(value)) {
_context.next = 8;
_context.next = 9;
break;

@@ -83,8 +86,25 @@ }

case 8:
case 9:
if (!(0, _isPlainObject2.default)(currentSchema.type)) {
_context.next = 63;
_context.next = 69;
break;
}
if (!(typeof currentSchema.type.__skipChildValidation === 'function')) {
_context.next = 15;
break;
}
_context.next = 13;
return currentSchema.type.__skipChildValidation(value, info);
case 13:
if (!_context.sent) {
_context.next = 15;
break;
}
return _context.abrupt('return');
case 15:
schemaKeys = Object.keys(currentSchema.type).filter(function (key) {

@@ -96,8 +116,8 @@ return !key.startsWith('__');

_iteratorError = undefined;
_context.prev = 13;
_context.prev = 19;
_iterator = schemaKeys[Symbol.iterator]();
case 15:
case 21:
if (_iteratorNormalCompletion = (_step = _iterator.next()).done) {
_context.next = 26;
_context.next = 32;
break;

@@ -112,32 +132,27 @@ }

keyItemKeys.push(key);
_context.next = 23;
return doValidation({
schema: schema,
doc: doc,
_context.next = 29;
return doValidation(_extends({}, info, {
value: itemValue,
currentSchema: itemSchema,
keys: keyItemKeys,
addError: addError,
options: options,
args: args
});
keys: keyItemKeys
}));
case 23:
case 29:
_iteratorNormalCompletion = true;
_context.next = 15;
_context.next = 21;
break;
case 26:
_context.next = 32;
case 32:
_context.next = 38;
break;
case 28:
_context.prev = 28;
_context.t0 = _context['catch'](13);
case 34:
_context.prev = 34;
_context.t0 = _context['catch'](19);
_didIteratorError = true;
_iteratorError = _context.t0;
case 32:
_context.prev = 32;
_context.prev = 33;
case 38:
_context.prev = 38;
_context.prev = 39;

@@ -148,7 +163,7 @@ if (!_iteratorNormalCompletion && _iterator.return) {

case 35:
_context.prev = 35;
case 41:
_context.prev = 41;
if (!_didIteratorError) {
_context.next = 38;
_context.next = 44;
break;

@@ -159,9 +174,9 @@ }

case 38:
return _context.finish(35);
case 44:
return _context.finish(41);
case 39:
return _context.finish(32);
case 45:
return _context.finish(38);
case 40:
case 46:
documentKeys = Object.keys(value);

@@ -172,3 +187,3 @@ notInSchemaKeys = (0, _difference2.default)(documentKeys, schemaKeys);

_iteratorError2 = undefined;
_context.prev = 45;
_context.prev = 51;

@@ -182,14 +197,14 @@ for (_iterator2 = notInSchemaKeys[Symbol.iterator](); !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {

}
_context.next = 53;
_context.next = 59;
break;
case 49:
_context.prev = 49;
_context.t1 = _context['catch'](45);
case 55:
_context.prev = 55;
_context.t1 = _context['catch'](51);
_didIteratorError2 = true;
_iteratorError2 = _context.t1;
case 53:
_context.prev = 53;
_context.prev = 54;
case 59:
_context.prev = 59;
_context.prev = 60;

@@ -200,7 +215,7 @@ if (!_iteratorNormalCompletion2 && _iterator2.return) {

case 56:
_context.prev = 56;
case 62:
_context.prev = 62;
if (!_didIteratorError2) {
_context.next = 59;
_context.next = 65;
break;

@@ -211,15 +226,15 @@ }

case 59:
return _context.finish(56);
case 65:
return _context.finish(62);
case 60:
return _context.finish(53);
case 66:
return _context.finish(59);
case 61:
_context.next = 75;
case 67:
_context.next = 81;
break;
case 63:
case 69:
if (!(0, _isArray2.default)(currentSchema.type)) {
_context.next = 75;
_context.next = 81;
break;

@@ -231,5 +246,5 @@ }

case 66:
case 72:
if (!(i < value.length)) {
_context.next = 75;
_context.next = 81;
break;

@@ -242,20 +257,15 @@ }

_keyItemKeys2.push(i);
_context.next = 72;
return doValidation({
schema: schema,
doc: doc,
_context.next = 78;
return doValidation(_extends({}, info, {
value: _itemValue,
currentSchema: { type: _itemSchema },
keys: _keyItemKeys2,
addError: addError,
options: options,
args: args
});
keys: _keyItemKeys2
}));
case 72:
case 78:
i++;
_context.next = 66;
_context.next = 72;
break;
case 75:
case 81:
case 'end':

@@ -265,3 +275,3 @@ return _context.stop();

}
}, _callee, this, [[13, 28, 32, 40], [33,, 35, 39], [45, 49, 53, 61], [54,, 56, 60]]);
}, _callee, this, [[19, 34, 38, 46], [39,, 41, 45], [51, 55, 59, 67], [60,, 62, 66]]);
}));

@@ -268,0 +278,0 @@

@@ -40,3 +40,5 @@ 'use strict';

args = _ref2$args === undefined ? [] : _ref2$args;
var info, validatorKey, validator, error, customError;
var info, validatorKey, validator, error, customError, _currentSchema$type, typeError;
return regeneratorRuntime.wrap(function _callee$(_context) {

@@ -99,5 +101,24 @@ while (1) {

case 20:
if (!currentSchema.type.__validate) {
_context.next = 26;
break;
}
_context.next = 23;
return (_currentSchema$type = currentSchema.type).__validate.apply(_currentSchema$type, [value, info].concat(_toConsumableArray(args)));
case 23:
typeError = _context.sent;
if (!typeError) {
_context.next = 26;
break;
}
return _context.abrupt('return', typeError);
case 26:
return _context.abrupt('return', null);
case 21:
case 27:
case 'end':

@@ -104,0 +125,0 @@ return _context.stop();

@@ -248,2 +248,132 @@ 'use strict';

}, _callee5, undefined);
})));
test('can validate object type with custom validation', _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7() {
var person, schema;
return regeneratorRuntime.wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
person = {
name: {
type: String
},
__validate: function __validate(value) {
var _this = this;
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() {
return regeneratorRuntime.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
return _context6.abrupt('return', value.name === 'Nicolás' ? null : 'no');
case 1:
case 'end':
return _context6.stop();
}
}
}, _callee6, _this);
}))();
}
};
schema = {
person: {
type: person
}
};
_context7.t0 = expect;
_context7.next = 5;
return (0, _index2.default)(schema, {
person: { name: 'Nicolás' }
});
case 5:
_context7.t1 = _context7.sent;
(0, _context7.t0)(_context7.t1).toBeNull();
_context7.t2 = expect;
_context7.next = 10;
return (0, _index2.default)(schema, {
person: { name: 'Joaquin' }
});
case 10:
_context7.t3 = _context7.sent;
_context7.t4 = {
person: 'no'
};
(0, _context7.t2)(_context7.t3).toEqual(_context7.t4);
case 13:
case 'end':
return _context7.stop();
}
}
}, _callee7, undefined);
})));
test('skip child validation if specified', _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee9() {
var person, schema, errors;
return regeneratorRuntime.wrap(function _callee9$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
person = {
firstName: {
type: String
},
lastName: {
type: String
},
__skipChildValidation: function __skipChildValidation(value) {
var _this2 = this;
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee8() {
return regeneratorRuntime.wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
return _context8.abrupt('return', value.firstName === 'Nicolás');
case 1:
case 'end':
return _context8.stop();
}
}
}, _callee8, _this2);
}))();
}
};
schema = {
persons: {
type: [person]
}
};
_context9.t0 = expect;
_context9.next = 5;
return (0, _index2.default)(schema, {
persons: [{ firstName: 'Nicolás' }]
});
case 5:
_context9.t1 = _context9.sent;
(0, _context9.t0)(_context9.t1).toBeNull();
_context9.next = 9;
return (0, _index2.default)(schema, {
persons: [{ firstName: 'Joaquin' }]
});
case 9:
errors = _context9.sent;
expect(errors).toEqual({
'persons.0.lastName': _Errors2.default.REQUIRED
});
case 11:
case 'end':
return _context9.stop();
}
}
}, _callee9, undefined);
})));
{
"name": "@orion-js/schema",
"version": "0.0.18",
"version": "0.0.21",
"main": "index.js",

@@ -16,3 +16,2 @@ "author": "nicolaslopezj",

"dependencies": {
"babel-polyfill": "^6.26.0",
"lodash": "^4.17.10"

@@ -19,0 +18,0 @@ },

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