Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance ZodArray .min() and .length() Types #2656

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1425,12 +1425,16 @@ const nonEmptyStrings = z.string().array().nonempty({

```ts
z.string().array().min(5); // must contain 5 or more items
// the inferred type is now
// [string, string, string, string, string, ...string[]]

z.string().array().max(5); // must contain 5 or fewer items

z.string().array().length(5); // must contain 5 items exactly
// the inferred type is now
// [string, string, string, string, string]
```

Unlike `.nonempty()` these methods do not change the inferred type.

## Tuples

Unlike arrays, tuples have a fixed number of elements and each element can have a different type.
Expand Down
8 changes: 6 additions & 2 deletions deno/lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1425,12 +1425,16 @@ const nonEmptyStrings = z.string().array().nonempty({

```ts
z.string().array().min(5); // must contain 5 or more items
// the inferred type is now
// [string, string, string, string, string, ...string[]]

z.string().array().max(5); // must contain 5 or fewer items

z.string().array().length(5); // must contain 5 items exactly
// the inferred type is now
// [string, string, string, string, string]
```

Unlike `.nonempty()` these methods do not change the inferred type.

## Tuples

Unlike arrays, tuples have a fixed number of elements and each element can have a different type.
Expand Down
5 changes: 4 additions & 1 deletion deno/lib/__tests__/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ type t1 = z.infer<typeof nonEmptyMax>;
util.assertEqual<[string, ...string[]], t1>(true);

type t2 = z.infer<typeof minTwo>;
util.assertEqual<string[], t2>(true);
util.assertEqual<[string, string, ...string[]], t2>(true);

type t3 = z.infer<typeof justTwo>;
util.assertEqual<[string, string], t3>(true);

test("passing validations", () => {
minTwo.parse(["a", "a"]);
Expand Down
43 changes: 31 additions & 12 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1974,22 +1974,35 @@ export interface ZodArrayDef<T extends ZodTypeAny = ZodTypeAny>
maxLength: { value: number; message?: string } | null;
}

export type ArrayCardinality = "many" | "atleastone";
export type ArrayCardinality = "many" | "atleast" | "exactly";

type FixedArray<T, N extends number, R extends T[] = []> = R["length"] extends N
? R
: FixedArray<T, N, [T, ...R]>;

type MinArray<T, N extends number> = [...FixedArray<T, N>, ...T[]];

export type arrayOutputType<
T extends ZodTypeAny,
Cardinality extends ArrayCardinality = "many"
> = Cardinality extends "atleastone"
? [T["_output"], ...T["_output"][]]
Cardinality extends ArrayCardinality = "many",
CardinalityAmount extends number = 0
> = Cardinality extends "atleast"
? MinArray<T["_output"], CardinalityAmount>
: Cardinality extends "exactly"
? FixedArray<T["_output"], CardinalityAmount>
: T["_output"][];

export class ZodArray<
T extends ZodTypeAny,
Cardinality extends ArrayCardinality = "many"
Cardinality extends ArrayCardinality = "many",
CardinalityAmount extends number = 0
> extends ZodType<
arrayOutputType<T, Cardinality>,
arrayOutputType<T, Cardinality, CardinalityAmount>,
ZodArrayDef<T>,
Cardinality extends "atleastone"
? [T["_input"], ...T["_input"][]]
Cardinality extends "atleast"
? MinArray<T["_input"], CardinalityAmount>
: Cardinality extends "exactly"
? FixedArray<T["_input"], CardinalityAmount>
: T["_input"][]
> {
_parse(input: ParseInput): ParseReturnType<this["_output"]> {
Expand Down Expand Up @@ -2076,7 +2089,10 @@ export class ZodArray<
return this._def.type;
}

min(minLength: number, message?: errorUtil.ErrMessage): this {
min<N extends number>(
minLength: N,
message?: errorUtil.ErrMessage
): ZodArray<T, "atleast", N> {
return new ZodArray({
...this._def,
minLength: { value: minLength, message: errorUtil.toString(message) },
Expand All @@ -2090,14 +2106,17 @@ export class ZodArray<
}) as any;
}

length(len: number, message?: errorUtil.ErrMessage): this {
length<N extends number>(
len: N,
message?: errorUtil.ErrMessage
): ZodArray<T, "exactly", N> {
return new ZodArray({
...this._def,
exactLength: { value: len, message: errorUtil.toString(message) },
}) as any;
}

nonempty(message?: errorUtil.ErrMessage): ZodArray<T, "atleastone"> {
nonempty(message?: errorUtil.ErrMessage): ZodArray<T, "atleast", 1> {
return this.min(1, message) as any;
}

Expand All @@ -2116,7 +2135,7 @@ export class ZodArray<
};
}

export type ZodNonEmptyArray<T extends ZodTypeAny> = ZodArray<T, "atleastone">;
export type ZodNonEmptyArray<T extends ZodTypeAny> = ZodArray<T, "atleast", 1>;

/////////////////////////////////////////
/////////////////////////////////////////
Expand Down
5 changes: 4 additions & 1 deletion src/__tests__/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ type t1 = z.infer<typeof nonEmptyMax>;
util.assertEqual<[string, ...string[]], t1>(true);

type t2 = z.infer<typeof minTwo>;
util.assertEqual<string[], t2>(true);
util.assertEqual<[string, string, ...string[]], t2>(true);

type t3 = z.infer<typeof justTwo>;
util.assertEqual<[string, string], t3>(true);

test("passing validations", () => {
minTwo.parse(["a", "a"]);
Expand Down
43 changes: 31 additions & 12 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1974,22 +1974,35 @@ export interface ZodArrayDef<T extends ZodTypeAny = ZodTypeAny>
maxLength: { value: number; message?: string } | null;
}

export type ArrayCardinality = "many" | "atleastone";
export type ArrayCardinality = "many" | "atleast" | "exactly";

type FixedArray<T, N extends number, R extends T[] = []> = R["length"] extends N
? R
: FixedArray<T, N, [T, ...R]>;

type MinArray<T, N extends number> = [...FixedArray<T, N>, ...T[]];

export type arrayOutputType<
T extends ZodTypeAny,
Cardinality extends ArrayCardinality = "many"
> = Cardinality extends "atleastone"
? [T["_output"], ...T["_output"][]]
Cardinality extends ArrayCardinality = "many",
CardinalityAmount extends number = 0
> = Cardinality extends "atleast"
? MinArray<T["_output"], CardinalityAmount>
: Cardinality extends "exactly"
? FixedArray<T["_output"], CardinalityAmount>
: T["_output"][];

export class ZodArray<
T extends ZodTypeAny,
Cardinality extends ArrayCardinality = "many"
Cardinality extends ArrayCardinality = "many",
CardinalityAmount extends number = 0
> extends ZodType<
arrayOutputType<T, Cardinality>,
arrayOutputType<T, Cardinality, CardinalityAmount>,
ZodArrayDef<T>,
Cardinality extends "atleastone"
? [T["_input"], ...T["_input"][]]
Cardinality extends "atleast"
? MinArray<T["_input"], CardinalityAmount>
: Cardinality extends "exactly"
? FixedArray<T["_input"], CardinalityAmount>
: T["_input"][]
> {
_parse(input: ParseInput): ParseReturnType<this["_output"]> {
Expand Down Expand Up @@ -2076,7 +2089,10 @@ export class ZodArray<
return this._def.type;
}

min(minLength: number, message?: errorUtil.ErrMessage): this {
min<N extends number>(
minLength: N,
message?: errorUtil.ErrMessage
): ZodArray<T, "atleast", N> {
return new ZodArray({
...this._def,
minLength: { value: minLength, message: errorUtil.toString(message) },
Expand All @@ -2090,14 +2106,17 @@ export class ZodArray<
}) as any;
}

length(len: number, message?: errorUtil.ErrMessage): this {
length<N extends number>(
len: N,
message?: errorUtil.ErrMessage
): ZodArray<T, "exactly", N> {
return new ZodArray({
...this._def,
exactLength: { value: len, message: errorUtil.toString(message) },
}) as any;
}

nonempty(message?: errorUtil.ErrMessage): ZodArray<T, "atleastone"> {
nonempty(message?: errorUtil.ErrMessage): ZodArray<T, "atleast", 1> {
return this.min(1, message) as any;
}

Expand All @@ -2116,7 +2135,7 @@ export class ZodArray<
};
}

export type ZodNonEmptyArray<T extends ZodTypeAny> = ZodArray<T, "atleastone">;
export type ZodNonEmptyArray<T extends ZodTypeAny> = ZodArray<T, "atleast", 1>;

/////////////////////////////////////////
/////////////////////////////////////////
Expand Down
Loading