Zod の .nullable()
と .nullish()
は undefind
を許容するか。
.nullable()
は undefind
を許容せず、 .nullish()
は許容する。
.nullable()
.nullable()
は null
のみを許容する。
使用方法は以下のとおり。
z.nullable()
を使用する。
const nullableString = z.nullable(z.string());
nullableString.parse("asdf"); // => "asdf"
nullableString.parse(null); // => null
または、 .nullable()
メソッドを使用する。
const E = z.string().nullable(); // equivalent to nullableString
type E = z.infer<typeof E>; // string | null
.nullish()
.nullish()
は null
と undefind
を許容する。
使用方法は以下のとおり .nullish()
を使用する。
const nullishString = z.string().nullish(); // string | null | undefined
// equivalent to
z.string().nullable().optional();