New function getErrorsFromSemanticValidationForUnifiedPipeline (#533)
* Revert "Update function getErrorsFromSemanticValidation to extract inner error correctly (#530)"
This reverts commit cd3896e62c
.
* New function for unified pipeline to extract semantic validation inner error.
* export serializeErrorsForUnifiedPipeline
Co-authored-by: t-zhangw <codespace@72c482c53b75>
This commit is contained in:
Родитель
cd3896e62c
Коммит
6eafef4e1d
|
@ -1,8 +1,8 @@
|
|||
# Changelog
|
||||
|
||||
## 09/02/2020 0.22.6
|
||||
## 09/07/2020 0.22.6
|
||||
|
||||
- Update function getErrorsFromSemanticValidation to extract inner error correctly.
|
||||
- New function for unified pipeline to extract semantic validation inner error.
|
||||
|
||||
## 08/18/2020 0.22.5
|
||||
|
||||
|
|
1
index.ts
1
index.ts
|
@ -21,6 +21,7 @@ export { ValidationResultSource } from "./lib/util/validationResultSource"
|
|||
export { getErrorsFromModelValidation } from "./lib/util/getErrorsFromModelValidation"
|
||||
export {
|
||||
getErrorsFromSemanticValidation,
|
||||
getErrorsFromSemanticValidationForUnifiedPipeline,
|
||||
SemanticValidationError
|
||||
} from "./lib/util/getErrorsFromSemanticValidation"
|
||||
export {
|
||||
|
|
|
@ -1,7 +1,12 @@
|
|||
import { SpecValidationResult } from "../validators/specValidator"
|
||||
|
||||
import { BaseValidationError } from "./baseValidationError"
|
||||
import { errorCodeToErrorMetadata, NodeError, serializeErrors } from "./validationError"
|
||||
import {
|
||||
errorCodeToErrorMetadata,
|
||||
NodeError,
|
||||
serializeErrors,
|
||||
serializeErrorsForUnifiedPipeline
|
||||
} from "./validationError"
|
||||
import { ValidationResultSource } from "./validationResultSource"
|
||||
|
||||
export interface SemanticValidationError extends BaseValidationError<NodeError<any>> {
|
||||
|
@ -27,9 +32,42 @@ export const getErrorsFromSemanticValidation = (
|
|||
return []
|
||||
}
|
||||
|
||||
return validationResult.validateSpec.errors.reduce((acc, rawError) => {
|
||||
const serializedErrors: any[] = serializeErrors(rawError.inner || rawError, [])
|
||||
|
||||
// process serialized errors
|
||||
const semanticErrors: SemanticValidationError[] = serializedErrors.map(serializedError => {
|
||||
const severity = errorCodeToErrorMetadata(serializedError.code).severity
|
||||
const semanticError: SemanticValidationError = {
|
||||
source: ValidationResultSource.GLOBAL,
|
||||
code: serializedError.code,
|
||||
details: serializedError,
|
||||
path: rawError["json-path"],
|
||||
severity
|
||||
}
|
||||
return semanticError
|
||||
})
|
||||
return [...acc, ...semanticErrors]
|
||||
}, new Array<SemanticValidationError>())
|
||||
}
|
||||
|
||||
/**
|
||||
* From the raw validator engine semantic validation results process errors to be served.
|
||||
* This function is used only for unified pipeline that would serialize all errors
|
||||
*/
|
||||
export const getErrorsFromSemanticValidationForUnifiedPipeline = (
|
||||
validationResult: SpecValidationResult & ValidationResult
|
||||
): SemanticValidationError[] => {
|
||||
if (!validationResult.validateSpec || !validationResult.validateSpec.errors) {
|
||||
return []
|
||||
}
|
||||
|
||||
return validationResult.validateSpec.errors
|
||||
.reduce((acc, rawError) => {
|
||||
const serializedErrors: any[] = serializeErrors(rawError.inner || rawError, [])
|
||||
const serializedErrors: any[] = serializeErrorsForUnifiedPipeline(
|
||||
rawError.inner || rawError,
|
||||
[]
|
||||
)
|
||||
|
||||
// process serialized errors
|
||||
const semanticErrors: SemanticValidationError[] = serializedErrors.map(serializedError => {
|
||||
|
|
|
@ -225,7 +225,7 @@ export function processValidationErrors<T extends NodeError<T>>(errorsNode: T) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Serializes error tree
|
||||
* Serializes error tree except discriminatorError
|
||||
*/
|
||||
export function serializeErrors<T extends NodeError<T>>(node: T, path: PathComponent[]): T[] {
|
||||
if (isLeaf(node)) {
|
||||
|
@ -280,6 +280,72 @@ export function serializeErrors<T extends NodeError<T>>(node: T, path: PathCompo
|
|||
new Array<T>()
|
||||
)
|
||||
|
||||
if (isDiscriminatorError(node)) {
|
||||
setPathProperties(node, path)
|
||||
node.inner = serializedInner
|
||||
return [node]
|
||||
}
|
||||
return [...serializedErrors, ...serializedInner]
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes error tree for all errors
|
||||
*/
|
||||
export function serializeErrorsForUnifiedPipeline<T extends NodeError<T>>(
|
||||
node: T,
|
||||
path: PathComponent[]
|
||||
): T[] {
|
||||
if (isLeaf(node)) {
|
||||
if (isTrueError(node)) {
|
||||
setPathProperties(node, path)
|
||||
return [node]
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
if (node.path) {
|
||||
// in this case the path will be set to the url instead of the path to the property
|
||||
if (node.code === "INVALID_REQUEST_PARAMETER" && node.in === "body") {
|
||||
node.path = []
|
||||
} else if (
|
||||
(node.in === "query" || node.in === "path") &&
|
||||
node.path[0] === "paths" &&
|
||||
node.name
|
||||
) {
|
||||
// in this case we will want to normalize the path with the uri and the paramter name
|
||||
node.path = [node.path[1], node.name]
|
||||
}
|
||||
path = consolidatePath(path, node.path)
|
||||
}
|
||||
|
||||
const serializedErrors = flatMap(node.errors, validationError =>
|
||||
serializeErrors(validationError, path)
|
||||
).toArray()
|
||||
|
||||
const serializedInner = fold(
|
||||
node.inner,
|
||||
(acc, validationError) => {
|
||||
const errs = serializeErrors(validationError, path)
|
||||
errs.forEach(err => {
|
||||
const similarErr = acc.find(el => areErrorsSimilar(err, el))
|
||||
if (similarErr && similarErr.path) {
|
||||
if (!similarErr.similarPaths) {
|
||||
similarErr.similarPaths = []
|
||||
}
|
||||
similarErr.similarPaths.push(err.path as string)
|
||||
|
||||
if (!similarErr.similarJsonPaths) {
|
||||
similarErr.similarJsonPaths = []
|
||||
}
|
||||
similarErr.similarJsonPaths.push(err.jsonPath as string)
|
||||
} else {
|
||||
acc.push(err)
|
||||
}
|
||||
})
|
||||
return acc
|
||||
},
|
||||
new Array<T>()
|
||||
)
|
||||
return [...serializedErrors, ...serializedInner]
|
||||
}
|
||||
|
||||
|
@ -356,6 +422,9 @@ const arePathsSimilar = (
|
|||
return _.xor(p1, p2).every(v => Number.isInteger(+v))
|
||||
}
|
||||
|
||||
const isDiscriminatorError = <T extends NodeError<T>>(node: T) =>
|
||||
node.code === "ONE_OF_MISSING" && node.inner && node.inner.length > 0
|
||||
|
||||
const isTrueError = <T extends NodeError<T>>(node: T): boolean =>
|
||||
// this is necessary to filter out extra errors coming from doing the ONE_OF transformation on
|
||||
// the models to allow "null"
|
||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
Загрузка…
Ссылка в новой задаче