schema-utils
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -5,2 +5,9 @@ # Changelog | ||
## [2.2.0](https://github.com/webpack/schema-utils/compare/v2.1.0...v2.2.0) (2019-09-02) | ||
### Features | ||
* better error output for `oneOf` and `anyOf` ([#48](https://github.com/webpack/schema-utils/issues/48)) ([#50](https://github.com/webpack/schema-utils/issues/50)) ([332242f](https://github.com/webpack/schema-utils/commit/332242f)) | ||
## [2.1.0](https://github.com/webpack-contrib/schema-utils/compare/v2.0.1...v2.1.0) (2019-08-07) | ||
@@ -7,0 +14,0 @@ |
@@ -53,3 +53,87 @@ "use strict"; | ||
} | ||
/** | ||
* Find all children errors | ||
* @param children | ||
* @param {string[]} schemaPaths | ||
* @return {number} returns index of first child | ||
*/ | ||
function findAllChildren(children, schemaPaths) { | ||
let i = children.length - 1; | ||
const predicate = schemaPath => children[i].schemaPath.indexOf(schemaPath) !== 0; | ||
while (i > -1 && !schemaPaths.every(predicate)) { | ||
if (children[i].keyword === 'anyOf' || children[i].keyword === 'oneOf') { | ||
const refs = extractRefs(children[i]); | ||
const childrenStart = findAllChildren(children.slice(0, i), refs.concat(children[i].schemaPath)); | ||
i = childrenStart - 1; | ||
} else { | ||
i -= 1; | ||
} | ||
} | ||
return i + 1; | ||
} | ||
/** | ||
* Extracts all refs from schema | ||
* @param error | ||
* @return {string[]} | ||
*/ | ||
function extractRefs(error) { | ||
const { | ||
schema | ||
} = error; | ||
if (!Array.isArray(schema)) { | ||
return []; | ||
} | ||
return schema.map(({ | ||
$ref | ||
}) => $ref).filter(s => s); | ||
} | ||
/** | ||
* Groups children by their first level parent (assuming that error is root) | ||
* @param children | ||
* @return {any[]} | ||
*/ | ||
function groupChildrenByFirstChild(children) { | ||
const result = []; | ||
let i = children.length - 1; | ||
while (i > 0) { | ||
const child = children[i]; | ||
if (child.keyword === 'anyOf' || child.keyword === 'oneOf') { | ||
const refs = extractRefs(child); | ||
const childrenStart = findAllChildren(children.slice(0, i), refs.concat(child.schemaPath)); | ||
if (childrenStart !== i) { | ||
result.push(Object.assign({}, child, { | ||
children: children.slice(childrenStart, i) | ||
})); | ||
i = childrenStart; | ||
} else { | ||
result.push(child); | ||
} | ||
} else { | ||
result.push(child); | ||
} | ||
i -= 1; | ||
} | ||
if (i === 0) { | ||
result.push(children[i]); | ||
} | ||
return result.reverse(); | ||
} | ||
function indent(str, prefix) { | ||
@@ -546,3 +630,3 @@ return str.replace(/\n(?!$)/g, `\n${prefix}`); | ||
const children = filterChildren(error.children); | ||
let children = filterChildren(error.children); | ||
@@ -553,2 +637,3 @@ if (children.length === 1) { | ||
children = groupChildrenByFirstChild(children); | ||
return `${dataPath} should be one of these:\n${this.getSchemaPartText(error.parentSchema)}\nDetails:\n${children.map(nestedError => ` * ${indent(this.formatValidationError(nestedError), ' ')}`).join('\n')}`; | ||
@@ -555,0 +640,0 @@ } |
{ | ||
"name": "schema-utils", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "webpack Validation Utils", | ||
"license": "MIT", | ||
"repository": "webpack-contrib/schema-utils", | ||
"repository": "webpack/schema-utils", | ||
"author": "webpack Contrib (https://github.com/webpack-contrib)", | ||
"homepage": "https://github.com/webpack-contrib/schema-utils", | ||
"bugs": "https://github.com/webpack-contrib/schema-utils/issues", | ||
"homepage": "https://github.com/webpack/schema-utils", | ||
"bugs": "https://github.com/webpack/schema-utils/issues", | ||
"main": "dist/index.js", | ||
@@ -37,28 +37,28 @@ "engines": { | ||
"dependencies": { | ||
"ajv": "^6.1.0", | ||
"ajv-keywords": "^3.1.0" | ||
"ajv": "^6.10.2", | ||
"ajv-keywords": "^3.4.1" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.4.4", | ||
"@babel/core": "^7.4.5", | ||
"@babel/preset-env": "^7.4.5", | ||
"@babel/cli": "^7.5.5", | ||
"@babel/core": "^7.5.5", | ||
"@babel/preset-env": "^7.5.5", | ||
"@commitlint/cli": "^8.0.0", | ||
"@commitlint/config-conventional": "^8.0.0", | ||
"@webpack-contrib/defaults": "^5.0.0", | ||
"@webpack-contrib/defaults": "^5.0.2", | ||
"@webpack-contrib/eslint-config-webpack": "^3.0.0", | ||
"babel-jest": "^24.8.0", | ||
"babel-jest": "^24.9.0", | ||
"commitlint-azure-pipelines-cli": "^1.0.2", | ||
"cross-env": "^5.2.0", | ||
"del": "^5.0.0", | ||
"cross-env": "^5.2.1", | ||
"del": "^5.1.0", | ||
"del-cli": "^2.0.0", | ||
"eslint": "^6.0.1", | ||
"eslint-config-prettier": "^6.0.0", | ||
"eslint-plugin-import": "^2.0.0", | ||
"husky": "^3.0.0", | ||
"jest": "^24.8.0", | ||
"jest-junit": "^6.4.0", | ||
"lint-staged": "^9.2.0", | ||
"eslint": "^6.3.0", | ||
"eslint-config-prettier": "^6.1.0", | ||
"eslint-plugin-import": "^2.18.2", | ||
"husky": "^3.0.5", | ||
"jest": "^24.9.0", | ||
"jest-junit": "^8.0.0", | ||
"lint-staged": "^9.2.5", | ||
"npm-run-all": "^4.1.5", | ||
"prettier": "^1.0.0", | ||
"standard-version": "^6.0.1" | ||
"prettier": "^1.18.2", | ||
"standard-version": "^7.0.0" | ||
}, | ||
@@ -65,0 +65,0 @@ "keywords": [ |
42197
639
Updatedajv@^6.10.2
Updatedajv-keywords@^3.4.1