Skip to content

Commit

Permalink
feat: added zod types for validation inside the JSON schema
Browse files Browse the repository at this point in the history
  • Loading branch information
keyurparalkar committed Apr 14, 2024
1 parent 872b514 commit e637093
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
4 changes: 0 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import { SchemaDef } from "./interfaces/schema";

const schema = SchemaDef.parse(personalInfoSchema);

// type FormValues<T extends typeof schema> = {
// [P in keyof T]: T[P];
// };
const FormValues = schema.fields.reduce((acc, currValue) => {
if (currValue.type === "field") {
acc[currValue.accessorKey] = "";
Expand All @@ -18,7 +15,6 @@ const FormValues = schema.fields.reduce((acc, currValue) => {
type TFormValues = typeof FormValues;

function App() {
console.log({ schema });
const { register, handleSubmit } = useForm<TFormValues>();

const onSubmit = (data: TFormValues) => {
Expand Down
30 changes: 30 additions & 0 deletions src/interfaces/field.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
import { z } from "zod";

const RequiredDef = z.object({
value: z.boolean(),
message: z.string(),
});

const MaxMinLengthDef = z.object({
value: z.number(),
message: z.string(),
});

const MaxMinDef = z.object({
value: z.number(),
message: z.string(),
});

const PatternDef = z.object({
value: z.string(),
message: z.string(),
});

export const FieldTypeDef = z.object({
type: z.literal("field"),
dataType: z.enum(["text", "number", "email"]),
fieldName: z.string(),
accessorKey: z.string(),
validation: z
.object({
required: RequiredDef.optional(),
maxLength: MaxMinLengthDef.optional(),
minLength: MaxMinLengthDef.optional(),
max: MaxMinDef.optional(),
min: MaxMinDef.optional(),
pattern: PatternDef.optional(),
})
.optional(),
});

0 comments on commit e637093

Please sign in to comment.