Created
January 17, 2022 14:27
-
-
Save iskenxan/9a6e3e98fe7771cf2d4afe188aa56b49 to your computer and use it in GitHub Desktop.
Typescript schema validation with zod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { z } from "zod"; | |
const stringSchema = z.string().nonempty().min(8).max(32); | |
stringSchema.parse(""); | |
stringSchema.parse(""); // throws an exception | |
stringSchema.parse("I am a valid password"); // returns "I am a valid password" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const User = z.object({ | |
email: z.string().email(), | |
name: z.string(), | |
phoneNumber: z.number() | |
}); | |
const Hobby = z.object({ | |
hobbyName: z.string().min(1) | |
}); | |
const Hobbyist = User.merge(Hobby); | |
const result = Hobbyist.safeParse({ | |
email: "[email protected]", | |
name: "Hello World", | |
phoneNumber: 123 | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const result = User.parse({ | |
email: "[email protected]", | |
name: "Hello World" | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"code": "invalid_type", | |
"expected": "number", | |
"received": "undefined", | |
"path": [ | |
"phoneNumber" | |
], | |
"message": "Required" | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { z } from "zod"; | |
const User = z.object({ | |
email: z.string().email(), | |
name: z.string(), | |
phoneNumber: z.number() | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
z.string().optional().array(); // (string | undefined)[] | |
z.string().array().optional(); // string[] | undefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mySchema.safeParse(""I am a valid password""); // => { success: true; data: "I am a valid password" } | |
mySchema.safeParse(""); // => { success: false; error: ZodError } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type UserType = z.infer<typeof User>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"code": "too_small", | |
"minimum": 1, | |
"type": "string", | |
"inclusive": true, | |
"message": "Should be at least 1 characters", | |
"path": [] | |
}, | |
{ | |
"code": "too_small", | |
"minimum": 8, | |
"type": "string", | |
"inclusive": true, | |
"message": "Should be at least 8 characters", | |
"path": [] | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment