diff --git a/components/pages/Contact/Form/Form.tsx b/components/pages/Contact/Form/Form.tsx new file mode 100644 index 0000000..e69de29 diff --git a/components/pages/Contact/Form/validationSchema.ts b/components/pages/Contact/Form/validationSchema.ts new file mode 100644 index 0000000..c2a050d --- /dev/null +++ b/components/pages/Contact/Form/validationSchema.ts @@ -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; + +export default validationSchema; diff --git a/components/pages/Contact/TextField/TextField.tsx b/components/pages/Contact/TextField/TextField.tsx new file mode 100644 index 0000000..e69de29 diff --git a/package-lock.json b/package-lock.json index babfbea..6053296 100644 --- a/package-lock.json +++ b/package-lock.json @@ -35,7 +35,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", @@ -5196,6 +5197,14 @@ "node": "^10 || ^12 || >=14" } }, + "node_modules/next/node_modules/zod": { + "version": "3.21.4", + "resolved": "https://registry.npmjs.org/zod/-/zod-3.21.4.tgz", + "integrity": "sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==", + "funding": { + "url": "https://github.com/sponsors/colinhacks" + } + }, "node_modules/node-fetch": { "version": "2.6.11", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.11.tgz", @@ -7515,9 +7524,9 @@ } }, "node_modules/zod": { - "version": "3.21.4", - "resolved": "https://registry.npmjs.org/zod/-/zod-3.21.4.tgz", - "integrity": "sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==", + "version": "3.22.2", + "resolved": "https://registry.npmjs.org/zod/-/zod-3.22.2.tgz", + "integrity": "sha512-wvWkphh5WQsJbVk1tbx1l1Ly4yg+XecD+Mq280uBGt9wa5BKSWf4Mhp6GmrkPixhMxmabYY7RbzlwVP32pbGCg==", "funding": { "url": "https://github.com/sponsors/colinhacks" } @@ -11215,6 +11224,11 @@ "picocolors": "^1.0.0", "source-map-js": "^1.0.2" } + }, + "zod": { + "version": "3.21.4", + "resolved": "https://registry.npmjs.org/zod/-/zod-3.21.4.tgz", + "integrity": "sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==" } } }, @@ -12852,9 +12866,9 @@ "dev": true }, "zod": { - "version": "3.21.4", - "resolved": "https://registry.npmjs.org/zod/-/zod-3.21.4.tgz", - "integrity": "sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==" + "version": "3.22.2", + "resolved": "https://registry.npmjs.org/zod/-/zod-3.22.2.tgz", + "integrity": "sha512-wvWkphh5WQsJbVk1tbx1l1Ly4yg+XecD+Mq280uBGt9wa5BKSWf4Mhp6GmrkPixhMxmabYY7RbzlwVP32pbGCg==" } } } diff --git a/package.json b/package.json index d0544a4..9688d4e 100644 --- a/package.json +++ b/package.json @@ -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",