Skip to content

Commit

Permalink
add form validation schema #8
Browse files Browse the repository at this point in the history
  • Loading branch information
ukorvl committed Sep 4, 2023
1 parent b4db03b commit f4b6295
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
Empty file.
26 changes: 26 additions & 0 deletions components/pages/Contact/Form/validationSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {z} from 'zod';

const requiredFields = z.object({
name: z.string().min(1, 'Name is required').max(50, 'Too Long!'),
message: z.string().max(500, 'Too Long!'),
});

const optionalFields = z
.object({
email: z.string().email('Invalid email address').max(50, 'Too Long!'),
telegram: z.string().startsWith('@', 'Invalid telegram nickname').max(50, 'Too Long!'),
})
.partial()
.refine(({email, telegram}) => email || telegram, {
message: 'Either email or telegram is required',
path: ['email', 'telegram'],
});

const validationSchema = requiredFields.and(optionalFields);

/**
* Type for the form data.
*/
export type ContactFormData = z.infer<typeof validationSchema>;

export default validationSchema;
Empty file.
28 changes: 21 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"react-spinners": "^0.13.8",
"tailwind-merge": "^1.13.1",
"tailwindcss": "^3.3.3",
"typescript": "5.0.4"
"typescript": "5.0.4",
"zod": "^3.22.2"
},
"devDependencies": {
"@next/bundle-analyzer": "^13.4.5",
Expand Down

0 comments on commit f4b6295

Please sign in to comment.