Skip to content

Commit

Permalink
ZodString.minLength defaults to null to be consistent with maxLength (#…
Browse files Browse the repository at this point in the history
…1248)

* Replaced maxLength null value to Infinity and improved min/max syntax

* Added tests

* Improved consistency between ZodNumber and ZodString
  • Loading branch information
Balastrong authored Jul 17, 2022
1 parent 0af1085 commit d78047e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
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

0 comments on commit d78047e

Please sign in to comment.