A
arktype4mo ago
dibbo

Latest "keys:distilled" equivalent?

Hello, I started using ArkType a back in v1 and it has obviously come on leaps and bounds since then which is great to see. However, back in v1 you could do something like
export const loginPayload = type(
{
email: 'email',
password: '1<string<999',
},
{ keys: 'distilled' }
);
export const loginPayload = type(
{
email: 'email',
password: '1<string<999',
},
{ keys: 'distilled' }
);
and once you validated a payload against this you would only ever get an object with email/password keys. I'm wondering how to achieve a similar result with the latest version of the library. Thanks.
7 Replies
ssalbdivad
ssalbdivad4mo ago
Two ways to do this:
const t = type({
a: "string"
}).onUndeclaredKey("delete")

const o = type({ "+": "delete", a: "string[]" })
const t = type({
a: "string"
}).onUndeclaredKey("delete")

const o = type({ "+": "delete", a: "string[]" })
The second is slightly more optimal perf-wise since it doesn't require creating two types. Sorry, working on new docs for this now!
dibbo
dibboOP4mo ago
No problem at all, thank you for the (impressively fast) response! 👍
aabad_ankit
aabad_ankit3mo ago
@ArkDavid If I add onUndeclaredKey("delete"), I can't further use this type to construct more types const t = type({ a: "string" }).onUndeclaredKey("delete") const t2 = t.and({b:"string"})
Dimava
Dimava3mo ago
@aabad_ankit use ```ts code ``` for code There is .merge
import { type } from 'arktype'

const t = type({
a: "string",
'+': 'delete'
})
const t2 = t.merge({b:"string"})

console.log(t2({a:'1', b: '2', c: '3'}))
import { type } from 'arktype'

const t = type({
a: "string",
'+': 'delete'
})
const t2 = t.merge({b:"string"})

console.log(t2({a:'1', b: '2', c: '3'}))
aabad_ankit
aabad_ankit3mo ago
Great, any reason why and doesn't work but merge works.
ssalbdivad
ssalbdivad3mo ago
Yes, because and means "satisfies both of these types". But if you add new keys to a type with onUndeclaredKey: delete, you haven't satisfied the original type, which would have deleted that key Merge means "combine the property declarations of each of these types, giving priority to the right side", so it's fine Kind of like how you can override previously declared props with ... when you're merging objects
aabad_ankit
aabad_ankit3mo ago
Got it. Thank you
Want results from more Discord servers?
Add your server