diff --git a/packages/arktype-validator/src/index.test.ts b/packages/arktype-validator/src/index.test.ts index acf5312b..b77d5a35 100644 --- a/packages/arktype-validator/src/index.test.ts +++ b/packages/arktype-validator/src/index.test.ts @@ -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') }) }) diff --git a/packages/arktype-validator/src/index.ts b/packages/arktype-validator/src/index.ts index 4dbdde8d..d3254902 100644 --- a/packages/arktype-validator/src/index.ts +++ b/packages/arktype-validator/src/index.ts @@ -10,6 +10,10 @@ export type Hook = ( type HasUndefined = 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) } + for (const field of restrictedFields) { + delete dataCopy[field] + } + + error.data = dataCopy + } + + return error + }) + : out, }, 400 )