123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- Object.defineProperty(exports, "ValidationError", {
- enumerable: true,
- get: function () {
- return _ValidationError.default;
- }
- });
- exports.disableValidation = disableValidation;
- exports.enableValidation = enableValidation;
- exports.needValidate = needValidate;
- exports.validate = validate;
- var _ValidationError = _interopRequireDefault(require("./ValidationError"));
- var _memorize = _interopRequireDefault(require("./util/memorize"));
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
- const getAjv = (0, _memorize.default)(() => {
-
-
- const Ajv = require("ajv").default;
-
- const ajvKeywords = require("ajv-keywords").default;
-
- const addFormats = require("ajv-formats").default;
-
- const ajv = new Ajv({
- strict: false,
- allErrors: true,
- verbose: true,
- $data: true
- });
- ajvKeywords(ajv, ["instanceof", "patternRequired"]);
-
- addFormats(ajv, {
- keywords: false
- });
-
-
- const addAbsolutePathKeyword = require("./keywords/absolutePath").default;
- addAbsolutePathKeyword(ajv);
-
- const addLimitKeyword = require("./keywords/limit").default;
- addLimitKeyword(ajv);
- const addUndefinedAsNullKeyword =
-
- require("./keywords/undefinedAsNull").default;
- addUndefinedAsNullKeyword(ajv);
- return ajv;
- });
- function applyPrefix(error, idx) {
-
- error.instancePath = `[${idx}]${error.instancePath}`;
- if (error.children) {
- error.children.forEach(err => applyPrefix(err, idx));
- }
- return error;
- }
- let skipValidation = false;
- function enableValidation() {
- skipValidation = false;
-
- if (process && process.env) {
- process.env.SKIP_VALIDATION = "n";
- }
- }
- function disableValidation() {
- skipValidation = true;
- if (process && process.env) {
- process.env.SKIP_VALIDATION = "y";
- }
- }
- function needValidate() {
- if (skipValidation) {
- return false;
- }
- if (process && process.env && process.env.SKIP_VALIDATION) {
- const value = process.env.SKIP_VALIDATION.trim();
- if (/^(?:y|yes|true|1|on)$/i.test(value)) {
- return false;
- }
- if (/^(?:n|no|false|0|off)$/i.test(value)) {
- return true;
- }
- }
- return true;
- }
- function validate(schema, options, configuration) {
- if (!needValidate()) {
- return;
- }
- let errors = [];
- if (Array.isArray(options)) {
- for (let i = 0; i <= options.length - 1; i++) {
- errors.push(...validateObject(schema, options[i]).map(err => applyPrefix(err, i)));
- }
- } else {
- errors = validateObject(schema, options);
- }
- if (errors.length > 0) {
- throw new _ValidationError.default(errors, schema, configuration);
- }
- }
- function validateObject(schema, options) {
-
- const compiledSchema = getAjv().compile(schema);
- const valid = compiledSchema(options);
- if (valid) return [];
- return compiledSchema.errors ? filterErrors(compiledSchema.errors) : [];
- }
- function filterErrors(errors) {
-
- let newErrors = [];
- for (const error of (errors)) {
- const {
- instancePath
- } = error;
-
- let children = [];
- newErrors = newErrors.filter(oldError => {
- if (oldError.instancePath.includes(instancePath)) {
- if (oldError.children) {
- children = children.concat(oldError.children.slice(0));
- }
-
- oldError.children = undefined;
- children.push(oldError);
- return false;
- }
- return true;
- });
- if (children.length) {
- error.children = children;
- }
- newErrors.push(error);
- }
- return newErrors;
- }
|