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

Zod refine is not working #3548

Open
zack990420 opened this issue Jun 6, 2024 · 1 comment
Open

Zod refine is not working #3548

zack990420 opened this issue Jun 6, 2024 · 1 comment

Comments

@zack990420
Copy link

I have tried this two method of refine, but both of it are not working.

image

@pedramp
Copy link

pedramp commented Jun 6, 2024

It does work just fine for me.
tested with zod 3.23.8v

const z = require('zod');

const validation = z.object({
    nationality: z.string().refine((val) => val === 'X', {message: 'is not X', path: ['path-x']}),
    nric: z.string().optional()
}).refine((data) => data.nationality === 'X', {message: 'is not X(second refine)', path: ['path-x']})


const result = validation.parse({ nationality: 'X' })
console.log(result); // { nationality: 'X' }

and for error:

const result = validation.parse({ nationality: 'xyz' })
console.log(result);


ZodError: [
  {
    "code": "custom",
    "message": "is not X",
    "path": [
      "nationality",
      "path-x"
    ]
  },
  {
    "code": "custom",
    "message": "is not X(second refine)",
    "path": [
      "path-x"
    ]
  }
]

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

2 participants