@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
25 lines (21 loc) • 712 B
JavaScript
/**
* This function converts errors into a format that can be used by InstUI components.
*
* @param {string[] | import('@instructure/ui-form-field').FormFieldMessage} errors - The errors to normalize
* @returns {import('@instructure/ui-form-field').FormFieldMessage[]} The normalized errors
*/
export function normalizeErrors(errors) {
if (errors == null) {
return []
}
return errors.map(error => {
if (typeof error === 'string') {
return {text: error, type: 'newError'}
}
if (error.type !== 'error' && error.type !== 'newError') {
// This is a fallback for any other type of message
return error
}
return {text: error.text || '', type: 'newError'}
})
}