Skip to content

Commit

Permalink
chore!: remove translatedErrors from the helper-plugin (#19814)
Browse files Browse the repository at this point in the history
* chore: move translatedErrors into the admin

* chore: fix prettier errors
  • Loading branch information
simotae14 committed Mar 18, 2024
1 parent 8956c05 commit 1cb89ea
Show file tree
Hide file tree
Showing 33 changed files with 282 additions and 259 deletions.
76 changes: 28 additions & 48 deletions packages/core/admin/admin/src/content-manager/utils/validation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { translatedErrors } from '@strapi/helper-plugin';
import pipe from 'lodash/fp/pipe';
import * as yup from 'yup';

import { translatedErrors } from '../../utils/translatedErrors';
import { DOCUMENT_META_FIELDS } from '../constants/attributes';

import type { ComponentsDictionary, Schema } from '../hooks/useDocument';
Expand Down Expand Up @@ -128,55 +128,38 @@ const createAttributeSchema = (
case 'boolean':
return yup.boolean();
case 'blocks':
return yup.mixed().test(
'isBlocks',
{
id: translatedErrors.json,
defaultMessage: "This doesn't match the JSON format",
},
(value) => {
if (!value || Array.isArray(value)) {
return true;
} else {
return false;
}
return yup.mixed().test('isBlocks', translatedErrors.json, (value) => {
if (!value || Array.isArray(value)) {
return true;
} else {
return false;
}
);
});
case 'decimal':
case 'float':
case 'integer':
return yup.number();
case 'email':
return yup.string().email({
id: translatedErrors.email,
defaultMessage: 'This is not a valid email.',
});
return yup.string().email(translatedErrors.email);
case 'enumeration':
return yup.string().oneOf([...attribute.enum, null]);
case 'json':
return yup.mixed().test(
'isJSON',
{
id: translatedErrors.json,
defaultMessage: "This doesn't match the JSON format",
},
(value) => {
/**
* We don't want to validate the JSON field if it's empty.
*/
if (!value || (typeof value === 'string' && value.length === 0)) {
return true;
}
return yup.mixed().test('isJSON', translatedErrors.json, (value) => {
/**
* We don't want to validate the JSON field if it's empty.
*/
if (!value || (typeof value === 'string' && value.length === 0)) {
return true;
}

try {
JSON.parse(value);
try {
JSON.parse(value);

return true;
} catch (err) {
return false;
}
return true;
} catch (err) {
return false;
}
);
});
case 'password':
case 'richtext':
case 'string':
Expand Down Expand Up @@ -206,7 +189,7 @@ type ValidationFn = (
const addRequiredValidation: ValidationFn = (attribute) => (schema) => {
if (attribute.required) {
return schema.required({
id: translatedErrors.required,
id: translatedErrors.required.id,
defaultMessage: 'This field is required.',
});
}
Expand All @@ -224,8 +207,7 @@ const addMinLengthValidation: ValidationFn =
'min' in schema
) {
return schema.min(attribute.minLength, {
id: translatedErrors.minLength,
defaultMessage: 'The value is too short (min: {min}).',
...translatedErrors.minLength,
values: {
min: attribute.minLength,
},
Expand All @@ -245,8 +227,7 @@ const addMaxLengthValidation: ValidationFn =
'max' in schema
) {
return schema.max(attribute.maxLength, {
id: translatedErrors.maxLength,
defaultMessage: 'The value is too long (max: {max}).',
...translatedErrors.maxLength,
values: {
max: attribute.maxLength,
},
Expand All @@ -264,8 +245,7 @@ const addMinValidation: ValidationFn =

if ('min' in schema && min) {
return schema.min(min, {
id: translatedErrors.min,
defaultMessage: 'The value is too low (min: {min}).',
...translatedErrors.min,
values: {
min,
},
Expand All @@ -284,8 +264,7 @@ const addMaxValidation: ValidationFn =

if ('max' in schema && max) {
return schema.max(max, {
id: translatedErrors.max,
defaultMessage: 'The value is too high (max: {max}).',
...translatedErrors.max,
values: {
max,
},
Expand All @@ -311,9 +290,10 @@ const addRegexValidation: ValidationFn =
if ('regex' in attribute && attribute.regex && 'matches' in schema) {
return schema.matches(new RegExp(attribute.regex), {
message: {
id: translatedErrors.regex,
id: translatedErrors.regex.id,
defaultMessage: 'The value does not match the defined pattern.',
},

excludeEmptyString: !attribute.required,
}) as TSchema;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/core/admin/admin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ export type {
HeaderActionProps,
} from './core/apis/content-manager';

/**
* Utils
*/
export { translatedErrors } from './utils/translatedErrors';

export * from './content-manager/exports';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Box, Button, Flex, Main, Typography } from '@strapi/design-system';
import { Link } from '@strapi/design-system/v2';
import { translatedErrors, useAPIErrorHandler } from '@strapi/helper-plugin';
import { useAPIErrorHandler } from '@strapi/helper-plugin';
import { useIntl } from 'react-intl';
import { NavLink, useNavigate } from 'react-router-dom';
import * as yup from 'yup';
Expand All @@ -15,6 +15,7 @@ import {
} from '../../../layouts/UnauthenticatedLayout';
import { useForgotPasswordMutation } from '../../../services/auth';
import { isBaseQueryError } from '../../../utils/baseQuery';
import { translatedErrors } from '../../../utils/translatedErrors';

import type { ForgotPassword } from '../../../../../shared/contracts/authentication';

Expand Down Expand Up @@ -63,16 +64,10 @@ const ForgotPassword = () => {
}
}}
validationSchema={yup.object().shape({
email: yup
.string()
.email({
id: translatedErrors.email,
defaultMessage: 'This is not a valid email.',
})
.required({
id: translatedErrors.required,
defaultMessage: 'This field is required.',
}),
email: yup.string().email(translatedErrors.email).required({
id: translatedErrors.required.id,
defaultMessage: 'This field is required.',
}),
})}
>
<Flex direction="column" alignItems="stretch" gap={6}>
Expand Down
15 changes: 5 additions & 10 deletions packages/core/admin/admin/src/pages/Auth/components/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';

import { Box, Button, Flex, Main, Typography } from '@strapi/design-system';
import { Link } from '@strapi/design-system/v2';
import { translatedErrors, useQuery } from '@strapi/helper-plugin';
import { useQuery } from '@strapi/helper-plugin';
import camelCase from 'lodash/camelCase';
import { useIntl } from 'react-intl';
import { NavLink, useNavigate } from 'react-router-dom';
Expand All @@ -17,6 +17,7 @@ import {
Column,
LayoutContent,
} from '../../../layouts/UnauthenticatedLayout';
import { translatedErrors } from '../../../utils/translatedErrors';

import type { Login } from '../../../../../shared/contracts/authentication';

Expand All @@ -28,17 +29,11 @@ const LOGIN_SCHEMA = yup.object().shape({
email: yup
.string()
.email({
id: translatedErrors.email,
id: translatedErrors.email.id,
defaultMessage: 'Not a valid email',
})
.required({
id: translatedErrors.required,
defaultMessage: 'This value is required.',
}),
password: yup.string().required({
id: translatedErrors.required,
defaultMessage: 'This value is required.',
}),
.required(translatedErrors.required),
password: yup.string().required(translatedErrors.required),
rememberMe: yup.bool().nullable(),
});

Expand Down
36 changes: 14 additions & 22 deletions packages/core/admin/admin/src/pages/Auth/components/Register.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import * as React from 'react';

import { Box, Button, Flex, Grid, GridItem, Main, Typography } from '@strapi/design-system';
import { Box, Button, Flex, Grid, GridItem, Typography } from '@strapi/design-system';
import { Link } from '@strapi/design-system/v2';
import {
auth,
translatedErrors,
useAPIErrorHandler,
useNotification,
useQuery,
} from '@strapi/helper-plugin';
import { auth, useAPIErrorHandler, useNotification, useQuery } from '@strapi/helper-plugin';
import omit from 'lodash/omit';
import { useIntl } from 'react-intl';
import { NavLink, Navigate, useNavigate, useMatch } from 'react-router-dom';
Expand All @@ -34,17 +28,15 @@ import {
useRegisterUserMutation,
} from '../../../services/auth';
import { isBaseQueryError } from '../../../utils/baseQuery';
import { translatedErrors } from '../../../utils/translatedErrors';

const REGISTER_USER_SCHEMA = yup.object().shape({
firstname: yup.string().trim().required({
id: translatedErrors.required,
defaultMessage: 'Firstname is required',
}),
firstname: yup.string().trim().required(translatedErrors.required),
lastname: yup.string().nullable(),
password: yup
.string()
.min(8, {
id: translatedErrors.minLength,
id: translatedErrors.minLength.id,
defaultMessage: 'Password must be at least 8 characters',
values: { min: 8 },
})
Expand All @@ -67,35 +59,35 @@ const REGISTER_USER_SCHEMA = yup.object().shape({
},
})
.required({
id: translatedErrors.required,
id: translatedErrors.required.id,
defaultMessage: 'Password is required',
}),
confirmPassword: yup
.string()
.required({
id: translatedErrors.required,
id: translatedErrors.required.id,
defaultMessage: 'Confirm password is required',
})
.oneOf([yup.ref('password'), null], {
id: 'components.Input.error.password.noMatch',
defaultMessage: 'Passwords must match',
}),
registrationToken: yup.string().required({
id: translatedErrors.required,
id: translatedErrors.required.id,
defaultMessage: 'Registration token is required',
}),
});

const REGISTER_ADMIN_SCHEMA = yup.object().shape({
firstname: yup.string().trim().required({
id: translatedErrors.required,
id: translatedErrors.required.id,
defaultMessage: 'Firstname is required',
}),
lastname: yup.string().nullable(),
password: yup
.string()
.min(8, {
id: translatedErrors.minLength,
id: translatedErrors.minLength.id,
defaultMessage: 'Password must be at least 8 characters',
values: { min: 8 },
})
Expand All @@ -118,7 +110,7 @@ const REGISTER_ADMIN_SCHEMA = yup.object().shape({
},
})
.required({
id: translatedErrors.required,
id: translatedErrors.required.id,
defaultMessage: 'Password is required',
}),
confirmPassword: yup
Expand All @@ -134,16 +126,16 @@ const REGISTER_ADMIN_SCHEMA = yup.object().shape({
email: yup
.string()
.email({
id: translatedErrors.email,
id: translatedErrors.email.id,
defaultMessage: 'Not a valid email',
})
.strict()
.lowercase({
id: translatedErrors.lowercase,
id: translatedErrors.lowercase.id,
defaultMessage: 'Email must be lowercase',
})
.required({
id: translatedErrors.required,
id: translatedErrors.required.id,
defaultMessage: 'Email is required',
}),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Box, Button, Flex, Main, Typography } from '@strapi/design-system';
import { Link } from '@strapi/design-system/v2';
import { translatedErrors, useAPIErrorHandler, useQuery } from '@strapi/helper-plugin';
import { useAPIErrorHandler, useQuery } from '@strapi/helper-plugin';
import { useIntl } from 'react-intl';
import { NavLink, useNavigate, Navigate } from 'react-router-dom';
import * as yup from 'yup';
Expand All @@ -17,12 +17,13 @@ import {
} from '../../../layouts/UnauthenticatedLayout';
import { useResetPasswordMutation } from '../../../services/auth';
import { isBaseQueryError } from '../../../utils/baseQuery';
import { translatedErrors } from '../../../utils/translatedErrors';

const RESET_PASSWORD_SCHEMA = yup.object().shape({
password: yup
.string()
.min(8, {
id: translatedErrors.minLength,
id: translatedErrors.minLength.id,
defaultMessage: 'Password must be at least 8 characters',
values: { min: 8 },
})
Expand All @@ -45,13 +46,13 @@ const RESET_PASSWORD_SCHEMA = yup.object().shape({
},
})
.required({
id: translatedErrors.required,
id: translatedErrors.required.id,
defaultMessage: 'Password is required',
}),
confirmPassword: yup
.string()
.required({
id: translatedErrors.required,
id: translatedErrors.required.id,
defaultMessage: 'Confirm password is required',
})
.oneOf([yup.ref('password'), null], {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('ResetPassword', () => {

fireEvent.click(getByRole('button', { name: 'Send Email' }));

expect(await findByText('This is not a valid email.')).toBeInTheDocument();
expect(await findByText(/This is not a valid email./)).toBeInTheDocument();
});
});
});

0 comments on commit 1cb89ea

Please sign in to comment.