Skip to content

Commit 21f5699

Browse files
committed
fix: make array schema respected as primitive not tuple
Signed-off-by: tunnckoCore <5038030+tunnckoCore@users.noreply.github.com>
1 parent 7e3e06b commit 21f5699

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ export class Zagora<
455455
: TResult;
456456

457457
return forwardProcedure as TInputSchema extends AnySchema
458-
? InferSchemaInput<TInputSchema> extends readonly any[]
458+
? InferSchemaInput<TInputSchema> extends readonly [any, ...any[]]
459459
? SpreadTuple<InferSchemaInput<TInputSchema>, TFinalResult>
460460
: (arg: InferSchemaInput<TInputSchema>) => TFinalResult
461461
: () => TFinalResult;

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,12 @@ export type ResolveProcedure<
157157
TEnvVarsMap extends AnySchema | undefined,
158158
> = TDisableOptions extends true
159159
? TInputSchema extends AnySchema
160-
? InferSchemaOutput<TInputSchema> extends readonly any[]
160+
? InferSchemaOutput<TInputSchema> extends readonly [any, ...any[]]
161161
? SpreadTuple<InferSchemaOutput<TInputSchema>, any>
162162
: (arg: InferSchemaOutput<TInputSchema>) => any
163163
: () => any
164164
: TInputSchema extends AnySchema
165-
? InferSchemaOutput<TInputSchema> extends readonly any[]
165+
? InferSchemaOutput<TInputSchema> extends readonly [any, ...any[]]
166166
? SpreadTuple<
167167
[
168168
Prettify<ResolveHandlerOptions<TContext, TErrorsMap, TEnvVarsMap>>,

test/main.test.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -287,24 +287,28 @@ test("Error.cause is set on wrapped errors", () => {
287287
});
288288

289289
// TODO: fix to support array schemas, and not treat it as tuple schemas!
290-
// test("input schema array of string should work", () => {
291-
// const fn = zagora()
292-
// .input(z.array(z.string()))
293-
// .output(z.string())
294-
// .handler((_, input) => {
295-
// expectTypeOf(input).toEqualTypeOf<string[]>();
296-
// return input.map((x) => x.toUpperCase());
297-
// })
298-
// .callable();
299-
300-
// const res = fn(["foo", "bar"]);
301-
302-
// if (res.ok) {
303-
// expect(res.data).toBe(["foo", "bar"]);
304-
// } else {
305-
// expect(false, "Expected success").toBe(true);
306-
// }
307-
// });
290+
test("input schema array of string should work", () => {
291+
const fn = zagora()
292+
.input(z.array(z.string()))
293+
.output(z.array(z.string()))
294+
.handler((_, input) => {
295+
expectTypeOf(input).toEqualTypeOf<string[]>();
296+
return input.map((x) => {
297+
expectTypeOf(x).toEqualTypeOf<string>();
298+
299+
return x.toUpperCase();
300+
});
301+
})
302+
.callable();
303+
304+
const res = fn(["foo", "bar"]);
305+
306+
if (res.ok) {
307+
expect(res.data).toStrictEqual(["FOO", "BAR"]);
308+
} else {
309+
expect(false, "Expected success").toBe(true);
310+
}
311+
});
308312

309313
test("input validation failure", () => {
310314
const fn = zagora()

0 commit comments

Comments
 (0)