Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using the date type always fails validation #15

Open
samislam opened this issue Dec 20, 2023 · 2 comments
Open

Using the date type always fails validation #15

samislam opened this issue Dec 20, 2023 · 2 comments

Comments

@samislam
Copy link

If you use a date type in your zod schema as follows it will always fail validation, because JSON does not have date type, only strings, and zod will always think that string is not of type date, and thus, validation fails:

const mySchema = z.object({
  myDate: z.date()
})
import { processRequestBody} from 'zod-express-middleware'
app.get(processRequestBody(mySchema), (req,res,next)=>{ console.log("validation passed") })

Are there any plans to solve this problem?

Codesandbox here

[
  {
    "type": "Body",
    "errors": {
      "issues": [
        {
          "code": "invalid_type",
          "expected": "date",
          "received": "string",
          "path": [
            "myDate"
          ],
          "message": "Expected date, received string"
        }
      ],
      "name": "ZodError"
    }
  }
]
@AngaBlue
Copy link

AngaBlue commented Jan 7, 2024

Instead of using z.date() try using z.coerce.date(). While you may receive a string, Zod will try coerce it into a Date before performing validation. I haven't tested this against your exact use case but I do remember this working in the past for a similar issue.

@masar3141
Copy link

Using .coerce results in error in console + schema validation fail:
The specified value "Sat Jan 01 2000 01:00:00 GMT+0100 (Central European Standard Time)" does not conform to the required format, "yyyy-MM-dd"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants