Replies: 4 comments 5 replies
-
If const nullableSchema = z.string().optional().nullable()
const nonNullableSchema = nullableSchema.unwrap()
type NonNullableType = z.infer<typeof nonNullableSchema>
//type NonNullableType = string | undefined |
Beta Was this translation helpful? Give feedback.
2 replies
-
I think what you are looking for is .extend const optionalPost = PostModel.extend( {
userId: z.string().optional(),
teamId: z.string().optional(),
} )
type OptionalPost = z.infer<typeof optionalPost>
//-> OptionalPost = {
// userId?: string | undefined;
// teamId?: string | undefined;
// id: number;
// } |
Beta Was this translation helpful? Give feedback.
2 replies
-
Understanding how the types work is a little tricky, but this should work for any size ZodObject import _ from 'lodash'
import { z } from 'zod'
const PostModel = z.object( {
id: z.number().int(),
userId: z.string().nullable(),
teamId: z.string().nullable(),
} )
type PostModelShape = typeof PostModel.shape
type PickByType<T, U> = Pick<T, { [ K in keyof T ]: U extends T[ K ] ? K : never }[ keyof T ]>
type PostModelJustNullableStrings = PickByType<PostModelShape, z.ZodNullable<z.ZodString>>
//-> PostModelJustNullableStrings = {
// userId: z.ZodNullable<z.ZodString>;
// teamId: z.ZodNullable<z.ZodString>;
// }
const postModelJustNullableStrings = _.pickBy(
PostModel.shape, value => value.isNullable()
) as PostModelJustNullableStrings
const newShape = _.mapValues(
postModelJustNullableStrings,
value => value.unwrap().optional()
)
const optionalPost = PostModel.extend( newShape )
type OptionalPost = z.infer<typeof optionalPost>
//-> OptionalPost = {
// userId?: string | undefined;
// teamId?: string | undefined;
// id: number;
// } |
Beta Was this translation helpful? Give feedback.
0 replies
-
This is a short code I made by modifying somebody else's answer on how to remove optional from nullish. export const removeNullableValidation = <Schema extends z.AnyZodObject>(schema: Schema) => {
const entries = Object.entries(schema.shape) as [keyof Schema['shape'], z.ZodTypeAny][]
const newProps = entries.reduce(
(acc, [key, value]) => {
acc[key] = value instanceof z.ZodNullable ? value.unwrap() : value
return acc
},
{} as {
[key in keyof Schema['shape']]: Schema['shape'][key] extends z.ZodNullable<infer T>
? z.ZodDefault<T>
: Schema['shape'][key]
},
)
return z.object(newProps)
} now you can remove all the nullable values like so: const NotNullablePostModel = removeNullableValidation(PostModel)
// const NotNullablePostModel = z.object({
// id: z.number().int(),
// userId: z.string(),
// teamId: z.string(),
// }) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
If I have an object schema with several properties that can return
string | null | undefined
how can I remove thenull
so I instead end up withstring | undefined
? Is there something similar tononnullable
?Example
We have some auto generated schemas (that we shouldn't edit)
Both
userId
andteamId
both usestring | null | undefined
, but when trying to use them in our use case we should only allowstring | undefined
.Maybe something like typescript NonNullable:
Beta Was this translation helpful? Give feedback.
All reactions