Is there a way to rename the key in parse result

const t = type({name:"string"}) const result = t({name:"ankit"}) i want the result to be {alias:"ankit"} how can i replace the key "name" with "alias"
2 Replies
Dimava
Dimava3w ago
No, you can't so this You have do do it manually
import { type } from 'arktype'

console.clear()
let n = type({name: 'string'})
.pipe(o => {
let { name, ...rest } = o
return { alias: name, ...rest }
})

console.log(
n({city: 'foo'}) + '',
n({name: 'foo'}),
n({city: 'foo', name: 'foo'}),
n({}) + '',
)
import { type } from 'arktype'

console.clear()
let n = type({name: 'string'})
.pipe(o => {
let { name, ...rest } = o
return { alias: name, ...rest }
})

console.log(
n({city: 'foo'}) + '',
n({name: 'foo'}),
n({city: 'foo', name: 'foo'}),
n({}) + '',
)
name must be a string (was missing) {
alias: "foo",
} {
alias: "foo",
city: "foo",
} name must be a string (was missing)
name must be a string (was missing) {
alias: "foo",
} {
alias: "foo",
city: "foo",
} name must be a string (was missing)
aabad_ankit
aabad_ankit3w ago
This will work, Thank you
Want results from more Discord servers?
Add your server