Files

63 lines
2.0 KiB
TypeScript
Raw Permalink Normal View History

2026-10-08 18:49:15 -04:00
import type { AdminUser, UserValues } from '@/api/admin/users/queries';
2026-11-23 14:16:00 +00:00
import i18next from 'i18next';
2026-10-08 18:49:15 -04:00
import { requiredWithMaxLength } from '@/components/form/validators';
import { initialExtensionValues } from '@/extensions/forms';
2026-10-27 11:42:22 +00:00
import { PANEL_DEFAULT_LANGUAGE } from '@/components/admin/languageOptions';
2026-10-08 18:49:15 -04:00
export const userFormValues = (user: AdminUser): UserValues => ({
email: user.attributes.email,
username: user.attributes.username,
nameFirst: user.attributes.first_name ?? '',
nameLast: user.attributes.last_name ?? '',
password: '',
rootAdmin: user.attributes.root_admin,
2026-10-27 11:42:22 +00:00
language: user.attributes.language ?? PANEL_DEFAULT_LANGUAGE,
extensions: initialExtensionValues(),
2026-10-08 18:49:15 -04:00
});
export const validateUserPassword = ({ value }: { value: string }): string | undefined => {
if (value.length === 0) {
return undefined;
}
if (value.length < 8) {
2026-11-23 14:16:00 +00:00
return i18next.t('Password must be at least 8 characters.');
2026-10-08 18:49:15 -04:00
}
if (value.length > 191) {
2026-11-23 14:16:00 +00:00
return i18next.t('Password must not exceed 191 characters.');
2026-10-08 18:49:15 -04:00
}
return undefined;
};
export const validateUserEmail = ({ value }: { value: string }): string | undefined => {
if (value.length < 1) {
2026-11-23 14:16:00 +00:00
return i18next.t('An email address must be provided.');
2026-10-08 18:49:15 -04:00
}
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) {
2026-11-23 14:16:00 +00:00
return i18next.t('A valid email address must be provided.');
2026-10-08 18:49:15 -04:00
}
return undefined;
};
export const validateUsername = requiredWithMaxLength(
191,
2026-11-23 14:16:00 +00:00
() => i18next.t('A username must be provided.'),
() => i18next.t('A username must not exceed 191 characters.')
2026-10-08 18:49:15 -04:00
);
export const validateFirstName = requiredWithMaxLength(
191,
2026-11-23 14:16:00 +00:00
() => i18next.t('A first name must be provided.'),
() => i18next.t('First name must not exceed 191 characters.')
2026-10-08 18:49:15 -04:00
);
export const validateLastName = requiredWithMaxLength(
191,
2026-11-23 14:16:00 +00:00
() => i18next.t('A last name must be provided.'),
() => i18next.t('Last name must not exceed 191 characters.')
2026-10-08 18:49:15 -04:00
);