1 import type { FormikErrors } from 'formik';
2 import { c, msgid } from 'ttag';
4 import { MAX_ITEM_NAME_LENGTH, MAX_ITEM_NOTE_LENGTH } from '@proton/pass/constants';
5 import type { BaseItemValues, Maybe } from '@proton/pass/types';
6 import { isEmptyString } from '@proton/pass/utils/string/is-empty-string';
8 export const validateItemName = (name: string): Maybe<string> => {
9 if (isEmptyString(name)) return c('Warning').t`Title is required`;
11 /* safeguarding : these validation errors should
12 * not `maxLength` trigger as we're leveraging the
13 * default input attribute */
14 if (name.length > MAX_ITEM_NAME_LENGTH) {
15 const maxLength = MAX_ITEM_NAME_LENGTH;
16 /* translator: maxLength is numeric value, example "Maximum length is 10 characters" */
17 return c('Warning').ngettext(
18 msgid`Maximum length is ${maxLength} character`,
19 `Maximum length is ${maxLength} characters`,
25 export const validateItemErrors = <T extends BaseItemValues = BaseItemValues>(values: T): FormikErrors<T> => {
26 const errors: FormikErrors<BaseItemValues> = {};
28 const nameError = validateItemName(values.name);
29 if (nameError) errors.name = nameError;
31 if (values.note.length > MAX_ITEM_NOTE_LENGTH) {
32 const maxLength = MAX_ITEM_NOTE_LENGTH;
33 /* translator: maxLength is numeric value, example "Maximum length is 10 characters" */
34 errors.note = c('Warning').ngettext(
35 msgid`Maximum length is ${maxLength} character`,
36 `Maximum length is ${maxLength} characters`,
41 return errors as FormikErrors<T>;