Skip to content

Commit 99bf8fc

Browse files
glowcloudebottacinchar0n
authored
fix(spec): validation errors formatting (#9687)
Co-authored-by: Enrico Bottacin <[email protected]> Co-authored-by: Vladimír Gorej <[email protected]>
1 parent 8784145 commit 99bf8fc

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

src/core/plugins/spec/selectors.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,12 @@ export const validationErrors = (state, pathMethod) => {
496496

497497
paramValues.forEach( (p) => {
498498
let errors = p.get("errors")
499-
if ( errors && errors.count() ) {
500-
errors.forEach( e => result.push(e))
499+
if (errors && errors.count()) {
500+
errors
501+
.map((e) => (Map.isMap(e) ? `${e.get("propKey")}: ${e.get("error")}` : e))
502+
.forEach((e) => result.push(e))
501503
}
502504
})
503-
504505
return result
505506
}
506507

test/unit/core/plugins/spec/selectors.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
parameterInclusionSettingFor,
1515
consumesOptionsFor,
1616
taggedOperations,
17-
isMediaTypeSchemaPropertiesEqual
17+
isMediaTypeSchemaPropertiesEqual,
18+
validationErrors
1819
} from "core/plugins/spec/selectors"
1920

2021
import Petstore from "./assets/petstore.json"
@@ -1380,3 +1381,55 @@ describe("isMediaTypeSchemaPropertiesEqual", () => {
13801381
})
13811382
})
13821383
})
1384+
describe("validationErrors", function() {
1385+
const state = fromJS({
1386+
meta: {
1387+
paths: {
1388+
"/": {
1389+
get: {
1390+
parameters: {
1391+
id: {
1392+
errors: [
1393+
"Value must be an integer"
1394+
]
1395+
}
1396+
}
1397+
},
1398+
post: {
1399+
parameters: {
1400+
body: {
1401+
errors: [
1402+
{
1403+
error: "Value must be an integer",
1404+
propKey: "id"
1405+
},
1406+
{
1407+
error: "Value must be a string",
1408+
propKey: "name"
1409+
}
1410+
]
1411+
}
1412+
}
1413+
}
1414+
}
1415+
}
1416+
}
1417+
})
1418+
1419+
it("should return validation errors without formatting them", function () {
1420+
const result = validationErrors(state, ["/", "get"])
1421+
1422+
expect(result).toEqual([
1423+
"Value must be an integer"
1424+
])
1425+
})
1426+
1427+
it("should return formatted validation errors", function () {
1428+
const result = validationErrors(state, ["/", "post"])
1429+
1430+
expect(result).toEqual([
1431+
"id: Value must be an integer",
1432+
"name: Value must be a string"
1433+
])
1434+
})
1435+
})

0 commit comments

Comments
 (0)