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

ZodString.minLength defaults to null to be consistent with maxLength #1248

Merged
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
2 changes: 2 additions & 0 deletions deno/lib/__tests__/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,11 @@ test("checks getters", () => {
test("min max getters", () => {
expect(z.string().min(5).minLength).toEqual(5);
expect(z.string().min(5).min(10).minLength).toEqual(10);
expect(z.string().minLength).toEqual(null);

expect(z.string().max(5).maxLength).toEqual(5);
expect(z.string().max(5).max(1).maxLength).toEqual(1);
expect(z.string().maxLength).toEqual(null);
});

test("trim", () => {
Expand Down
18 changes: 7 additions & 11 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,25 +662,21 @@ export class ZodString extends ZodType<string, ZodStringDef> {
return !!this._def.checks.find((ch) => ch.kind === "cuid");
}
get minLength() {
let min: number | null = -Infinity;
this._def.checks.map((ch) => {
let min: number | null = null;
for (const ch of this._def.checks) {
if (ch.kind === "min") {
if (min === null || ch.value > min) {
min = ch.value;
}
if (min === null || ch.value > min) min = ch.value;
}
});
}
return min;
}
get maxLength() {
let max: number | null = null;
this._def.checks.map((ch) => {
for (const ch of this._def.checks) {
if (ch.kind === "max") {
if (max === null || ch.value < max) {
max = ch.value;
}
if (max === null || ch.value < max) max = ch.value;
}
});
}
return max;
}

Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ test("checks getters", () => {
test("min max getters", () => {
expect(z.string().min(5).minLength).toEqual(5);
expect(z.string().min(5).min(10).minLength).toEqual(10);
expect(z.string().minLength).toEqual(null);

expect(z.string().max(5).maxLength).toEqual(5);
expect(z.string().max(5).max(1).maxLength).toEqual(1);
expect(z.string().maxLength).toEqual(null);
});

test("trim", () => {
Expand Down
18 changes: 7 additions & 11 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,25 +662,21 @@ export class ZodString extends ZodType<string, ZodStringDef> {
return !!this._def.checks.find((ch) => ch.kind === "cuid");
}
get minLength() {
let min: number | null = -Infinity;
this._def.checks.map((ch) => {
let min: number | null = null;
for (const ch of this._def.checks) {
if (ch.kind === "min") {
if (min === null || ch.value > min) {
min = ch.value;
}
if (min === null || ch.value > min) min = ch.value;
}
});
}
return min;
}
get maxLength() {
let max: number | null = null;
this._def.checks.map((ch) => {
for (const ch of this._def.checks) {
if (ch.kind === "max") {
if (max === null || ch.value < max) {
max = ch.value;
}
if (max === null || ch.value < max) max = ch.value;
}
});
}
return max;
}

Expand Down