(
Classes: {
Codec?: typeof schemas.$ZodCodec;
Boolean?: typeof schemas.$ZodBoolean;
String?: typeof schemas.$ZodString;
},
_params?: string | $ZodStringBoolParams
)
| 1736 | |
| 1737 | // @__NO_SIDE_EFFECTS__ |
| 1738 | export function _stringbool( |
| 1739 | Classes: { |
| 1740 | Codec?: typeof schemas.$ZodCodec; |
| 1741 | Boolean?: typeof schemas.$ZodBoolean; |
| 1742 | String?: typeof schemas.$ZodString; |
| 1743 | }, |
| 1744 | _params?: string | $ZodStringBoolParams |
| 1745 | ): schemas.$ZodCodec<schemas.$ZodString, schemas.$ZodBoolean> { |
| 1746 | const params = util.normalizeParams(_params); |
| 1747 | |
| 1748 | let truthyArray = params.truthy ?? ["true", "1", "yes", "on", "y", "enabled"]; |
| 1749 | let falsyArray = params.falsy ?? ["false", "0", "no", "off", "n", "disabled"]; |
| 1750 | if (params.case !== "sensitive") { |
| 1751 | truthyArray = truthyArray.map((v) => (typeof v === "string" ? v.toLowerCase() : v)); |
| 1752 | falsyArray = falsyArray.map((v) => (typeof v === "string" ? v.toLowerCase() : v)); |
| 1753 | } |
| 1754 | |
| 1755 | const truthySet = new Set(truthyArray); |
| 1756 | const falsySet = new Set(falsyArray); |
| 1757 | |
| 1758 | const _Codec = Classes.Codec ?? schemas.$ZodCodec; |
| 1759 | const _Boolean = Classes.Boolean ?? schemas.$ZodBoolean; |
| 1760 | const _String = Classes.String ?? schemas.$ZodString; |
| 1761 | |
| 1762 | const stringSchema = new _String({ type: "string", error: params.error }); |
| 1763 | const booleanSchema = new _Boolean({ type: "boolean", error: params.error }); |
| 1764 | |
| 1765 | const codec = new _Codec({ |
| 1766 | type: "pipe", |
| 1767 | in: stringSchema as any, |
| 1768 | out: booleanSchema as any, |
| 1769 | transform: ((input: string, payload: schemas.ParsePayload<string>) => { |
| 1770 | let data: string = input; |
| 1771 | if (params.case !== "sensitive") data = data.toLowerCase(); |
| 1772 | if (truthySet.has(data)) { |
| 1773 | return true; |
| 1774 | } else if (falsySet.has(data)) { |
| 1775 | return false; |
| 1776 | } else { |
| 1777 | payload.issues.push({ |
| 1778 | code: "invalid_value", |
| 1779 | expected: "stringbool", |
| 1780 | values: [...truthySet, ...falsySet], |
| 1781 | input: payload.value, |
| 1782 | inst: codec, |
| 1783 | continue: false, |
| 1784 | }); |
| 1785 | return {} as never; |
| 1786 | } |
| 1787 | }) as any, |
| 1788 | reverseTransform: ((input: boolean, _payload: schemas.ParsePayload<boolean>) => { |
| 1789 | if (input === true) { |
| 1790 | return truthyArray[0] || "true"; |
| 1791 | } else { |
| 1792 | return falsyArray[0] || "false"; |
| 1793 | } |
| 1794 | }) as any, |
| 1795 | error: params.error, |
nothing calls this directly
no test coverage detected