From e07a466901d32b22ebd2ce75664380e40ce327ef Mon Sep 17 00:00:00 2001 From: Ben Irvin Date: Wed, 29 Jan 2025 16:54:15 +0100 Subject: [PATCH] fix: validation message shape --- .../src/pages/Auth/components/Register.tsx | 4 +- .../pages/Auth/components/ResetPassword.tsx | 19 ++++++-- .../Settings/pages/Users/utils/validation.ts | 2 +- .../src/validation/common-validators.ts | 2 +- .../validation/__tests__/auth.test.js | 7 ++- .../server/controllers/validation/auth.js | 48 ++++++++----------- .../content-api/auth.test.api.js | 1 + 7 files changed, 46 insertions(+), 37 deletions(-) diff --git a/packages/core/admin/admin/src/pages/Auth/components/Register.tsx b/packages/core/admin/admin/src/pages/Auth/components/Register.tsx index 57496f130f..3715d9efc7 100644 --- a/packages/core/admin/admin/src/pages/Auth/components/Register.tsx +++ b/packages/core/admin/admin/src/pages/Auth/components/Register.tsx @@ -47,7 +47,7 @@ const REGISTER_USER_SCHEMA = yup.object().shape({ id: 'components.Input.error.contain.maxBytes', defaultMessage: 'Password must be less than 73 bytes', }, - (value) => { + function (value) { if (!value) return true; return new TextEncoder().encode(value).length <= 72; } @@ -115,7 +115,7 @@ const REGISTER_ADMIN_SCHEMA = yup.object().shape({ id: 'components.Input.error.contain.maxBytes', defaultMessage: 'Password must be less than 73 bytes', }, - (value) => { + function (value) { if (!value) return true; return new TextEncoder().encode(value).length <= 72; } diff --git a/packages/core/admin/admin/src/pages/Auth/components/ResetPassword.tsx b/packages/core/admin/admin/src/pages/Auth/components/ResetPassword.tsx index 7862595a49..59eaa14149 100644 --- a/packages/core/admin/admin/src/pages/Auth/components/ResetPassword.tsx +++ b/packages/core/admin/admin/src/pages/Auth/components/ResetPassword.tsx @@ -30,11 +30,20 @@ const RESET_PASSWORD_SCHEMA = yup.object().shape({ values: { min: 8 }, }) // bcrypt has a max length of 72 bytes (not characters!) - .test('required-byte-size', 'Password must be less than 73 bytes', (value) => { - if (!value) return true; - const byteSize = new TextEncoder().encode(value).length; - return byteSize <= 72; - }) + .test( + 'required-byte-size', + { + message: { + id: 'components.Input.error.contain.maxBytes', + defaultMessage: 'Password must be less than 73 bytes', + }, + }, + function (value) { + if (!value) return true; + const byteSize = new TextEncoder().encode(value).length; + return byteSize <= 72; + } + ) .matches(/[a-z]/, { message: { id: 'components.Input.error.contain.lowercase', diff --git a/packages/core/admin/admin/src/pages/Settings/pages/Users/utils/validation.ts b/packages/core/admin/admin/src/pages/Settings/pages/Users/utils/validation.ts index 60aa9edc51..596d9c6ed7 100644 --- a/packages/core/admin/admin/src/pages/Settings/pages/Users/utils/validation.ts +++ b/packages/core/admin/admin/src/pages/Settings/pages/Users/utils/validation.ts @@ -33,7 +33,7 @@ const COMMON_USER_SCHEMA = { id: 'components.Input.error.contain.maxBytes', defaultMessage: 'Password must be less than 73 bytes', }, - (value) => { + function (value) { if (!value) return true; return new TextEncoder().encode(value).length <= 72; } diff --git a/packages/core/admin/server/src/validation/common-validators.ts b/packages/core/admin/server/src/validation/common-validators.ts index 505d48ab41..5d3232cfeb 100644 --- a/packages/core/admin/server/src/validation/common-validators.ts +++ b/packages/core/admin/server/src/validation/common-validators.ts @@ -23,7 +23,7 @@ export const username = yup.string().min(1); export const password = yup .string() .min(8) - .test('required-byte-size', '${path} must be less than 73 bytes', (value) => { + .test('required-byte-size', '${path} must be less than 73 bytes', function (value) { if (!value) return true; const byteSize = new TextEncoder().encode(value).length; return byteSize <= 72; diff --git a/packages/plugins/users-permissions/server/controllers/validation/__tests__/auth.test.js b/packages/plugins/users-permissions/server/controllers/validation/__tests__/auth.test.js index 837fa0cf4a..470f3b82be 100644 --- a/packages/plugins/users-permissions/server/controllers/validation/__tests__/auth.test.js +++ b/packages/plugins/users-permissions/server/controllers/validation/__tests__/auth.test.js @@ -477,7 +477,12 @@ describe('user-permissions auth', () => { const authorization = auth({ strapi: global.strapi }); if (expectedMessage) { - await expect(authorization.resetPassword(ctx)).rejects.toThrow(expectedMessage); + await expect(authorization.resetPassword(ctx)).rejects.toThrowError( + expect.objectContaining({ + name: 'ValidationError', + message: expectedMessage, + }) + ); expect(ctx.send).toHaveBeenCalledTimes(0); } else { await authorization.resetPassword(ctx); diff --git a/packages/plugins/users-permissions/server/controllers/validation/auth.js b/packages/plugins/users-permissions/server/controllers/validation/auth.js index 0c9da5e938..5819c840f6 100644 --- a/packages/plugins/users-permissions/server/controllers/validation/auth.js +++ b/packages/plugins/users-permissions/server/controllers/validation/auth.js @@ -14,16 +14,14 @@ const createRegisterSchema = (config) => password: yup .string() .required() - .test( - 'max-bytes', - { - message: 'Password must be less than 73 bytes', - }, - (value) => { - if (!value) return true; - return new TextEncoder().encode(value).length <= 72; + .test(function (value) { + if (!value) return true; + const isValid = new TextEncoder().encode(value).length <= 72; + if (!isValid) { + return this.createError({ message: 'Password must be less than 73 bytes' }); } - ) + return true; + }) .test(async function (value) { if (typeof config?.validatePassword === 'function') { try { @@ -59,16 +57,14 @@ const createResetPasswordSchema = (config) => password: yup .string() .required() - .test( - 'max-bytes', - { - message: 'Password must be less than 73 bytes', - }, - (value) => { - if (!value) return true; - return new TextEncoder().encode(value).length <= 72; + .test(function (value) { + if (!value) return true; + const isValid = new TextEncoder().encode(value).length <= 72; + if (!isValid) { + return this.createError({ message: 'Password must be less than 73 bytes' }); } - ) + return true; + }) .test(async function (value) { if (typeof config?.validatePassword === 'function') { try { @@ -97,16 +93,14 @@ const createChangePasswordSchema = (config) => password: yup .string() .required() - .test( - 'max-bytes', - { - message: 'Password must be less than 73 bytes', - }, - (value) => { - if (!value) return true; - return new TextEncoder().encode(value).length <= 72; + .test(function (value) { + if (!value) return true; + const isValid = new TextEncoder().encode(value).length <= 72; + if (!isValid) { + return this.createError({ message: 'Password must be less than 73 bytes' }); } - ) + return true; + }) .test(async function (value) { if (typeof config?.validatePassword === 'function') { try { diff --git a/tests/api/plugins/users-permissions/content-api/auth.test.api.js b/tests/api/plugins/users-permissions/content-api/auth.test.api.js index 78ba9de2b6..8ee2a38542 100644 --- a/tests/api/plugins/users-permissions/content-api/auth.test.api.js +++ b/tests/api/plugins/users-permissions/content-api/auth.test.api.js @@ -34,6 +34,7 @@ describe('Auth API', () => { }); afterAll(async () => { + await strapi.db.query('plugin::users-permissions.user').deleteMany(); await strapi.destroy(); });