diff --git a/src/hydra/fetchHydra.test.ts b/src/hydra/fetchHydra.test.ts index a3c2dfd0..d1f9e033 100644 --- a/src/hydra/fetchHydra.test.ts +++ b/src/hydra/fetchHydra.test.ts @@ -55,6 +55,32 @@ test.each([ 'At least one product must be selected if policy is restricted.', }, ], + [ + 'problem+json', + { + '@context': '/contexts/ConstraintViolation', + '@id': '/validation_errors/2881c032-660f-46b6-8153-d352d9706640', + '@type': 'ConstraintViolation', + status: 422, + violations: [ + { + propertyPath: 'isbn', + 'ConstraintViolation/message': + 'This value is neither a valid ISBN-10 nor a valid ISBN-13.', + 'ConstraintViolation/code': '2881c032-660f-46b6-8153-d352d9706640', + }, + ], + detail: + 'isbn: This value is neither a valid ISBN-10 nor a valid ISBN-13.', + description: + 'isbn: This value is neither a valid ISBN-10 nor a valid ISBN-13.', + type: '/validation_errors/2881c032-660f-46b6-8153-d352d9706640', + title: 'An error occurred', + }, + { + isbn: 'This value is neither a valid ISBN-10 nor a valid ISBN-13.', + }, + ], ])( '%s violation list expanding', async (format: string, resBody: object, expected: object) => { diff --git a/src/hydra/schemaAnalyzer.ts b/src/hydra/schemaAnalyzer.ts index 36b8da86..082cb3e3 100644 --- a/src/hydra/schemaAnalyzer.ts +++ b/src/hydra/schemaAnalyzer.ts @@ -68,6 +68,10 @@ const getFieldType = (field: Field) => { } }; +const getViolationMessage = (violation: JsonLdObj, base: string) => + violation[`${base}#message`] ?? + violation[`${base}#ConstraintViolation/message`]; + const getSubmissionErrors = (error: HttpError) => { if (!error.body?.[0]) { return null; @@ -84,15 +88,16 @@ const getSubmissionErrors = (error: HttpError) => { const violations: SubmissionErrors = content[violationKey].reduce( (previousViolations: SubmissionErrors, violation: JsonLdObj) => - !violation[`${base}#propertyPath`] || !violation[`${base}#message`] + !violation[`${base}#propertyPath`] || + !getViolationMessage(violation, base) ? previousViolations : { ...previousViolations, [(violation[`${base}#propertyPath`] as JsonLdObj[])[0]?.[ '@value' - ] as string]: (violation[`${base}#message`] as JsonLdObj[])[0]?.[ - '@value' - ], + ] as string]: ( + getViolationMessage(violation, base) as JsonLdObj[] + )[0]?.['@value'], }, {}, );