1 import type { FormikErrors } from 'formik';
2 import isEmpty from 'lodash/isEmpty';
3 import { c } from 'ttag';
5 import { parseOTPValue } from '@proton/pass/lib/otp/otp';
6 import type { ExtraFieldGroupValues, UnsafeItemExtraField } from '@proton/pass/types';
7 import { isEmptyString } from '@proton/pass/utils/string/is-empty-string';
9 export type ExtraFieldErrors = FormikErrors<UnsafeItemExtraField>;
11 export const validateExtraFields = <T extends ExtraFieldGroupValues>(values: T) => {
12 const errors = values.extraFields.map((field) => {
13 const fieldErrors: ExtraFieldErrors = {};
15 if (isEmptyString(field.fieldName)) fieldErrors.fieldName = c('Validation').t`Field name is required`;
19 const isTotpEmpty = isEmptyString(field.data.totpUri);
22 fieldErrors.data = { totpUri: c('Validation').t`OTP secret or URI required` };
26 if (!isTotpEmpty && !parseOTPValue(field.data.totpUri)) {
27 fieldErrors.data = { totpUri: c('Validation').t`OTP secret or URI is invalid` };
35 return Boolean(errors.some((fieldErrors) => !isEmpty(fieldErrors)))
36 ? ({ extraFields: errors } as FormikErrors<T>)