| 729 | } |
| 730 | |
| 731 | export class ZodString extends ZodType<string, ZodStringDef, string> { |
| 732 | _parse(input: ParseInput): ParseReturnType<string> { |
| 733 | if (this._def.coerce) { |
| 734 | input.data = String(input.data); |
| 735 | } |
| 736 | const parsedType = this._getType(input); |
| 737 | |
| 738 | if (parsedType !== ZodParsedType.string) { |
| 739 | const ctx = this._getOrReturnCtx(input); |
| 740 | addIssueToContext(ctx, { |
| 741 | code: ZodIssueCode.invalid_type, |
| 742 | expected: ZodParsedType.string, |
| 743 | received: ctx.parsedType, |
| 744 | }); |
| 745 | return INVALID; |
| 746 | } |
| 747 | |
| 748 | const status = new ParseStatus(); |
| 749 | let ctx: undefined | ParseContext = undefined; |
| 750 | |
| 751 | for (const check of this._def.checks) { |
| 752 | if (check.kind === "min") { |
| 753 | if (input.data.length < check.value) { |
| 754 | ctx = this._getOrReturnCtx(input, ctx); |
| 755 | addIssueToContext(ctx, { |
| 756 | code: ZodIssueCode.too_small, |
| 757 | minimum: check.value, |
| 758 | type: "string", |
| 759 | inclusive: true, |
| 760 | exact: false, |
| 761 | message: check.message, |
| 762 | }); |
| 763 | status.dirty(); |
| 764 | } |
| 765 | } else if (check.kind === "max") { |
| 766 | if (input.data.length > check.value) { |
| 767 | ctx = this._getOrReturnCtx(input, ctx); |
| 768 | addIssueToContext(ctx, { |
| 769 | code: ZodIssueCode.too_big, |
| 770 | maximum: check.value, |
| 771 | type: "string", |
| 772 | inclusive: true, |
| 773 | exact: false, |
| 774 | message: check.message, |
| 775 | }); |
| 776 | status.dirty(); |
| 777 | } |
| 778 | } else if (check.kind === "length") { |
| 779 | const tooBig = input.data.length > check.value; |
| 780 | const tooSmall = input.data.length < check.value; |
| 781 | if (tooBig || tooSmall) { |
| 782 | ctx = this._getOrReturnCtx(input, ctx); |
| 783 | if (tooBig) { |
| 784 | addIssueToContext(ctx, { |
| 785 | code: ZodIssueCode.too_big, |
| 786 | maximum: check.value, |
| 787 | type: "string", |
| 788 | inclusive: true, |
nothing calls this directly
no test coverage detected