mirror of
https://github.com/strapi/strapi.git
synced 2025-11-01 10:23:34 +00:00
refactor(helper-plugin): extract params based on error type
test(helper-plugin): getYupInnerErrors
This commit is contained in:
parent
d4e111a027
commit
6d5fd07184
@ -1,24 +1,24 @@
|
||||
/**
|
||||
* Extract relevant values from the Yup error that can be used to enhance front
|
||||
* end error messaging
|
||||
* @param {Object} yupErrorParams
|
||||
* @returns {Object}
|
||||
* @param {String} errorType
|
||||
* @param {Object} errorParams
|
||||
* @returns {Object} values to pass to error translation string
|
||||
*/
|
||||
const extractValuesFromYupError = (yupErrorParams = {}) =>
|
||||
Object.keys(yupErrorParams)
|
||||
.filter((key) => !['label', 'originalValue', 'path', 'value'].includes(key))
|
||||
.reduce((current, key) => Object.assign(current, { [key]: yupErrorParams[key] }), {});
|
||||
const extractValuesFromYupError = (errorType, errorParams = {}) =>
|
||||
Object.keys(errorParams)
|
||||
.filter((key) => errorType === key)
|
||||
.reduce((current, key) => Object.assign(current, { [key]: errorParams[key] }), {});
|
||||
|
||||
const getYupInnerErrors = (error) => {
|
||||
return (error?.inner || []).reduce((acc, currentError) => {
|
||||
const getYupInnerErrors = (error) =>
|
||||
(error?.inner || []).reduce((acc, currentError) => {
|
||||
acc[currentError.path.split('[').join('.').split(']').join('')] = {
|
||||
id: currentError.message,
|
||||
defaultMessage: currentError.message,
|
||||
values: extractValuesFromYupError(currentError?.params || {}),
|
||||
values: extractValuesFromYupError(currentError.type, currentError?.params || {}),
|
||||
};
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
};
|
||||
|
||||
export default getYupInnerErrors;
|
||||
|
||||
@ -0,0 +1,24 @@
|
||||
import getYupInnerErrors from '../index';
|
||||
|
||||
describe('getYupInnerErrors', () => {
|
||||
test('can extract relevant parameters from an error', () => {
|
||||
const maxError = {
|
||||
inner: [
|
||||
{
|
||||
path: 'Name',
|
||||
type: 'max',
|
||||
params: { max: 5 },
|
||||
message: 'components.Input.error.validation.maxLength',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
expect(getYupInnerErrors(maxError)).toMatchObject({
|
||||
Name: {
|
||||
id: 'components.Input.error.validation.maxLength',
|
||||
defaultMessage: 'components.Input.error.validation.maxLength',
|
||||
values: { max: 5 },
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user