Replies: 2 comments
-
|
By default, schemas will strip unknown keys, so this should work: const json = JSON.stringify(Person.parse({ name: "Foo", secret: "Bar" })); |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
@scotttrinh suggestion works, or you can also use const Person = z.object( { name: z.string() } )
const personToString = Person.transform( JSON.stringify )
personToString.parse( { name: 'Foo' } ) // => {"name":"Foo"}
personToString.parse( { name: 'Foo', secret: 'Bar' } ) // => {"name":"Foo"}
personToString.parse( { name: 42 } ) // throws errorMore Info: |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Let's assume I have a schema like:
Is there a way to use the
Person-scheme to turn an object into a JSON while removing any additional values that are not present in the schema?So,
Person.serialize({name: 'Foo', secret: 'Bar'})should serialize to a JSON and removesecretBeta Was this translation helpful? Give feedback.
All reactions