Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Q: Partially execute a parse action #853

Closed
sschneider-ihre-pvs opened this issue Jan 13, 2022 · 4 comments
Closed

Q: Partially execute a parse action #853

sschneider-ihre-pvs opened this issue Jan 13, 2022 · 4 comments

Comments

@sschneider-ihre-pvs
Copy link

So using vee-validate with zod for form validation brought up a thing with how zod is used in that context. So lets take a z.object({}) with some validation rules for fields. Since there is only .parse or .safeParse and so on for the whole object, this is always applied when the form validates no matter if all those values have been changed.
This brought up a problem with legacy data that might be not valid but also not changeable.
So my question is, is it possible to define a z.object({}) but then only parse certain elements of it like z.object({a, b}).parse(data, ['a']) something like that, so instead of form validation, field validation would be possible if there was a way to partially executing a schema.
One obstacle would remain though, handling refines that have a reference to a field that is not selected have to be handled too.

@scotttrinh
Copy link
Collaborator

That's definitely achievable with .partial, but as you noted, refinements make that a bit harder. I suggested an approach on a similar issue here which embraces the potential Partial nature so you can reuse the same refinement function on a partial or full schema.

If you really only care about validating certain data, but don't want the other fields to be stripped from the object, you could use passthrough and only define the fields you care about on the schema. It would only check those fields, but the parsed data would contain any other fields which might have been defined. You have to be careful with that since TypeScript won't know anything about those other properties, but if you just need to pass it back to your backend, that should be fine. If you do need to use those fields, I would suggest not lying to the compiler and properly handle those "optional" fields even if you don't want them to be optional.

@cefn
Copy link
Contributor

cefn commented Jan 16, 2022

Have you considered the approach below, that allows you to base a new schema on picking any parts of an existing schema that you want...

import {z} from "zod";

const tightSchema = z.object({
  id:z.string(),
  rev:z.string(),
  message:z.string(),
})

const {shape} = tightSchema;

const looseSchema = z.object({
  message:shape.message,
  id: shape.id.optional(),
})

image

@sschneider-ihre-pvs
Copy link
Author

Thanks for the two suggestions, I will look into it.

@JacobWeisenburger
Copy link
Contributor

This seems like this question was answered. May I close this?

@JacobWeisenburger JacobWeisenburger added the closeable? This might be ready for closing label Feb 23, 2022
@JacobWeisenburger JacobWeisenburger removed the closeable? This might be ready for closing label Feb 23, 2022
Repository owner locked and limited conversation to collaborators Feb 23, 2022
@JacobWeisenburger JacobWeisenburger converted this issue into discussion #960 Feb 23, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants