@@ -55,16 +55,11 @@ export class AlertError extends Error {
5555 * @returns {null | { loc: string[], msg: string } }
5656 */
5757function getSimpleValidationMessage ( reason , statusCode ) {
58- if (
59- statusCode !== 422 ||
60- ! ( 'detail' in reason ) ||
61- ! Array . isArray ( reason . detail ) ||
62- reason . detail . length !== 1
63- ) {
58+ if ( ! isValidationError ( reason , statusCode ) || reason . detail . length !== 1 ) {
6459 return null ;
6560 }
6661 const err = reason . detail [ 0 ] ;
67- if ( ! Array . isArray ( err . loc ) || ! err . msg || err . type !== 'value_error' ) {
62+ if ( ! isValueError ( err ) ) {
6863 return null ;
6964 }
7065 return {
@@ -73,6 +68,46 @@ function getSimpleValidationMessage(reason, statusCode) {
7368 } ;
7469}
7570
71+ /**
72+ * @param {any } reason
73+ * @param {number | null } statusCode
74+ * @returns {null | { [key: string]: string } }
75+ */
76+ export function getValidationMessagesMap ( reason , statusCode ) {
77+ if ( ! isValidationError ( reason , statusCode ) || reason . detail . length === 0 ) {
78+ return null ;
79+ }
80+ /** @type {{[key: string]: string} } */
81+ const map = { } ;
82+ for ( const error of reason . detail ) {
83+ if ( ! isValueError ( error ) ) {
84+ return null ;
85+ }
86+ if ( error . loc . length !== 2 || error . loc [ 0 ] !== 'body' ) {
87+ return null ;
88+ }
89+ map [ error . loc [ 1 ] ] = error . msg ;
90+ }
91+ return map ;
92+ }
93+
94+ /**
95+ * @param {any } reason
96+ * @param {number | null } statusCode
97+ * @returns {boolean }
98+ */
99+ function isValidationError ( reason , statusCode ) {
100+ return statusCode === 422 && 'detail' in reason && Array . isArray ( reason . detail ) ;
101+ }
102+
103+ /**
104+ * @param {any } err
105+ * @returns {boolean }
106+ */
107+ function isValueError ( err ) {
108+ return Array . isArray ( err . loc ) && ! ! err . msg && err . type === 'value_error' ;
109+ }
110+
76111/**
77112 * Display a standard error alert on the desired HTML element.
78113 * @param {any } error
0 commit comments