Registry Mirror
Skip to content

File tree

‎packages/contract/src/plugins/request-validation.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ORPCError } from '@orpc/client'
22
import { StandardLink } from '@orpc/client/standard'
3+
import { type } from 'arktype'
34
import * as z from 'zod'
45
import { ValidationError } from '../error'
56
import { ContractProcedure } from '../procedure'
@@ -94,6 +95,22 @@ describe('requestValidationPlugin', () => {
9495
}),
9596
)
9697
})
98+
99+
it('returns issues as a plain array', async () => {
100+
// ArkType returns issues as an Array subclass whose `.map` keeps the subclass and whose `toJSON` throws on plain objects.
101+
const link = new StandardLink(codec, client, {
102+
plugins: [
103+
new RequestValidationPlugin({
104+
procedure: new ContractProcedure({ inputSchema: type({ value: 'number' }), errorMap: {}, meta: {}, route: {} }),
105+
}),
106+
],
107+
})
108+
109+
const error: any = await link.call(['procedure'], { value: 'not a number' }, { context: {} }).catch(e => e)
110+
111+
expect(error.data.issues.constructor).toBe(Array)
112+
expect(() => JSON.stringify(error.data)).not.toThrow()
113+
})
97114
})
98115

99116
it('throw if not find matching contract', async () => {

‎packages/contract/src/plugins/request-validation.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ORPCError } from '@orpc/client'
55
import { get } from '@orpc/shared'
66
import { ValidationError } from '../error'
77
import { isContractProcedure } from '../procedure'
8+
import { toPublicSchemaIssue } from '../schema-utils'
89

910
export class RequestValidationPluginError extends Error {}
1011

@@ -42,7 +43,7 @@ export class RequestValidationPlugin<T extends ClientContext> implements Standar
4243
throw new ORPCError('BAD_REQUEST', {
4344
message: 'Input validation failed',
4445
data: {
45-
issues: result.issues,
46+
issues: Array.from(result.issues, toPublicSchemaIssue),
4647
},
4748
cause: new ValidationError({
4849
message: 'Input validation failed',

‎packages/contract/src/schema-utils.test.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type } from 'arktype'
22
import * as v from 'valibot'
33
import * as z from 'zod'
4-
import { isSchemaIssue } from './schema-utils'
4+
import { isSchemaIssue, toPublicSchemaIssue } from './schema-utils'
55

66
describe('isSchemaIssue', async () => {
77
it('works', () => {
@@ -26,3 +26,20 @@ describe('isSchemaIssue', async () => {
2626
expect(issues?.every(isSchemaIssue)).toBe(true)
2727
})
2828
})
29+
30+
describe('toPublicSchemaIssue', () => {
31+
it('keeps only message and path', async () => {
32+
const schema = v.object({ name: v.string() })
33+
const result = await schema['~standard'].validate({ name: { nested: 'value' } })
34+
35+
expect(result.issues?.map(toPublicSchemaIssue)).toEqual([
36+
{ message: 'Invalid type: Expected string but received Object', path: [{ key: 'name' }] },
37+
])
38+
})
39+
40+
it('reduces object path segments to { key }', () => {
41+
const issue = { message: 'invalid', path: ['a', { key: 1, input: [1, 2] }] }
42+
43+
expect(toPublicSchemaIssue(issue)).toEqual({ message: 'invalid', path: ['a', { key: 1 }] })
44+
})
45+
})

‎packages/contract/src/schema-utils.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,13 @@ export function isSchemaIssue(issue: unknown): issue is SchemaIssue {
2020

2121
return true
2222
}
23+
24+
/**
25+
* Keeps only `message` and `path` (object segments become `{ key }`), so the issue is safe to send to clients.
26+
*/
27+
export function toPublicSchemaIssue(issue: SchemaIssue): SchemaIssue {
28+
return {
29+
message: issue.message,
30+
path: issue.path?.map(segment => typeof segment === 'object' && segment !== null ? { key: segment.key } : segment),
31+
}
32+
}

‎packages/server/src/procedure-client.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,27 @@ it('still work without InputSchema', async () => {
613613
expect(handler).toHaveBeenCalledWith({ input: 'anything', context: {}, path: [], procedure })
614614
})
615615

616+
it('returns input issues as a plain array', async () => {
617+
// Some libraries (e.g. ArkType) return issues as an Array subclass whose `.map` keeps the subclass.
618+
class Issues extends Array {}
619+
620+
const procedure = new Procedure({
621+
inputSchema: { '~standard': { version: 1, vendor: 'test', validate: () => ({ issues: Issues.from([{ message: 'invalid' }]) }) } },
622+
errorMap: {},
623+
route: {},
624+
meta: {},
625+
handler,
626+
middlewares: [],
627+
inputValidationIndex: 0,
628+
outputValidationIndex: 0,
629+
})
630+
631+
const error = await createProcedureClient(procedure)('anything').catch(e => e)
632+
633+
expect(error.data.issues.constructor).toBe(Array)
634+
expect(error.data.issues).toEqual([{ message: 'invalid' }])
635+
})
636+
616637
it('still work without OutputSchema', async () => {
617638
const procedure = new Procedure({
618639
inputSchema: schema,

‎packages/server/src/procedure-client.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { ORPCErrorConstructorMap } from './error'
66
import type { Lazyable } from './lazy'
77
import type { AnyProcedure, Procedure, ProcedureHandlerOptions } from './procedure'
88
import { mapEventIterator, ORPCError } from '@orpc/client'
9-
import { validateORPCError, ValidationError } from '@orpc/contract'
9+
import { toPublicSchemaIssue, validateORPCError, ValidationError } from '@orpc/contract'
1010
import { asyncIteratorWithSpan, intercept, isAsyncIteratorObject, overlayProxy, resolveMaybeOptionalOptions, runWithSpan, toArray, value } from '@orpc/shared'
1111
import { HibernationEventIterator } from '@orpc/standard-server'
1212
import { mergeCurrentContext } from './context'
@@ -180,7 +180,7 @@ async function validateInput(procedure: AnyProcedure, input: unknown): Promise<a
180180
throw new ORPCError('BAD_REQUEST', {
181181
message: 'Input validation failed',
182182
data: {
183-
issues: result.issues,
183+
issues: Array.from(result.issues, toPublicSchemaIssue),
184184
},
185185
cause: new ValidationError({
186186
message: 'Input validation failed',

0 commit comments

Comments
 (0)