diff --git a/client/cypress/e2e/scenario-creation.cy.ts b/client/cypress/e2e/scenario-creation.cy.ts index 2e7c301b9..d49eff1a7 100644 --- a/client/cypress/e2e/scenario-creation.cy.ts +++ b/client/cypress/e2e/scenario-creation.cy.ts @@ -41,9 +41,19 @@ describe('Scenario creation', () => { cy.get('[data-testid="create-scenario-button"]').should('be.disabled'); }); - it('a user types an invalid name and submit is disabled', () => { + it('a user types a name with a single character', () => { cy.url().should('contain', '/data/scenarios/new'); cy.get('[data-testid="scenario-name-input"]').type('?'); cy.get('[data-testid="create-scenario-button"]').should('be.disabled'); + cy.contains('Name must be at least 2 characters.').should('exist'); + }); + + it('a user types a name with more than 40 characters', () => { + cy.url().should('contain', '/data/scenarios/new'); + cy.get('[data-testid="scenario-name-input"]').type( + 'this is a very looong name for an scenario', + ); + cy.get('[data-testid="create-scenario-button"]').should('be.disabled'); + cy.contains('Name must be at most 40 characters.').should('exist'); }); }); diff --git a/client/src/containers/scenarios/form/component.tsx b/client/src/containers/scenarios/form/component.tsx index 7e09fe59e..64932fa03 100644 --- a/client/src/containers/scenarios/form/component.tsx +++ b/client/src/containers/scenarios/form/component.tsx @@ -1,6 +1,6 @@ import { useForm, Controller } from 'react-hook-form'; -import { yupResolver } from '@hookform/resolvers/yup'; -import * as yup from 'yup'; +import { zodResolver } from '@hookform/resolvers/zod'; +import * as z from 'zod'; import Input from 'components/forms/input'; import Textarea from 'components/forms/textarea'; @@ -17,14 +17,17 @@ type ScenarioFormProps = { onSubmit?: (scenario: ScenarioFormData) => void; }; -const schemaValidation = yup.object({ - title: yup.string().min(2).max(40).required(), - isPublic: yup.boolean(), - description: yup.string().optional().nullable(), +const schemaValidation = z.object({ + title: z + .string() + .min(2, { + message: 'Name must be at least 2 characters.', + }) + .max(40, { message: 'Name must be at most 40 characters.' }), + isPublic: z.boolean(), + description: z.string().optional().nullable(), }); -type SubSchema = yup.InferType; - const ScenarioForm: React.FC> = ({ children, scenario, @@ -36,13 +39,14 @@ const ScenarioForm: React.FC> = ({ control, handleSubmit, formState: { errors, isValid }, - } = useForm({ - resolver: yupResolver(schemaValidation), + } = useForm>({ + resolver: zodResolver(schemaValidation), defaultValues: { title: scenario?.title || null, isPublic: scenario?.isPublic || false, description: scenario?.description || null, }, + mode: 'onChange', criteriaMode: 'all', }); @@ -79,7 +83,7 @@ const ScenarioForm: React.FC> = ({
-
+