From c98253ceb7338d83e114ac2b1c701142fb429b19 Mon Sep 17 00:00:00 2001 From: Samuel Andert Date: Tue, 8 Aug 2023 09:13:56 +0200 Subject: [PATCH] added also required validation and generic error validation on typing. --- src/lib/components/refactor/ComposerForm.svelte | 5 ++--- src/lib/types/UserSchema.ts | 4 +++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lib/components/refactor/ComposerForm.svelte b/src/lib/components/refactor/ComposerForm.svelte index b75406c..378f520 100644 --- a/src/lib/components/refactor/ComposerForm.svelte +++ b/src/lib/components/refactor/ComposerForm.svelte @@ -31,14 +31,13 @@ // Update the isValidated property of the store whenever the errors object changes $: { - // Send events to the state machine based on the validation status - if (!($errors.name || $errors.email || $errors.about || $errors.age || $errors.favoriteFood)) { + let isValid = fields.every((field) => !$errors[field.name]); + if (isValid) { $me.do.state.send('VALIDATE'); } else { $me.do.state.send('INVALIDATE'); } } - async function handleSubmit() { // Manually validate the form const validationResult = await validate(); diff --git a/src/lib/types/UserSchema.ts b/src/lib/types/UserSchema.ts index b0a212f..a5b5c74 100644 --- a/src/lib/types/UserSchema.ts +++ b/src/lib/types/UserSchema.ts @@ -30,10 +30,12 @@ const validationMessages = { export const UserSchema = z.object({ name: z.string().nonempty('Name is required.').min(3, validationMessages.name.minLength).max(10, validationMessages.name.maxLength), - email: z.string().email(validationMessages.email.isEmail), + email: z.string().nonempty('Email is required.').email(validationMessages.email.isEmail), about: z.string().max(500, validationMessages.about.maxLength), age: z.number().min(18, validationMessages.age.min).max(120, validationMessages.age.max), favoriteFood: z.enum(['apple', 'banana', 'coconut', 'strawberry', 'mango']).refine(value => value !== '', validationMessages.favoriteFood.invalid), slider: z.number().min(0, validationMessages.slider.min).max(100, validationMessages.slider.max), toggle: z.boolean().refine(value => typeof value === 'boolean', validationMessages.toggle.isBoolean), +}).required({ + name: true, }); \ No newline at end of file