Skip to content

Commit

Permalink
feat: trim method for ZodString (#681)
Browse files Browse the repository at this point in the history
* feat: trim method for ZodString

* Update implementation

* Add documentation

* Fix conflict

Co-authored-by: Colin McDonnell <colinmcd@alum.mit.edu>
  • Loading branch information
Djaler and Colin McDonnell committed May 22, 2022
1 parent ff5de24 commit 4459e75
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,9 @@ z.string().uuid();
z.string().cuid();
z.string().regex(regex);

// trim whitespace
z.string().trim();

// deprecated, equivalent to .min(1)
z.string().nonempty();

Expand Down
8 changes: 8 additions & 0 deletions deno/lib/__tests__/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,11 @@ test("min max getters", () => {
expect(z.string().max(5).maxLength).toEqual(5);
expect(z.string().max(5).max(1).maxLength).toEqual(1);
});

test("trim", () => {
expect(z.string().trim().min(2).parse(" 12 ")).toEqual("12");

// ordering of methods is respected
expect(z.string().min(2).trim().parse(" 1 ")).toEqual("1");
expect(() => z.string().trim().min(2).parse(" 1 ")).toThrow();
});
14 changes: 13 additions & 1 deletion deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,8 @@ type ZodStringCheck =
| { kind: "url"; message?: string }
| { kind: "uuid"; message?: string }
| { kind: "cuid"; message?: string }
| { kind: "regex"; regex: RegExp; message?: string };
| { kind: "regex"; regex: RegExp; message?: string }
| { kind: "trim"; message?: string };

export interface ZodStringDef extends ZodTypeDef {
checks: ZodStringCheck[];
Expand Down Expand Up @@ -569,6 +570,10 @@ export class ZodString extends ZodType<string, ZodStringDef> {
});
status.dirty();
}
} else if (check.kind === "trim") {
input.data = input.data.trim();
} else {
util.assertNever(check);
}
}

Expand Down Expand Up @@ -640,6 +645,12 @@ export class ZodString extends ZodType<string, ZodStringDef> {
nonempty = (message?: errorUtil.ErrMessage) =>
this.min(1, errorUtil.errToObj(message));

trim = () =>
new ZodString({
...this._def,
checks: [...this._def.checks, { kind: "trim" }],
});

get isEmail() {
return !!this._def.checks.find((ch) => ch.kind === "email");
}
Expand Down Expand Up @@ -674,6 +685,7 @@ export class ZodString extends ZodType<string, ZodStringDef> {
});
return max;
}

static create = (params?: RawCreateParams): ZodString => {
return new ZodString({
checks: [],
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,11 @@ test("min max getters", () => {
expect(z.string().max(5).maxLength).toEqual(5);
expect(z.string().max(5).max(1).maxLength).toEqual(1);
});

test("trim", () => {
expect(z.string().trim().min(2).parse(" 12 ")).toEqual("12");

// ordering of methods is respected
expect(z.string().min(2).trim().parse(" 1 ")).toEqual("1");
expect(() => z.string().trim().min(2).parse(" 1 ")).toThrow();
});
14 changes: 13 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,8 @@ type ZodStringCheck =
| { kind: "url"; message?: string }
| { kind: "uuid"; message?: string }
| { kind: "cuid"; message?: string }
| { kind: "regex"; regex: RegExp; message?: string };
| { kind: "regex"; regex: RegExp; message?: string }
| { kind: "trim"; message?: string };

export interface ZodStringDef extends ZodTypeDef {
checks: ZodStringCheck[];
Expand Down Expand Up @@ -569,6 +570,10 @@ export class ZodString extends ZodType<string, ZodStringDef> {
});
status.dirty();
}
} else if (check.kind === "trim") {
input.data = input.data.trim();
} else {
util.assertNever(check);
}
}

Expand Down Expand Up @@ -640,6 +645,12 @@ export class ZodString extends ZodType<string, ZodStringDef> {
nonempty = (message?: errorUtil.ErrMessage) =>
this.min(1, errorUtil.errToObj(message));

trim = () =>
new ZodString({
...this._def,
checks: [...this._def.checks, { kind: "trim" }],
});

get isEmail() {
return !!this._def.checks.find((ch) => ch.kind === "email");
}
Expand Down Expand Up @@ -674,6 +685,7 @@ export class ZodString extends ZodType<string, ZodStringDef> {
});
return max;
}

static create = (params?: RawCreateParams): ZodString => {
return new ZodString({
checks: [],
Expand Down

0 comments on commit 4459e75

Please sign in to comment.