@orion-js/schema
Advanced tools
Comparing version 1.7.2 to 2.0.0-beta.0
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; | ||
@@ -12,156 +14,61 @@ var _getFieldType = _interopRequireDefault(require("../getValidationErrors/getError/getFieldType")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
async function _default(type, fieldSchema, value, info, ...args) { | ||
info.type = fieldSchema.type; | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
if (!info.type) { | ||
throw new Error('Cleaning field with no type'); | ||
} | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
const { | ||
clean: rootFieldClean | ||
} = await (0, _getFieldType.default)(type); | ||
function _default(_x, _x2, _x3, _x4) { | ||
return _ref.apply(this, arguments); | ||
} | ||
if (rootFieldClean && !(0, _isNil.default)(value)) { | ||
value = await rootFieldClean(value, info, ...args); | ||
} | ||
function _ref() { | ||
_ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(type, fieldSchema, value, info) { | ||
var _yield$getFieldType, | ||
rootFieldClean, | ||
_len, | ||
args, | ||
_key, | ||
needReClean, | ||
_fieldSchema$type, | ||
defaultValue, | ||
autoValue, | ||
clean, | ||
_args = arguments; | ||
let needReClean = false; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
info.type = fieldSchema.type; | ||
if (fieldSchema.type.__clean) { | ||
needReClean = true; | ||
value = await fieldSchema.type.__clean(value, info, ...args); | ||
} | ||
if (info.type) { | ||
_context.next = 3; | ||
break; | ||
} | ||
const { | ||
defaultValue | ||
} = fieldSchema; | ||
throw new Error('Cleaning field with no type'); | ||
if ((0, _isNil.default)(value) && !(0, _isNil.default)(defaultValue)) { | ||
needReClean = true; | ||
case 3: | ||
_context.next = 5; | ||
return (0, _getFieldType["default"])(type); | ||
if (typeof defaultValue === 'function') { | ||
value = await defaultValue(info, ...args); | ||
} else { | ||
value = defaultValue; | ||
} | ||
} | ||
case 5: | ||
_yield$getFieldType = _context.sent; | ||
rootFieldClean = _yield$getFieldType.clean; | ||
const { | ||
autoValue | ||
} = fieldSchema; | ||
for (_len = _args.length, args = new Array(_len > 4 ? _len - 4 : 0), _key = 4; _key < _len; _key++) { | ||
args[_key - 4] = _args[_key]; | ||
} | ||
if (autoValue) { | ||
needReClean = true; | ||
value = await autoValue(value, info, ...args); | ||
} | ||
if (!(rootFieldClean && !(0, _isNil["default"])(value))) { | ||
_context.next = 12; | ||
break; | ||
} | ||
const { | ||
clean | ||
} = fieldSchema; | ||
_context.next = 11; | ||
return rootFieldClean.apply(void 0, [value, info].concat(args)); | ||
if (clean) { | ||
needReClean = true; | ||
value = await clean(value, info, ...args); | ||
} | ||
case 11: | ||
value = _context.sent; | ||
if (needReClean && rootFieldClean && !(0, _isNil.default)(value)) { | ||
value = await rootFieldClean(value, info, ...args); | ||
} | ||
case 12: | ||
needReClean = false; | ||
if (!fieldSchema.type.__clean) { | ||
_context.next = 18; | ||
break; | ||
} | ||
needReClean = true; | ||
_context.next = 17; | ||
return (_fieldSchema$type = fieldSchema.type).__clean.apply(_fieldSchema$type, [value, info].concat(args)); | ||
case 17: | ||
value = _context.sent; | ||
case 18: | ||
defaultValue = fieldSchema.defaultValue; | ||
if (!((0, _isNil["default"])(value) && !(0, _isNil["default"])(defaultValue))) { | ||
_context.next = 28; | ||
break; | ||
} | ||
needReClean = true; | ||
if (!(typeof defaultValue === 'function')) { | ||
_context.next = 27; | ||
break; | ||
} | ||
_context.next = 24; | ||
return defaultValue.apply(void 0, [info].concat(args)); | ||
case 24: | ||
value = _context.sent; | ||
_context.next = 28; | ||
break; | ||
case 27: | ||
value = defaultValue; | ||
case 28: | ||
autoValue = fieldSchema.autoValue; | ||
if (!autoValue) { | ||
_context.next = 34; | ||
break; | ||
} | ||
needReClean = true; | ||
_context.next = 33; | ||
return autoValue.apply(void 0, [value, info].concat(args)); | ||
case 33: | ||
value = _context.sent; | ||
case 34: | ||
clean = fieldSchema.clean; | ||
if (!clean) { | ||
_context.next = 40; | ||
break; | ||
} | ||
needReClean = true; | ||
_context.next = 39; | ||
return clean.apply(void 0, [value, info].concat(args)); | ||
case 39: | ||
value = _context.sent; | ||
case 40: | ||
if (!(needReClean && rootFieldClean && !(0, _isNil["default"])(value))) { | ||
_context.next = 44; | ||
break; | ||
} | ||
_context.next = 43; | ||
return rootFieldClean.apply(void 0, [value, info].concat(args)); | ||
case 43: | ||
value = _context.sent; | ||
case 44: | ||
return _context.abrupt("return", value); | ||
case 45: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
return _ref.apply(this, arguments); | ||
return value; | ||
} |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; | ||
var _recursiveClean = _interopRequireDefault(require("./recursiveClean")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
var defaultOptions = { | ||
const defaultOptions = { | ||
autoConvert: true, | ||
@@ -29,53 +19,17 @@ filter: true, | ||
function _default(_x) { | ||
return _ref.apply(this, arguments); | ||
} | ||
function _ref() { | ||
_ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(schema) { | ||
var doc, | ||
passedOptions, | ||
options, | ||
_len, | ||
args, | ||
_key, | ||
params, | ||
_args = arguments; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
doc = _args.length > 1 && _args[1] !== undefined ? _args[1] : {}; | ||
passedOptions = _args.length > 2 && _args[2] !== undefined ? _args[2] : {}; | ||
options = _objectSpread(_objectSpread({}, defaultOptions), passedOptions); | ||
for (_len = _args.length, args = new Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) { | ||
args[_key - 3] = _args[_key]; | ||
} | ||
params = { | ||
schema: { | ||
type: schema | ||
}, | ||
value: doc, | ||
doc: options.forceDoc || doc, | ||
currentDoc: doc, | ||
options: options, | ||
args: args | ||
}; | ||
_context.next = 7; | ||
return (0, _recursiveClean["default"])(params); | ||
case 7: | ||
return _context.abrupt("return", _context.sent); | ||
case 8: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
return _ref.apply(this, arguments); | ||
async function _default(schema, doc = {}, passedOptions = {}, ...args) { | ||
const options = { ...defaultOptions, | ||
...passedOptions | ||
}; | ||
const params = { | ||
schema: { | ||
type: schema | ||
}, | ||
value: doc, | ||
doc: options.forceDoc || doc, | ||
currentDoc: doc, | ||
options, | ||
args | ||
}; | ||
return await (0, _recursiveClean.default)(params); | ||
} |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _index = _interopRequireDefault(require("./index")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
test('autoconverts values', async () => { | ||
const schema = { | ||
number: { | ||
type: Number | ||
}, | ||
string: { | ||
type: String | ||
}, | ||
string2: { | ||
type: String | ||
} | ||
}; | ||
const doc = { | ||
number: '12', | ||
string: 12, | ||
string2: 'string ' | ||
}; | ||
const cleaned = await (0, _index.default)(schema, doc); | ||
expect(cleaned).toEqual({ | ||
number: 12, | ||
string: '12', | ||
string2: 'string' | ||
}); | ||
}); | ||
test('dont autoConvert values', async () => { | ||
const schema = { | ||
number: { | ||
type: 'number' | ||
}, | ||
string: { | ||
type: String | ||
}, | ||
string2: { | ||
type: String | ||
} | ||
}; | ||
const doc = { | ||
number: '12', | ||
string: 12, | ||
string2: 'string ' | ||
}; | ||
const cleaned = await (0, _index.default)(schema, doc, { | ||
autoConvert: false, | ||
trimStrings: false | ||
}); | ||
expect(cleaned).toEqual({ | ||
number: '12', | ||
string: 12, | ||
string2: 'string ' | ||
}); | ||
}); | ||
test('dont remove null values', async () => { | ||
const schema = { | ||
string: { | ||
type: String | ||
} | ||
}; | ||
const doc = { | ||
string: null | ||
}; | ||
const cleaned = await (0, _index.default)(schema, doc); | ||
expect(cleaned).toEqual({ | ||
string: null | ||
}); | ||
}); | ||
test('cleans boolean correctly', async () => { | ||
const type = { | ||
type: Boolean | ||
}; | ||
const schema = { | ||
a: type, | ||
b: type, | ||
c: type, | ||
d: type, | ||
e: type | ||
}; | ||
const doc = { | ||
a: 1, | ||
b: 'true', | ||
c: 0, | ||
d: true, | ||
e: 'false', | ||
f: true | ||
}; | ||
const cleaned = await (0, _index.default)(schema, doc); | ||
expect(cleaned).toEqual({ | ||
a: true, | ||
b: true, | ||
c: false, | ||
d: true, | ||
e: false | ||
}); | ||
}); | ||
test('trims strings and filter them', async () => { | ||
const type = { | ||
type: String | ||
}; | ||
const schema = { | ||
aString: type, | ||
otherString: type | ||
}; | ||
const doc = { | ||
aString: ' 123 321 \n 123 \n\t d\t', | ||
otherString: ' ' | ||
}; | ||
const cleaned = await (0, _index.default)(schema, doc, { | ||
trimStrings: true, | ||
removeEmptyStrings: true | ||
}); | ||
expect(cleaned).toEqual({ | ||
aString: '123 321 \n 123 \n\t d' | ||
}); | ||
}); | ||
test('filter fields not in schema', async () => { | ||
const type = { | ||
type: String | ||
}; | ||
const schema = { | ||
a: type, | ||
b: type | ||
}; | ||
const doc = { | ||
b: 'hi', | ||
c: 'hello' | ||
}; | ||
const cleaned = await (0, _index.default)(schema, doc); | ||
expect(cleaned).toEqual({ | ||
b: 'hi' | ||
}); | ||
}); | ||
test('runs autovalues with arrays', async () => { | ||
const schema = { | ||
texts: { | ||
type: [String], | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
autoValue(values) { | ||
return values.map(val => val + ' world'); | ||
} | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
} | ||
}; | ||
const doc = { | ||
texts: ['hello', 'good bye'] | ||
}; | ||
const cleaned = await (0, _index.default)(schema, doc); | ||
expect(cleaned).toEqual({ | ||
texts: ['hello world', 'good bye world'] | ||
}); | ||
}); | ||
test('run autovalue when field is not present', async () => { | ||
const schema = { | ||
text: { | ||
type: String, | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
autoValue(values) { | ||
return 'a value'; | ||
} | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
} | ||
}; | ||
const doc = {}; | ||
const cleaned = await (0, _index.default)(schema, doc); | ||
expect(cleaned).toEqual({ | ||
text: 'a value' | ||
}); | ||
}); | ||
test('returns the default values', async () => { | ||
const schema = { | ||
text: { | ||
type: String, | ||
defaultValue: 'hello', | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
autoValue(value) { | ||
return value + ' world'; | ||
} | ||
test('autoconverts values', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var schema, doc, cleaned; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
schema = { | ||
number: { | ||
type: Number | ||
}, | ||
string: { | ||
type: String | ||
}, | ||
string2: { | ||
type: String | ||
} | ||
}; | ||
doc = { | ||
number: '12', | ||
string: 12, | ||
string2: 'string ' | ||
}; | ||
_context.next = 4; | ||
return (0, _index["default"])(schema, doc); | ||
}, | ||
text1: { | ||
type: String, | ||
defaultValue: 'text1' | ||
}, | ||
text2: { | ||
type: String, | ||
defaultValue: 'text2' | ||
}, | ||
text3: { | ||
type: String | ||
}, | ||
text4: { | ||
type: String, | ||
case 4: | ||
cleaned = _context.sent; | ||
expect(cleaned).toEqual({ | ||
number: 12, | ||
string: '12', | ||
string2: 'string' | ||
}); | ||
defaultValue() { | ||
return 'afunc'; | ||
} | ||
case 6: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
}))); | ||
test('dont autoConvert values', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
var schema, doc, cleaned; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
schema = { | ||
number: { | ||
type: 'number' | ||
}, | ||
string: { | ||
type: String | ||
}, | ||
string2: { | ||
type: String | ||
} | ||
}; | ||
doc = { | ||
number: '12', | ||
string: 12, | ||
string2: 'string ' | ||
}; | ||
_context2.next = 4; | ||
return (0, _index["default"])(schema, doc, { | ||
autoConvert: false, | ||
trimStrings: false | ||
}); | ||
}; | ||
const doc = { | ||
text1: 'pass' | ||
}; | ||
const cleaned = await (0, _index.default)(schema, doc); | ||
expect(cleaned).toEqual({ | ||
text: 'hello world', | ||
text1: 'pass', | ||
text2: 'text2', | ||
text4: 'afunc' | ||
}); | ||
}); | ||
test('run deep autovalues', async () => { | ||
const deep = { | ||
s: { | ||
type: String, | ||
case 4: | ||
cleaned = _context2.sent; | ||
expect(cleaned).toEqual({ | ||
number: '12', | ||
string: 12, | ||
string2: 'string ' | ||
}); | ||
autoValue() { | ||
return 'no'; | ||
} | ||
case 6: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2); | ||
}))); | ||
test('dont remove null values', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() { | ||
var schema, doc, cleaned; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
schema = { | ||
string: { | ||
type: String | ||
} | ||
}; | ||
doc = { | ||
string: null | ||
}; | ||
_context3.next = 4; | ||
return (0, _index["default"])(schema, doc); | ||
}; | ||
const schema = { | ||
text: { | ||
type: deep, | ||
case 4: | ||
cleaned = _context3.sent; | ||
expect(cleaned).toEqual({ | ||
string: null | ||
}); | ||
case 6: | ||
case "end": | ||
return _context3.stop(); | ||
autoValue(text) { | ||
return { ...text, | ||
type: 'text' | ||
}; | ||
} | ||
} | ||
}, _callee3); | ||
}))); | ||
test('cleans boolean correctly', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() { | ||
var type, schema, doc, cleaned; | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
type = { | ||
type: Boolean | ||
}; | ||
schema = { | ||
a: type, | ||
b: type, | ||
c: type, | ||
d: type, | ||
e: type | ||
}; | ||
doc = { | ||
a: 1, | ||
b: 'true', | ||
c: 0, | ||
d: true, | ||
e: 'false', | ||
f: true | ||
}; | ||
_context4.next = 5; | ||
return (0, _index["default"])(schema, doc); | ||
case 5: | ||
cleaned = _context4.sent; | ||
expect(cleaned).toEqual({ | ||
a: true, | ||
b: true, | ||
c: false, | ||
d: true, | ||
e: false | ||
}); | ||
}, | ||
texts: { | ||
type: [deep], | ||
case 7: | ||
case "end": | ||
return _context4.stop(); | ||
autoValue(texts) { | ||
return [texts[0], 'yes']; | ||
} | ||
} | ||
}, _callee4); | ||
}))); | ||
test('trims strings and filter them', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5() { | ||
var type, schema, doc, cleaned; | ||
return regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
type = { | ||
type: String | ||
}; | ||
schema = { | ||
aString: type, | ||
otherString: type | ||
}; | ||
doc = { | ||
aString: ' 123 321 \n 123 \n\t d\t', | ||
otherString: ' ' | ||
}; | ||
_context5.next = 5; | ||
return (0, _index["default"])(schema, doc, { | ||
trimStrings: true, | ||
removeEmptyStrings: true | ||
}); | ||
}; | ||
const doc = { | ||
text: { | ||
s: 'objecttext' | ||
}, | ||
texts: [{ | ||
s: 'arraytext' | ||
}, { | ||
s: 'noa' | ||
}] | ||
}; | ||
const cleaned = await (0, _index.default)(schema, doc); | ||
expect(cleaned).toEqual({ | ||
text: { | ||
s: 'no' | ||
}, | ||
texts: [{ | ||
s: 'no' | ||
}, 'yes'] | ||
}); | ||
}); | ||
test('perform custom cleaning from clean option in field', async () => { | ||
const person = { | ||
name: { | ||
type: String, | ||
case 5: | ||
cleaned = _context5.sent; | ||
expect(cleaned).toEqual({ | ||
aString: '123 321 \n 123 \n\t d' | ||
}); | ||
async clean(value) { | ||
if (value === 'Joaquin') { | ||
return 'Roberto'; | ||
} else { | ||
return value; | ||
} | ||
} | ||
case 7: | ||
case "end": | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee5); | ||
}))); | ||
test('filter fields not in schema', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() { | ||
var type, schema, doc, cleaned; | ||
return regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
type = { | ||
type: String | ||
}; | ||
schema = { | ||
a: type, | ||
b: type | ||
}; | ||
doc = { | ||
b: 'hi', | ||
c: 'hello' | ||
}; | ||
_context6.next = 5; | ||
return (0, _index["default"])(schema, doc); | ||
}; | ||
const schema = { | ||
persons: { | ||
type: [person] | ||
} | ||
}; | ||
const cleaned = await (0, _index.default)(schema, { | ||
persons: [{ | ||
name: 'Nicolás' | ||
}, { | ||
name: 'Joaquin' | ||
}] | ||
}); | ||
expect(cleaned).toEqual({ | ||
persons: [{ | ||
name: 'Nicolás' | ||
}, { | ||
name: 'Roberto' | ||
}] | ||
}); | ||
}); | ||
test('perform custom cleaning', async () => { | ||
const person = { | ||
name: { | ||
type: String | ||
}, | ||
case 5: | ||
cleaned = _context6.sent; | ||
expect(cleaned).toEqual({ | ||
b: 'hi' | ||
}); | ||
case 7: | ||
case "end": | ||
return _context6.stop(); | ||
async __clean(value) { | ||
if (value.name === 'Joaquin') { | ||
return { | ||
name: 'Roberto' | ||
}; | ||
} else { | ||
return value; | ||
} | ||
} | ||
}, _callee6); | ||
}))); | ||
test('runs autovalues with arrays', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7() { | ||
var schema, doc, cleaned; | ||
return regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
case 0: | ||
schema = { | ||
texts: { | ||
type: [String], | ||
autoValue: function autoValue(values) { | ||
return values.map(function (val) { | ||
return val + ' world'; | ||
}); | ||
} | ||
} | ||
}; | ||
doc = { | ||
texts: ['hello', 'good bye'] | ||
}; | ||
_context7.next = 4; | ||
return (0, _index["default"])(schema, doc); | ||
case 4: | ||
cleaned = _context7.sent; | ||
expect(cleaned).toEqual({ | ||
texts: ['hello world', 'good bye world'] | ||
}); | ||
}; | ||
const schema = { | ||
persons: { | ||
type: [person] | ||
} | ||
}; | ||
const cleaned = await (0, _index.default)(schema, { | ||
persons: [{ | ||
name: 'Nicolás' | ||
}, { | ||
name: 'Joaquin' | ||
}] | ||
}); | ||
expect(cleaned).toEqual({ | ||
persons: [{ | ||
name: 'Nicolás' | ||
}, { | ||
name: 'Roberto' | ||
}] | ||
}); | ||
}); | ||
test('perform non deep custom cleaning', async () => { | ||
const schema = { | ||
name: { | ||
type: String | ||
}, | ||
case 6: | ||
case "end": | ||
return _context7.stop(); | ||
async __clean(value) { | ||
if (value.name === 'Joaquin') { | ||
return { | ||
name: 'Roberto' | ||
}; | ||
} else { | ||
return value; | ||
} | ||
} | ||
}, _callee7); | ||
}))); | ||
test('run autovalue when field is not present', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee8() { | ||
var schema, doc, cleaned; | ||
return regeneratorRuntime.wrap(function _callee8$(_context8) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
schema = { | ||
text: { | ||
type: String, | ||
autoValue: function autoValue(values) { | ||
return 'a value'; | ||
} | ||
} | ||
}; | ||
doc = {}; | ||
_context8.next = 4; | ||
return (0, _index["default"])(schema, doc); | ||
case 4: | ||
cleaned = _context8.sent; | ||
expect(cleaned).toEqual({ | ||
text: 'a value' | ||
}); | ||
}; | ||
const cleaned = await (0, _index.default)(schema, { | ||
name: 'Joaquin' | ||
}); | ||
expect(cleaned).toEqual({ | ||
name: 'Roberto' | ||
}); | ||
}); | ||
test('Handle errors while cleaning', async () => { | ||
const schema = { | ||
name: { | ||
type: String, | ||
case 6: | ||
case "end": | ||
return _context8.stop(); | ||
async autoValue(value) { | ||
throw new Error('an error'); | ||
} | ||
} | ||
}, _callee8); | ||
}))); | ||
test('returns the default values', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee9() { | ||
var schema, doc, cleaned; | ||
return regeneratorRuntime.wrap(function _callee9$(_context9) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
schema = { | ||
text: { | ||
type: String, | ||
defaultValue: 'hello', | ||
autoValue: function autoValue(value) { | ||
return value + ' world'; | ||
} | ||
}, | ||
text1: { | ||
type: String, | ||
defaultValue: 'text1' | ||
}, | ||
text2: { | ||
type: String, | ||
defaultValue: 'text2' | ||
}, | ||
text3: { | ||
type: String | ||
}, | ||
text4: { | ||
type: String, | ||
defaultValue: function defaultValue() { | ||
return 'afunc'; | ||
} | ||
} | ||
}; | ||
doc = { | ||
text1: 'pass' | ||
}; | ||
_context9.next = 4; | ||
return (0, _index["default"])(schema, doc); | ||
}; | ||
case 4: | ||
cleaned = _context9.sent; | ||
expect(cleaned).toEqual({ | ||
text: 'hello world', | ||
text1: 'pass', | ||
text2: 'text2', | ||
text4: 'afunc' | ||
}); | ||
try { | ||
await (0, _index.default)(schema, { | ||
name: 'Joaquin' | ||
}); | ||
} catch (error) { | ||
expect(error.message).toBe('Error cleaning field name, error: an error'); | ||
} | ||
}); | ||
test('pass currentDoc cleaning arrays', async () => { | ||
const aItem = { | ||
name: 'Nicolás' | ||
}; | ||
const doc = { | ||
items: [aItem] | ||
}; | ||
const item = { | ||
name: { | ||
type: String, | ||
case 6: | ||
case "end": | ||
return _context9.stop(); | ||
async autoValue(name, { | ||
currentDoc | ||
}) { | ||
expect(currentDoc).toBe(aItem); | ||
} | ||
} | ||
}, _callee9); | ||
}))); | ||
test('run deep autovalues', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee10() { | ||
var deep, schema, doc, cleaned; | ||
return regeneratorRuntime.wrap(function _callee10$(_context10) { | ||
while (1) { | ||
switch (_context10.prev = _context10.next) { | ||
case 0: | ||
deep = { | ||
s: { | ||
type: String, | ||
autoValue: function autoValue() { | ||
return 'no'; | ||
} | ||
} | ||
}; | ||
schema = { | ||
text: { | ||
type: deep, | ||
autoValue: function autoValue(text) { | ||
return _objectSpread(_objectSpread({}, text), {}, { | ||
type: 'text' | ||
}); | ||
} | ||
}, | ||
texts: { | ||
type: [deep], | ||
autoValue: function autoValue(texts) { | ||
return [texts[0], 'yes']; | ||
} | ||
} | ||
}; | ||
doc = { | ||
text: { | ||
s: 'objecttext' | ||
}, | ||
texts: [{ | ||
s: 'arraytext' | ||
}, { | ||
s: 'noa' | ||
}] | ||
}; | ||
_context10.next = 5; | ||
return (0, _index["default"])(schema, doc); | ||
}; | ||
const schema = { | ||
items: { | ||
type: [item], | ||
case 5: | ||
cleaned = _context10.sent; | ||
expect(cleaned).toEqual({ | ||
text: { | ||
s: 'no' | ||
}, | ||
texts: [{ | ||
s: 'no' | ||
}, 'yes'] | ||
}); | ||
async autoValue(items, { | ||
currentDoc | ||
}) { | ||
expect(currentDoc).toBe(doc); | ||
} | ||
case 7: | ||
case "end": | ||
return _context10.stop(); | ||
} | ||
} | ||
}, _callee10); | ||
}))); | ||
test('perform custom cleaning from clean option in field', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee12() { | ||
var person, schema, cleaned; | ||
return regeneratorRuntime.wrap(function _callee12$(_context12) { | ||
while (1) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
person = { | ||
name: { | ||
type: String, | ||
clean: function clean(value) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee11() { | ||
return regeneratorRuntime.wrap(function _callee11$(_context11) { | ||
while (1) { | ||
switch (_context11.prev = _context11.next) { | ||
case 0: | ||
if (!(value === 'Joaquin')) { | ||
_context11.next = 4; | ||
break; | ||
} | ||
}; | ||
expect.assertions(2); | ||
await (0, _index.default)(schema, doc); | ||
}); | ||
test('omit undefined items in array', async () => { | ||
const doc = { | ||
items: [{ | ||
name: 'Nicolás' | ||
}] | ||
}; | ||
const item = { | ||
name: { | ||
type: String | ||
}, | ||
return _context11.abrupt("return", 'Roberto'); | ||
__clean() {} | ||
case 4: | ||
return _context11.abrupt("return", value); | ||
}; | ||
const schema = { | ||
items: { | ||
type: [item] | ||
} | ||
}; | ||
const result = await (0, _index.default)(schema, doc); | ||
expect(result).toEqual({ | ||
items: [] | ||
}); | ||
}); | ||
test('passes extra arguments to clean', async () => { | ||
const doc = { | ||
name: 'Nicolás' | ||
}; | ||
const schema = { | ||
name: { | ||
type: String, | ||
case 5: | ||
case "end": | ||
return _context11.stop(); | ||
} | ||
} | ||
}, _callee11); | ||
}))(); | ||
} | ||
} | ||
}; | ||
schema = { | ||
persons: { | ||
type: [person] | ||
} | ||
}; | ||
_context12.next = 4; | ||
return (0, _index["default"])(schema, { | ||
persons: [{ | ||
name: 'Nicolás' | ||
}, { | ||
name: 'Joaquin' | ||
}] | ||
}); | ||
autoValue(name, info, arg1, arg2) { | ||
expect(arg1).toBe(1); | ||
expect(arg2).toBe(2); | ||
} | ||
case 4: | ||
cleaned = _context12.sent; | ||
expect(cleaned).toEqual({ | ||
persons: [{ | ||
name: 'Nicolás' | ||
}, { | ||
name: 'Roberto' | ||
}] | ||
}); | ||
} | ||
}; | ||
expect.assertions(2); | ||
await (0, _index.default)(schema, doc, null, 1, 2); | ||
}); | ||
test('throws error when cleaning field with no type', async () => { | ||
const schema = { | ||
name: { | ||
type: null, | ||
case 6: | ||
case "end": | ||
return _context12.stop(); | ||
autoValue(name, info, arg1, arg2) { | ||
return 'Nicolás'; | ||
} | ||
} | ||
}, _callee12); | ||
}))); | ||
test('perform custom cleaning', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee14() { | ||
var person, schema, cleaned; | ||
return regeneratorRuntime.wrap(function _callee14$(_context14) { | ||
while (1) { | ||
switch (_context14.prev = _context14.next) { | ||
case 0: | ||
person = { | ||
name: { | ||
type: String | ||
}, | ||
__clean: function __clean(value) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee13() { | ||
return regeneratorRuntime.wrap(function _callee13$(_context13) { | ||
while (1) { | ||
switch (_context13.prev = _context13.next) { | ||
case 0: | ||
if (!(value.name === 'Joaquin')) { | ||
_context13.next = 4; | ||
break; | ||
} | ||
}; | ||
expect.assertions(1); | ||
return _context13.abrupt("return", { | ||
name: 'Roberto' | ||
}); | ||
try { | ||
await (0, _index.default)(schema); | ||
} catch (error) { | ||
expect(error.message).toBe('Error cleaning field name, error: Cleaning field with no type'); | ||
} | ||
}); | ||
test('cleans when no argument is passed', async () => { | ||
const schema = { | ||
name: { | ||
type: String, | ||
case 4: | ||
return _context13.abrupt("return", value); | ||
autoValue(name, info, arg1, arg2) { | ||
return 'Nicolás'; | ||
} | ||
case 5: | ||
case "end": | ||
return _context13.stop(); | ||
} | ||
} | ||
}, _callee13); | ||
}))(); | ||
} | ||
}; | ||
schema = { | ||
persons: { | ||
type: [person] | ||
} | ||
}; | ||
_context14.next = 4; | ||
return (0, _index["default"])(schema, { | ||
persons: [{ | ||
name: 'Nicolás' | ||
}, { | ||
name: 'Joaquin' | ||
}] | ||
}); | ||
} | ||
}; | ||
const result = await (0, _index.default)(schema); | ||
expect(result).toEqual({ | ||
name: 'Nicolás' | ||
}); | ||
}); | ||
test('pass currentDoc cleaning complex schemas', async () => { | ||
const aCar = { | ||
brand: 'Jeep' | ||
}; | ||
const aMom = { | ||
name: 'Paula', | ||
car: aCar | ||
}; | ||
const aItem = { | ||
name: 'Nicolás', | ||
mom: aMom | ||
}; | ||
const doc = { | ||
items: [aItem] | ||
}; | ||
const car = { | ||
brand: { | ||
type: String, | ||
case 4: | ||
cleaned = _context14.sent; | ||
expect(cleaned).toEqual({ | ||
persons: [{ | ||
name: 'Nicolás' | ||
}, { | ||
name: 'Roberto' | ||
}] | ||
}); | ||
case 6: | ||
case "end": | ||
return _context14.stop(); | ||
async autoValue(value, { | ||
currentDoc | ||
}) { | ||
expect(value).toEqual(aCar.brand); | ||
expect(currentDoc).toEqual(aCar); | ||
return value; | ||
} | ||
} | ||
}, _callee14); | ||
}))); | ||
test('perform non deep custom cleaning', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee16() { | ||
var schema, cleaned; | ||
return regeneratorRuntime.wrap(function _callee16$(_context16) { | ||
while (1) { | ||
switch (_context16.prev = _context16.next) { | ||
case 0: | ||
schema = { | ||
name: { | ||
type: String | ||
}, | ||
__clean: function __clean(value) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee15() { | ||
return regeneratorRuntime.wrap(function _callee15$(_context15) { | ||
while (1) { | ||
switch (_context15.prev = _context15.next) { | ||
case 0: | ||
if (!(value.name === 'Joaquin')) { | ||
_context15.next = 4; | ||
break; | ||
} | ||
return _context15.abrupt("return", { | ||
name: 'Roberto' | ||
}); | ||
}, | ||
case 4: | ||
return _context15.abrupt("return", value); | ||
case 5: | ||
case "end": | ||
return _context15.stop(); | ||
} | ||
} | ||
}, _callee15); | ||
}))(); | ||
} | ||
}; | ||
_context16.next = 3; | ||
return (0, _index["default"])(schema, { | ||
name: 'Joaquin' | ||
}); | ||
case 3: | ||
cleaned = _context16.sent; | ||
expect(cleaned).toEqual({ | ||
name: 'Roberto' | ||
}); | ||
case 5: | ||
case "end": | ||
return _context16.stop(); | ||
} | ||
async __clean(value, info) { | ||
expect(value).toEqual(aMom.car); | ||
expect(info.currentDoc).toEqual(aMom); | ||
return value; | ||
} | ||
}, _callee16); | ||
}))); | ||
test('Handle errors while cleaning', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee18() { | ||
var schema; | ||
return regeneratorRuntime.wrap(function _callee18$(_context18) { | ||
while (1) { | ||
switch (_context18.prev = _context18.next) { | ||
case 0: | ||
schema = { | ||
name: { | ||
type: String, | ||
autoValue: function autoValue(value) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee17() { | ||
return regeneratorRuntime.wrap(function _callee17$(_context17) { | ||
while (1) { | ||
switch (_context17.prev = _context17.next) { | ||
case 0: | ||
throw new Error('an error'); | ||
case 1: | ||
case "end": | ||
return _context17.stop(); | ||
} | ||
} | ||
}, _callee17); | ||
}))(); | ||
} | ||
} | ||
}; | ||
_context18.prev = 1; | ||
_context18.next = 4; | ||
return (0, _index["default"])(schema, { | ||
name: 'Joaquin' | ||
}); | ||
}; | ||
const mom = { | ||
name: { | ||
type: String, | ||
case 4: | ||
_context18.next = 9; | ||
break; | ||
case 6: | ||
_context18.prev = 6; | ||
_context18.t0 = _context18["catch"](1); | ||
expect(_context18.t0.message).toBe('Error cleaning field name, error: an error'); | ||
case 9: | ||
case "end": | ||
return _context18.stop(); | ||
async autoValue(value, { | ||
currentDoc | ||
}) { | ||
expect(value).toEqual(aMom.name); | ||
expect(currentDoc).toEqual(aMom); | ||
return value; | ||
} | ||
} | ||
}, _callee18, null, [[1, 6]]); | ||
}))); | ||
test('pass currentDoc cleaning arrays', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee21() { | ||
var aItem, doc, item, schema; | ||
return regeneratorRuntime.wrap(function _callee21$(_context21) { | ||
while (1) { | ||
switch (_context21.prev = _context21.next) { | ||
case 0: | ||
aItem = { | ||
name: 'Nicolás' | ||
}; | ||
doc = { | ||
items: [aItem] | ||
}; | ||
item = { | ||
name: { | ||
type: String, | ||
autoValue: function autoValue(name, _ref16) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee19() { | ||
var currentDoc; | ||
return regeneratorRuntime.wrap(function _callee19$(_context19) { | ||
while (1) { | ||
switch (_context19.prev = _context19.next) { | ||
case 0: | ||
currentDoc = _ref16.currentDoc; | ||
expect(currentDoc).toBe(aItem); | ||
case 2: | ||
case "end": | ||
return _context19.stop(); | ||
} | ||
} | ||
}, _callee19); | ||
}))(); | ||
} | ||
} | ||
}; | ||
schema = { | ||
items: { | ||
type: [item], | ||
autoValue: function autoValue(items, _ref17) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee20() { | ||
var currentDoc; | ||
return regeneratorRuntime.wrap(function _callee20$(_context20) { | ||
while (1) { | ||
switch (_context20.prev = _context20.next) { | ||
case 0: | ||
currentDoc = _ref17.currentDoc; | ||
expect(currentDoc).toBe(doc); | ||
}, | ||
car: { | ||
type: car, | ||
case 2: | ||
case "end": | ||
return _context20.stop(); | ||
} | ||
} | ||
}, _callee20); | ||
}))(); | ||
} | ||
} | ||
}; | ||
expect.assertions(2); | ||
_context21.next = 7; | ||
return (0, _index["default"])(schema, doc); | ||
case 7: | ||
case "end": | ||
return _context21.stop(); | ||
async autoValue(value, { | ||
currentDoc, | ||
doc | ||
}) { | ||
expect(value).toEqual(aMom.car); | ||
expect(currentDoc).toEqual(aMom); | ||
return value; | ||
} | ||
} | ||
}, _callee21); | ||
}))); | ||
test('omit undefined items in array', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee22() { | ||
var doc, item, schema, result; | ||
return regeneratorRuntime.wrap(function _callee22$(_context22) { | ||
while (1) { | ||
switch (_context22.prev = _context22.next) { | ||
case 0: | ||
doc = { | ||
items: [{ | ||
name: 'Nicolás' | ||
}] | ||
}; | ||
item = { | ||
name: { | ||
type: String | ||
}, | ||
__clean: function __clean() {} | ||
}; | ||
schema = { | ||
items: { | ||
type: [item] | ||
} | ||
}; | ||
_context22.next = 5; | ||
return (0, _index["default"])(schema, doc); | ||
case 5: | ||
result = _context22.sent; | ||
expect(result).toEqual({ | ||
items: [] | ||
}); | ||
case 7: | ||
case "end": | ||
return _context22.stop(); | ||
} | ||
} | ||
}, _callee22); | ||
}))); | ||
test('passes extra arguments to clean', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee23() { | ||
var doc, schema; | ||
return regeneratorRuntime.wrap(function _callee23$(_context23) { | ||
while (1) { | ||
switch (_context23.prev = _context23.next) { | ||
case 0: | ||
doc = { | ||
name: 'Nicolás' | ||
}; | ||
schema = { | ||
name: { | ||
type: String, | ||
autoValue: function autoValue(name, info, arg1, arg2) { | ||
expect(arg1).toBe(1); | ||
expect(arg2).toBe(2); | ||
} | ||
} | ||
}; | ||
expect.assertions(2); | ||
_context23.next = 5; | ||
return (0, _index["default"])(schema, doc, null, 1, 2); | ||
}; | ||
const item = { | ||
name: { | ||
type: String, | ||
case 5: | ||
case "end": | ||
return _context23.stop(); | ||
async autoValue(value, { | ||
currentDoc | ||
}) { | ||
expect(value).toEqual(aItem.name); | ||
expect(currentDoc).toEqual(aItem); | ||
return value; | ||
} | ||
} | ||
}, _callee23); | ||
}))); | ||
test('throws error when cleaning field with no type', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee24() { | ||
var schema; | ||
return regeneratorRuntime.wrap(function _callee24$(_context24) { | ||
while (1) { | ||
switch (_context24.prev = _context24.next) { | ||
case 0: | ||
schema = { | ||
name: { | ||
type: null, | ||
autoValue: function autoValue(name, info, arg1, arg2) { | ||
return 'Nicolás'; | ||
} | ||
} | ||
}; | ||
expect.assertions(1); | ||
_context24.prev = 2; | ||
_context24.next = 5; | ||
return (0, _index["default"])(schema); | ||
case 5: | ||
_context24.next = 10; | ||
break; | ||
}, | ||
mom: { | ||
type: mom, | ||
case 7: | ||
_context24.prev = 7; | ||
_context24.t0 = _context24["catch"](2); | ||
expect(_context24.t0.message).toBe('Error cleaning field name, error: Cleaning field with no type'); | ||
async autoValue(value, { | ||
currentDoc | ||
}) { | ||
expect(value).toEqual(aItem.mom); | ||
expect(currentDoc).toEqual(aItem); | ||
return value; | ||
} | ||
case 10: | ||
case "end": | ||
return _context24.stop(); | ||
} | ||
} | ||
}, _callee24, null, [[2, 7]]); | ||
}))); | ||
test('cleans when no argument is passed', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee25() { | ||
var schema, result; | ||
return regeneratorRuntime.wrap(function _callee25$(_context25) { | ||
while (1) { | ||
switch (_context25.prev = _context25.next) { | ||
case 0: | ||
schema = { | ||
name: { | ||
type: String, | ||
autoValue: function autoValue(name, info, arg1, arg2) { | ||
return 'Nicolás'; | ||
} | ||
} | ||
}; | ||
_context25.next = 3; | ||
return (0, _index["default"])(schema); | ||
}; | ||
const schema = { | ||
items: { | ||
type: [item], | ||
case 3: | ||
result = _context25.sent; | ||
expect(result).toEqual({ | ||
name: 'Nicolás' | ||
}); | ||
case 5: | ||
case "end": | ||
return _context25.stop(); | ||
async autoValue(value, { | ||
currentDoc | ||
}) { | ||
expect(value).toEqual(doc.items); | ||
expect(currentDoc).toEqual(doc); | ||
return value; | ||
} | ||
} | ||
}, _callee25); | ||
}))); | ||
test('pass currentDoc cleaning complex schemas', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee33() { | ||
var aCar, aMom, aItem, doc, car, mom, item, schema; | ||
return regeneratorRuntime.wrap(function _callee33$(_context33) { | ||
while (1) { | ||
switch (_context33.prev = _context33.next) { | ||
case 0: | ||
aCar = { | ||
brand: 'Jeep' | ||
}; | ||
aMom = { | ||
name: 'Paula', | ||
car: aCar | ||
}; | ||
aItem = { | ||
name: 'Nicolás', | ||
mom: aMom | ||
}; | ||
doc = { | ||
items: [aItem] | ||
}; | ||
car = { | ||
brand: { | ||
type: String, | ||
autoValue: function autoValue(value, _ref23) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee26() { | ||
var currentDoc; | ||
return regeneratorRuntime.wrap(function _callee26$(_context26) { | ||
while (1) { | ||
switch (_context26.prev = _context26.next) { | ||
case 0: | ||
currentDoc = _ref23.currentDoc; | ||
expect(value).toEqual(aCar.brand); | ||
expect(currentDoc).toEqual(aCar); | ||
return _context26.abrupt("return", value); | ||
case 4: | ||
case "end": | ||
return _context26.stop(); | ||
} | ||
} | ||
}, _callee26); | ||
}))(); | ||
} | ||
}, | ||
__clean: function __clean(value, info) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee27() { | ||
return regeneratorRuntime.wrap(function _callee27$(_context27) { | ||
while (1) { | ||
switch (_context27.prev = _context27.next) { | ||
case 0: | ||
expect(value).toEqual(aMom.car); | ||
expect(info.currentDoc).toEqual(aMom); | ||
return _context27.abrupt("return", value); | ||
case 3: | ||
case "end": | ||
return _context27.stop(); | ||
} | ||
} | ||
}, _callee27); | ||
}))(); | ||
} | ||
}; | ||
mom = { | ||
name: { | ||
type: String, | ||
autoValue: function autoValue(value, _ref24) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee28() { | ||
var currentDoc; | ||
return regeneratorRuntime.wrap(function _callee28$(_context28) { | ||
while (1) { | ||
switch (_context28.prev = _context28.next) { | ||
case 0: | ||
currentDoc = _ref24.currentDoc; | ||
expect(value).toEqual(aMom.name); | ||
expect(currentDoc).toEqual(aMom); | ||
return _context28.abrupt("return", value); | ||
case 4: | ||
case "end": | ||
return _context28.stop(); | ||
} | ||
} | ||
}, _callee28); | ||
}))(); | ||
} | ||
}, | ||
car: { | ||
type: car, | ||
autoValue: function autoValue(value, _ref25) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee29() { | ||
var currentDoc, doc; | ||
return regeneratorRuntime.wrap(function _callee29$(_context29) { | ||
while (1) { | ||
switch (_context29.prev = _context29.next) { | ||
case 0: | ||
currentDoc = _ref25.currentDoc, doc = _ref25.doc; | ||
expect(value).toEqual(aMom.car); | ||
expect(currentDoc).toEqual(aMom); | ||
return _context29.abrupt("return", value); | ||
case 4: | ||
case "end": | ||
return _context29.stop(); | ||
} | ||
} | ||
}, _callee29); | ||
}))(); | ||
} | ||
} | ||
}; | ||
item = { | ||
name: { | ||
type: String, | ||
autoValue: function autoValue(value, _ref26) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee30() { | ||
var currentDoc; | ||
return regeneratorRuntime.wrap(function _callee30$(_context30) { | ||
while (1) { | ||
switch (_context30.prev = _context30.next) { | ||
case 0: | ||
currentDoc = _ref26.currentDoc; | ||
expect(value).toEqual(aItem.name); | ||
expect(currentDoc).toEqual(aItem); | ||
return _context30.abrupt("return", value); | ||
case 4: | ||
case "end": | ||
return _context30.stop(); | ||
} | ||
} | ||
}, _callee30); | ||
}))(); | ||
} | ||
}, | ||
mom: { | ||
type: mom, | ||
autoValue: function autoValue(value, _ref27) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee31() { | ||
var currentDoc; | ||
return regeneratorRuntime.wrap(function _callee31$(_context31) { | ||
while (1) { | ||
switch (_context31.prev = _context31.next) { | ||
case 0: | ||
currentDoc = _ref27.currentDoc; | ||
expect(value).toEqual(aItem.mom); | ||
expect(currentDoc).toEqual(aItem); | ||
return _context31.abrupt("return", value); | ||
case 4: | ||
case "end": | ||
return _context31.stop(); | ||
} | ||
} | ||
}, _callee31); | ||
}))(); | ||
} | ||
} | ||
}; | ||
schema = { | ||
items: { | ||
type: [item], | ||
autoValue: function autoValue(value, _ref28) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee32() { | ||
var currentDoc; | ||
return regeneratorRuntime.wrap(function _callee32$(_context32) { | ||
while (1) { | ||
switch (_context32.prev = _context32.next) { | ||
case 0: | ||
currentDoc = _ref28.currentDoc; | ||
expect(value).toEqual(doc.items); | ||
expect(currentDoc).toEqual(doc); | ||
return _context32.abrupt("return", value); | ||
case 4: | ||
case "end": | ||
return _context32.stop(); | ||
} | ||
} | ||
}, _callee32); | ||
}))(); | ||
} | ||
} | ||
}; | ||
expect.assertions(14); | ||
_context33.next = 11; | ||
return (0, _index["default"])(schema, doc); | ||
case 11: | ||
case "end": | ||
return _context33.stop(); | ||
} | ||
} | ||
}, _callee33); | ||
}))); | ||
}; | ||
expect.assertions(14); | ||
await (0, _index.default)(schema, doc); | ||
}); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = void 0; | ||
exports.default = void 0; | ||
@@ -18,240 +20,77 @@ var _isPlainObject = _interopRequireDefault(require("lodash/isPlainObject")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
const cleanObjectFields = async function ({ | ||
schema, | ||
value, | ||
...other | ||
}) { | ||
const keys = Object.keys(schema.type).filter(key => !key.startsWith('__')); | ||
let newDoc = {}; | ||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | ||
for (const key of keys) { | ||
try { | ||
const cleanOptions = { ...other, | ||
schema: schema.type[key], | ||
value: value[key], | ||
currentDoc: value | ||
}; | ||
const newValue = await clean(cleanOptions); | ||
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | ||
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); } | ||
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | ||
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
var cleanObjectFields = /*#__PURE__*/function () { | ||
var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(_ref) { | ||
var schema, value, other, keys, newDoc, _iterator, _step, key, cleanOptions, newValue; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
schema = _ref.schema, value = _ref.value, other = _objectWithoutProperties(_ref, ["schema", "value"]); | ||
keys = Object.keys(schema.type).filter(function (key) { | ||
return !key.startsWith('__'); | ||
}); | ||
newDoc = {}; | ||
_iterator = _createForOfIteratorHelper(keys); | ||
_context.prev = 4; | ||
_iterator.s(); | ||
case 6: | ||
if ((_step = _iterator.n()).done) { | ||
_context.next = 21; | ||
break; | ||
} | ||
key = _step.value; | ||
_context.prev = 8; | ||
cleanOptions = _objectSpread(_objectSpread({}, other), {}, { | ||
schema: schema.type[key], | ||
value: value[key], | ||
currentDoc: value | ||
}); | ||
_context.next = 12; | ||
return clean(cleanOptions); | ||
case 12: | ||
newValue = _context.sent; | ||
if (!(0, _isUndefined["default"])(newValue)) { | ||
newDoc[key] = newValue; | ||
} | ||
_context.next = 19; | ||
break; | ||
case 16: | ||
_context.prev = 16; | ||
_context.t0 = _context["catch"](8); | ||
throw new Error("Error cleaning field ".concat(key, ", error: ").concat(_context.t0.message)); | ||
case 19: | ||
_context.next = 6; | ||
break; | ||
case 21: | ||
_context.next = 26; | ||
break; | ||
case 23: | ||
_context.prev = 23; | ||
_context.t1 = _context["catch"](4); | ||
_iterator.e(_context.t1); | ||
case 26: | ||
_context.prev = 26; | ||
_iterator.f(); | ||
return _context.finish(26); | ||
case 29: | ||
return _context.abrupt("return", newDoc); | ||
case 30: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
if (!(0, _isUndefined.default)(newValue)) { | ||
newDoc[key] = newValue; | ||
} | ||
}, _callee, null, [[4, 23, 26, 29], [8, 16]]); | ||
})); | ||
} catch (error) { | ||
throw new Error(`Error cleaning field ${key}, error: ${error.message}`); | ||
} | ||
} | ||
return function cleanObjectFields(_x) { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}(); | ||
return newDoc; | ||
}; | ||
var cleanArrayItems = /*#__PURE__*/function () { | ||
var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(_ref3) { | ||
var schema, value, other, newDoc, i, newValue; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
schema = _ref3.schema, value = _ref3.value, other = _objectWithoutProperties(_ref3, ["schema", "value"]); | ||
// clean array items | ||
newDoc = []; | ||
i = 0; | ||
const cleanArrayItems = async function ({ | ||
schema, | ||
value, | ||
...other | ||
}) { | ||
// clean array items | ||
const newDoc = []; | ||
case 3: | ||
if (!(i < value.length)) { | ||
_context2.next = 11; | ||
break; | ||
} | ||
for (let i = 0; i < value.length; i++) { | ||
const newValue = await clean({ ...other, | ||
schema: { | ||
type: schema.type[0] | ||
}, | ||
value: value[i], | ||
currentDoc: value | ||
}); | ||
_context2.next = 6; | ||
return clean(_objectSpread(_objectSpread({}, other), {}, { | ||
schema: { | ||
type: schema.type[0] | ||
}, | ||
value: value[i], | ||
currentDoc: value | ||
})); | ||
if (!(0, _isUndefined.default)(newValue)) { | ||
newDoc.push(newValue); | ||
} | ||
} | ||
case 6: | ||
newValue = _context2.sent; | ||
return newDoc; | ||
}; | ||
if (!(0, _isUndefined["default"])(newValue)) { | ||
newDoc.push(newValue); | ||
} | ||
const clean = async function (info) { | ||
let { | ||
schema, | ||
args, | ||
value | ||
} = info; | ||
case 8: | ||
i++; | ||
_context2.next = 3; | ||
break; | ||
if ((0, _isPlainObject.default)(schema.type) && (0, _isPlainObject.default)(value)) { | ||
let newDoc = await cleanObjectFields(info); | ||
const result = await (0, _cleanType.default)('plainObject', schema, newDoc, info, ...args); | ||
return result; | ||
} else if ((0, _isArray.default)(schema.type) && !(0, _isNil.default)(info.value)) { | ||
if (!(0, _isArray.default)(value)) info.value = [value]; | ||
const newDoc = await cleanArrayItems(info); | ||
return await (0, _cleanType.default)('array', schema, newDoc, info, ...args); | ||
} else { | ||
const result = await (0, _cleanType.default)(schema.type, schema, value, info, ...args); | ||
return result; | ||
} | ||
}; | ||
case 11: | ||
return _context2.abrupt("return", newDoc); | ||
case 12: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2); | ||
})); | ||
return function cleanArrayItems(_x2) { | ||
return _ref4.apply(this, arguments); | ||
}; | ||
}(); | ||
var clean = /*#__PURE__*/function () { | ||
var _ref5 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(info) { | ||
var schema, args, value, newDoc, result, _newDoc, _result; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
schema = info.schema, args = info.args, value = info.value; | ||
if (!((0, _isPlainObject["default"])(schema.type) && (0, _isPlainObject["default"])(value))) { | ||
_context3.next = 11; | ||
break; | ||
} | ||
_context3.next = 4; | ||
return cleanObjectFields(info); | ||
case 4: | ||
newDoc = _context3.sent; | ||
_context3.next = 7; | ||
return _cleanType["default"].apply(void 0, ['plainObject', schema, newDoc, info].concat(_toConsumableArray(args))); | ||
case 7: | ||
result = _context3.sent; | ||
return _context3.abrupt("return", result); | ||
case 11: | ||
if (!((0, _isArray["default"])(schema.type) && !(0, _isNil["default"])(info.value))) { | ||
_context3.next = 21; | ||
break; | ||
} | ||
if (!(0, _isArray["default"])(value)) info.value = [value]; | ||
_context3.next = 15; | ||
return cleanArrayItems(info); | ||
case 15: | ||
_newDoc = _context3.sent; | ||
_context3.next = 18; | ||
return _cleanType["default"].apply(void 0, ['array', schema, _newDoc, info].concat(_toConsumableArray(args))); | ||
case 18: | ||
return _context3.abrupt("return", _context3.sent); | ||
case 21: | ||
_context3.next = 23; | ||
return _cleanType["default"].apply(void 0, [schema.type, schema, value, info].concat(_toConsumableArray(args))); | ||
case 23: | ||
_result = _context3.sent; | ||
return _context3.abrupt("return", _result); | ||
case 25: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee3); | ||
})); | ||
return function clean(_x3) { | ||
return _ref5.apply(this, arguments); | ||
}; | ||
}(); | ||
var _default = clean; | ||
exports["default"] = _default; | ||
exports.default = _default; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; | ||
@@ -12,80 +14,26 @@ var _clean = _interopRequireDefault(require("./clean")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
var defaultOptions = { | ||
const defaultOptions = { | ||
filter: true | ||
}; | ||
function _default(_x, _x2, _x3) { | ||
return _ref.apply(this, arguments); | ||
} | ||
async function _default(schema, key, value, passedOptions = {}, ...args) { | ||
const options = { ...defaultOptions, | ||
...passedOptions | ||
}; | ||
const keySchema = (0, _dotGetSchema.default)(schema, key); | ||
function _ref() { | ||
_ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(schema, key, value) { | ||
var passedOptions, | ||
options, | ||
keySchema, | ||
_len, | ||
args, | ||
_key, | ||
result, | ||
_args = arguments; | ||
if (!keySchema) { | ||
if (options.filter) { | ||
return; | ||
} else { | ||
return value; | ||
} | ||
} | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
passedOptions = _args.length > 3 && _args[3] !== undefined ? _args[3] : {}; | ||
options = _objectSpread(_objectSpread({}, defaultOptions), passedOptions); | ||
keySchema = (0, _dotGetSchema["default"])(schema, key); | ||
if (keySchema) { | ||
_context.next = 9; | ||
break; | ||
} | ||
if (!options.filter) { | ||
_context.next = 8; | ||
break; | ||
} | ||
return _context.abrupt("return"); | ||
case 8: | ||
return _context.abrupt("return", value); | ||
case 9: | ||
for (_len = _args.length, args = new Array(_len > 4 ? _len - 4 : 0), _key = 4; _key < _len; _key++) { | ||
args[_key - 4] = _args[_key]; | ||
} | ||
_context.next = 12; | ||
return _clean["default"].apply(void 0, [{ | ||
clean: keySchema | ||
}, { | ||
clean: value | ||
}, options].concat(args)); | ||
case 12: | ||
result = _context.sent; | ||
return _context.abrupt("return", result.clean); | ||
case 14: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
return _ref.apply(this, arguments); | ||
const result = await (0, _clean.default)({ | ||
clean: keySchema | ||
}, { | ||
clean: value | ||
}, options, ...args); | ||
return result.clean; | ||
} |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _cleanKey = _interopRequireDefault(require("./cleanKey")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
test('autoconvert value', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var schema, cleaned; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
schema = { | ||
number: { | ||
type: Number | ||
} | ||
}; | ||
_context.next = 3; | ||
return (0, _cleanKey["default"])(schema, 'number', '12'); | ||
case 3: | ||
cleaned = _context.sent; | ||
expect(cleaned).toBe(12); | ||
case 5: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
test('autoconvert value', async () => { | ||
const schema = { | ||
number: { | ||
type: Number | ||
} | ||
}, _callee); | ||
}))); | ||
test('deep clean fields', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
var tag, car, schema; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
tag = { | ||
name: { | ||
type: String | ||
} | ||
}; | ||
car = { | ||
brand: { | ||
type: String | ||
}, | ||
tags: { | ||
type: [tag] | ||
} | ||
}; | ||
schema = { | ||
name: { | ||
type: String | ||
}, | ||
car: { | ||
type: car | ||
} | ||
}; | ||
_context2.t0 = expect; | ||
_context2.next = 6; | ||
return (0, _cleanKey["default"])(schema, 'car.tags', { | ||
name: 12 | ||
}); | ||
case 6: | ||
_context2.t1 = _context2.sent; | ||
(0, _context2.t0)(_context2.t1).toEqual([{ | ||
name: '12' | ||
}]); | ||
_context2.t2 = expect; | ||
_context2.next = 11; | ||
return (0, _cleanKey["default"])(schema, 'car.brand', 'Nissan'); | ||
case 11: | ||
_context2.t3 = _context2.sent; | ||
(0, _context2.t2)(_context2.t3).toBe('Nissan'); | ||
_context2.t4 = expect; | ||
_context2.next = 16; | ||
return (0, _cleanKey["default"])(schema, 'car.tags', 'Nice'); | ||
case 16: | ||
_context2.t5 = _context2.sent; | ||
(0, _context2.t4)(_context2.t5).toEqual(['Nice']); | ||
_context2.t6 = expect; | ||
_context2.next = 21; | ||
return (0, _cleanKey["default"])(schema, 'car.tags.$.name', 12); | ||
case 21: | ||
_context2.t7 = _context2.sent; | ||
(0, _context2.t6)(_context2.t7).toBe('12'); | ||
_context2.t8 = expect; | ||
_context2.next = 26; | ||
return (0, _cleanKey["default"])(schema, 'car.tags.100.name', 12); | ||
case 26: | ||
_context2.t9 = _context2.sent; | ||
(0, _context2.t8)(_context2.t9).toBe('12'); | ||
case 28: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
}; | ||
const cleaned = await (0, _cleanKey.default)(schema, 'number', '12'); | ||
expect(cleaned).toBe(12); | ||
}); | ||
test('deep clean fields', async () => { | ||
const tag = { | ||
name: { | ||
type: String | ||
} | ||
}, _callee2); | ||
}))); | ||
test('filters keys not in schema', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() { | ||
var schema; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
schema = { | ||
services: { | ||
type: 'blackbox' | ||
} | ||
}; | ||
_context3.t0 = expect; | ||
_context3.next = 4; | ||
return (0, _cleanKey["default"])(schema, 'person.name', 'Nicolás'); | ||
case 4: | ||
_context3.t1 = _context3.sent; | ||
(0, _context3.t0)(_context3.t1).toBe(undefined); | ||
case 6: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
}; | ||
const car = { | ||
brand: { | ||
type: String | ||
}, | ||
tags: { | ||
type: [tag] | ||
} | ||
}, _callee3); | ||
}))); | ||
test('dont filter keys not in schema if specified', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() { | ||
var schema; | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
schema = { | ||
services: { | ||
type: 'blackbox' | ||
} | ||
}; | ||
_context4.t0 = expect; | ||
_context4.next = 4; | ||
return (0, _cleanKey["default"])(schema, 'person.name', 'Nicolás', { | ||
filter: false | ||
}); | ||
case 4: | ||
_context4.t1 = _context4.sent; | ||
(0, _context4.t0)(_context4.t1).toBe('Nicolás'); | ||
case 6: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
}; | ||
const schema = { | ||
name: { | ||
type: String | ||
}, | ||
car: { | ||
type: car | ||
} | ||
}, _callee4); | ||
}))); | ||
test('clean blackbox key', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5() { | ||
var schema; | ||
return regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
schema = { | ||
services: { | ||
type: 'blackbox' | ||
} | ||
}; | ||
_context5.t0 = expect; | ||
_context5.next = 4; | ||
return (0, _cleanKey["default"])(schema, 'services.password', '123456'); | ||
}; | ||
expect(await (0, _cleanKey.default)(schema, 'car.tags', { | ||
name: 12 | ||
})).toEqual([{ | ||
name: '12' | ||
}]); | ||
expect(await (0, _cleanKey.default)(schema, 'car.brand', 'Nissan')).toBe('Nissan'); | ||
expect(await (0, _cleanKey.default)(schema, 'car.tags', 'Nice')).toEqual(['Nice']); | ||
expect(await (0, _cleanKey.default)(schema, 'car.tags.$.name', 12)).toBe('12'); | ||
expect(await (0, _cleanKey.default)(schema, 'car.tags.100.name', 12)).toBe('12'); | ||
}); | ||
test('filters keys not in schema', async () => { | ||
const schema = { | ||
services: { | ||
type: 'blackbox' | ||
} | ||
}; | ||
expect(await (0, _cleanKey.default)(schema, 'person.name', 'Nicolás')).toBe(undefined); | ||
}); | ||
test('dont filter keys not in schema if specified', async () => { | ||
const schema = { | ||
services: { | ||
type: 'blackbox' | ||
} | ||
}; | ||
expect(await (0, _cleanKey.default)(schema, 'person.name', 'Nicolás', { | ||
filter: false | ||
})).toBe('Nicolás'); | ||
}); | ||
test('clean blackbox key', async () => { | ||
const schema = { | ||
services: { | ||
type: 'blackbox' | ||
} | ||
}; | ||
expect(await (0, _cleanKey.default)(schema, 'services.password', '123456')).toBe('123456'); | ||
}); | ||
test('clean key with custom clean function', async () => { | ||
let calls = 0; | ||
const schema = { | ||
services: { | ||
type: { | ||
password: { | ||
type: String | ||
}, | ||
case 4: | ||
_context5.t1 = _context5.sent; | ||
(0, _context5.t0)(_context5.t1).toBe('123456'); | ||
case 6: | ||
case "end": | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee5); | ||
}))); | ||
test('clean key with custom clean function', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() { | ||
var calls, schema; | ||
return regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
calls = 0; | ||
schema = { | ||
services: { | ||
type: { | ||
password: { | ||
type: String | ||
}, | ||
__clean: function __clean(_ref7) { | ||
var password = _ref7.password; | ||
calls++; | ||
return { | ||
password: password.slice(0, 2) | ||
}; | ||
} | ||
} | ||
} | ||
__clean({ | ||
password | ||
}) { | ||
calls++; | ||
return { | ||
password: password.slice(0, 2) | ||
}; | ||
_context6.t0 = expect; | ||
_context6.next = 5; | ||
return (0, _cleanKey["default"])(schema, 'services', { | ||
password: '123456' | ||
}); | ||
} | ||
case 5: | ||
_context6.t1 = _context6.sent; | ||
(0, _context6.t0)(_context6.t1).toEqual({ | ||
password: '12' | ||
}); | ||
expect(calls).toBe(1); | ||
case 8: | ||
case "end": | ||
return _context6.stop(); | ||
} | ||
} | ||
}, _callee6); | ||
}))); | ||
test('should handle $ correctly', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7() { | ||
var Email, user, cleaned; | ||
return regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
case 0: | ||
Email = { | ||
address: { | ||
type: String | ||
}, | ||
verified: { | ||
type: Boolean | ||
} | ||
}; | ||
user = { | ||
emails: { | ||
type: [Email] | ||
} | ||
}; | ||
_context7.next = 4; | ||
return (0, _cleanKey["default"])(user, 'emails.$.verified', 'true'); | ||
}; | ||
expect(await (0, _cleanKey.default)(schema, 'services', { | ||
password: '123456' | ||
})).toEqual({ | ||
password: '12' | ||
}); | ||
expect(calls).toBe(1); | ||
}); | ||
test('should handle $ correctly', async () => { | ||
const Email = { | ||
address: { | ||
type: String | ||
}, | ||
verified: { | ||
type: Boolean | ||
} | ||
}; | ||
const user = { | ||
emails: { | ||
type: [Email] | ||
} | ||
}; | ||
const cleaned = await (0, _cleanKey.default)(user, 'emails.$.verified', 'true'); | ||
expect(cleaned).toEqual(true); | ||
}); | ||
test('should inject doc on cleanKey', async () => { | ||
const item = { | ||
data: 20 | ||
}; | ||
const user = { | ||
data: { | ||
type: String, | ||
case 4: | ||
cleaned = _context7.sent; | ||
expect(cleaned).toEqual(true); | ||
case 6: | ||
case "end": | ||
return _context7.stop(); | ||
clean(value, { | ||
doc | ||
}) { | ||
console.log(value, doc); | ||
expect(doc).toEqual(item); | ||
return value; | ||
} | ||
} | ||
}, _callee7); | ||
}))); | ||
test('should inject doc on cleanKey', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee8() { | ||
var item, user, cleaned; | ||
return regeneratorRuntime.wrap(function _callee8$(_context8) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
item = { | ||
data: 20 | ||
}; | ||
user = { | ||
data: { | ||
type: String, | ||
clean: function clean(value, _ref10) { | ||
var doc = _ref10.doc; | ||
console.log(value, doc); | ||
expect(doc).toEqual(item); | ||
return value; | ||
} | ||
} | ||
}; | ||
_context8.next = 4; | ||
return (0, _cleanKey["default"])(user, 'data', item.data, { | ||
forceDoc: item | ||
}); | ||
case 4: | ||
cleaned = _context8.sent; | ||
expect(cleaned).toBe('20'); | ||
expect.assertions(2); | ||
case 7: | ||
case "end": | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _callee8); | ||
}))); | ||
}; | ||
const cleaned = await (0, _cleanKey.default)(user, 'data', item.data, { | ||
forceDoc: item | ||
}); | ||
expect(cleaned).toBe('20'); | ||
expect.assertions(2); | ||
}); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = void 0; | ||
exports.default = void 0; | ||
var _dotGetSchema = _interopRequireDefault(require("./validateKey/dotGetSchema")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var _default = _dotGetSchema["default"]; | ||
exports["default"] = _default; | ||
var _default = _dotGetSchema.default; | ||
exports.default = _default; |
@@ -6,3 +6,3 @@ "use strict"; | ||
}); | ||
exports["default"] = void 0; | ||
exports.default = void 0; | ||
var _default = { | ||
@@ -28,2 +28,2 @@ NOT_IN_SCHEMA: 'notInSchema', | ||
}; | ||
exports["default"] = _default; | ||
exports.default = _default; |
@@ -6,25 +6,14 @@ "use strict"; | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function _default({ | ||
name, | ||
validate, | ||
clean, | ||
...otherFields | ||
}) { | ||
return { ...otherFields, | ||
name, | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | ||
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | ||
function _default(_ref) { | ||
var name = _ref.name, | ||
_validate = _ref.validate, | ||
_clean = _ref.clean, | ||
otherFields = _objectWithoutProperties(_ref, ["name", "validate", "clean"]); | ||
return _objectSpread(_objectSpread({}, otherFields), {}, { | ||
name: name, | ||
validate: function validate(value) { | ||
var info = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
validate(value, info = {}) { | ||
if (!info.currentSchema) { | ||
@@ -34,7 +23,6 @@ info.currentSchema = {}; | ||
return _validate(value, info); | ||
return validate(value, info); | ||
}, | ||
clean: function clean(value) { | ||
var info = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
clean(value, info = {}) { | ||
if (!info.options) { | ||
@@ -44,4 +32,4 @@ info.options = {}; | ||
if (_clean) { | ||
return _clean(value, info); | ||
if (clean) { | ||
return clean(value, info); | ||
} | ||
@@ -51,4 +39,5 @@ | ||
}, | ||
_isFieldType: true | ||
}); | ||
}; | ||
} |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = void 0; | ||
exports.default = void 0; | ||
@@ -14,14 +16,14 @@ var _fieldType = _interopRequireDefault(require("../fieldType")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var _default = (0, _fieldType.default)({ | ||
name: 'array', | ||
var _default = (0, _fieldType["default"])({ | ||
name: 'array', | ||
validate: function validate(value) { | ||
if (!(0, _isArray["default"])(value)) return _Errors["default"].NOT_AN_ARRAY; | ||
validate(value) { | ||
if (!(0, _isArray.default)(value)) return _Errors.default.NOT_AN_ARRAY; | ||
}, | ||
clean: function clean(value, _ref) { | ||
var options = _ref.options; | ||
clean(value, { | ||
options | ||
}) { | ||
if (options.autoConvert) { | ||
if (!(0, _isArray["default"])(value)) { | ||
if (!(0, _isArray.default)(value)) { | ||
value = [value]; | ||
@@ -33,4 +35,5 @@ } | ||
} | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _array = _interopRequireDefault(require("./array")); | ||
@@ -7,72 +9,32 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
it('should convert a single item into and array when cleaning', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var options; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
options = { | ||
autoConvert: true | ||
}; | ||
expect(_array["default"].clean('a string', { | ||
options: options | ||
})).toEqual(['a string']); | ||
expect(_array["default"].clean({ | ||
anObject: true | ||
}, { | ||
options: options | ||
})).toEqual([{ | ||
anObject: true | ||
}]); | ||
expect(_array["default"].clean({ | ||
anObject: true | ||
})).toEqual({ | ||
anObject: true | ||
}); | ||
case 4: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
}))); | ||
test('return an error when the value is incorrect', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
expect(_array["default"].validate('a string')).toBe(_Errors["default"].NOT_AN_ARRAY); | ||
expect(_array["default"].validate(new Date())).toBe(_Errors["default"].NOT_AN_ARRAY); | ||
expect(_array["default"].validate({ | ||
anObject: true | ||
})).toBe(_Errors["default"].NOT_AN_ARRAY); | ||
case 3: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2); | ||
}))); | ||
test('return no error when the value is correct', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() { | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
expect(_array["default"].validate(['hello'])).toBeFalsy(); | ||
expect(_array["default"].validate([])).toBeFalsy(); | ||
case 2: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee3); | ||
}))); | ||
it('should convert a single item into and array when cleaning', async () => { | ||
const options = { | ||
autoConvert: true | ||
}; | ||
expect(_array.default.clean('a string', { | ||
options | ||
})).toEqual(['a string']); | ||
expect(_array.default.clean({ | ||
anObject: true | ||
}, { | ||
options | ||
})).toEqual([{ | ||
anObject: true | ||
}]); | ||
expect(_array.default.clean({ | ||
anObject: true | ||
})).toEqual({ | ||
anObject: true | ||
}); | ||
}); | ||
test('return an error when the value is incorrect', async () => { | ||
expect(_array.default.validate('a string')).toBe(_Errors.default.NOT_AN_ARRAY); | ||
expect(_array.default.validate(new Date())).toBe(_Errors.default.NOT_AN_ARRAY); | ||
expect(_array.default.validate({ | ||
anObject: true | ||
})).toBe(_Errors.default.NOT_AN_ARRAY); | ||
}); | ||
test('return no error when the value is correct', async () => { | ||
expect(_array.default.validate(['hello'])).toBeFalsy(); | ||
expect(_array.default.validate([])).toBeFalsy(); | ||
}); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = void 0; | ||
exports.default = void 0; | ||
@@ -14,11 +16,11 @@ var _fieldType = _interopRequireDefault(require("../fieldType")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var _default = (0, _fieldType.default)({ | ||
name: 'blackbox', | ||
var _default = (0, _fieldType["default"])({ | ||
name: 'blackbox', | ||
validate: function validate(value) { | ||
if (!(0, _isPlainObject["default"])(value)) return _Errors["default"].NOT_AN_OBJECT; | ||
validate(value) { | ||
if (!(0, _isPlainObject.default)(value)) return _Errors.default.NOT_AN_OBJECT; | ||
} | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _blackbox = _interopRequireDefault(require("./blackbox")); | ||
@@ -7,42 +9,14 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
test('return an error when the value is incorrect', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
expect(_blackbox["default"].validate('a string')).toBe(_Errors["default"].NOT_AN_OBJECT); | ||
expect(_blackbox["default"].validate(new Date())).toBe(_Errors["default"].NOT_AN_OBJECT); | ||
expect(_blackbox["default"].validate([])).toBe(_Errors["default"].NOT_AN_OBJECT); | ||
case 3: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
}))); | ||
test('return no error when the value is correct', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
expect(_blackbox["default"].validate({ | ||
name: null | ||
})).toBeFalsy(); | ||
expect(_blackbox["default"].validate({ | ||
name: 'Nicolás' | ||
})).toBeFalsy(); | ||
case 2: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2); | ||
}))); | ||
test('return an error when the value is incorrect', async () => { | ||
expect(_blackbox.default.validate('a string')).toBe(_Errors.default.NOT_AN_OBJECT); | ||
expect(_blackbox.default.validate(new Date())).toBe(_Errors.default.NOT_AN_OBJECT); | ||
expect(_blackbox.default.validate([])).toBe(_Errors.default.NOT_AN_OBJECT); | ||
}); | ||
test('return no error when the value is correct', async () => { | ||
expect(_blackbox.default.validate({ | ||
name: null | ||
})).toBeFalsy(); | ||
expect(_blackbox.default.validate({ | ||
name: 'Nicolás' | ||
})).toBeFalsy(); | ||
}); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = void 0; | ||
exports.default = void 0; | ||
@@ -14,12 +16,12 @@ var _fieldType = _interopRequireDefault(require("../fieldType")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var _default = (0, _fieldType.default)({ | ||
name: 'boolean', | ||
var _default = (0, _fieldType["default"])({ | ||
name: 'boolean', | ||
validate: function validate(value) { | ||
if (!(0, _isBoolean["default"])(value)) return _Errors["default"].NOT_A_BOOLEAN; | ||
validate(value) { | ||
if (!(0, _isBoolean.default)(value)) return _Errors.default.NOT_A_BOOLEAN; | ||
}, | ||
clean: function clean(value, _ref) { | ||
var options = _ref.options; | ||
clean(value, { | ||
options | ||
}) { | ||
if (options.autoConvert) { | ||
@@ -47,4 +49,5 @@ if (typeof value === 'string') { | ||
} | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; |
"use strict"; | ||
var _boolean2 = _interopRequireDefault(require("./boolean")); | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _boolean = _interopRequireDefault(require("./boolean")); | ||
var _Errors = _interopRequireDefault(require("../Errors")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
test('return an error when the value is incorrect', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
expect(_boolean2["default"].validate(['Hello'])).toBe(_Errors["default"].NOT_A_BOOLEAN); | ||
expect(_boolean2["default"].validate({ | ||
name: 'Nicolás' | ||
})).toBe(_Errors["default"].NOT_A_BOOLEAN); | ||
expect(_boolean2["default"].validate(new Date())).toBe(_Errors["default"].NOT_A_BOOLEAN); | ||
expect(_boolean2["default"].validate(1)).toBe(_Errors["default"].NOT_A_BOOLEAN); | ||
expect(_boolean2["default"].validate(true)).toBeFalsy(); | ||
expect(_boolean2["default"].validate(false)).toBeFalsy(); | ||
case 6: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
test('return an error when the value is incorrect', async () => { | ||
expect(_boolean.default.validate(['Hello'])).toBe(_Errors.default.NOT_A_BOOLEAN); | ||
expect(_boolean.default.validate({ | ||
name: 'Nicolás' | ||
})).toBe(_Errors.default.NOT_A_BOOLEAN); | ||
expect(_boolean.default.validate(new Date())).toBe(_Errors.default.NOT_A_BOOLEAN); | ||
expect(_boolean.default.validate(1)).toBe(_Errors.default.NOT_A_BOOLEAN); | ||
expect(_boolean.default.validate(true)).toBeFalsy(); | ||
expect(_boolean.default.validate(false)).toBeFalsy(); | ||
}); | ||
test('autoconvert', async () => { | ||
const info = { | ||
options: { | ||
autoConvert: true | ||
} | ||
}, _callee); | ||
}))); | ||
test('autoconvert', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
var info; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
info = { | ||
options: { | ||
autoConvert: true | ||
} | ||
}; | ||
expect(_boolean2["default"].clean('hello', info)).toBe('hello'); | ||
expect(_boolean2["default"].clean('true', info)).toBe(true); | ||
expect(_boolean2["default"].clean('false', info)).toBe(false); | ||
expect(_boolean2["default"].clean(1, info)).toBe(true); | ||
expect(_boolean2["default"].clean(0, info)).toBe(false); | ||
expect(_boolean2["default"].clean(true, info)).toBe(true); | ||
expect(_boolean2["default"].clean(false, info)).toBe(false); // dont autoconvert | ||
}; | ||
expect(_boolean.default.clean('hello', info)).toBe('hello'); | ||
expect(_boolean.default.clean('true', info)).toBe(true); | ||
expect(_boolean.default.clean('false', info)).toBe(false); | ||
expect(_boolean.default.clean(1, info)).toBe(true); | ||
expect(_boolean.default.clean(0, info)).toBe(false); | ||
expect(_boolean.default.clean(true, info)).toBe(true); | ||
expect(_boolean.default.clean(false, info)).toBe(false); // dont autoconvert | ||
expect(_boolean2["default"].clean('false')).toBe('false'); | ||
case 9: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2); | ||
}))); | ||
expect(_boolean.default.clean('false')).toBe('false'); | ||
}); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = void 0; | ||
exports.default = void 0; | ||
@@ -18,15 +20,15 @@ var _fieldType = _interopRequireDefault(require("../fieldType")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var _default = (0, _fieldType.default)({ | ||
name: 'date', | ||
var _default = (0, _fieldType["default"])({ | ||
name: 'date', | ||
validate: function validate(value) { | ||
if (!(0, _isDate["default"])(value)) return _Errors["default"].NOT_A_DATE; | ||
validate(value) { | ||
if (!(0, _isDate.default)(value)) return _Errors.default.NOT_A_DATE; | ||
}, | ||
clean: function clean(value, _ref) { | ||
var options = _ref.options; | ||
clean(value, { | ||
options | ||
}) { | ||
if (options.autoConvert) { | ||
if ((0, _isString["default"])(value)) { | ||
var result = new Date(value); | ||
if ((0, _isString.default)(value)) { | ||
const result = new Date(value); | ||
@@ -38,10 +40,10 @@ if (isNaN(result.getTime())) { | ||
value = result; | ||
} else if ((0, _isNumber["default"])(value)) { | ||
var _result = new Date(value); | ||
} else if ((0, _isNumber.default)(value)) { | ||
const result = new Date(value); | ||
if (isNaN(_result.getTime())) { | ||
if (isNaN(result.getTime())) { | ||
return value; | ||
} | ||
value = _result; | ||
value = result; | ||
} | ||
@@ -52,4 +54,5 @@ } | ||
} | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _date = _interopRequireDefault(require("./date")); | ||
@@ -7,141 +9,53 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
test('return an error when the value is incorrect', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
expect(_date["default"].validate(['Hello'])).toBe(_Errors["default"].NOT_A_DATE); | ||
expect(_date["default"].validate({ | ||
name: 'Nicolás' | ||
})).toBe(_Errors["default"].NOT_A_DATE); | ||
expect(_date["default"].validate('A real string')).toBe(_Errors["default"].NOT_A_DATE); | ||
expect(_date["default"].validate(1234)).toBe(_Errors["default"].NOT_A_DATE); | ||
case 4: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
test('return an error when the value is incorrect', async () => { | ||
expect(_date.default.validate(['Hello'])).toBe(_Errors.default.NOT_A_DATE); | ||
expect(_date.default.validate({ | ||
name: 'Nicolás' | ||
})).toBe(_Errors.default.NOT_A_DATE); | ||
expect(_date.default.validate('A real string')).toBe(_Errors.default.NOT_A_DATE); | ||
expect(_date.default.validate(1234)).toBe(_Errors.default.NOT_A_DATE); | ||
}); | ||
test('return no error when the value is correct', async () => { | ||
expect(_date.default.validate(new Date())).toBeFalsy(); | ||
}); | ||
test('dont autoConvert if not specified', async () => { | ||
const info = { | ||
options: { | ||
autoConvert: false | ||
} | ||
}, _callee); | ||
}))); | ||
test('return no error when the value is correct', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
expect(_date["default"].validate(new Date())).toBeFalsy(); | ||
case 1: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
}; | ||
expect(_date.default.clean('2018-07-21T16:00:00.000Z', info)).toBe('2018-07-21T16:00:00.000Z'); | ||
}); | ||
test('return the same type if invalid is passed', async () => { | ||
const info = { | ||
options: { | ||
autoConvert: true | ||
} | ||
}, _callee2); | ||
}))); | ||
test('dont autoConvert if not specified', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() { | ||
var info; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
info = { | ||
options: { | ||
autoConvert: false | ||
} | ||
}; | ||
expect(_date["default"].clean('2018-07-21T16:00:00.000Z', info)).toBe('2018-07-21T16:00:00.000Z'); | ||
case 2: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
}; | ||
expect(_date.default.clean([123], info)).toEqual([123]); | ||
}); | ||
test('clean string types', async () => { | ||
const info = { | ||
options: { | ||
autoConvert: true | ||
} | ||
}, _callee3); | ||
}))); | ||
test('return the same type if invalid is passed', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() { | ||
var info; | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
info = { | ||
options: { | ||
autoConvert: true | ||
} | ||
}; | ||
expect(_date["default"].clean([123], info)).toEqual([123]); | ||
case 2: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
}; | ||
expect(_date.default.clean('2018-07-21T16:00:00.000Z', info)).toEqual(new Date('2018-07-21T16:00:00.000Z')); | ||
}); | ||
test('clean number types', async () => { | ||
const info = { | ||
options: { | ||
autoConvert: true | ||
} | ||
}, _callee4); | ||
}))); | ||
test('clean string types', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5() { | ||
var info; | ||
return regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
info = { | ||
options: { | ||
autoConvert: true | ||
} | ||
}; | ||
expect(_date["default"].clean('2018-07-21T16:00:00.000Z', info)).toEqual(new Date('2018-07-21T16:00:00.000Z')); | ||
case 2: | ||
case "end": | ||
return _context5.stop(); | ||
} | ||
}; | ||
expect(_date.default.clean(1532188800000, info)).toEqual(new Date('2018-07-21T16:00:00.000Z')); | ||
}); | ||
test('return the same value when invalid types are passed', async () => { | ||
const info = { | ||
options: { | ||
autoConvert: true | ||
} | ||
}, _callee5); | ||
}))); | ||
test('clean number types', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() { | ||
var info; | ||
return regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
info = { | ||
options: { | ||
autoConvert: true | ||
} | ||
}; | ||
expect(_date["default"].clean(1532188800000, info)).toEqual(new Date('2018-07-21T16:00:00.000Z')); | ||
case 2: | ||
case "end": | ||
return _context6.stop(); | ||
} | ||
} | ||
}, _callee6); | ||
}))); | ||
test('return the same value when invalid types are passed', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7() { | ||
var info; | ||
return regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
case 0: | ||
info = { | ||
options: { | ||
autoConvert: true | ||
} | ||
}; | ||
expect(_date["default"].clean('Nicolás', info)).toBe('Nicolás'); | ||
expect(_date["default"].clean(1532188800000000000, info)).toBe(1532188800000000000); | ||
case 3: | ||
case "end": | ||
return _context7.stop(); | ||
} | ||
} | ||
}, _callee7); | ||
}))); | ||
}; | ||
expect(_date.default.clean('Nicolás', info)).toBe('Nicolás'); | ||
expect(_date.default.clean(1532188800000000000, info)).toBe(1532188800000000000); | ||
}); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = void 0; | ||
exports.default = void 0; | ||
@@ -14,13 +16,13 @@ var _fieldType = _interopRequireDefault(require("../fieldType")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var _default = (0, _fieldType["default"])({ | ||
var _default = (0, _fieldType.default)({ | ||
name: 'email', | ||
validate: function validate(value) { | ||
if (!(0, _isString["default"])(value)) return _Errors["default"].NOT_A_STRING; // eslint-disable-next-line | ||
var regex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||
if (!regex.test(value)) return _Errors["default"].NOT_AN_EMAIL; | ||
validate(value) { | ||
if (!(0, _isString.default)(value)) return _Errors.default.NOT_A_STRING; // eslint-disable-next-line | ||
const regex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||
if (!regex.test(value)) return _Errors.default.NOT_AN_EMAIL; | ||
}, | ||
clean: function clean(value) { | ||
clean(value) { | ||
if (typeof value === 'string') { | ||
@@ -32,4 +34,5 @@ value = value.toLowerCase(); | ||
} | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _email = _interopRequireDefault(require("./email")); | ||
@@ -7,40 +9,12 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
test('return an error when the value is incorrect', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
expect(_email["default"].validate(['Hello'])).toBe(_Errors["default"].NOT_A_STRING); | ||
expect(_email["default"].validate({ | ||
name: 'Nicolás' | ||
})).toBe(_Errors["default"].NOT_A_STRING); | ||
expect(_email["default"].validate(new Date())).toBe(_Errors["default"].NOT_A_STRING); | ||
expect(_email["default"].validate('astring')).toBe(_Errors["default"].NOT_AN_EMAIL); | ||
case 4: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
}))); | ||
test('return no error when the value is correct', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
expect(_email["default"].validate('aemail@email.com')).toBeFalsy(); | ||
case 1: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2); | ||
}))); | ||
test('return an error when the value is incorrect', async () => { | ||
expect(_email.default.validate(['Hello'])).toBe(_Errors.default.NOT_A_STRING); | ||
expect(_email.default.validate({ | ||
name: 'Nicolás' | ||
})).toBe(_Errors.default.NOT_A_STRING); | ||
expect(_email.default.validate(new Date())).toBe(_Errors.default.NOT_A_STRING); | ||
expect(_email.default.validate('astring')).toBe(_Errors.default.NOT_AN_EMAIL); | ||
}); | ||
test('return no error when the value is correct', async () => { | ||
expect(_email.default.validate('aemail@email.com')).toBeFalsy(); | ||
}); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = void 0; | ||
exports.default = void 0; | ||
@@ -16,14 +18,17 @@ var _fieldType = _interopRequireDefault(require("../fieldType")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var _default = (0, _fieldType.default)({ | ||
name: 'ID', | ||
var _default = (0, _fieldType["default"])({ | ||
name: 'ID', | ||
validate: function validate(value) { | ||
if (!(0, _isString["default"])(value) && !(0, _isInteger["default"])(value)) return _Errors["default"].NOT_AN_ID; | ||
validate(value) { | ||
if (!(0, _isString.default)(value) && !(0, _isInteger.default)(value)) return _Errors.default.NOT_AN_ID; | ||
}, | ||
clean: function clean(value, _ref) { | ||
var options = _ref.options; | ||
if (!(0, _isString["default"])(value) && !(0, _isInteger["default"])(value)) return value; | ||
var trimStrings = options.trimStrings, | ||
removeEmptyStrings = options.removeEmptyStrings; | ||
clean(value, { | ||
options | ||
}) { | ||
if (!(0, _isString.default)(value) && !(0, _isInteger.default)(value)) return value; | ||
const { | ||
trimStrings, | ||
removeEmptyStrings | ||
} = options; | ||
value = String(value); | ||
@@ -41,4 +46,5 @@ | ||
} | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _ID = _interopRequireDefault(require("./ID")); | ||
@@ -7,41 +9,13 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
test('return an error when the value is incorrect', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
expect(_ID["default"].validate(false)).toBe(_Errors["default"].NOT_AN_ID); | ||
expect(_ID["default"].validate(new Date())).toBe(_Errors["default"].NOT_AN_ID); | ||
expect(_ID["default"].validate(NaN)).toBe(_Errors["default"].NOT_AN_ID); | ||
expect(_ID["default"].validate(1.01)).toBe(_Errors["default"].NOT_AN_ID); | ||
expect(_ID["default"].validate(Infinity)).toBe(_Errors["default"].NOT_AN_ID); | ||
case 5: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
}))); | ||
test('return no error when the value is correct', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
expect(_ID["default"].clean(1234)).toBe('1234'); | ||
expect(_ID["default"].validate('helloworld')).toBeFalsy(); | ||
expect(_ID["default"].validate('123456')).toBeFalsy(); | ||
case 3: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2); | ||
}))); | ||
test('return an error when the value is incorrect', async () => { | ||
expect(_ID.default.validate(false)).toBe(_Errors.default.NOT_AN_ID); | ||
expect(_ID.default.validate(new Date())).toBe(_Errors.default.NOT_AN_ID); | ||
expect(_ID.default.validate(NaN)).toBe(_Errors.default.NOT_AN_ID); | ||
expect(_ID.default.validate(1.01)).toBe(_Errors.default.NOT_AN_ID); | ||
expect(_ID.default.validate(Infinity)).toBe(_Errors.default.NOT_AN_ID); | ||
}); | ||
test('return no error when the value is correct', async () => { | ||
expect(_ID.default.clean(1234)).toBe('1234'); | ||
expect(_ID.default.validate('helloworld')).toBeFalsy(); | ||
expect(_ID.default.validate('123456')).toBeFalsy(); | ||
}); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = void 0; | ||
exports.default = void 0; | ||
@@ -22,3 +24,3 @@ var _array = _interopRequireDefault(require("./array")); | ||
var _boolean2 = _interopRequireDefault(require("./boolean")); | ||
var _boolean = _interopRequireDefault(require("./boolean")); | ||
@@ -29,16 +31,14 @@ var _email = _interopRequireDefault(require("./email")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var _default = { | ||
array: _array["default"], | ||
plainObject: _plainObject["default"], | ||
string: _string["default"], | ||
date: _date["default"], | ||
integer: _integer["default"], | ||
number: _number["default"], | ||
ID: _ID["default"], | ||
"boolean": _boolean2["default"], | ||
email: _email["default"], | ||
blackbox: _blackbox["default"] | ||
array: _array.default, | ||
plainObject: _plainObject.default, | ||
string: _string.default, | ||
date: _date.default, | ||
integer: _integer.default, | ||
number: _number.default, | ||
ID: _ID.default, | ||
boolean: _boolean.default, | ||
email: _email.default, | ||
blackbox: _blackbox.default | ||
}; | ||
exports["default"] = _default; | ||
exports.default = _default; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = void 0; | ||
exports.default = void 0; | ||
@@ -16,12 +18,12 @@ var _fieldType = _interopRequireDefault(require("../fieldType")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var _default = (0, _fieldType.default)({ | ||
name: 'integer', | ||
var _default = (0, _fieldType["default"])({ | ||
name: 'integer', | ||
validate: function validate(value, info) { | ||
if (!(0, _isInteger["default"])(value)) return _Errors["default"].NOT_AN_INTEGER; | ||
return _number["default"].validate(value, info); | ||
validate(value, info) { | ||
if (!(0, _isInteger.default)(value)) return _Errors.default.NOT_AN_INTEGER; | ||
return _number.default.validate(value, info); | ||
} | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _integer = _interopRequireDefault(require("./integer")); | ||
@@ -7,40 +9,12 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
test('return an error when the value is incorrect', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
expect(_integer["default"].validate('1234')).toBe(_Errors["default"].NOT_AN_INTEGER); | ||
expect(_integer["default"].validate(12.24)).toBe(_Errors["default"].NOT_AN_INTEGER); | ||
expect(_integer["default"].validate(NaN)).toBe(_Errors["default"].NOT_AN_INTEGER); | ||
expect(_integer["default"].validate(Infinity)).toBe(_Errors["default"].NOT_AN_INTEGER); | ||
case 4: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
}))); | ||
test('return no error when the value is correct', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
expect(_integer["default"].validate(99999999999999999999999)).toBeFalsy(); | ||
expect(_integer["default"].validate(0)).toBeFalsy(); | ||
expect(_integer["default"].validate(10 * 10)).toBeFalsy(); | ||
case 3: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2); | ||
}))); | ||
test('return an error when the value is incorrect', async () => { | ||
expect(_integer.default.validate('1234')).toBe(_Errors.default.NOT_AN_INTEGER); | ||
expect(_integer.default.validate(12.24)).toBe(_Errors.default.NOT_AN_INTEGER); | ||
expect(_integer.default.validate(NaN)).toBe(_Errors.default.NOT_AN_INTEGER); | ||
expect(_integer.default.validate(Infinity)).toBe(_Errors.default.NOT_AN_INTEGER); | ||
}); | ||
test('return no error when the value is correct', async () => { | ||
expect(_integer.default.validate(99999999999999999999999)).toBeFalsy(); | ||
expect(_integer.default.validate(0)).toBeFalsy(); | ||
expect(_integer.default.validate(10 * 10)).toBeFalsy(); | ||
}); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = void 0; | ||
exports.default = void 0; | ||
@@ -16,27 +18,30 @@ var _fieldType = _interopRequireDefault(require("../fieldType")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var _default = (0, _fieldType["default"])({ | ||
var _default = (0, _fieldType.default)({ | ||
name: 'number', | ||
validate: function validate(value, _ref) { | ||
var currentSchema = _ref.currentSchema; | ||
if (!(0, _isFinite["default"])(value)) return _Errors["default"].NOT_A_NUMBER; | ||
if ((0, _isFinite["default"])(currentSchema.min)) { | ||
validate(value, { | ||
currentSchema | ||
}) { | ||
if (!(0, _isFinite.default)(value)) return _Errors.default.NOT_A_NUMBER; | ||
if ((0, _isFinite.default)(currentSchema.min)) { | ||
if (value < currentSchema.min) { | ||
return _Errors["default"].NUMBER_TOO_SMALL; | ||
return _Errors.default.NUMBER_TOO_SMALL; | ||
} | ||
} | ||
if ((0, _isFinite["default"])(currentSchema.max)) { | ||
if ((0, _isFinite.default)(currentSchema.max)) { | ||
if (value > currentSchema.max) { | ||
return _Errors["default"].NUMBER_TOO_BIG; | ||
return _Errors.default.NUMBER_TOO_BIG; | ||
} | ||
} | ||
}, | ||
clean: function clean(value, _ref2) { | ||
var autoConvert = _ref2.options.autoConvert; | ||
clean(value, { | ||
options: { | ||
autoConvert | ||
} | ||
}) { | ||
if (typeof value === 'string' && autoConvert) { | ||
value = (0, _toNumber["default"])(value); | ||
value = (0, _toNumber.default)(value); | ||
} | ||
@@ -46,4 +51,5 @@ | ||
} | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _number = _interopRequireDefault(require("./number")); | ||
@@ -7,64 +9,24 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
test('return an error when the value is incorrect', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
expect(_number["default"].validate('1234')).toBe(_Errors["default"].NOT_A_NUMBER); | ||
expect(_number["default"].validate(new Date())).toBe(_Errors["default"].NOT_A_NUMBER); | ||
expect(_number["default"].validate([123])).toBe(_Errors["default"].NOT_A_NUMBER); | ||
expect(_number["default"].validate(NaN)).toBe(_Errors["default"].NOT_A_NUMBER); | ||
expect(_number["default"].validate(Infinity)).toBe(_Errors["default"].NOT_A_NUMBER); | ||
case 5: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
test('return an error when the value is incorrect', async () => { | ||
expect(_number.default.validate('1234')).toBe(_Errors.default.NOT_A_NUMBER); | ||
expect(_number.default.validate(new Date())).toBe(_Errors.default.NOT_A_NUMBER); | ||
expect(_number.default.validate([123])).toBe(_Errors.default.NOT_A_NUMBER); | ||
expect(_number.default.validate(NaN)).toBe(_Errors.default.NOT_A_NUMBER); | ||
expect(_number.default.validate(Infinity)).toBe(_Errors.default.NOT_A_NUMBER); | ||
}); | ||
test('return no error when the value is correct', async () => { | ||
expect(_number.default.validate(99999999999999999999999)).toBeFalsy(); | ||
expect(_number.default.validate(10 / 3)).toBeFalsy(); | ||
expect(_number.default.validate(10 * 10)).toBeFalsy(); | ||
}); | ||
test('validate correctly min and max', async () => { | ||
const info = { | ||
currentSchema: { | ||
min: 0, | ||
max: 10 | ||
} | ||
}, _callee); | ||
}))); | ||
test('return no error when the value is correct', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
expect(_number["default"].validate(99999999999999999999999)).toBeFalsy(); | ||
expect(_number["default"].validate(10 / 3)).toBeFalsy(); | ||
expect(_number["default"].validate(10 * 10)).toBeFalsy(); | ||
case 3: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2); | ||
}))); | ||
test('validate correctly min and max', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() { | ||
var info; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
info = { | ||
currentSchema: { | ||
min: 0, | ||
max: 10 | ||
} | ||
}; | ||
expect(_number["default"].validate(10, info)).toBeFalsy(); | ||
expect(_number["default"].validate(-10, info)).toBe(_Errors["default"].NUMBER_TOO_SMALL); | ||
expect(_number["default"].validate(10.1, info)).toBe(_Errors["default"].NUMBER_TOO_BIG); | ||
case 4: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee3); | ||
}))); | ||
}; | ||
expect(_number.default.validate(10, info)).toBeFalsy(); | ||
expect(_number.default.validate(-10, info)).toBe(_Errors.default.NUMBER_TOO_SMALL); | ||
expect(_number.default.validate(10.1, info)).toBe(_Errors.default.NUMBER_TOO_BIG); | ||
}); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = void 0; | ||
exports.default = void 0; | ||
@@ -16,37 +18,22 @@ var _fieldType = _interopRequireDefault(require("../fieldType")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var _default = (0, _fieldType.default)({ | ||
name: 'plainObject', | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } | ||
validate(value) { | ||
if (!(0, _isPlainObject.default)(value)) return _Errors.default.NOT_AN_OBJECT; | ||
}, | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
clean(value, { | ||
type, | ||
options | ||
}) { | ||
if (!(0, _isPlainObject.default)(value)) return value; | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
var _default = (0, _fieldType["default"])({ | ||
name: 'plainObject', | ||
validate: function validate(value) { | ||
if (!(0, _isPlainObject["default"])(value)) return _Errors["default"].NOT_AN_OBJECT; | ||
}, | ||
clean: function clean(value, _ref) { | ||
var type = _ref.type, | ||
options = _ref.options; | ||
if (!(0, _isPlainObject["default"])(value)) return value; | ||
if (options.filter) { | ||
var documentKeys = Object.keys(value); | ||
var schemaKeys = Object.keys(type); | ||
var notInSchemaKeys = (0, _difference["default"])(documentKeys, schemaKeys); | ||
const documentKeys = Object.keys(value); | ||
const schemaKeys = Object.keys(type); | ||
const notInSchemaKeys = (0, _difference.default)(documentKeys, schemaKeys); | ||
var _iterator = _createForOfIteratorHelper(notInSchemaKeys), | ||
_step; | ||
try { | ||
for (_iterator.s(); !(_step = _iterator.n()).done;) { | ||
var key = _step.value; | ||
delete value[key]; | ||
} | ||
} catch (err) { | ||
_iterator.e(err); | ||
} finally { | ||
_iterator.f(); | ||
for (const key of notInSchemaKeys) { | ||
delete value[key]; | ||
} | ||
@@ -57,4 +44,5 @@ } | ||
} | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _plainObject = _interopRequireDefault(require("./plainObject")); | ||
@@ -7,91 +9,40 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
test('return an error when the value is incorrect', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
expect(_plainObject["default"].validate('a string')).toBe(_Errors["default"].NOT_AN_OBJECT); | ||
expect(_plainObject["default"].validate(new Date())).toBe(_Errors["default"].NOT_AN_OBJECT); | ||
expect(_plainObject["default"].validate([])).toBe(_Errors["default"].NOT_AN_OBJECT); | ||
case 3: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
test('return an error when the value is incorrect', async () => { | ||
expect(_plainObject.default.validate('a string')).toBe(_Errors.default.NOT_AN_OBJECT); | ||
expect(_plainObject.default.validate(new Date())).toBe(_Errors.default.NOT_AN_OBJECT); | ||
expect(_plainObject.default.validate([])).toBe(_Errors.default.NOT_AN_OBJECT); | ||
}); | ||
test('return no error when the value is correct', async () => { | ||
expect(_plainObject.default.validate({})).toBeFalsy(); | ||
expect(_plainObject.default.validate({ | ||
name: 'Nicolás' | ||
})).toBeFalsy(); | ||
}); | ||
test('should return same value when cleaning non-object', async () => { | ||
expect(_plainObject.default.clean('a string')).toBe('a string'); | ||
}); | ||
test('should filter keys not in schema', async () => { | ||
const schema = { | ||
yes: { | ||
type: String | ||
} | ||
}, _callee); | ||
}))); | ||
test('return no error when the value is correct', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
expect(_plainObject["default"].validate({})).toBeFalsy(); | ||
expect(_plainObject["default"].validate({ | ||
name: 'Nicolás' | ||
})).toBeFalsy(); | ||
case 2: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
}; | ||
const value = { | ||
yes: 'yes', | ||
no: 'no' | ||
}; | ||
expect(_plainObject.default.clean(value, { | ||
type: schema, | ||
options: { | ||
filter: true | ||
} | ||
}, _callee2); | ||
}))); | ||
test('should return same value when cleaning non-object', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() { | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
expect(_plainObject["default"].clean('a string')).toBe('a string'); | ||
case 1: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
})).toEqual({ | ||
yes: 'yes' | ||
}); | ||
expect(_plainObject.default.clean(value, { | ||
type: schema, | ||
options: { | ||
filter: false | ||
} | ||
}, _callee3); | ||
}))); | ||
test('should filter keys not in schema', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() { | ||
var schema, value; | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
schema = { | ||
yes: { | ||
type: String | ||
} | ||
}; | ||
value = { | ||
yes: 'yes', | ||
no: 'no' | ||
}; | ||
expect(_plainObject["default"].clean(value, { | ||
type: schema, | ||
options: { | ||
filter: true | ||
} | ||
})).toEqual({ | ||
yes: 'yes' | ||
}); | ||
expect(_plainObject["default"].clean(value, { | ||
type: schema, | ||
options: { | ||
filter: false | ||
} | ||
})).toEqual(value); | ||
case 4: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee4); | ||
}))); | ||
})).toEqual(value); | ||
}); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = void 0; | ||
exports.default = void 0; | ||
@@ -18,13 +20,13 @@ var _fieldType = _interopRequireDefault(require("../fieldType")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var _default = (0, _fieldType["default"])({ | ||
var _default = (0, _fieldType.default)({ | ||
name: 'string', | ||
validate: function validate(value, _ref) { | ||
var currentSchema = _ref.currentSchema; | ||
if (!(0, _isString["default"])(value)) return _Errors["default"].NOT_A_STRING; | ||
validate(value, { | ||
currentSchema | ||
}) { | ||
if (!(0, _isString.default)(value)) return _Errors.default.NOT_A_STRING; | ||
if (isFinite(currentSchema.min)) { | ||
if (value.length < currentSchema.min) { | ||
return _Errors["default"].STRING_TOO_SHORT; | ||
return _Errors.default.STRING_TOO_SHORT; | ||
} | ||
@@ -35,9 +37,9 @@ } | ||
if (value.length > currentSchema.max) { | ||
return _Errors["default"].STRING_TOO_LONG; | ||
return _Errors.default.STRING_TOO_LONG; | ||
} | ||
} | ||
if ((0, _isArray["default"])(currentSchema.allowedValues)) { | ||
if (!(0, _includes["default"])(currentSchema.allowedValues, value)) { | ||
return _Errors["default"].NOT_AN_ALLOWED_VALUE; | ||
if ((0, _isArray.default)(currentSchema.allowedValues)) { | ||
if (!(0, _includes.default)(currentSchema.allowedValues, value)) { | ||
return _Errors.default.NOT_AN_ALLOWED_VALUE; | ||
} | ||
@@ -47,11 +49,13 @@ } | ||
if (value === '' && !currentSchema.optional) { | ||
return _Errors["default"].REQUIRED; | ||
return _Errors.default.REQUIRED; | ||
} | ||
}, | ||
clean: function clean(value, _ref2) { | ||
var _ref2$options = _ref2.options, | ||
autoConvert = _ref2$options.autoConvert, | ||
trimStrings = _ref2$options.trimStrings, | ||
removeEmptyStrings = _ref2$options.removeEmptyStrings; | ||
clean(value, { | ||
options: { | ||
autoConvert, | ||
trimStrings, | ||
removeEmptyStrings | ||
} | ||
}) { | ||
if (autoConvert) { | ||
@@ -71,4 +75,5 @@ value = String(value); | ||
} | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _string = _interopRequireDefault(require("./string")); | ||
@@ -7,90 +9,37 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
test('return an error when the value is incorrect', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
expect(_string["default"].validate(['Hello'])).toBe(_Errors["default"].NOT_A_STRING); | ||
expect(_string["default"].validate({ | ||
name: 'Nicolás' | ||
})).toBe(_Errors["default"].NOT_A_STRING); | ||
expect(_string["default"].validate(new Date())).toBe(_Errors["default"].NOT_A_STRING); | ||
case 3: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
test('return an error when the value is incorrect', async () => { | ||
expect(_string.default.validate(['Hello'])).toBe(_Errors.default.NOT_A_STRING); | ||
expect(_string.default.validate({ | ||
name: 'Nicolás' | ||
})).toBe(_Errors.default.NOT_A_STRING); | ||
expect(_string.default.validate(new Date())).toBe(_Errors.default.NOT_A_STRING); | ||
}); | ||
test('return no error when the value is correct', async () => { | ||
const info = { | ||
currentSchema: { | ||
optional: true | ||
} | ||
}, _callee); | ||
}))); | ||
test('return no error when the value is correct', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
var info; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
info = { | ||
currentSchema: { | ||
optional: true | ||
} | ||
}; | ||
expect(_string["default"].validate('', info)).toBeFalsy(); | ||
expect(_string["default"].validate('Nicolás')).toBeFalsy(); | ||
case 3: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
}; | ||
expect(_string.default.validate('', info)).toBeFalsy(); | ||
expect(_string.default.validate('Nicolás')).toBeFalsy(); | ||
}); | ||
test('validate correctly min and max', async () => { | ||
const info = { | ||
currentSchema: { | ||
min: 3, | ||
max: 5 | ||
} | ||
}, _callee2); | ||
}))); | ||
test('validate correctly min and max', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() { | ||
var info; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
info = { | ||
currentSchema: { | ||
min: 3, | ||
max: 5 | ||
} | ||
}; | ||
expect(_string["default"].validate('hello', info)).toBeFalsy(); | ||
expect(_string["default"].validate('hi', info)).toBe(_Errors["default"].STRING_TOO_SHORT); | ||
expect(_string["default"].validate('hello my friend', info)).toBe(_Errors["default"].STRING_TOO_LONG); | ||
case 4: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
}; | ||
expect(_string.default.validate('hello', info)).toBeFalsy(); | ||
expect(_string.default.validate('hi', info)).toBe(_Errors.default.STRING_TOO_SHORT); | ||
expect(_string.default.validate('hello my friend', info)).toBe(_Errors.default.STRING_TOO_LONG); | ||
}); | ||
test('validate correctly allowed values', async () => { | ||
const info = { | ||
currentSchema: { | ||
allowedValues: ['hello'] | ||
} | ||
}, _callee3); | ||
}))); | ||
test('validate correctly allowed values', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() { | ||
var info; | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
info = { | ||
currentSchema: { | ||
allowedValues: ['hello'] | ||
} | ||
}; | ||
expect(_string["default"].validate('hello', info)).toBeFalsy(); | ||
expect(_string["default"].validate('hi', info)).toBe(_Errors["default"].NOT_AN_ALLOWED_VALUE); | ||
case 3: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee4); | ||
}))); | ||
}; | ||
expect(_string.default.validate('hello', info)).toBeFalsy(); | ||
expect(_string.default.validate('hi', info)).toBe(_Errors.default.NOT_AN_ALLOWED_VALUE); | ||
}); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = doValidation; | ||
exports.default = doValidation; | ||
@@ -22,203 +24,83 @@ var _getError = _interopRequireDefault(require("./getError")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
async function doValidation({ | ||
schema, | ||
doc, | ||
currentDoc, | ||
value, | ||
currentSchema, | ||
keys = [], | ||
addError, | ||
options, | ||
args | ||
}) { | ||
const info = { | ||
schema, | ||
doc, | ||
currentDoc, | ||
value, | ||
currentSchema, | ||
keys, | ||
options, | ||
args, | ||
addError | ||
}; | ||
const error = await (0, _getError.default)(info); | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
if (error) { | ||
addError(keys, error); | ||
return; | ||
} | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
if ((0, _isNil.default)(value)) return; | ||
/** | ||
* Deep validation | ||
*/ | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
if ((0, _isPlainObject.default)(currentSchema.type)) { | ||
if (typeof currentSchema.type.__skipChildValidation === 'function') { | ||
if (await currentSchema.type.__skipChildValidation(value, info)) { | ||
return; | ||
} | ||
} | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } | ||
const schemaKeys = Object.keys(currentSchema.type).filter(key => !key.startsWith('__')); | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
for (const key of schemaKeys) { | ||
const itemSchema = currentSchema.type[key]; | ||
const itemValue = value[key]; | ||
const keyItemKeys = (0, _clone.default)(keys); | ||
keyItemKeys.push(key); | ||
await doValidation({ ...info, | ||
currentDoc: value, | ||
value: itemValue, | ||
currentSchema: itemSchema, | ||
keys: keyItemKeys | ||
}); | ||
} | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
const documentKeys = Object.keys(value); | ||
const notInSchemaKeys = (0, _difference.default)(documentKeys, schemaKeys); | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
for (const key of notInSchemaKeys) { | ||
const keyItemKeys = (0, _clone.default)(keys); | ||
keyItemKeys.push(key); | ||
addError(keyItemKeys, _Errors.default.NOT_IN_SCHEMA); | ||
} | ||
} else if ((0, _isArray.default)(currentSchema.type)) { | ||
const itemSchema = currentSchema.type[0]; | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
function doValidation(_x) { | ||
return _doValidation.apply(this, arguments); | ||
} | ||
function _doValidation() { | ||
_doValidation = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(_ref) { | ||
var schema, doc, currentDoc, value, currentSchema, _ref$keys, keys, addError, options, args, info, error, schemaKeys, _iterator, _step, key, itemSchema, itemValue, keyItemKeys, documentKeys, notInSchemaKeys, _iterator2, _step2, _key, _keyItemKeys, _itemSchema, i, _itemValue, _keyItemKeys2; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
schema = _ref.schema, doc = _ref.doc, currentDoc = _ref.currentDoc, value = _ref.value, currentSchema = _ref.currentSchema, _ref$keys = _ref.keys, keys = _ref$keys === void 0 ? [] : _ref$keys, addError = _ref.addError, options = _ref.options, args = _ref.args; | ||
info = { | ||
schema: schema, | ||
doc: doc, | ||
currentDoc: currentDoc, | ||
value: value, | ||
currentSchema: currentSchema, | ||
keys: keys, | ||
options: options, | ||
args: args, | ||
addError: addError | ||
}; | ||
_context.next = 4; | ||
return (0, _getError["default"])(info); | ||
case 4: | ||
error = _context.sent; | ||
if (!error) { | ||
_context.next = 8; | ||
break; | ||
} | ||
addError(keys, error); | ||
return _context.abrupt("return"); | ||
case 8: | ||
if (!(0, _isNil["default"])(value)) { | ||
_context.next = 10; | ||
break; | ||
} | ||
return _context.abrupt("return"); | ||
case 10: | ||
if (!(0, _isPlainObject["default"])(currentSchema.type)) { | ||
_context.next = 44; | ||
break; | ||
} | ||
if (!(typeof currentSchema.type.__skipChildValidation === 'function')) { | ||
_context.next = 16; | ||
break; | ||
} | ||
_context.next = 14; | ||
return currentSchema.type.__skipChildValidation(value, info); | ||
case 14: | ||
if (!_context.sent) { | ||
_context.next = 16; | ||
break; | ||
} | ||
return _context.abrupt("return"); | ||
case 16: | ||
schemaKeys = Object.keys(currentSchema.type).filter(function (key) { | ||
return !key.startsWith('__'); | ||
}); | ||
_iterator = _createForOfIteratorHelper(schemaKeys); | ||
_context.prev = 18; | ||
_iterator.s(); | ||
case 20: | ||
if ((_step = _iterator.n()).done) { | ||
_context.next = 30; | ||
break; | ||
} | ||
key = _step.value; | ||
itemSchema = currentSchema.type[key]; | ||
itemValue = value[key]; | ||
keyItemKeys = (0, _clone["default"])(keys); | ||
keyItemKeys.push(key); | ||
_context.next = 28; | ||
return doValidation(_objectSpread(_objectSpread({}, info), {}, { | ||
currentDoc: value, | ||
value: itemValue, | ||
currentSchema: itemSchema, | ||
keys: keyItemKeys | ||
})); | ||
case 28: | ||
_context.next = 20; | ||
break; | ||
case 30: | ||
_context.next = 35; | ||
break; | ||
case 32: | ||
_context.prev = 32; | ||
_context.t0 = _context["catch"](18); | ||
_iterator.e(_context.t0); | ||
case 35: | ||
_context.prev = 35; | ||
_iterator.f(); | ||
return _context.finish(35); | ||
case 38: | ||
documentKeys = Object.keys(value); | ||
notInSchemaKeys = (0, _difference["default"])(documentKeys, schemaKeys); | ||
_iterator2 = _createForOfIteratorHelper(notInSchemaKeys); | ||
try { | ||
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { | ||
_key = _step2.value; | ||
_keyItemKeys = (0, _clone["default"])(keys); | ||
_keyItemKeys.push(_key); | ||
addError(_keyItemKeys, _Errors["default"].NOT_IN_SCHEMA); | ||
} | ||
} catch (err) { | ||
_iterator2.e(err); | ||
} finally { | ||
_iterator2.f(); | ||
} | ||
_context.next = 56; | ||
break; | ||
case 44: | ||
if (!(0, _isArray["default"])(currentSchema.type)) { | ||
_context.next = 56; | ||
break; | ||
} | ||
_itemSchema = currentSchema.type[0]; | ||
i = 0; | ||
case 47: | ||
if (!(i < value.length)) { | ||
_context.next = 56; | ||
break; | ||
} | ||
_itemValue = value[i]; | ||
_keyItemKeys2 = (0, _clone["default"])(keys); | ||
_keyItemKeys2.push(i); | ||
_context.next = 53; | ||
return doValidation(_objectSpread(_objectSpread({}, info), {}, { | ||
currentDoc: value, | ||
value: _itemValue, | ||
currentSchema: { | ||
type: _itemSchema | ||
}, | ||
keys: _keyItemKeys2 | ||
})); | ||
case 53: | ||
i++; | ||
_context.next = 47; | ||
break; | ||
case 56: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, null, [[18, 32, 35, 38]]); | ||
})); | ||
return _doValidation.apply(this, arguments); | ||
for (let i = 0; i < value.length; i++) { | ||
const itemValue = value[i]; | ||
const keyItemKeys = (0, _clone.default)(keys); | ||
keyItemKeys.push(i); | ||
await doValidation({ ...info, | ||
currentDoc: value, | ||
value: itemValue, | ||
currentSchema: { | ||
type: itemSchema | ||
}, | ||
keys: keyItemKeys | ||
}); | ||
} | ||
} | ||
} |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; | ||
@@ -12,8 +14,6 @@ var _getFieldValidator = _interopRequireDefault(require("./getFieldValidator")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function _default(type) { | ||
var validatorKey = (0, _getFieldValidator["default"])(type); | ||
var validator = validatorKey === 'custom' ? type : _fieldTypes["default"][validatorKey]; | ||
const validatorKey = (0, _getFieldValidator.default)(type); | ||
const validator = validatorKey === 'custom' ? type : _fieldTypes.default[validatorKey]; | ||
return validator; | ||
} |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; | ||
@@ -18,6 +20,4 @@ var _isPlainObject = _interopRequireDefault(require("lodash/isPlainObject")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function _default(type) { | ||
if ((0, _isPlainObject["default"])(type)) { | ||
if ((0, _isPlainObject.default)(type)) { | ||
if (type._isFieldType) return 'custom'; | ||
@@ -27,3 +27,3 @@ return 'plainObject'; | ||
if ((0, _isArray["default"])(type)) return 'array'; | ||
if ((0, _isArray.default)(type)) return 'array'; | ||
if (type === String) return 'string'; | ||
@@ -34,7 +34,7 @@ if (type === Date) return 'date'; | ||
if (!(0, _isString["default"])(type)) { | ||
if (!(0, _isString.default)(type)) { | ||
throw new Error('Field type is invalid. Pass a string or a custom field type. Got ' + type); | ||
} | ||
var exists = (0, _has["default"])(_fieldTypes["default"], type); | ||
const exists = (0, _has.default)(_fieldTypes.default, type); | ||
@@ -41,0 +41,0 @@ if (!exists) { |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _getFieldValidator = _interopRequireDefault(require("./getFieldValidator")); | ||
@@ -9,182 +11,58 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
test('returns object validator when an object is passed', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var validator; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
validator = (0, _getFieldValidator["default"])({ | ||
name: { | ||
type: String | ||
} | ||
}); | ||
expect(validator).toBe('plainObject'); | ||
case 2: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
test('returns object validator when an object is passed', async () => { | ||
const validator = (0, _getFieldValidator.default)({ | ||
name: { | ||
type: String | ||
} | ||
}, _callee); | ||
}))); | ||
test('returns array validator when an array is passed', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
var validator; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
validator = (0, _getFieldValidator["default"])([String]); | ||
expect(validator).toBe('array'); | ||
}); | ||
expect(validator).toBe('plainObject'); | ||
}); | ||
test('returns array validator when an array is passed', async () => { | ||
const validator = (0, _getFieldValidator.default)([String]); | ||
expect(validator).toBe('array'); | ||
}); | ||
test('returns string validator when String is passed', async () => { | ||
const validator = (0, _getFieldValidator.default)(String); | ||
expect(validator).toBe('string'); | ||
}); | ||
test('returns number validator when Number is passed', async () => { | ||
const validator = (0, _getFieldValidator.default)(Number); | ||
expect(validator).toBe('number'); | ||
}); | ||
test('returns date validator when Date is passed', async () => { | ||
const validator = (0, _getFieldValidator.default)(Date); | ||
expect(validator).toBe('date'); | ||
}); | ||
test('returns string validator when a string key is passed', async () => { | ||
const validator = (0, _getFieldValidator.default)('string'); | ||
expect(validator).toBe('string'); | ||
}); | ||
test('returns integer validator when a integer key is passed', async () => { | ||
const validator = (0, _getFieldValidator.default)('integer'); | ||
expect(validator).toBe('integer'); | ||
}); | ||
test('get custom field type validator', async () => { | ||
const aFieldType = (0, _fieldType.default)({ | ||
name: 'customFieldType' | ||
}); | ||
const result = (0, _getFieldType.default)(aFieldType); | ||
expect(result).toBe(aFieldType); | ||
}); | ||
test('returns unkown field validator when an unkown type function is passed', async () => { | ||
expect.assertions(1); | ||
case 2: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2); | ||
}))); | ||
test('returns string validator when String is passed', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() { | ||
var validator; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
validator = (0, _getFieldValidator["default"])(String); | ||
expect(validator).toBe('string'); | ||
try { | ||
(0, _getFieldValidator.default)(() => {}); | ||
} catch (error) { | ||
expect(error.message).toMatch(/Field type is invalid/); | ||
} | ||
}); | ||
test('returns unkown field validator when an unkown type string is passed', async () => { | ||
expect.assertions(1); | ||
case 2: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee3); | ||
}))); | ||
test('returns number validator when Number is passed', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() { | ||
var validator; | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
validator = (0, _getFieldValidator["default"])(Number); | ||
expect(validator).toBe('number'); | ||
case 2: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee4); | ||
}))); | ||
test('returns date validator when Date is passed', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5() { | ||
var validator; | ||
return regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
validator = (0, _getFieldValidator["default"])(Date); | ||
expect(validator).toBe('date'); | ||
case 2: | ||
case "end": | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee5); | ||
}))); | ||
test('returns string validator when a string key is passed', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() { | ||
var validator; | ||
return regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
validator = (0, _getFieldValidator["default"])('string'); | ||
expect(validator).toBe('string'); | ||
case 2: | ||
case "end": | ||
return _context6.stop(); | ||
} | ||
} | ||
}, _callee6); | ||
}))); | ||
test('returns integer validator when a integer key is passed', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7() { | ||
var validator; | ||
return regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
case 0: | ||
validator = (0, _getFieldValidator["default"])('integer'); | ||
expect(validator).toBe('integer'); | ||
case 2: | ||
case "end": | ||
return _context7.stop(); | ||
} | ||
} | ||
}, _callee7); | ||
}))); | ||
test('get custom field type validator', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee8() { | ||
var aFieldType, result; | ||
return regeneratorRuntime.wrap(function _callee8$(_context8) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
aFieldType = (0, _fieldType["default"])({ | ||
name: 'customFieldType' | ||
}); | ||
result = (0, _getFieldType["default"])(aFieldType); | ||
expect(result).toBe(aFieldType); | ||
case 3: | ||
case "end": | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _callee8); | ||
}))); | ||
test('returns unkown field validator when an unkown type function is passed', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee9() { | ||
return regeneratorRuntime.wrap(function _callee9$(_context9) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
expect.assertions(1); | ||
try { | ||
(0, _getFieldValidator["default"])(function () {}); | ||
} catch (error) { | ||
expect(error.message).toMatch(/Field type is invalid/); | ||
} | ||
case 2: | ||
case "end": | ||
return _context9.stop(); | ||
} | ||
} | ||
}, _callee9); | ||
}))); | ||
test('returns unkown field validator when an unkown type string is passed', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee10() { | ||
return regeneratorRuntime.wrap(function _callee10$(_context10) { | ||
while (1) { | ||
switch (_context10.prev = _context10.next) { | ||
case 0: | ||
expect.assertions(1); | ||
try { | ||
(0, _getFieldValidator["default"])('an unknown type'); | ||
} catch (error) { | ||
expect(error.message).toMatch(/Field type does not exist/); | ||
} | ||
case 2: | ||
case "end": | ||
return _context10.stop(); | ||
} | ||
} | ||
}, _callee10); | ||
}))); | ||
try { | ||
(0, _getFieldValidator.default)('an unknown type'); | ||
} catch (error) { | ||
expect(error.message).toMatch(/Field type does not exist/); | ||
} | ||
}); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; | ||
@@ -16,126 +18,56 @@ var _isNil = _interopRequireDefault(require("lodash/isNil")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
async function _default(params) { | ||
const { | ||
schema, | ||
doc, | ||
currentDoc, | ||
value, | ||
currentSchema, | ||
keys, | ||
options = {}, | ||
args = [] | ||
} = params; | ||
const info = { | ||
schema, | ||
doc, | ||
currentDoc, | ||
keys, | ||
currentSchema, | ||
options | ||
}; | ||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | ||
if ((0, _isNil.default)(value)) { | ||
if (!currentSchema.optional && !options.omitRequired) { | ||
return _Errors.default.REQUIRED; | ||
} | ||
} else { | ||
const validatorKey = (0, _getFieldValidator.default)(currentSchema.type); | ||
const validator = validatorKey === 'custom' ? currentSchema.type : _fieldTypes.default[validatorKey]; | ||
const error = await validator.validate(value, info, ...args); | ||
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | ||
if (error) { | ||
return error; | ||
} | ||
} // to not deprecate yet custom field | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); } | ||
if (currentSchema.custom) currentSchema.validate = currentSchema.custom; | ||
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | ||
if (currentSchema.validate) { | ||
const customError = await currentSchema.validate(value, info, ...args); | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
if (customError) { | ||
return customError; | ||
} | ||
} | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
if (currentSchema.type.__validate) { | ||
const typeError = await currentSchema.type.__validate(value, info, ...args); | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
if (typeError) { | ||
return typeError; | ||
} | ||
} | ||
function _default(_x) { | ||
return _ref.apply(this, arguments); | ||
} | ||
function _ref() { | ||
_ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(params) { | ||
var schema, doc, currentDoc, value, currentSchema, keys, _params$options, options, _params$args, args, info, validatorKey, validator, error, customError, _currentSchema$type, typeError; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
schema = params.schema, doc = params.doc, currentDoc = params.currentDoc, value = params.value, currentSchema = params.currentSchema, keys = params.keys, _params$options = params.options, options = _params$options === void 0 ? {} : _params$options, _params$args = params.args, args = _params$args === void 0 ? [] : _params$args; | ||
info = { | ||
schema: schema, | ||
doc: doc, | ||
currentDoc: currentDoc, | ||
keys: keys, | ||
currentSchema: currentSchema, | ||
options: options | ||
}; | ||
if (!(0, _isNil["default"])(value)) { | ||
_context.next = 7; | ||
break; | ||
} | ||
if (!(!currentSchema.optional && !options.omitRequired)) { | ||
_context.next = 5; | ||
break; | ||
} | ||
return _context.abrupt("return", _Errors["default"].REQUIRED); | ||
case 5: | ||
_context.next = 14; | ||
break; | ||
case 7: | ||
validatorKey = (0, _getFieldValidator["default"])(currentSchema.type); | ||
validator = validatorKey === 'custom' ? currentSchema.type : _fieldTypes["default"][validatorKey]; | ||
_context.next = 11; | ||
return validator.validate.apply(validator, [value, info].concat(_toConsumableArray(args))); | ||
case 11: | ||
error = _context.sent; | ||
if (!error) { | ||
_context.next = 14; | ||
break; | ||
} | ||
return _context.abrupt("return", error); | ||
case 14: | ||
// to not deprecate yet custom field | ||
if (currentSchema.custom) currentSchema.validate = currentSchema.custom; | ||
if (!currentSchema.validate) { | ||
_context.next = 21; | ||
break; | ||
} | ||
_context.next = 18; | ||
return currentSchema.validate.apply(currentSchema, [value, info].concat(_toConsumableArray(args))); | ||
case 18: | ||
customError = _context.sent; | ||
if (!customError) { | ||
_context.next = 21; | ||
break; | ||
} | ||
return _context.abrupt("return", customError); | ||
case 21: | ||
if (!currentSchema.type.__validate) { | ||
_context.next = 27; | ||
break; | ||
} | ||
_context.next = 24; | ||
return (_currentSchema$type = currentSchema.type).__validate.apply(_currentSchema$type, [value, info].concat(_toConsumableArray(args))); | ||
case 24: | ||
typeError = _context.sent; | ||
if (!typeError) { | ||
_context.next = 27; | ||
break; | ||
} | ||
return _context.abrupt("return", typeError); | ||
case 27: | ||
return _context.abrupt("return", null); | ||
case 28: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
return _ref.apply(this, arguments); | ||
return null; | ||
} |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _index = _interopRequireDefault(require("./index")); | ||
@@ -9,169 +11,64 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
test('pass a simple string validation', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var error; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return (0, _index["default"])({ | ||
value: 'A valid string', | ||
currentSchema: { | ||
type: String | ||
} | ||
}); | ||
case 2: | ||
error = _context.sent; | ||
expect(error).toBeNull(); | ||
case 4: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
test('pass a simple string validation', async () => { | ||
const error = await (0, _index.default)({ | ||
value: 'A valid string', | ||
currentSchema: { | ||
type: String | ||
} | ||
}, _callee); | ||
}))); | ||
test('detect required field when value is null', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
var error; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_context2.next = 2; | ||
return (0, _index["default"])({ | ||
value: null, | ||
currentSchema: { | ||
type: String | ||
} | ||
}); | ||
case 2: | ||
error = _context2.sent; | ||
expect(error).toBe(_Errors["default"].REQUIRED); | ||
case 4: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
}); | ||
expect(error).toBeNull(); | ||
}); | ||
test('detect required field when value is null', async () => { | ||
const error = await (0, _index.default)({ | ||
value: null, | ||
currentSchema: { | ||
type: String | ||
} | ||
}, _callee2); | ||
}))); | ||
test('dont detect required field when value is null and omit required is passed', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() { | ||
var error; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
_context3.next = 2; | ||
return (0, _index["default"])({ | ||
options: { | ||
omitRequired: true | ||
}, | ||
value: null, | ||
currentSchema: { | ||
type: String | ||
} | ||
}); | ||
}); | ||
expect(error).toBe(_Errors.default.REQUIRED); | ||
}); | ||
test('dont detect required field when value is null and omit required is passed', async () => { | ||
const error = await (0, _index.default)({ | ||
options: { | ||
omitRequired: true | ||
}, | ||
value: null, | ||
currentSchema: { | ||
type: String | ||
} | ||
}); | ||
expect(error).toBeNull(); | ||
}); | ||
test('run custom validation if passed', async () => { | ||
const customType = (0, _fieldType.default)({ | ||
validate(value) { | ||
return 'hello'; | ||
} | ||
case 2: | ||
error = _context3.sent; | ||
expect(error).toBeNull(); | ||
case 4: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
}); | ||
const error = await (0, _index.default)({ | ||
value: 'A string', | ||
currentSchema: { | ||
type: customType | ||
} | ||
}, _callee3); | ||
}))); | ||
test('run custom validation if passed', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() { | ||
var customType, error; | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
customType = (0, _fieldType["default"])({ | ||
validate: function validate(value) { | ||
return 'hello'; | ||
} | ||
}); | ||
_context4.next = 3; | ||
return (0, _index["default"])({ | ||
value: 'A string', | ||
currentSchema: { | ||
type: customType | ||
} | ||
}); | ||
}); | ||
expect(error).toBe('hello'); | ||
}); | ||
test('run async custom validation', async () => { | ||
const customType = (0, _fieldType.default)({ | ||
async validate(value) { | ||
const sleep = time => new Promise(resolve => setTimeout(resolve, time)); | ||
case 3: | ||
error = _context4.sent; | ||
expect(error).toBe('hello'); | ||
case 5: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
await sleep(1); | ||
return 'hello'; | ||
} | ||
}, _callee4); | ||
}))); | ||
test('run async custom validation', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() { | ||
var customType, error; | ||
return regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
customType = (0, _fieldType["default"])({ | ||
validate: function validate(value) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5() { | ||
var sleep; | ||
return regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
sleep = function sleep(time) { | ||
return new Promise(function (resolve) { | ||
return setTimeout(resolve, time); | ||
}); | ||
}; | ||
_context5.next = 3; | ||
return sleep(1); | ||
case 3: | ||
return _context5.abrupt("return", 'hello'); | ||
case 4: | ||
case "end": | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee5); | ||
}))(); | ||
} | ||
}); | ||
_context6.next = 3; | ||
return (0, _index["default"])({ | ||
value: 'A string', | ||
currentSchema: { | ||
type: customType | ||
} | ||
}); | ||
case 3: | ||
error = _context6.sent; | ||
expect(error).toBe('hello'); | ||
case 5: | ||
case "end": | ||
return _context6.stop(); | ||
} | ||
}); | ||
const error = await (0, _index.default)({ | ||
value: 'A string', | ||
currentSchema: { | ||
type: customType | ||
} | ||
}, _callee6); | ||
}))); | ||
}); | ||
expect(error).toBe('hello'); | ||
}); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; | ||
var _dotObject = _interopRequireDefault(require("dot-object")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function _default(validationErrors) { | ||
if (validationErrors.length === 0) return null; | ||
var errors = {}; | ||
const errors = {}; | ||
var _iterator = _createForOfIteratorHelper(validationErrors), | ||
_step; | ||
try { | ||
for (_iterator.s(); !(_step = _iterator.n()).done;) { | ||
var validationError = _step.value; | ||
errors[validationError.key] = validationError.code; | ||
} | ||
} catch (err) { | ||
_iterator.e(err); | ||
} finally { | ||
_iterator.f(); | ||
for (const validationError of validationErrors) { | ||
errors[validationError.key] = validationError.code; | ||
} | ||
return _dotObject["default"].dot(errors); | ||
return _dotObject.default.dot(errors); | ||
} |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; | ||
@@ -12,77 +14,32 @@ var _doValidation = _interopRequireDefault(require("./doValidation")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
var defaultOptions = { | ||
const defaultOptions = { | ||
omitRequired: false | ||
}; | ||
function _default(_x, _x2) { | ||
return _ref.apply(this, arguments); | ||
} | ||
async function _default(schema, doc, passedOptions = {}, ...args) { | ||
const options = { ...defaultOptions, | ||
...passedOptions | ||
}; | ||
const errors = []; | ||
function _ref() { | ||
_ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(schema, doc) { | ||
var passedOptions, | ||
options, | ||
errors, | ||
addError, | ||
_len, | ||
args, | ||
_key, | ||
_args = arguments; | ||
const addError = function (keys, code) { | ||
errors.push({ | ||
key: keys.join('.'), | ||
code | ||
}); | ||
}; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
passedOptions = _args.length > 2 && _args[2] !== undefined ? _args[2] : {}; | ||
options = _objectSpread(_objectSpread({}, defaultOptions), passedOptions); | ||
errors = []; | ||
addError = function addError(keys, code) { | ||
errors.push({ | ||
key: keys.join('.'), | ||
code: code | ||
}); | ||
}; | ||
for (_len = _args.length, args = new Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) { | ||
args[_key - 3] = _args[_key]; | ||
} | ||
_context.next = 7; | ||
return (0, _doValidation["default"])({ | ||
schema: schema, | ||
doc: doc, | ||
currentDoc: doc, | ||
value: doc, | ||
currentSchema: { | ||
type: schema | ||
}, | ||
addError: addError, | ||
options: options, | ||
args: args | ||
}); | ||
case 7: | ||
return _context.abrupt("return", (0, _getValidationErrorsObject["default"])(errors)); | ||
case 8: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
return _ref.apply(this, arguments); | ||
await (0, _doValidation.default)({ | ||
schema, | ||
doc, | ||
currentDoc: doc, | ||
value: doc, | ||
currentSchema: { | ||
type: schema | ||
}, | ||
addError, | ||
options, | ||
args | ||
}); | ||
return (0, _getValidationErrorsObject.default)(errors); | ||
} |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _index = _interopRequireDefault(require("./index")); | ||
@@ -7,9 +9,3 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
var friend = { | ||
const friend = { | ||
firstName: { | ||
@@ -19,3 +15,3 @@ type: String | ||
}; | ||
var car = { | ||
const car = { | ||
brand: { | ||
@@ -28,3 +24,3 @@ type: String | ||
}; | ||
var schema = { | ||
const schema = { | ||
firstName: { | ||
@@ -45,896 +41,469 @@ type: String | ||
}; | ||
test('returns null when object is valid', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var errors; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return (0, _index["default"])(schema, { | ||
firstName: 'Nicolás', | ||
lastName: 'López', | ||
friends: [{ | ||
firstName: 'Roberto' | ||
}, { | ||
firstName: 'Joaquin' | ||
}], | ||
car: { | ||
brand: 'Nissan', | ||
model: 'Kicks' | ||
} | ||
}); | ||
case 2: | ||
errors = _context.sent; | ||
expect(errors).toBeNull(); | ||
case 4: | ||
case "end": | ||
return _context.stop(); | ||
test('returns null when object is valid', async () => { | ||
const errors = await (0, _index.default)(schema, { | ||
firstName: 'Nicolás', | ||
lastName: 'López', | ||
friends: [{ | ||
firstName: 'Roberto' | ||
}, { | ||
firstName: 'Joaquin' | ||
}], | ||
car: { | ||
brand: 'Nissan', | ||
model: 'Kicks' | ||
} | ||
}); | ||
expect(errors).toBeNull(); | ||
}); | ||
test('returns an array with the respective errors', async () => { | ||
const errors = await (0, _index.default)(schema, { | ||
lastName: 'López', | ||
friends: [{ | ||
lastName: 'Zibert' | ||
}, { | ||
firstName: 'Joaquin' | ||
}] | ||
}); | ||
expect(errors).toEqual({ | ||
firstName: _Errors.default.REQUIRED, | ||
'friends.0.firstName': _Errors.default.REQUIRED, | ||
'friends.0.lastName': _Errors.default.NOT_IN_SCHEMA | ||
}); | ||
}); | ||
test('omits required if specified', async () => { | ||
const errors = await (0, _index.default)(schema, { | ||
lastName: 'López', | ||
friends: [{}, { | ||
firstName: 'Joaquin' | ||
}] | ||
}, { | ||
omitRequired: true | ||
}); | ||
expect(errors).toBeNull(); | ||
}); | ||
test('gives error when a document field is not present in schema', async () => { | ||
const errors = await (0, _index.default)({ | ||
name: { | ||
type: String | ||
} | ||
}, { | ||
name: 'Nicolás López', | ||
age: 25 | ||
}); | ||
expect(errors).toEqual({ | ||
age: _Errors.default.NOT_IN_SCHEMA | ||
}); | ||
}); | ||
test('dont give error when array is optional and its not passed', async () => { | ||
const schema = { | ||
name: { | ||
type: String | ||
}, | ||
tags: { | ||
type: [String], | ||
optional: true | ||
} | ||
}; | ||
const errors = await (0, _index.default)(schema, { | ||
name: 'Nicolás López', | ||
tags: [] | ||
}); | ||
expect(errors).toBeNull(); | ||
}); | ||
test('gives an error if a optional object is present and has missing values', async () => { | ||
const errors = await (0, _index.default)(schema, { | ||
firstName: 'Nicolás', | ||
lastName: 'López', | ||
friends: [{ | ||
firstName: 'Joaquin' | ||
}], | ||
car: { | ||
brand: 'Nissan' | ||
} | ||
}); | ||
expect(errors).toEqual({ | ||
'car.model': _Errors.default.REQUIRED | ||
}); | ||
}); | ||
test('can check errors in deeply nested keys', async () => { | ||
const children = { | ||
name: { | ||
type: String | ||
}, | ||
car: { | ||
type: car, | ||
optional: true | ||
} | ||
}; | ||
const mother = { | ||
name: { | ||
type: String | ||
}, | ||
children: { | ||
type: [children] | ||
}, | ||
car: { | ||
type: car | ||
} | ||
}; | ||
const family = { | ||
name: { | ||
type: String | ||
}, | ||
mother: { | ||
type: mother | ||
} | ||
}; | ||
const deepSchema = { | ||
name: { | ||
type: String | ||
}, | ||
family: { | ||
type: family | ||
} | ||
}; | ||
const errors = await (0, _index.default)(deepSchema, { | ||
name: 'Nicolás', | ||
family: { | ||
name: 'López', | ||
mother: { | ||
name: 'Paula', | ||
car: { | ||
model: 'Wrangler' | ||
}, | ||
children: [{ | ||
name: 'Paula', | ||
car: { | ||
brand: 'Citroën' | ||
} | ||
}, { | ||
name: 'Diego', | ||
age: 21 | ||
}] | ||
} | ||
} | ||
}, _callee); | ||
}))); | ||
test('returns an array with the respective errors', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
var errors; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_context2.next = 2; | ||
return (0, _index["default"])(schema, { | ||
lastName: 'López', | ||
friends: [{ | ||
lastName: 'Zibert' | ||
}, { | ||
firstName: 'Joaquin' | ||
}] | ||
}); | ||
}); | ||
expect(errors).toEqual({ | ||
'family.mother.car.brand': _Errors.default.REQUIRED, | ||
'family.mother.children.0.car.model': _Errors.default.REQUIRED, | ||
'family.mother.children.1.age': _Errors.default.NOT_IN_SCHEMA | ||
}); | ||
}); | ||
test('run validate validation when field is optional and no value is passed', async () => { | ||
const person = { | ||
name: { | ||
type: String, | ||
optional: true, | ||
case 2: | ||
errors = _context2.sent; | ||
expect(errors).toEqual({ | ||
firstName: _Errors["default"].REQUIRED, | ||
'friends.0.firstName': _Errors["default"].REQUIRED, | ||
'friends.0.lastName': _Errors["default"].NOT_IN_SCHEMA | ||
}); | ||
case 4: | ||
case "end": | ||
return _context2.stop(); | ||
validate(value) { | ||
return 'No'; | ||
} | ||
} | ||
}, _callee2); | ||
}))); | ||
test('omits required if specified', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() { | ||
var errors; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
_context3.next = 2; | ||
return (0, _index["default"])(schema, { | ||
lastName: 'López', | ||
friends: [{}, { | ||
firstName: 'Joaquin' | ||
}] | ||
}, { | ||
omitRequired: true | ||
}); | ||
case 2: | ||
errors = _context3.sent; | ||
expect(errors).toBeNull(); | ||
}, | ||
case 4: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
async __validate(value) { | ||
if (!value) return 'No object'; | ||
} | ||
}, _callee3); | ||
}))); | ||
test('gives error when a document field is not present in schema', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() { | ||
var errors; | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
_context4.next = 2; | ||
return (0, _index["default"])({ | ||
name: { | ||
type: String | ||
} | ||
}, { | ||
name: 'Nicolás López', | ||
age: 25 | ||
}); | ||
case 2: | ||
errors = _context4.sent; | ||
expect(errors).toEqual({ | ||
age: _Errors["default"].NOT_IN_SCHEMA | ||
}); | ||
}; | ||
const schema = { | ||
person: { | ||
type: person, | ||
optional: true | ||
}, | ||
number: { | ||
type: Number, | ||
case 4: | ||
case "end": | ||
return _context4.stop(); | ||
validate() { | ||
return 'No'; | ||
} | ||
} | ||
}, _callee4); | ||
}))); | ||
test('dont give error when array is optional and its not passed', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5() { | ||
var schema, errors; | ||
return regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
schema = { | ||
name: { | ||
type: String | ||
}, | ||
tags: { | ||
type: [String], | ||
optional: true | ||
} | ||
}; | ||
_context5.next = 3; | ||
return (0, _index["default"])(schema, { | ||
name: 'Nicolás López', | ||
tags: [] | ||
}); | ||
}; | ||
expect(await (0, _index.default)(schema, { | ||
person: { | ||
name: null | ||
} | ||
})).toEqual({ | ||
number: _Errors.default.REQUIRED, | ||
'person.name': 'No' | ||
}); | ||
expect(await (0, _index.default)(schema, { | ||
person: null, | ||
number: null | ||
})).toEqual({ | ||
number: _Errors.default.REQUIRED, | ||
person: 'No object' | ||
}); | ||
expect(await (0, _index.default)(schema, { | ||
number: 123 | ||
})).toEqual({ | ||
number: 'No', | ||
person: 'No object' | ||
}); | ||
}); | ||
test('can validate object type with validate validation', async () => { | ||
const person = { | ||
name: { | ||
type: String | ||
}, | ||
case 3: | ||
errors = _context5.sent; | ||
expect(errors).toBeNull(); | ||
async __validate(value) { | ||
return value.name === 'Nicolás' ? null : 'no'; | ||
} | ||
case 5: | ||
case "end": | ||
return _context5.stop(); | ||
} | ||
}; | ||
const schema = { | ||
person: { | ||
type: person | ||
} | ||
}, _callee5); | ||
}))); | ||
test('gives an error if a optional object is present and has missing values', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() { | ||
var errors; | ||
return regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
_context6.next = 2; | ||
return (0, _index["default"])(schema, { | ||
firstName: 'Nicolás', | ||
lastName: 'López', | ||
friends: [{ | ||
firstName: 'Joaquin' | ||
}], | ||
car: { | ||
brand: 'Nissan' | ||
} | ||
}); | ||
}; | ||
expect(await (0, _index.default)(schema, { | ||
person: { | ||
name: 'Nicolás' | ||
} | ||
})).toBeNull(); | ||
expect(await (0, _index.default)(schema, { | ||
person: { | ||
name: 'Joaquin' | ||
} | ||
})).toEqual({ | ||
person: 'no' | ||
}); | ||
}); | ||
test('skip child validation if specified', async () => { | ||
const person = { | ||
firstName: { | ||
type: String | ||
}, | ||
lastName: { | ||
type: String | ||
}, | ||
case 2: | ||
errors = _context6.sent; | ||
expect(errors).toEqual({ | ||
'car.model': _Errors["default"].REQUIRED | ||
}); | ||
async __skipChildValidation(value) { | ||
return value.firstName === 'Nicolás'; | ||
} | ||
case 4: | ||
case "end": | ||
return _context6.stop(); | ||
} | ||
}; | ||
const schema = { | ||
persons: { | ||
type: [person] | ||
} | ||
}, _callee6); | ||
}))); | ||
test('can check errors in deeply nested keys', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7() { | ||
var children, mother, family, deepSchema, errors; | ||
return regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
case 0: | ||
children = { | ||
name: { | ||
type: String | ||
}, | ||
car: { | ||
type: car, | ||
optional: true | ||
} | ||
}; | ||
mother = { | ||
name: { | ||
type: String | ||
}, | ||
children: { | ||
type: [children] | ||
}, | ||
car: { | ||
type: car | ||
} | ||
}; | ||
family = { | ||
name: { | ||
type: String | ||
}, | ||
mother: { | ||
type: mother | ||
} | ||
}; | ||
deepSchema = { | ||
name: { | ||
type: String | ||
}, | ||
family: { | ||
type: family | ||
} | ||
}; | ||
_context7.next = 6; | ||
return (0, _index["default"])(deepSchema, { | ||
name: 'Nicolás', | ||
family: { | ||
name: 'López', | ||
mother: { | ||
name: 'Paula', | ||
car: { | ||
model: 'Wrangler' | ||
}, | ||
children: [{ | ||
name: 'Paula', | ||
car: { | ||
brand: 'Citroën' | ||
} | ||
}, { | ||
name: 'Diego', | ||
age: 21 | ||
}] | ||
} | ||
} | ||
}); | ||
}; | ||
expect(await (0, _index.default)(schema, { | ||
persons: [{ | ||
firstName: 'Nicolás' | ||
}] | ||
})).toBeNull(); | ||
const errors = await (0, _index.default)(schema, { | ||
persons: [{ | ||
firstName: 'Joaquin' | ||
}] | ||
}); | ||
expect(errors).toEqual({ | ||
'persons.0.lastName': _Errors.default.REQUIRED | ||
}); | ||
}); | ||
test('run custom validation with custom key', async () => { | ||
const schema = { | ||
name: { | ||
type: String, | ||
case 6: | ||
errors = _context7.sent; | ||
expect(errors).toEqual({ | ||
'family.mother.car.brand': _Errors["default"].REQUIRED, | ||
'family.mother.children.0.car.model': _Errors["default"].REQUIRED, | ||
'family.mother.children.1.age': _Errors["default"].NOT_IN_SCHEMA | ||
}); | ||
async custom(value) { | ||
return value; | ||
} | ||
case 8: | ||
case "end": | ||
return _context7.stop(); | ||
} | ||
} | ||
}, _callee7); | ||
}))); | ||
test('run validate validation when field is optional and no value is passed', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee9() { | ||
var person, schema; | ||
return regeneratorRuntime.wrap(function _callee9$(_context9) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
person = { | ||
name: { | ||
type: String, | ||
optional: true, | ||
validate: function validate(value) { | ||
return 'No'; | ||
} | ||
}, | ||
__validate: function __validate(value) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee8() { | ||
return regeneratorRuntime.wrap(function _callee8$(_context8) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
if (value) { | ||
_context8.next = 2; | ||
break; | ||
} | ||
}; | ||
const errors = await (0, _index.default)(schema, { | ||
name: 'error' | ||
}); | ||
expect(errors).toEqual({ | ||
name: 'error' | ||
}); | ||
}); | ||
test('allow custom validation to pass an error object', async () => { | ||
const schema = { | ||
person: { | ||
type: 'blackbox', | ||
return _context8.abrupt("return", 'No object'); | ||
async validate(person) { | ||
return { | ||
name: 'required', | ||
lastName: 'tooShort' | ||
}; | ||
} | ||
case 2: | ||
case "end": | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _callee8); | ||
}))(); | ||
} | ||
}; | ||
schema = { | ||
person: { | ||
type: person, | ||
optional: true | ||
}, | ||
number: { | ||
type: Number, | ||
validate: function validate() { | ||
return 'No'; | ||
} | ||
} | ||
}; | ||
_context9.t0 = expect; | ||
_context9.next = 5; | ||
return (0, _index["default"])(schema, { | ||
person: { | ||
name: null | ||
} | ||
}); | ||
} | ||
}; | ||
const errors = await (0, _index.default)(schema, { | ||
person: { | ||
lastName: 'López' | ||
} | ||
}); | ||
expect(errors).toEqual({ | ||
'person.name': 'required', | ||
'person.lastName': 'tooShort' | ||
}); | ||
}); | ||
test('allow custom validation to pass an complex error object', async () => { | ||
const schema = { | ||
person: { | ||
type: 'blackbox', | ||
case 5: | ||
_context9.t1 = _context9.sent; | ||
(0, _context9.t0)(_context9.t1).toEqual({ | ||
number: _Errors["default"].REQUIRED, | ||
'person.name': 'No' | ||
}); | ||
_context9.t2 = expect; | ||
_context9.next = 10; | ||
return (0, _index["default"])(schema, { | ||
person: null, | ||
number: null | ||
}); | ||
async validate(person) { | ||
return { | ||
name: 'required', | ||
'car.name': 'required', | ||
friends: [{ | ||
name: 'required' | ||
}] | ||
}; | ||
} | ||
case 10: | ||
_context9.t3 = _context9.sent; | ||
(0, _context9.t2)(_context9.t3).toEqual({ | ||
number: _Errors["default"].REQUIRED, | ||
person: 'No object' | ||
}); | ||
_context9.t4 = expect; | ||
_context9.next = 15; | ||
return (0, _index["default"])(schema, { | ||
number: 123 | ||
}); | ||
} | ||
}; | ||
const errors = await (0, _index.default)(schema, { | ||
person: { | ||
lastName: 'López' | ||
} | ||
}); | ||
expect(errors).toEqual({ | ||
'person.name': 'required', | ||
'person.car.name': 'required', | ||
'person.friends.0.name': 'required' | ||
}); | ||
}); | ||
test('pass currentDoc validating arrays', async () => { | ||
const aItem = { | ||
name: 'Nicolás' | ||
}; | ||
const doc = { | ||
items: [aItem] | ||
}; | ||
const item = { | ||
name: { | ||
type: String, | ||
case 15: | ||
_context9.t5 = _context9.sent; | ||
(0, _context9.t4)(_context9.t5).toEqual({ | ||
number: 'No', | ||
person: 'No object' | ||
}); | ||
async validate(name, { | ||
currentDoc | ||
}) { | ||
expect(currentDoc).toBe(aItem); | ||
} | ||
case 17: | ||
case "end": | ||
return _context9.stop(); | ||
} | ||
} | ||
}, _callee9); | ||
}))); | ||
test('can validate object type with validate validation', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee11() { | ||
var person, schema; | ||
return regeneratorRuntime.wrap(function _callee11$(_context11) { | ||
while (1) { | ||
switch (_context11.prev = _context11.next) { | ||
case 0: | ||
person = { | ||
name: { | ||
type: String | ||
}, | ||
__validate: function __validate(value) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee10() { | ||
return regeneratorRuntime.wrap(function _callee10$(_context10) { | ||
while (1) { | ||
switch (_context10.prev = _context10.next) { | ||
case 0: | ||
return _context10.abrupt("return", value.name === 'Nicolás' ? null : 'no'); | ||
}; | ||
const schema = { | ||
items: { | ||
type: [item], | ||
case 1: | ||
case "end": | ||
return _context10.stop(); | ||
} | ||
} | ||
}, _callee10); | ||
}))(); | ||
} | ||
}; | ||
schema = { | ||
person: { | ||
type: person | ||
} | ||
}; | ||
_context11.t0 = expect; | ||
_context11.next = 5; | ||
return (0, _index["default"])(schema, { | ||
person: { | ||
name: 'Nicolás' | ||
} | ||
}); | ||
async validate(items, { | ||
currentDoc | ||
}) { | ||
expect(currentDoc).toBe(doc); | ||
} | ||
case 5: | ||
_context11.t1 = _context11.sent; | ||
(0, _context11.t0)(_context11.t1).toBeNull(); | ||
_context11.t2 = expect; | ||
_context11.next = 10; | ||
return (0, _index["default"])(schema, { | ||
person: { | ||
name: 'Joaquin' | ||
} | ||
}); | ||
} | ||
}; | ||
expect.assertions(2); | ||
await (0, _index.default)(schema, doc); | ||
}); | ||
test('pass currentDoc validating complex schemas', async () => { | ||
const aCar = { | ||
brand: 'Jeep' | ||
}; | ||
const aMom = { | ||
name: 'Paula', | ||
car: aCar | ||
}; | ||
const aItem = { | ||
name: 'Nicolás', | ||
mom: aMom | ||
}; | ||
const doc = { | ||
items: [aItem] | ||
}; | ||
const car = { | ||
brand: { | ||
type: String, | ||
case 10: | ||
_context11.t3 = _context11.sent; | ||
(0, _context11.t2)(_context11.t3).toEqual({ | ||
person: 'no' | ||
}); | ||
case 12: | ||
case "end": | ||
return _context11.stop(); | ||
async validate(value, { | ||
currentDoc | ||
}) { | ||
expect(value).toEqual(aCar.brand); | ||
expect(currentDoc).toEqual(aCar); | ||
} | ||
} | ||
}, _callee11); | ||
}))); | ||
test('skip child validation if specified', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee13() { | ||
var person, schema, errors; | ||
return regeneratorRuntime.wrap(function _callee13$(_context13) { | ||
while (1) { | ||
switch (_context13.prev = _context13.next) { | ||
case 0: | ||
person = { | ||
firstName: { | ||
type: String | ||
}, | ||
lastName: { | ||
type: String | ||
}, | ||
__skipChildValidation: function __skipChildValidation(value) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee12() { | ||
return regeneratorRuntime.wrap(function _callee12$(_context12) { | ||
while (1) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
return _context12.abrupt("return", value.firstName === 'Nicolás'); | ||
case 1: | ||
case "end": | ||
return _context12.stop(); | ||
} | ||
} | ||
}, _callee12); | ||
}))(); | ||
} | ||
}; | ||
schema = { | ||
persons: { | ||
type: [person] | ||
} | ||
}; | ||
_context13.t0 = expect; | ||
_context13.next = 5; | ||
return (0, _index["default"])(schema, { | ||
persons: [{ | ||
firstName: 'Nicolás' | ||
}] | ||
}); | ||
}, | ||
case 5: | ||
_context13.t1 = _context13.sent; | ||
(0, _context13.t0)(_context13.t1).toBeNull(); | ||
_context13.next = 9; | ||
return (0, _index["default"])(schema, { | ||
persons: [{ | ||
firstName: 'Joaquin' | ||
}] | ||
}); | ||
async __validate(value, info) { | ||
expect(value).toEqual(aMom.car); | ||
expect(info.currentDoc).toEqual(aMom); | ||
} | ||
case 9: | ||
errors = _context13.sent; | ||
expect(errors).toEqual({ | ||
'persons.0.lastName': _Errors["default"].REQUIRED | ||
}); | ||
}; | ||
const mom = { | ||
name: { | ||
type: String, | ||
case 11: | ||
case "end": | ||
return _context13.stop(); | ||
async validate(value, { | ||
currentDoc | ||
}) { | ||
expect(value).toEqual(aMom.name); | ||
expect(currentDoc).toEqual(aMom); | ||
} | ||
} | ||
}, _callee13); | ||
}))); | ||
test('run custom validation with custom key', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee15() { | ||
var schema, errors; | ||
return regeneratorRuntime.wrap(function _callee15$(_context15) { | ||
while (1) { | ||
switch (_context15.prev = _context15.next) { | ||
case 0: | ||
schema = { | ||
name: { | ||
type: String, | ||
custom: function custom(value) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee14() { | ||
return regeneratorRuntime.wrap(function _callee14$(_context14) { | ||
while (1) { | ||
switch (_context14.prev = _context14.next) { | ||
case 0: | ||
return _context14.abrupt("return", value); | ||
case 1: | ||
case "end": | ||
return _context14.stop(); | ||
} | ||
} | ||
}, _callee14); | ||
}))(); | ||
} | ||
} | ||
}; | ||
_context15.next = 3; | ||
return (0, _index["default"])(schema, { | ||
name: 'error' | ||
}); | ||
}, | ||
car: { | ||
type: car, | ||
case 3: | ||
errors = _context15.sent; | ||
expect(errors).toEqual({ | ||
name: 'error' | ||
}); | ||
async validate(value, { | ||
currentDoc, | ||
doc | ||
}) { | ||
expect(value).toEqual(aMom.car); | ||
expect(currentDoc).toEqual(aMom); | ||
} | ||
case 5: | ||
case "end": | ||
return _context15.stop(); | ||
} | ||
} | ||
}, _callee15); | ||
}))); | ||
test('allow custom validation to pass an error object', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee17() { | ||
var schema, errors; | ||
return regeneratorRuntime.wrap(function _callee17$(_context17) { | ||
while (1) { | ||
switch (_context17.prev = _context17.next) { | ||
case 0: | ||
schema = { | ||
person: { | ||
type: 'blackbox', | ||
validate: function validate(person) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee16() { | ||
return regeneratorRuntime.wrap(function _callee16$(_context16) { | ||
while (1) { | ||
switch (_context16.prev = _context16.next) { | ||
case 0: | ||
return _context16.abrupt("return", { | ||
name: 'required', | ||
lastName: 'tooShort' | ||
}); | ||
}; | ||
const item = { | ||
name: { | ||
type: String, | ||
case 1: | ||
case "end": | ||
return _context16.stop(); | ||
} | ||
} | ||
}, _callee16); | ||
}))(); | ||
} | ||
} | ||
}; | ||
_context17.next = 3; | ||
return (0, _index["default"])(schema, { | ||
person: { | ||
lastName: 'López' | ||
} | ||
}); | ||
async validate(value, { | ||
currentDoc | ||
}) { | ||
expect(value).toEqual(aItem.name); | ||
expect(currentDoc).toEqual(aItem); | ||
} | ||
case 3: | ||
errors = _context17.sent; | ||
expect(errors).toEqual({ | ||
'person.name': 'required', | ||
'person.lastName': 'tooShort' | ||
}); | ||
}, | ||
mom: { | ||
type: mom, | ||
case 5: | ||
case "end": | ||
return _context17.stop(); | ||
async validate(value, { | ||
currentDoc | ||
}) { | ||
expect(value).toEqual(aItem.mom); | ||
expect(currentDoc).toEqual(aItem); | ||
} | ||
} | ||
}, _callee17); | ||
}))); | ||
test('allow custom validation to pass an complex error object', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee19() { | ||
var schema, errors; | ||
return regeneratorRuntime.wrap(function _callee19$(_context19) { | ||
while (1) { | ||
switch (_context19.prev = _context19.next) { | ||
case 0: | ||
schema = { | ||
person: { | ||
type: 'blackbox', | ||
validate: function validate(person) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee18() { | ||
return regeneratorRuntime.wrap(function _callee18$(_context18) { | ||
while (1) { | ||
switch (_context18.prev = _context18.next) { | ||
case 0: | ||
return _context18.abrupt("return", { | ||
name: 'required', | ||
'car.name': 'required', | ||
friends: [{ | ||
name: 'required' | ||
}] | ||
}); | ||
case 1: | ||
case "end": | ||
return _context18.stop(); | ||
} | ||
} | ||
}, _callee18); | ||
}))(); | ||
} | ||
} | ||
}; | ||
_context19.next = 3; | ||
return (0, _index["default"])(schema, { | ||
person: { | ||
lastName: 'López' | ||
} | ||
}); | ||
case 3: | ||
errors = _context19.sent; | ||
expect(errors).toEqual({ | ||
'person.name': 'required', | ||
'person.car.name': 'required', | ||
'person.friends.0.name': 'required' | ||
}); | ||
case 5: | ||
case "end": | ||
return _context19.stop(); | ||
} | ||
} | ||
}, _callee19); | ||
}))); | ||
test('pass currentDoc validating arrays', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee22() { | ||
var aItem, doc, item, schema; | ||
return regeneratorRuntime.wrap(function _callee22$(_context22) { | ||
while (1) { | ||
switch (_context22.prev = _context22.next) { | ||
case 0: | ||
aItem = { | ||
name: 'Nicolás' | ||
}; | ||
doc = { | ||
items: [aItem] | ||
}; | ||
item = { | ||
name: { | ||
type: String, | ||
validate: function validate(name, _ref15) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee20() { | ||
var currentDoc; | ||
return regeneratorRuntime.wrap(function _callee20$(_context20) { | ||
while (1) { | ||
switch (_context20.prev = _context20.next) { | ||
case 0: | ||
currentDoc = _ref15.currentDoc; | ||
expect(currentDoc).toBe(aItem); | ||
}; | ||
const schema = { | ||
items: { | ||
type: [item], | ||
case 2: | ||
case "end": | ||
return _context20.stop(); | ||
} | ||
} | ||
}, _callee20); | ||
}))(); | ||
} | ||
} | ||
}; | ||
schema = { | ||
items: { | ||
type: [item], | ||
validate: function validate(items, _ref16) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee21() { | ||
var currentDoc; | ||
return regeneratorRuntime.wrap(function _callee21$(_context21) { | ||
while (1) { | ||
switch (_context21.prev = _context21.next) { | ||
case 0: | ||
currentDoc = _ref16.currentDoc; | ||
expect(currentDoc).toBe(doc); | ||
case 2: | ||
case "end": | ||
return _context21.stop(); | ||
} | ||
} | ||
}, _callee21); | ||
}))(); | ||
} | ||
} | ||
}; | ||
expect.assertions(2); | ||
_context22.next = 7; | ||
return (0, _index["default"])(schema, doc); | ||
case 7: | ||
case "end": | ||
return _context22.stop(); | ||
async validate(value, { | ||
currentDoc | ||
}) { | ||
expect(value).toEqual(doc.items); | ||
expect(currentDoc).toEqual(doc); | ||
} | ||
} | ||
}, _callee22); | ||
}))); | ||
test('pass currentDoc validating complex schemas', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee30() { | ||
var aCar, aMom, aItem, doc, car, mom, item, schema; | ||
return regeneratorRuntime.wrap(function _callee30$(_context30) { | ||
while (1) { | ||
switch (_context30.prev = _context30.next) { | ||
case 0: | ||
aCar = { | ||
brand: 'Jeep' | ||
}; | ||
aMom = { | ||
name: 'Paula', | ||
car: aCar | ||
}; | ||
aItem = { | ||
name: 'Nicolás', | ||
mom: aMom | ||
}; | ||
doc = { | ||
items: [aItem] | ||
}; | ||
car = { | ||
brand: { | ||
type: String, | ||
validate: function validate(value, _ref18) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee23() { | ||
var currentDoc; | ||
return regeneratorRuntime.wrap(function _callee23$(_context23) { | ||
while (1) { | ||
switch (_context23.prev = _context23.next) { | ||
case 0: | ||
currentDoc = _ref18.currentDoc; | ||
expect(value).toEqual(aCar.brand); | ||
expect(currentDoc).toEqual(aCar); | ||
case 3: | ||
case "end": | ||
return _context23.stop(); | ||
} | ||
} | ||
}, _callee23); | ||
}))(); | ||
} | ||
}, | ||
__validate: function __validate(value, info) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee24() { | ||
return regeneratorRuntime.wrap(function _callee24$(_context24) { | ||
while (1) { | ||
switch (_context24.prev = _context24.next) { | ||
case 0: | ||
expect(value).toEqual(aMom.car); | ||
expect(info.currentDoc).toEqual(aMom); | ||
case 2: | ||
case "end": | ||
return _context24.stop(); | ||
} | ||
} | ||
}, _callee24); | ||
}))(); | ||
} | ||
}; | ||
mom = { | ||
name: { | ||
type: String, | ||
validate: function validate(value, _ref19) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee25() { | ||
var currentDoc; | ||
return regeneratorRuntime.wrap(function _callee25$(_context25) { | ||
while (1) { | ||
switch (_context25.prev = _context25.next) { | ||
case 0: | ||
currentDoc = _ref19.currentDoc; | ||
expect(value).toEqual(aMom.name); | ||
expect(currentDoc).toEqual(aMom); | ||
case 3: | ||
case "end": | ||
return _context25.stop(); | ||
} | ||
} | ||
}, _callee25); | ||
}))(); | ||
} | ||
}, | ||
car: { | ||
type: car, | ||
validate: function validate(value, _ref20) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee26() { | ||
var currentDoc, doc; | ||
return regeneratorRuntime.wrap(function _callee26$(_context26) { | ||
while (1) { | ||
switch (_context26.prev = _context26.next) { | ||
case 0: | ||
currentDoc = _ref20.currentDoc, doc = _ref20.doc; | ||
expect(value).toEqual(aMom.car); | ||
expect(currentDoc).toEqual(aMom); | ||
case 3: | ||
case "end": | ||
return _context26.stop(); | ||
} | ||
} | ||
}, _callee26); | ||
}))(); | ||
} | ||
} | ||
}; | ||
item = { | ||
name: { | ||
type: String, | ||
validate: function validate(value, _ref21) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee27() { | ||
var currentDoc; | ||
return regeneratorRuntime.wrap(function _callee27$(_context27) { | ||
while (1) { | ||
switch (_context27.prev = _context27.next) { | ||
case 0: | ||
currentDoc = _ref21.currentDoc; | ||
expect(value).toEqual(aItem.name); | ||
expect(currentDoc).toEqual(aItem); | ||
case 3: | ||
case "end": | ||
return _context27.stop(); | ||
} | ||
} | ||
}, _callee27); | ||
}))(); | ||
} | ||
}, | ||
mom: { | ||
type: mom, | ||
validate: function validate(value, _ref22) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee28() { | ||
var currentDoc; | ||
return regeneratorRuntime.wrap(function _callee28$(_context28) { | ||
while (1) { | ||
switch (_context28.prev = _context28.next) { | ||
case 0: | ||
currentDoc = _ref22.currentDoc; | ||
expect(value).toEqual(aItem.mom); | ||
expect(currentDoc).toEqual(aItem); | ||
case 3: | ||
case "end": | ||
return _context28.stop(); | ||
} | ||
} | ||
}, _callee28); | ||
}))(); | ||
} | ||
} | ||
}; | ||
schema = { | ||
items: { | ||
type: [item], | ||
validate: function validate(value, _ref23) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee29() { | ||
var currentDoc; | ||
return regeneratorRuntime.wrap(function _callee29$(_context29) { | ||
while (1) { | ||
switch (_context29.prev = _context29.next) { | ||
case 0: | ||
currentDoc = _ref23.currentDoc; | ||
expect(value).toEqual(doc.items); | ||
expect(currentDoc).toEqual(doc); | ||
case 3: | ||
case "end": | ||
return _context29.stop(); | ||
} | ||
} | ||
}, _callee29); | ||
}))(); | ||
} | ||
} | ||
}; | ||
expect.assertions(14); | ||
_context30.next = 11; | ||
return (0, _index["default"])(schema, doc); | ||
case 11: | ||
case "end": | ||
return _context30.stop(); | ||
} | ||
} | ||
}, _callee30); | ||
}))); | ||
}; | ||
expect.assertions(14); | ||
await (0, _index.default)(schema, doc); | ||
}); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -8,4 +10,4 @@ value: true | ||
enumerable: true, | ||
get: function get() { | ||
return _validate["default"]; | ||
get: function () { | ||
return _validate.default; | ||
} | ||
@@ -15,4 +17,4 @@ }); | ||
enumerable: true, | ||
get: function get() { | ||
return _ValidationError["default"]; | ||
get: function () { | ||
return _ValidationError.default; | ||
} | ||
@@ -22,4 +24,4 @@ }); | ||
enumerable: true, | ||
get: function get() { | ||
return _getValidationErrors["default"]; | ||
get: function () { | ||
return _getValidationErrors.default; | ||
} | ||
@@ -29,4 +31,4 @@ }); | ||
enumerable: true, | ||
get: function get() { | ||
return _isValid["default"]; | ||
get: function () { | ||
return _isValid.default; | ||
} | ||
@@ -36,4 +38,4 @@ }); | ||
enumerable: true, | ||
get: function get() { | ||
return _getFieldType["default"]; | ||
get: function () { | ||
return _getFieldType.default; | ||
} | ||
@@ -43,4 +45,4 @@ }); | ||
enumerable: true, | ||
get: function get() { | ||
return _fieldType["default"]; | ||
get: function () { | ||
return _fieldType.default; | ||
} | ||
@@ -50,4 +52,4 @@ }); | ||
enumerable: true, | ||
get: function get() { | ||
return _clean["default"]; | ||
get: function () { | ||
return _clean.default; | ||
} | ||
@@ -57,4 +59,4 @@ }); | ||
enumerable: true, | ||
get: function get() { | ||
return _cleanKey["default"]; | ||
get: function () { | ||
return _cleanKey.default; | ||
} | ||
@@ -64,4 +66,4 @@ }); | ||
enumerable: true, | ||
get: function get() { | ||
return _validateKey["default"]; | ||
get: function () { | ||
return _validateKey.default; | ||
} | ||
@@ -71,4 +73,4 @@ }); | ||
enumerable: true, | ||
get: function get() { | ||
return _dotGetSchema["default"]; | ||
get: function () { | ||
return _dotGetSchema.default; | ||
} | ||
@@ -95,4 +97,2 @@ }); | ||
var _dotGetSchema = _interopRequireDefault(require("./dotGetSchema")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var _dotGetSchema = _interopRequireDefault(require("./dotGetSchema")); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; | ||
var _getValidationErrors = _interopRequireDefault(require("./getValidationErrors")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
function _default() { | ||
return _ref.apply(this, arguments); | ||
} | ||
function _ref() { | ||
_ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var validationErrors, | ||
_args = arguments; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return _getValidationErrors["default"].apply(void 0, _args); | ||
case 2: | ||
validationErrors = _context.sent; | ||
return _context.abrupt("return", !validationErrors); | ||
case 4: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
return _ref.apply(this, arguments); | ||
async function _default(...args) { | ||
const validationErrors = await (0, _getValidationErrors.default)(...args); | ||
return !validationErrors; | ||
} |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _isValid = _interopRequireDefault(require("./isValid")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
var schema = { | ||
const schema = { | ||
firstName: { | ||
@@ -20,48 +16,16 @@ type: String | ||
}; | ||
var validObject = { | ||
const validObject = { | ||
firstName: 'Nicolás', | ||
lastName: 'López' | ||
}; | ||
var invalidObject = { | ||
const invalidObject = { | ||
lastName: 'López' | ||
}; | ||
test('returns true when object is valid', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var result; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return (0, _isValid["default"])(schema, validObject); | ||
case 2: | ||
result = _context.sent; | ||
expect(result).toBe(true); | ||
case 4: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
}))); | ||
test('returns false when object is invalid', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
var result; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_context2.next = 2; | ||
return (0, _isValid["default"])(schema, invalidObject); | ||
case 2: | ||
result = _context2.sent; | ||
expect(result).toBe(false); | ||
case 4: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2); | ||
}))); | ||
test('returns true when object is valid', async () => { | ||
const result = await (0, _isValid.default)(schema, validObject); | ||
expect(result).toBe(true); | ||
}); | ||
test('returns false when object is invalid', async () => { | ||
const result = await (0, _isValid.default)(schema, invalidObject); | ||
expect(result).toBe(false); | ||
}); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; | ||
@@ -12,41 +14,8 @@ var _ValidationError = _interopRequireDefault(require("./ValidationError")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
async function _default(...args) { | ||
const validationErrors = await (0, _getValidationErrors.default)(...args); | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
function _default() { | ||
return _ref.apply(this, arguments); | ||
} | ||
function _ref() { | ||
_ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var validationErrors, | ||
_args = arguments; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return _getValidationErrors["default"].apply(void 0, _args); | ||
case 2: | ||
validationErrors = _context.sent; | ||
if (!validationErrors) { | ||
_context.next = 5; | ||
break; | ||
} | ||
throw new _ValidationError["default"](validationErrors); | ||
case 5: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
return _ref.apply(this, arguments); | ||
if (validationErrors) { | ||
throw new _ValidationError.default(validationErrors); | ||
} | ||
} |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _validate = _interopRequireDefault(require("./validate")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
var schema = { | ||
const schema = { | ||
firstName: { | ||
@@ -20,49 +16,20 @@ type: String | ||
}; | ||
var validObject = { | ||
const validObject = { | ||
firstName: 'Nicolás', | ||
lastName: 'López' | ||
}; | ||
var invalidObject = { | ||
const invalidObject = { | ||
lastName: 'López' | ||
}; | ||
test("doesn't throw any error when object is valid", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return (0, _validate["default"])(schema, validObject); | ||
test("doesn't throw any error when object is valid", async () => { | ||
await (0, _validate.default)(schema, validObject); | ||
}); | ||
test('throws validation error when object is invalid', async () => { | ||
expect.assertions(1); | ||
case 2: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
}))); | ||
test('throws validation error when object is invalid', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
expect.assertions(1); | ||
_context2.prev = 1; | ||
_context2.next = 4; | ||
return (0, _validate["default"])(schema, invalidObject); | ||
case 4: | ||
_context2.next = 9; | ||
break; | ||
case 6: | ||
_context2.prev = 6; | ||
_context2.t0 = _context2["catch"](1); | ||
expect(_context2.t0.code).toBe('validationError'); | ||
case 9: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, null, [[1, 6]]); | ||
}))); | ||
try { | ||
await (0, _validate.default)(schema, invalidObject); | ||
} catch (error) { | ||
expect(error.code).toBe('validationError'); | ||
} | ||
}); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; | ||
@@ -12,10 +14,8 @@ var _isPlainObject = _interopRequireDefault(require("lodash/isPlainObject")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var dotGet = function dotGet(object, path) { | ||
const dotGet = function dotGet(object, path) { | ||
if (path === '') return object; | ||
var pathParts = path.split('.'); | ||
var first = pathParts.shift(); | ||
var remainingPath = pathParts.join('.'); | ||
var levelObject = object.type; | ||
const pathParts = path.split('.'); | ||
let first = pathParts.shift(); | ||
const remainingPath = pathParts.join('.'); | ||
const levelObject = object.type; | ||
@@ -26,3 +26,3 @@ if (first === '$' || /^[0-9]+$/.test(first)) { | ||
}, remainingPath); | ||
} else if ((0, _isPlainObject["default"])(levelObject[first])) { | ||
} else if ((0, _isPlainObject.default)(levelObject[first])) { | ||
return dotGet(levelObject[first], remainingPath); | ||
@@ -43,3 +43,3 @@ } | ||
function _default(schema, path) { | ||
if ((0, _isNil["default"])(schema)) { | ||
if ((0, _isNil.default)(schema)) { | ||
throw new Error('You need to pass a schema'); | ||
@@ -46,0 +46,0 @@ } |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _dotGetSchema = _interopRequireDefault(require("./dotGetSchema")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
var tag = { | ||
const tag = { | ||
name: { | ||
@@ -16,3 +12,3 @@ type: String | ||
}; | ||
var car = { | ||
const car = { | ||
brand: { | ||
@@ -25,3 +21,3 @@ type: String | ||
}; | ||
var schema = { | ||
const schema = { | ||
name: { | ||
@@ -34,106 +30,36 @@ type: String | ||
}; | ||
test('handle deep schemas', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var value; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
value = (0, _dotGetSchema["default"])(schema, 'car.brand'); | ||
expect(value).toBe(schema.car.type.brand); | ||
test('handle deep schemas', async () => { | ||
const value = (0, _dotGetSchema.default)(schema, 'car.brand'); | ||
expect(value).toBe(schema.car.type.brand); | ||
}); | ||
test('throw error when no schema is passed', async () => { | ||
expect.assertions(1); | ||
case 2: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
try { | ||
(0, _dotGetSchema.default)(null, 'car.brand'); | ||
} catch (error) { | ||
expect(error.message).toBe('You need to pass a schema'); | ||
} | ||
}); | ||
test('handle invalid paths', async () => { | ||
const value = (0, _dotGetSchema.default)(schema, 'car.brand.name'); | ||
expect(value).toBeNull(); | ||
}); | ||
test('finds array first item when $ is passed', async () => { | ||
const value = (0, _dotGetSchema.default)(schema, 'car.tags.$.name'); | ||
expect(value).toBe(tag.name); | ||
}); | ||
test('replaces numbers to $', async () => { | ||
expect((0, _dotGetSchema.default)(schema, 'car.tags.0.name')).toBe(tag.name); | ||
expect((0, _dotGetSchema.default)(schema, 'car.tags.1123.name')).toBe(tag.name); | ||
}); | ||
test('returns information when is blackbox child', async () => { | ||
const schema = { | ||
services: { | ||
type: 'blackbox' | ||
} | ||
}, _callee); | ||
}))); | ||
test('throw error when no schema is passed', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
expect.assertions(1); | ||
try { | ||
(0, _dotGetSchema["default"])(null, 'car.brand'); | ||
} catch (error) { | ||
expect(error.message).toBe('You need to pass a schema'); | ||
} | ||
case 2: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2); | ||
}))); | ||
test('handle invalid paths', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() { | ||
var value; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
value = (0, _dotGetSchema["default"])(schema, 'car.brand.name'); | ||
expect(value).toBeNull(); | ||
case 2: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee3); | ||
}))); | ||
test('finds array first item when $ is passed', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() { | ||
var value; | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
value = (0, _dotGetSchema["default"])(schema, 'car.tags.$.name'); | ||
expect(value).toBe(tag.name); | ||
case 2: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee4); | ||
}))); | ||
test('replaces numbers to $', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5() { | ||
return regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
expect((0, _dotGetSchema["default"])(schema, 'car.tags.0.name')).toBe(tag.name); | ||
expect((0, _dotGetSchema["default"])(schema, 'car.tags.1123.name')).toBe(tag.name); | ||
case 2: | ||
case "end": | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee5); | ||
}))); | ||
test('returns information when is blackbox child', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() { | ||
var schema; | ||
return regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
schema = { | ||
services: { | ||
type: 'blackbox' | ||
} | ||
}; | ||
expect((0, _dotGetSchema["default"])(schema, 'services')).toBe(schema.services); | ||
expect((0, _dotGetSchema["default"])(schema, 'services').isBlackboxChild).toBeUndefined(); | ||
expect((0, _dotGetSchema["default"])(schema, 'services.phoneVerification').isBlackboxChild).toBe(true); | ||
case 4: | ||
case "end": | ||
return _context6.stop(); | ||
} | ||
} | ||
}, _callee6); | ||
}))); | ||
}; | ||
expect((0, _dotGetSchema.default)(schema, 'services')).toBe(schema.services); | ||
expect((0, _dotGetSchema.default)(schema, 'services').isBlackboxChild).toBeUndefined(); | ||
expect((0, _dotGetSchema.default)(schema, 'services.phoneVerification').isBlackboxChild).toBe(true); | ||
}); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = _default; | ||
exports.default = _default; | ||
@@ -14,97 +16,31 @@ var _dotGetSchema = _interopRequireDefault(require("./dotGetSchema")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
var defaultOptions = { | ||
const defaultOptions = { | ||
filter: false | ||
}; | ||
function _default(_x, _x2, _x3) { | ||
return _ref.apply(this, arguments); | ||
} | ||
async function _default(schema, key, value, passedOptions = {}, ...args) { | ||
const options = { ...defaultOptions, | ||
...passedOptions | ||
}; | ||
const keySchema = (0, _dotGetSchema.default)(schema, key); | ||
function _ref() { | ||
_ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(schema, key, value) { | ||
var passedOptions, | ||
options, | ||
keySchema, | ||
_len, | ||
args, | ||
_key, | ||
result, | ||
_args = arguments; | ||
if (!keySchema) { | ||
if (options.filter) { | ||
return _Errors.default.NOT_IN_SCHEMA; | ||
} else { | ||
return null; | ||
} | ||
} | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
passedOptions = _args.length > 3 && _args[3] !== undefined ? _args[3] : {}; | ||
options = _objectSpread(_objectSpread({}, defaultOptions), passedOptions); | ||
keySchema = (0, _dotGetSchema["default"])(schema, key); | ||
if (keySchema.isBlackboxChild) { | ||
return null; | ||
} | ||
if (keySchema) { | ||
_context.next = 9; | ||
break; | ||
} | ||
if (!options.filter) { | ||
_context.next = 8; | ||
break; | ||
} | ||
return _context.abrupt("return", _Errors["default"].NOT_IN_SCHEMA); | ||
case 8: | ||
return _context.abrupt("return", null); | ||
case 9: | ||
if (!keySchema.isBlackboxChild) { | ||
_context.next = 11; | ||
break; | ||
} | ||
return _context.abrupt("return", null); | ||
case 11: | ||
for (_len = _args.length, args = new Array(_len > 4 ? _len - 4 : 0), _key = 4; _key < _len; _key++) { | ||
args[_key - 4] = _args[_key]; | ||
} | ||
_context.next = 14; | ||
return _getValidationErrors["default"].apply(void 0, [{ | ||
validate: keySchema | ||
}, { | ||
validate: value | ||
}, options].concat(args)); | ||
case 14: | ||
result = _context.sent; | ||
if (result) { | ||
_context.next = 17; | ||
break; | ||
} | ||
return _context.abrupt("return", null); | ||
case 17: | ||
return _context.abrupt("return", result.validate); | ||
case 18: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
return _ref.apply(this, arguments); | ||
const result = await (0, _getValidationErrors.default)({ | ||
validate: keySchema | ||
}, { | ||
validate: value | ||
}, options, ...args); | ||
if (!result) return null; | ||
return result.validate; | ||
} |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _index = _interopRequireDefault(require("./index")); | ||
@@ -7,227 +9,85 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
test('autoconvert value', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var schema, errors; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
schema = { | ||
number: { | ||
type: Number | ||
} | ||
}; | ||
_context.next = 3; | ||
return (0, _index["default"])(schema, 'number', '12'); | ||
case 3: | ||
errors = _context.sent; | ||
expect(errors).toEqual(_Errors["default"].NOT_A_NUMBER); | ||
case 5: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
test('autoconvert value', async () => { | ||
const schema = { | ||
number: { | ||
type: Number | ||
} | ||
}, _callee); | ||
}))); | ||
test('deep validate fields', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
var tag, car, schema; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
tag = { | ||
name: { | ||
type: String | ||
} | ||
}; | ||
car = { | ||
brand: { | ||
type: String | ||
}, | ||
tags: { | ||
type: [tag] | ||
} | ||
}; | ||
schema = { | ||
name: { | ||
type: String | ||
}, | ||
car: { | ||
type: car | ||
} | ||
}; | ||
_context2.t0 = expect; | ||
_context2.next = 6; | ||
return (0, _index["default"])(schema, 'car.brand', 'Nissan'); | ||
case 6: | ||
_context2.t1 = _context2.sent; | ||
(0, _context2.t0)(_context2.t1).toBeNull(); | ||
_context2.t2 = expect; | ||
_context2.next = 11; | ||
return (0, _index["default"])(schema, 'car.tags', 'Nice'); | ||
case 11: | ||
_context2.t3 = _context2.sent; | ||
(0, _context2.t2)(_context2.t3).toBe(_Errors["default"].NOT_AN_ARRAY); | ||
_context2.t4 = expect; | ||
_context2.next = 16; | ||
return (0, _index["default"])(schema, 'name', null); | ||
case 16: | ||
_context2.t5 = _context2.sent; | ||
(0, _context2.t4)(_context2.t5).toBe(_Errors["default"].REQUIRED); | ||
_context2.t6 = expect; | ||
_context2.next = 21; | ||
return (0, _index["default"])(schema, 'car', null); | ||
case 21: | ||
_context2.t7 = _context2.sent; | ||
(0, _context2.t6)(_context2.t7).toBe(_Errors["default"].REQUIRED); | ||
_context2.t8 = expect; | ||
_context2.next = 26; | ||
return (0, _index["default"])(schema, 'car.tags.$.name', 12); | ||
case 26: | ||
_context2.t9 = _context2.sent; | ||
(0, _context2.t8)(_context2.t9).toBe(_Errors["default"].NOT_A_STRING); | ||
_context2.t10 = expect; | ||
_context2.next = 31; | ||
return (0, _index["default"])(schema, 'car.tags.100.name', 12); | ||
case 31: | ||
_context2.t11 = _context2.sent; | ||
(0, _context2.t10)(_context2.t11).toBe(_Errors["default"].NOT_A_STRING); | ||
case 33: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
}; | ||
const errors = await (0, _index.default)(schema, 'number', '12'); | ||
expect(errors).toEqual(_Errors.default.NOT_A_NUMBER); | ||
}); | ||
test('deep validate fields', async () => { | ||
const tag = { | ||
name: { | ||
type: String | ||
} | ||
}, _callee2); | ||
}))); | ||
test('filters keys not in schema', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() { | ||
var schema; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
schema = { | ||
services: { | ||
type: 'blackbox' | ||
} | ||
}; | ||
_context3.t0 = expect; | ||
_context3.next = 4; | ||
return (0, _index["default"])(schema, 'person.name', 'Nicolás'); | ||
case 4: | ||
_context3.t1 = _context3.sent; | ||
(0, _context3.t0)(_context3.t1).toBeNull(); | ||
case 6: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
}; | ||
const car = { | ||
brand: { | ||
type: String | ||
}, | ||
tags: { | ||
type: [tag] | ||
} | ||
}, _callee3); | ||
}))); | ||
test('dont filter keys not in schema if specified', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() { | ||
var schema; | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
schema = { | ||
services: { | ||
type: 'blackbox' | ||
} | ||
}; | ||
_context4.t0 = expect; | ||
_context4.next = 4; | ||
return (0, _index["default"])(schema, 'person.name', 'Nicolás', { | ||
filter: true | ||
}); | ||
case 4: | ||
_context4.t1 = _context4.sent; | ||
(0, _context4.t0)(_context4.t1).toBe(_Errors["default"].NOT_IN_SCHEMA); | ||
case 6: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
}; | ||
const schema = { | ||
name: { | ||
type: String | ||
}, | ||
car: { | ||
type: car | ||
} | ||
}, _callee4); | ||
}))); | ||
test('should handle $ correctly', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5() { | ||
var Email, user; | ||
return regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
Email = { | ||
address: { | ||
type: String | ||
}, | ||
verified: { | ||
type: Boolean | ||
} | ||
}; | ||
user = { | ||
emails: { | ||
type: [Email] | ||
} | ||
}; | ||
_context5.t0 = expect; | ||
_context5.next = 5; | ||
return (0, _index["default"])(user, 'emails.$.verified', true); | ||
case 5: | ||
_context5.t1 = _context5.sent; | ||
(0, _context5.t0)(_context5.t1).toBeNull(); | ||
case 7: | ||
case "end": | ||
return _context5.stop(); | ||
} | ||
}; | ||
expect(await (0, _index.default)(schema, 'car.brand', 'Nissan')).toBeNull(); | ||
expect(await (0, _index.default)(schema, 'car.tags', 'Nice')).toBe(_Errors.default.NOT_AN_ARRAY); | ||
expect(await (0, _index.default)(schema, 'name', null)).toBe(_Errors.default.REQUIRED); | ||
expect(await (0, _index.default)(schema, 'car', null)).toBe(_Errors.default.REQUIRED); | ||
expect(await (0, _index.default)(schema, 'car.tags.$.name', 12)).toBe(_Errors.default.NOT_A_STRING); | ||
expect(await (0, _index.default)(schema, 'car.tags.100.name', 12)).toBe(_Errors.default.NOT_A_STRING); | ||
}); | ||
test('filters keys not in schema', async () => { | ||
const schema = { | ||
services: { | ||
type: 'blackbox' | ||
} | ||
}, _callee5); | ||
}))); | ||
test('validate blackbox child', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() { | ||
var schema; | ||
return regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
schema = { | ||
_id: { | ||
type: 'ID' | ||
}, | ||
services: { | ||
type: 'blackbox' | ||
} | ||
}; | ||
console.log('will validtea blackbox'); | ||
_context6.t0 = expect; | ||
_context6.next = 5; | ||
return (0, _index["default"])(schema, 'services.phoneVerification.tries', 1); | ||
case 5: | ||
_context6.t1 = _context6.sent; | ||
(0, _context6.t0)(_context6.t1).toBeNull(); | ||
case 7: | ||
case "end": | ||
return _context6.stop(); | ||
} | ||
}; | ||
expect(await (0, _index.default)(schema, 'person.name', 'Nicolás')).toBeNull(); | ||
}); | ||
test('dont filter keys not in schema if specified', async () => { | ||
const schema = { | ||
services: { | ||
type: 'blackbox' | ||
} | ||
}, _callee6); | ||
}))); | ||
}; | ||
expect(await (0, _index.default)(schema, 'person.name', 'Nicolás', { | ||
filter: true | ||
})).toBe(_Errors.default.NOT_IN_SCHEMA); | ||
}); | ||
test('should handle $ correctly', async () => { | ||
const Email = { | ||
address: { | ||
type: String | ||
}, | ||
verified: { | ||
type: Boolean | ||
} | ||
}; | ||
const user = { | ||
emails: { | ||
type: [Email] | ||
} | ||
}; | ||
expect(await (0, _index.default)(user, 'emails.$.verified', true)).toBeNull(); | ||
}); | ||
test('validate blackbox child', async () => { | ||
const schema = { | ||
_id: { | ||
type: 'ID' | ||
}, | ||
services: { | ||
type: 'blackbox' | ||
} | ||
}; | ||
console.log('will validtea blackbox'); | ||
expect(await (0, _index.default)(schema, 'services.phoneVerification.tries', 1)).toBeNull(); | ||
}); |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = void 0; | ||
exports.default = void 0; | ||
var _isPlainObject = _interopRequireDefault(require("lodash/isPlainObject")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } | ||
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } | ||
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); } | ||
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); } | ||
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } | ||
function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; } | ||
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } | ||
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } | ||
var ValidationError = /*#__PURE__*/function (_Error) { | ||
_inherits(ValidationError, _Error); | ||
var _super = _createSuper(ValidationError); | ||
function ValidationError(validationErrors) { | ||
var _this; | ||
_classCallCheck(this, ValidationError); | ||
if (!(0, _isPlainObject["default"])(validationErrors)) { | ||
class ValidationError extends Error { | ||
constructor(validationErrors) { | ||
if (!(0, _isPlainObject.default)(validationErrors)) { | ||
throw new Error('ValidationError must be initialized with an errors object'); | ||
} | ||
var printableErrors = Object.keys(validationErrors).map(function (key) { | ||
return "".concat(key, ": ").concat(validationErrors[key]); | ||
const printableErrors = Object.keys(validationErrors).map(key => { | ||
return `${key}: ${validationErrors[key]}`; | ||
}).join(', '); | ||
var message = "Validation Error: {".concat(printableErrors, "}"); | ||
_this = _super.call(this, message); | ||
Error.captureStackTrace(_assertThisInitialized(_this), _this.constructor); | ||
_this.code = 'validationError'; | ||
_this.isValidationError = true; | ||
_this.isOrionError = true; | ||
_this.validationErrors = validationErrors; | ||
const message = `Validation Error: {${printableErrors}}`; | ||
super(message); | ||
Error.captureStackTrace(this, this.constructor); | ||
this.code = 'validationError'; | ||
this.isValidationError = true; | ||
this.isOrionError = true; | ||
this.validationErrors = validationErrors; | ||
_this.getInfo = function () { | ||
this.getInfo = () => { | ||
return { | ||
@@ -69,9 +37,8 @@ error: 'validationError', | ||
_this.prependKey = function (prepend) { | ||
var newErrors = {}; | ||
var keys = Object.keys(_this.validationErrors); | ||
this.prependKey = prepend => { | ||
const newErrors = {}; | ||
const keys = Object.keys(this.validationErrors); | ||
for (var _i = 0, _keys = keys; _i < _keys.length; _i++) { | ||
var key = _keys[_i]; | ||
newErrors["".concat(prepend, ".").concat(key)] = _this.validationErrors[key]; | ||
for (const key of keys) { | ||
newErrors[`${prepend}.${key}`] = this.validationErrors[key]; | ||
} | ||
@@ -81,9 +48,6 @@ | ||
}; | ||
return _this; | ||
} | ||
return ValidationError; | ||
}( /*#__PURE__*/_wrapNativeSuper(Error)); | ||
} | ||
exports["default"] = ValidationError; | ||
exports.default = ValidationError; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _ValidationError = _interopRequireDefault(require("./ValidationError")); | ||
@@ -7,38 +9,36 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
test("don't allow to create a ValidationError with no error", function () { | ||
expect(function () { | ||
test("don't allow to create a ValidationError with no error", () => { | ||
expect(() => { | ||
// eslint-disable-next-line | ||
new _ValidationError["default"](); | ||
new _ValidationError.default(); | ||
}).toThrow(); | ||
}); | ||
test('ValidationError is instance of Error', function () { | ||
var error = new _ValidationError["default"]({ | ||
name: _Errors["default"].REQUIRED | ||
test('ValidationError is instance of Error', () => { | ||
const error = new _ValidationError.default({ | ||
name: _Errors.default.REQUIRED | ||
}); | ||
expect(error).toBeInstanceOf(Error); | ||
}); | ||
test('message to be show error information', function () { | ||
var error = new _ValidationError["default"]({ | ||
name: _Errors["default"].REQUIRED | ||
test('message to be show error information', () => { | ||
const error = new _ValidationError.default({ | ||
name: _Errors.default.REQUIRED | ||
}); | ||
expect(error.message).toBe('Validation Error: {name: required}'); | ||
}); | ||
test('prepends keys', function () { | ||
var error = new _ValidationError["default"]({ | ||
name: _Errors["default"].REQUIRED | ||
test('prepends keys', () => { | ||
const error = new _ValidationError.default({ | ||
name: _Errors.default.REQUIRED | ||
}); | ||
expect(error.prependKey('person').message).toBe('Validation Error: {person.name: required}'); | ||
}); | ||
test('getInfo to return error information in correct format', function () { | ||
var validationError = { | ||
name: _Errors["default"].REQUIRED | ||
test('getInfo to return error information in correct format', () => { | ||
const validationError = { | ||
name: _Errors.default.REQUIRED | ||
}; | ||
var error = new _ValidationError["default"](validationError); | ||
var info = { | ||
const error = new _ValidationError.default(validationError); | ||
const info = { | ||
error: 'validationError', | ||
message: 'Validation Error', | ||
validationErrors: { | ||
name: _Errors["default"].REQUIRED | ||
name: _Errors.default.REQUIRED | ||
} | ||
@@ -45,0 +45,0 @@ }; |
{ | ||
"name": "@orion-js/schema", | ||
"version": "1.7.2", | ||
"main": "index.js", | ||
"version": "2.0.0-beta.0", | ||
"main": "lib/index.js", | ||
"author": "nicolaslopezj", | ||
@@ -15,3 +15,4 @@ "license": "MIT", | ||
"dot-object": "^1.7.0", | ||
"lodash": "^4.17.10" | ||
"lodash": "^4.17.10", | ||
"@babel/runtime": "^7.5.5" | ||
}, | ||
@@ -23,3 +24,2 @@ "devDependencies": { | ||
"@babel/preset-env": "^7.5.5", | ||
"@babel/runtime": "^7.5.5", | ||
"babel-jest": "^23.4.0", | ||
@@ -30,4 +30,3 @@ "jest": "^23.4.1" | ||
"access": "public" | ||
}, | ||
"gitHead": "932840475c9e2dca4b85d00ec407123e1ea5cea2" | ||
} | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
6
79452
3
52
2820
2
1
+ Added@babel/runtime@^7.5.5
+ Added@babel/runtime@7.25.6(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)