Possible Incomplete Records Example? #1651
Answered
by
JacobWeisenburger
antontolken
asked this question in
Q&A
-
i'm learning about zod, and looking at this example it feels like there may be something missing? for example what is const userStore: UserStore = {};
userStore["77d2586b-9e8e-4ecf-8b21-ea7e0530eadd"] = {
name: "Carlotta",
}; // passes
userStore["77d2586b-9e8e-4ecf-8b21-ea7e0530eadd"] = {
whatever: "Ice cream sundae",
}; // TypeError related docs link: https://zod.dev/?id=records |
Beta Was this translation helpful? Give feedback.
Answered by
JacobWeisenburger
Dec 12, 2022
Replies: 2 comments 2 replies
-
You are correct that this example is not very good. Feel free to submit a PR to improve it. Thanks. |
Beta Was this translation helpful? Give feedback.
1 reply
-
It probably should look something like this const userSchema = z.object( { name: z.string() } )
const userStoreSchema = z.record( userSchema )
type UserStore = z.infer<typeof userStoreSchema>
// type UserStore = { [ x: string ]: { name: string } }
const userStore: UserStore = {}
userStore[ '77d2586b-9e8e-4ecf-8b21-ea7e0530eadd' ] = {
name: 'Carlotta',
}
console.log( userStoreSchema.safeParse( userStore ).success ) // true
userStore[ '77d2586b-9e8e-4ecf-8b21-ea7e0530eadd' ] = {
whatever: 'Ice cream sundae',
}
console.log( userStoreSchema.safeParse( userStore ).success ) // false |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
JacobWeisenburger
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It probably should look something like this