colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
33.09k stars 1.15k forks source link

required not work #3630

Closed UchihaYuki closed 2 months ago

UchihaYuki commented 2 months ago
const User = z
  .object({
    username: z.string(),
  })
  .required();

User.parse({ username: "Ludwig" });

type User = z.infer<typeof User>;
// actual { username?: string }
// expected  { username: string }

typescript version: Version 5.5.3 zod version: 3.23.8

Atharv2433 commented 2 months ago

import { z } from 'zod';

const User = z .object({ username: z.string(), }) .strict();

User.parse({ username: "Ludwig" });

type User = z.infer; // expected { username: string }

use strict instead of required

UchihaYuki commented 2 months ago

import { z } from 'zod';

const User = z .object({ username: z.string(), }) .strict();

User.parse({ username: "Ludwig" });

type User = z.infer; // expected { username: string }

use strict instead of required

The result is the same.

The code I pasted is from the official doc. The actual result is different from what doc says.

codyfrisch commented 2 months ago

const User = z .object({ username: z.string().required(), })

Is that more what you want? Making the username key value required. Not just the object itself.

UchihaYuki commented 2 months ago

const User = z .object({ username: z.string(), }) .required();

User.parse({ username: "Ludwig" });

type User = z.infer; // actual { username?: string } // expected { username: string }

required only exists on object. https://zod.dev/?id=required

The username property shouldn't be optional according to the doc, even if I just use z.object(). https://zod.dev/?id=basic-usage

UchihaYuki commented 2 months ago

Hi, if any combination of typescript and zod versions works for you, please let me know, I've been stuck here for 1 day now.

UchihaYuki commented 2 months ago

··· // tsconfig.json { // ... "compilerOptions": { // ... "strict": true } } ··· The solution!

korzo commented 2 months ago

Also "strictNullChecks" must be set to true.
Please add this to documentation, because some frameworks ship with with "strictNullChecks": false,

colin-oos commented 1 month ago

··· // tsconfig.json { // ... "compilerOptions": { // ... "strict": true } } ··· The solution!

I have strict and strictNullChecks both set to true yet I'm still having this issue. My required properties are still optional on my parsed data.

korzo commented 1 month ago

@colin-oos this is configuration, that works for me

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2021",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",

    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": true,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false
  }
}