1 Star 0 Fork 0

animalcoder/Vue

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
validateSchema.js 1.55 KB
一键复制 编辑 原始数据 按行查看 历史
animalcoder 提交于 2020-03-23 09:59 +08:00 . Vue6
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Gajus Kuizinas @gajus
*/
"use strict";
const Ajv = require("ajv");
const ajv = new Ajv({
errorDataPath: "configuration",
allErrors: true,
verbose: true
});
require("ajv-keywords")(ajv, ["instanceof"]);
require("../schemas/ajv.absolutePath")(ajv);
const validateSchema = (schema, options) => {
if (Array.isArray(options)) {
const errors = options.map(options => validateObject(schema, options));
errors.forEach((list, idx) => {
const applyPrefix = err => {
err.dataPath = `[${idx}]${err.dataPath}`;
if (err.children) {
err.children.forEach(applyPrefix);
}
};
list.forEach(applyPrefix);
});
return errors.reduce((arr, items) => {
return arr.concat(items);
}, []);
} else {
return validateObject(schema, options);
}
};
const validateObject = (schema, options) => {
const validate = ajv.compile(schema);
const valid = validate(options);
return valid ? [] : filterErrors(validate.errors);
};
const filterErrors = errors => {
let newErrors = [];
for (const err of errors) {
const dataPath = err.dataPath;
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) {
err.children = children;
}
newErrors.push(err);
}
return newErrors;
};
module.exports = validateSchema;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/animalcoder/Vue.git
git@gitee.com:animalcoder/Vue.git
animalcoder
Vue
Vue
master

搜索帮助