123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
- var _absolutePath = _interopRequireDefault(require("./keywords/absolutePath"));
- var _ValidationError = _interopRequireDefault(require("./ValidationError"));
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- const Ajv = require('ajv');
- const ajvKeywords = require('ajv-keywords');
- const ajv = new Ajv({
- allErrors: true,
- verbose: true,
- $data: true
- });
- ajvKeywords(ajv, ['instanceof', 'formatMinimum', 'formatMaximum', 'patternRequired']);
- (0, _absolutePath.default)(ajv);
- function validate(schema, options, configuration) {
- let errors = [];
- if (Array.isArray(options)) {
- errors = Array.from(options, nestedOptions => validateObject(schema, nestedOptions));
- errors.forEach((list, idx) => {
- const applyPrefix =
-
- error => {
-
- error.dataPath = `[${idx}]${error.dataPath}`;
- if (error.children) {
- error.children.forEach(applyPrefix);
- }
- };
- list.forEach(applyPrefix);
- });
- errors = errors.reduce((arr, items) => {
- arr.push(...items);
- return arr;
- }, []);
- } else {
- errors = validateObject(schema, options);
- }
- if (errors.length > 0) {
- throw new _ValidationError.default(errors, schema, configuration);
- }
- }
- function validateObject(schema, options) {
- const compiledSchema = ajv.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 {
- dataPath
- } = error;
-
- let children = [];
- newErrors = newErrors.filter(oldError => {
- if (oldError.dataPath.includes(dataPath)) {
- 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;
- }
- validate.ValidationError = _ValidationError.default;
- validate.ValidateError = _ValidationError.default;
- var _default = validate;
- exports.default = _default;
|