fix(arktype-validator): add restricted fields that are not returned in the "data" field of the error

pull/1137/head
Andrew Bobkov 2025-04-25 17:27:55 +00:00
parent 1b7e645c3d
commit 7831f2bf26
2 changed files with 29 additions and 2 deletions

View File

@ -123,7 +123,6 @@ describe('Basic', () => {
expect(res.status).toBe(400)
const data = (await res.json()) as { succcess: false; errors: type.errors }
expect(data.errors).toHaveLength(1)
console.log(data.errors)
expect(data.errors[0].data).not.toHaveProperty('cookie')
})
})

View File

@ -10,6 +10,10 @@ export type Hook<T, E extends Env, P extends string, O = {}> = (
type HasUndefined<T> = undefined extends T ? true : false
const RESTRICTED_DATA_FIELDS = {
header: ['cookie'],
}
export const arktypeValidator = <
T extends Type,
Target extends keyof ValidationTargets,
@ -54,7 +58,31 @@ export const arktypeValidator = <
return c.json(
{
success: false,
errors: out,
errors:
target in RESTRICTED_DATA_FIELDS
? out.map((error) => {
const restrictedFields =
RESTRICTED_DATA_FIELDS[target as keyof typeof RESTRICTED_DATA_FIELDS] || []
if (
error &&
typeof error === 'object' &&
'data' in error &&
typeof error.data === 'object' &&
error.data !== null &&
!Array.isArray(error.data)
) {
const dataCopy = { ...(error.data as Record<string, unknown>) }
for (const field of restrictedFields) {
delete dataCopy[field]
}
error.data = dataCopy
}
return error
})
: out,
},
400
)