Skip to content

Commit 683beb0

Browse files
committed
fix(hydra): server-side validation errors
1 parent abc22f6 commit 683beb0

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/hydra/fetchHydra.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,32 @@ test.each([
5555
'At least one product must be selected if policy is restricted.',
5656
},
5757
],
58+
[
59+
'problem+json',
60+
{
61+
'@context': '/contexts/ConstraintViolation',
62+
'@id': '/validation_errors/2881c032-660f-46b6-8153-d352d9706640',
63+
'@type': 'ConstraintViolation',
64+
status: 422,
65+
violations: [
66+
{
67+
propertyPath: 'isbn',
68+
'ConstraintViolation/message':
69+
'This value is neither a valid ISBN-10 nor a valid ISBN-13.',
70+
'ConstraintViolation/code': '2881c032-660f-46b6-8153-d352d9706640',
71+
},
72+
],
73+
detail:
74+
'isbn: This value is neither a valid ISBN-10 nor a valid ISBN-13.',
75+
description:
76+
'isbn: This value is neither a valid ISBN-10 nor a valid ISBN-13.',
77+
type: '/validation_errors/2881c032-660f-46b6-8153-d352d9706640',
78+
title: 'An error occurred',
79+
},
80+
{
81+
isbn: 'This value is neither a valid ISBN-10 nor a valid ISBN-13.',
82+
},
83+
],
5884
])(
5985
'%s violation list expanding',
6086
async (format: string, resBody: object, expected: object) => {

src/hydra/schemaAnalyzer.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ const getFieldType = (field: Field) => {
6868
}
6969
};
7070

71+
const getViolationMessage = (violation: JsonLdObj, base: string) =>
72+
violation[`${base}#message`] ??
73+
violation[`${base}#ConstraintViolation/message`];
74+
7175
const getSubmissionErrors = (error: HttpError) => {
7276
if (!error.body?.[0]) {
7377
return null;
@@ -84,15 +88,16 @@ const getSubmissionErrors = (error: HttpError) => {
8488

8589
const violations: SubmissionErrors = content[violationKey].reduce(
8690
(previousViolations: SubmissionErrors, violation: JsonLdObj) =>
87-
!violation[`${base}#propertyPath`] || !violation[`${base}#message`]
91+
!violation[`${base}#propertyPath`] ||
92+
!getViolationMessage(violation, base)
8893
? previousViolations
8994
: {
9095
...previousViolations,
9196
[(violation[`${base}#propertyPath`] as JsonLdObj[])[0]?.[
9297
'@value'
93-
] as string]: (violation[`${base}#message`] as JsonLdObj[])[0]?.[
94-
'@value'
95-
],
98+
] as string]: (
99+
getViolationMessage(violation, base) as JsonLdObj[]
100+
)[0]?.['@value'],
96101
},
97102
{},
98103
);

0 commit comments

Comments
 (0)