This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
website/hooks/useForm/getErrorTranslationKey.ts
2021-10-26 16:38:55 +02:00

20 lines
562 B
TypeScript

import type { ErrorObject } from 'ajv'
const knownErrorKeywords = ['minLength', 'maxLength', 'format']
export const getErrorTranslationKey = (error: ErrorObject): string => {
if (knownErrorKeywords.includes(error?.keyword)) {
if (error.keyword === 'minLength' && error.params.limit === 1) {
return 'errors:required'
}
if (error.keyword === 'format') {
if (error.params.format === 'email') {
return 'errors:email'
}
return 'errors:invalid'
}
return `errors:${error.keyword}`
}
return 'errors:invalid'
}