Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(spec): validation errors formatting #9687

Merged
merged 8 commits into from
Mar 13, 2024
7 changes: 4 additions & 3 deletions src/core/plugins/spec/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,12 @@ export const validationErrors = (state, pathMethod) => {

paramValues.forEach( (p) => {
let errors = p.get("errors")
if ( errors && errors.count() ) {
errors.forEach( e => result.push(e))
if (errors && errors.count()) {
errors
.map((e) => (Map.isMap(e) ? `${e.get("propKey")}: ${e.get("error")}` : e))
.forEach((e) => result.push(e))
}
})

return result
}

Expand Down
55 changes: 54 additions & 1 deletion test/unit/core/plugins/spec/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
parameterInclusionSettingFor,
consumesOptionsFor,
taggedOperations,
isMediaTypeSchemaPropertiesEqual
isMediaTypeSchemaPropertiesEqual,
validationErrors
} from "core/plugins/spec/selectors"

import Petstore from "./assets/petstore.json"
Expand Down Expand Up @@ -1380,3 +1381,55 @@ describe("isMediaTypeSchemaPropertiesEqual", () => {
})
})
})
describe("validationErrors", function() {
const state = fromJS({
meta: {
paths: {
"/": {
get: {
parameters: {
id: {
errors: [
"Value must be an integer"
]
}
}
},
post: {
parameters: {
body: {
errors: [
{
error: "Value must be an integer",
propKey: "id"
},
{
error: "Value must be a string",
propKey: "name"
}
]
}
}
}
}
}
}
})

it("should return validation errors without formatting them", function () {
const result = validationErrors(state, ["/", "get"])

expect(result).toEqual([
"Value must be an integer"
])
})

it("should return formatted validation errors", function () {
const result = validationErrors(state, ["/", "post"])

expect(result).toEqual([
"id: Value must be an integer",
"name: Value must be a string"
])
})
})