Skip to content

Commit

Permalink
nonNullable disallows undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
sezanzeb committed Apr 2, 2024
1 parent 991750c commit 82eddeb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
8 changes: 8 additions & 0 deletions deno/lib/__tests__/nonNullable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ test("fails is passed null", () => {
}).toThrow(z.ZodError)
});

test("fails is passed undefined", () => {
const a = z.nullable(z.number()).nonNullable();

expect(() => {
a.parse(undefined);
}).toThrow(z.ZodError)
});

test("unwrap", () => {
const unwrapped = z.any().nonNullable().unwrap();
expect(unwrapped).toBeInstanceOf(z.ZodAny);
Expand Down
5 changes: 4 additions & 1 deletion deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4480,7 +4480,10 @@ export class ZodNonNullable<T extends ZodTypeAny> extends ZodType<
> {
_parse(input: ParseInput): ParseReturnType<this["_output"]> {
const parsedType = this._getType(input);
if (parsedType === ZodParsedType.null) {
if (
parsedType === ZodParsedType.null ||
parsedType === ZodParsedType.undefined
) {
const { ctx } = this._processInputParams(input);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/nonNullable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ test("fails is passed null", () => {
}).toThrow(z.ZodError)
});

test("fails is passed undefined", () => {
const a = z.nullable(z.number()).nonNullable();

expect(() => {
a.parse(undefined);
}).toThrow(z.ZodError)
});

test("unwrap", () => {
const unwrapped = z.any().nonNullable().unwrap();
expect(unwrapped).toBeInstanceOf(z.ZodAny);
Expand Down
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4480,7 +4480,10 @@ export class ZodNonNullable<T extends ZodTypeAny> extends ZodType<
> {
_parse(input: ParseInput): ParseReturnType<this["_output"]> {
const parsedType = this._getType(input);
if (parsedType === ZodParsedType.null) {
if (
parsedType === ZodParsedType.null ||
parsedType === ZodParsedType.undefined
) {
const { ctx } = this._processInputParams(input);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
Expand Down

0 comments on commit 82eddeb

Please sign in to comment.