vuito
Version:
Simple, lightweight, isomorphic, and template-based validation library.
78 lines (69 loc) • 3.42 kB
JavaScript
;
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol */
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
function getCheckRowFct(tests) {
return (value, parent) => new Promise((resolve, reject) => {
for (const { message, test, onlyIf } of tests) {
// Conditional check
if (onlyIf && parent && !onlyIf(parent))
continue;
if (value && typeof value === 'string') {
value = value.trim();
}
// If test fail => we reject with error message
if (!test(value))
reject(message);
}
// All tests passed
resolve();
});
}
function getGlobalChecker(vKeys) {
return function (object) {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
for (const [key, tests] of Object.entries(vKeys)) {
// Skip `check` method in the template
// if (key === 'check') return;
// Perform all sub tests, if any fail, the exit with the error message
yield tests.check(object[key], object).catch(reject);
}
// All tests passed
resolve();
}));
};
}
function injectSubCheckers(template) {
return Object.fromEntries(Object.entries(template).map(([key, tests]) => {
return [key, Object.assign(Object.assign({}, tests), { check: getCheckRowFct(tests) })];
}));
}
function templateify(template) {
const injectedTemplate = injectSubCheckers(template);
return Object.assign(Object.assign({}, injectedTemplate), { check: getGlobalChecker(injectedTemplate) });
}
exports.templateify = templateify;
//# sourceMappingURL=template.cjs.js.map