remove values in runtime output like zod safeParse #1336
-
Is there a way with Arktype to remove values where they are omitted in the type or plans to implement a parse function similar to zod's safeParse? If I use safeParse like this it removes the data completely from the parsed/output data In this example the content in the browser will not have the "private" field at all assuming I have omitted it in the schema . import { userSchema } from 'schemas/user.ts'
import { safeParse } from 'zod'
//...
const parsed = userSchema.safeParse({
name: "Fred",
email: "[email protected]",
private: "something private"
}) With Arktype I can omit the "private" field and get a typescript error in my IDE but if I run dev the actual data is still there in the browser. The best I can find so far is to use the .narrow() method and my own doesObjectContainKey function to make it throw a runtime error if it receives a user with the "private" field included. So do I need to create my own parse method to remove the field if it exists or is there something like that already baked in to Arktype? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @isimmons, it looks like what you're after is const userSchema = type({
name: "string",
email: "email"
}).onUndeclaredKey("delete");
const parsed = useSchema({name: "fred", email: "[email protected]", private: "something private");
console.log(parsed); // doesn't have the "private" key This can be found on the docs here: https://arktype.io/docs/type-api There's also a shorthand where instead of const userSchema = type({
name: "string",
email": "email",
"+": "delete"
}); And this can be configured per-type as shown above, or if you want this to apply to all created types by default, you can configure import { configure } from "arktype/config";
configure({ onUndeclaredKey: "delete" });
// NB: The core arktype import must be AFTER the config has been set
import { type } from "arktype"'
const userSchema = type({
name: "string",
email: "email"
}); Hope this helps! |
Beta Was this translation helpful? Give feedback.
Hi @isimmons, it looks like what you're after is
onUndeclaredKey
:This can be found on the docs here: https://arktype.io/docs/type-api
There's also a shorthand where instead of
.onUndeclaredKey("delete")
you can do"+": "delete"
within the schema itself, i.e.And this can be configured per-type as shown above, or if you want this to apply to all created types…