MCPcopy
hub / github.com/colinhacks/zod / ZodDate

Class ZodDate

packages/zod/src/v3/types.ts:1877–1996  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1875}
1876
1877export class ZodDate extends ZodType<Date, ZodDateDef, Date> {
1878 _parse(input: ParseInput): ParseReturnType<this["_output"]> {
1879 if (this._def.coerce) {
1880 input.data = new Date(input.data);
1881 }
1882 const parsedType = this._getType(input);
1883
1884 if (parsedType !== ZodParsedType.date) {
1885 const ctx = this._getOrReturnCtx(input);
1886 addIssueToContext(ctx, {
1887 code: ZodIssueCode.invalid_type,
1888 expected: ZodParsedType.date,
1889 received: ctx.parsedType,
1890 });
1891 return INVALID;
1892 }
1893
1894 if (Number.isNaN(input.data.getTime())) {
1895 const ctx = this._getOrReturnCtx(input);
1896 addIssueToContext(ctx, {
1897 code: ZodIssueCode.invalid_date,
1898 });
1899 return INVALID;
1900 }
1901
1902 const status = new ParseStatus();
1903 let ctx: undefined | ParseContext = undefined;
1904
1905 for (const check of this._def.checks) {
1906 if (check.kind === "min") {
1907 if (input.data.getTime() < check.value) {
1908 ctx = this._getOrReturnCtx(input, ctx);
1909 addIssueToContext(ctx, {
1910 code: ZodIssueCode.too_small,
1911 message: check.message,
1912 inclusive: true,
1913 exact: false,
1914 minimum: check.value,
1915 type: "date",
1916 });
1917 status.dirty();
1918 }
1919 } else if (check.kind === "max") {
1920 if (input.data.getTime() > check.value) {
1921 ctx = this._getOrReturnCtx(input, ctx);
1922 addIssueToContext(ctx, {
1923 code: ZodIssueCode.too_big,
1924 message: check.message,
1925 inclusive: true,
1926 exact: false,
1927 maximum: check.value,
1928 type: "date",
1929 });
1930 status.dirty();
1931 }
1932 } else {
1933 util.assertNever(check);
1934 }

Callers

nothing calls this directly

Calls 1

processCreateParamsFunction · 0.85

Tested by

no test coverage detected