git-coco 0.14.8 → 0.14.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.esm.mjs +4533 -126
- package/dist/index.js +4532 -125
- package/package.json +10 -10
package/dist/index.js
CHANGED
|
@@ -77,7 +77,7 @@ var readline__namespace = /*#__PURE__*/_interopNamespaceDefault(readline$1);
|
|
|
77
77
|
/**
|
|
78
78
|
* Current build version from package.json
|
|
79
79
|
*/
|
|
80
|
-
const BUILD_VERSION = "0.14.
|
|
80
|
+
const BUILD_VERSION = "0.14.10";
|
|
81
81
|
|
|
82
82
|
const isInteractive = (config) => {
|
|
83
83
|
return config?.mode === 'interactive' || !!config?.interactive;
|
|
@@ -732,6 +732,10 @@ const schema$1 = {
|
|
|
732
732
|
"type": "number",
|
|
733
733
|
"description": "Maximum number of tokens to generate in the completion. -1 returns as many tokens as possible given the prompt and the model's maximum context size."
|
|
734
734
|
},
|
|
735
|
+
"maxCompletionTokens": {
|
|
736
|
+
"type": "number",
|
|
737
|
+
"description": "Maximum number of tokens to generate in the completion. -1 returns as many tokens as possible given the prompt and the model's maximum context size. Alias for `maxTokens` for reasoning models."
|
|
738
|
+
},
|
|
735
739
|
"topP": {
|
|
736
740
|
"type": "number",
|
|
737
741
|
"description": "Total probability mass of tokens to consider at each step"
|
|
@@ -770,7 +774,8 @@ const schema$1 = {
|
|
|
770
774
|
},
|
|
771
775
|
"modelName": {
|
|
772
776
|
"type": "string",
|
|
773
|
-
"description": "Model name to use Alias for `model`"
|
|
777
|
+
"description": "Model name to use Alias for `model`",
|
|
778
|
+
"deprecated": "Use \"model\" instead."
|
|
774
779
|
},
|
|
775
780
|
"model": {
|
|
776
781
|
"type": "string",
|
|
@@ -1798,75 +1803,4314 @@ class Logger {
|
|
|
1798
1803
|
};
|
|
1799
1804
|
return this;
|
|
1800
1805
|
}
|
|
1801
|
-
log(message, options = { color: 'blue' }) {
|
|
1802
|
-
if (this.config?.silent) {
|
|
1803
|
-
return this;
|
|
1806
|
+
log(message, options = { color: 'blue' }) {
|
|
1807
|
+
if (this.config?.silent) {
|
|
1808
|
+
return this;
|
|
1809
|
+
}
|
|
1810
|
+
let outputMessage = message;
|
|
1811
|
+
if (options.color) {
|
|
1812
|
+
outputMessage = chalk[options.color](outputMessage);
|
|
1813
|
+
}
|
|
1814
|
+
console.log(outputMessage);
|
|
1815
|
+
return this;
|
|
1816
|
+
}
|
|
1817
|
+
verbose(message, options = {}) {
|
|
1818
|
+
if (!this.config?.verbose || this.config?.silent) {
|
|
1819
|
+
return this;
|
|
1820
|
+
}
|
|
1821
|
+
this.log(message, options);
|
|
1822
|
+
return this;
|
|
1823
|
+
}
|
|
1824
|
+
startTimer() {
|
|
1825
|
+
this.timerStart = now();
|
|
1826
|
+
return this;
|
|
1827
|
+
}
|
|
1828
|
+
stopTimer(message, options = { color: 'yellow' }) {
|
|
1829
|
+
if (!this.config?.verbose || !this.timerStart || this.config?.silent) {
|
|
1830
|
+
return this;
|
|
1831
|
+
}
|
|
1832
|
+
const elapsedTime = prettyMilliseconds(now() - this.timerStart);
|
|
1833
|
+
let outputMessage = message ? `${message} (⏲ ${elapsedTime})` : `⏲ ${elapsedTime}`;
|
|
1834
|
+
if (options.color) {
|
|
1835
|
+
outputMessage = chalk[options.color](outputMessage);
|
|
1836
|
+
}
|
|
1837
|
+
console.log(outputMessage);
|
|
1838
|
+
return this;
|
|
1839
|
+
}
|
|
1840
|
+
startSpinner(message, options = { color: 'green' }) {
|
|
1841
|
+
if (this.config?.silent) {
|
|
1842
|
+
return this;
|
|
1843
|
+
}
|
|
1844
|
+
const spinnerMessage = options.color ? chalk[options.color](message) : message;
|
|
1845
|
+
this.spinner = ora(spinnerMessage).start();
|
|
1846
|
+
return this;
|
|
1847
|
+
}
|
|
1848
|
+
stopSpinner(message = '', options = { mode: 'succeed', color: 'green' }) {
|
|
1849
|
+
if (this.config?.silent) {
|
|
1850
|
+
return this;
|
|
1851
|
+
}
|
|
1852
|
+
const spinnerMessage = options?.color ? chalk[options.color](message) : message;
|
|
1853
|
+
this.spinner?.[options.mode || 'succeed'](spinnerMessage);
|
|
1854
|
+
this.spinner = null;
|
|
1855
|
+
return this;
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
|
|
1859
|
+
function commandExecutor(handler) {
|
|
1860
|
+
return async (argv) => {
|
|
1861
|
+
const options = loadConfig(argv);
|
|
1862
|
+
const logger = new Logger(options);
|
|
1863
|
+
try {
|
|
1864
|
+
await handler(argv, logger);
|
|
1865
|
+
}
|
|
1866
|
+
catch (error) {
|
|
1867
|
+
logger.log('\nFailed to execute command', { color: 'yellow' });
|
|
1868
|
+
logger.verbose(`\nError: "${error.message}"`, { color: 'red' });
|
|
1869
|
+
logger.log('\nThanks for using coco, make it a great day! 👋🤖', { color: 'blue' });
|
|
1870
|
+
process.exit(0);
|
|
1871
|
+
}
|
|
1872
|
+
};
|
|
1873
|
+
}
|
|
1874
|
+
|
|
1875
|
+
var util;
|
|
1876
|
+
(function (util) {
|
|
1877
|
+
util.assertEqual = (val) => val;
|
|
1878
|
+
function assertIs(_arg) { }
|
|
1879
|
+
util.assertIs = assertIs;
|
|
1880
|
+
function assertNever(_x) {
|
|
1881
|
+
throw new Error();
|
|
1882
|
+
}
|
|
1883
|
+
util.assertNever = assertNever;
|
|
1884
|
+
util.arrayToEnum = (items) => {
|
|
1885
|
+
const obj = {};
|
|
1886
|
+
for (const item of items) {
|
|
1887
|
+
obj[item] = item;
|
|
1888
|
+
}
|
|
1889
|
+
return obj;
|
|
1890
|
+
};
|
|
1891
|
+
util.getValidEnumValues = (obj) => {
|
|
1892
|
+
const validKeys = util.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
|
|
1893
|
+
const filtered = {};
|
|
1894
|
+
for (const k of validKeys) {
|
|
1895
|
+
filtered[k] = obj[k];
|
|
1896
|
+
}
|
|
1897
|
+
return util.objectValues(filtered);
|
|
1898
|
+
};
|
|
1899
|
+
util.objectValues = (obj) => {
|
|
1900
|
+
return util.objectKeys(obj).map(function (e) {
|
|
1901
|
+
return obj[e];
|
|
1902
|
+
});
|
|
1903
|
+
};
|
|
1904
|
+
util.objectKeys = typeof Object.keys === "function" // eslint-disable-line ban/ban
|
|
1905
|
+
? (obj) => Object.keys(obj) // eslint-disable-line ban/ban
|
|
1906
|
+
: (object) => {
|
|
1907
|
+
const keys = [];
|
|
1908
|
+
for (const key in object) {
|
|
1909
|
+
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
1910
|
+
keys.push(key);
|
|
1911
|
+
}
|
|
1912
|
+
}
|
|
1913
|
+
return keys;
|
|
1914
|
+
};
|
|
1915
|
+
util.find = (arr, checker) => {
|
|
1916
|
+
for (const item of arr) {
|
|
1917
|
+
if (checker(item))
|
|
1918
|
+
return item;
|
|
1919
|
+
}
|
|
1920
|
+
return undefined;
|
|
1921
|
+
};
|
|
1922
|
+
util.isInteger = typeof Number.isInteger === "function"
|
|
1923
|
+
? (val) => Number.isInteger(val) // eslint-disable-line ban/ban
|
|
1924
|
+
: (val) => typeof val === "number" && isFinite(val) && Math.floor(val) === val;
|
|
1925
|
+
function joinValues(array, separator = " | ") {
|
|
1926
|
+
return array
|
|
1927
|
+
.map((val) => (typeof val === "string" ? `'${val}'` : val))
|
|
1928
|
+
.join(separator);
|
|
1929
|
+
}
|
|
1930
|
+
util.joinValues = joinValues;
|
|
1931
|
+
util.jsonStringifyReplacer = (_, value) => {
|
|
1932
|
+
if (typeof value === "bigint") {
|
|
1933
|
+
return value.toString();
|
|
1934
|
+
}
|
|
1935
|
+
return value;
|
|
1936
|
+
};
|
|
1937
|
+
})(util || (util = {}));
|
|
1938
|
+
var objectUtil;
|
|
1939
|
+
(function (objectUtil) {
|
|
1940
|
+
objectUtil.mergeShapes = (first, second) => {
|
|
1941
|
+
return {
|
|
1942
|
+
...first,
|
|
1943
|
+
...second, // second overwrites first
|
|
1944
|
+
};
|
|
1945
|
+
};
|
|
1946
|
+
})(objectUtil || (objectUtil = {}));
|
|
1947
|
+
const ZodParsedType = util.arrayToEnum([
|
|
1948
|
+
"string",
|
|
1949
|
+
"nan",
|
|
1950
|
+
"number",
|
|
1951
|
+
"integer",
|
|
1952
|
+
"float",
|
|
1953
|
+
"boolean",
|
|
1954
|
+
"date",
|
|
1955
|
+
"bigint",
|
|
1956
|
+
"symbol",
|
|
1957
|
+
"function",
|
|
1958
|
+
"undefined",
|
|
1959
|
+
"null",
|
|
1960
|
+
"array",
|
|
1961
|
+
"object",
|
|
1962
|
+
"unknown",
|
|
1963
|
+
"promise",
|
|
1964
|
+
"void",
|
|
1965
|
+
"never",
|
|
1966
|
+
"map",
|
|
1967
|
+
"set",
|
|
1968
|
+
]);
|
|
1969
|
+
const getParsedType = (data) => {
|
|
1970
|
+
const t = typeof data;
|
|
1971
|
+
switch (t) {
|
|
1972
|
+
case "undefined":
|
|
1973
|
+
return ZodParsedType.undefined;
|
|
1974
|
+
case "string":
|
|
1975
|
+
return ZodParsedType.string;
|
|
1976
|
+
case "number":
|
|
1977
|
+
return isNaN(data) ? ZodParsedType.nan : ZodParsedType.number;
|
|
1978
|
+
case "boolean":
|
|
1979
|
+
return ZodParsedType.boolean;
|
|
1980
|
+
case "function":
|
|
1981
|
+
return ZodParsedType.function;
|
|
1982
|
+
case "bigint":
|
|
1983
|
+
return ZodParsedType.bigint;
|
|
1984
|
+
case "symbol":
|
|
1985
|
+
return ZodParsedType.symbol;
|
|
1986
|
+
case "object":
|
|
1987
|
+
if (Array.isArray(data)) {
|
|
1988
|
+
return ZodParsedType.array;
|
|
1989
|
+
}
|
|
1990
|
+
if (data === null) {
|
|
1991
|
+
return ZodParsedType.null;
|
|
1992
|
+
}
|
|
1993
|
+
if (data.then &&
|
|
1994
|
+
typeof data.then === "function" &&
|
|
1995
|
+
data.catch &&
|
|
1996
|
+
typeof data.catch === "function") {
|
|
1997
|
+
return ZodParsedType.promise;
|
|
1998
|
+
}
|
|
1999
|
+
if (typeof Map !== "undefined" && data instanceof Map) {
|
|
2000
|
+
return ZodParsedType.map;
|
|
2001
|
+
}
|
|
2002
|
+
if (typeof Set !== "undefined" && data instanceof Set) {
|
|
2003
|
+
return ZodParsedType.set;
|
|
2004
|
+
}
|
|
2005
|
+
if (typeof Date !== "undefined" && data instanceof Date) {
|
|
2006
|
+
return ZodParsedType.date;
|
|
2007
|
+
}
|
|
2008
|
+
return ZodParsedType.object;
|
|
2009
|
+
default:
|
|
2010
|
+
return ZodParsedType.unknown;
|
|
2011
|
+
}
|
|
2012
|
+
};
|
|
2013
|
+
|
|
2014
|
+
const ZodIssueCode = util.arrayToEnum([
|
|
2015
|
+
"invalid_type",
|
|
2016
|
+
"invalid_literal",
|
|
2017
|
+
"custom",
|
|
2018
|
+
"invalid_union",
|
|
2019
|
+
"invalid_union_discriminator",
|
|
2020
|
+
"invalid_enum_value",
|
|
2021
|
+
"unrecognized_keys",
|
|
2022
|
+
"invalid_arguments",
|
|
2023
|
+
"invalid_return_type",
|
|
2024
|
+
"invalid_date",
|
|
2025
|
+
"invalid_string",
|
|
2026
|
+
"too_small",
|
|
2027
|
+
"too_big",
|
|
2028
|
+
"invalid_intersection_types",
|
|
2029
|
+
"not_multiple_of",
|
|
2030
|
+
"not_finite",
|
|
2031
|
+
]);
|
|
2032
|
+
const quotelessJson = (obj) => {
|
|
2033
|
+
const json = JSON.stringify(obj, null, 2);
|
|
2034
|
+
return json.replace(/"([^"]+)":/g, "$1:");
|
|
2035
|
+
};
|
|
2036
|
+
class ZodError extends Error {
|
|
2037
|
+
constructor(issues) {
|
|
2038
|
+
super();
|
|
2039
|
+
this.issues = [];
|
|
2040
|
+
this.addIssue = (sub) => {
|
|
2041
|
+
this.issues = [...this.issues, sub];
|
|
2042
|
+
};
|
|
2043
|
+
this.addIssues = (subs = []) => {
|
|
2044
|
+
this.issues = [...this.issues, ...subs];
|
|
2045
|
+
};
|
|
2046
|
+
const actualProto = new.target.prototype;
|
|
2047
|
+
if (Object.setPrototypeOf) {
|
|
2048
|
+
// eslint-disable-next-line ban/ban
|
|
2049
|
+
Object.setPrototypeOf(this, actualProto);
|
|
2050
|
+
}
|
|
2051
|
+
else {
|
|
2052
|
+
this.__proto__ = actualProto;
|
|
2053
|
+
}
|
|
2054
|
+
this.name = "ZodError";
|
|
2055
|
+
this.issues = issues;
|
|
2056
|
+
}
|
|
2057
|
+
get errors() {
|
|
2058
|
+
return this.issues;
|
|
2059
|
+
}
|
|
2060
|
+
format(_mapper) {
|
|
2061
|
+
const mapper = _mapper ||
|
|
2062
|
+
function (issue) {
|
|
2063
|
+
return issue.message;
|
|
2064
|
+
};
|
|
2065
|
+
const fieldErrors = { _errors: [] };
|
|
2066
|
+
const processError = (error) => {
|
|
2067
|
+
for (const issue of error.issues) {
|
|
2068
|
+
if (issue.code === "invalid_union") {
|
|
2069
|
+
issue.unionErrors.map(processError);
|
|
2070
|
+
}
|
|
2071
|
+
else if (issue.code === "invalid_return_type") {
|
|
2072
|
+
processError(issue.returnTypeError);
|
|
2073
|
+
}
|
|
2074
|
+
else if (issue.code === "invalid_arguments") {
|
|
2075
|
+
processError(issue.argumentsError);
|
|
2076
|
+
}
|
|
2077
|
+
else if (issue.path.length === 0) {
|
|
2078
|
+
fieldErrors._errors.push(mapper(issue));
|
|
2079
|
+
}
|
|
2080
|
+
else {
|
|
2081
|
+
let curr = fieldErrors;
|
|
2082
|
+
let i = 0;
|
|
2083
|
+
while (i < issue.path.length) {
|
|
2084
|
+
const el = issue.path[i];
|
|
2085
|
+
const terminal = i === issue.path.length - 1;
|
|
2086
|
+
if (!terminal) {
|
|
2087
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
2088
|
+
// if (typeof el === "string") {
|
|
2089
|
+
// curr[el] = curr[el] || { _errors: [] };
|
|
2090
|
+
// } else if (typeof el === "number") {
|
|
2091
|
+
// const errorArray: any = [];
|
|
2092
|
+
// errorArray._errors = [];
|
|
2093
|
+
// curr[el] = curr[el] || errorArray;
|
|
2094
|
+
// }
|
|
2095
|
+
}
|
|
2096
|
+
else {
|
|
2097
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
2098
|
+
curr[el]._errors.push(mapper(issue));
|
|
2099
|
+
}
|
|
2100
|
+
curr = curr[el];
|
|
2101
|
+
i++;
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
}
|
|
2105
|
+
};
|
|
2106
|
+
processError(this);
|
|
2107
|
+
return fieldErrors;
|
|
2108
|
+
}
|
|
2109
|
+
static assert(value) {
|
|
2110
|
+
if (!(value instanceof ZodError)) {
|
|
2111
|
+
throw new Error(`Not a ZodError: ${value}`);
|
|
2112
|
+
}
|
|
2113
|
+
}
|
|
2114
|
+
toString() {
|
|
2115
|
+
return this.message;
|
|
2116
|
+
}
|
|
2117
|
+
get message() {
|
|
2118
|
+
return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
|
|
2119
|
+
}
|
|
2120
|
+
get isEmpty() {
|
|
2121
|
+
return this.issues.length === 0;
|
|
2122
|
+
}
|
|
2123
|
+
flatten(mapper = (issue) => issue.message) {
|
|
2124
|
+
const fieldErrors = {};
|
|
2125
|
+
const formErrors = [];
|
|
2126
|
+
for (const sub of this.issues) {
|
|
2127
|
+
if (sub.path.length > 0) {
|
|
2128
|
+
fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
|
|
2129
|
+
fieldErrors[sub.path[0]].push(mapper(sub));
|
|
2130
|
+
}
|
|
2131
|
+
else {
|
|
2132
|
+
formErrors.push(mapper(sub));
|
|
2133
|
+
}
|
|
2134
|
+
}
|
|
2135
|
+
return { formErrors, fieldErrors };
|
|
2136
|
+
}
|
|
2137
|
+
get formErrors() {
|
|
2138
|
+
return this.flatten();
|
|
2139
|
+
}
|
|
2140
|
+
}
|
|
2141
|
+
ZodError.create = (issues) => {
|
|
2142
|
+
const error = new ZodError(issues);
|
|
2143
|
+
return error;
|
|
2144
|
+
};
|
|
2145
|
+
|
|
2146
|
+
const errorMap = (issue, _ctx) => {
|
|
2147
|
+
let message;
|
|
2148
|
+
switch (issue.code) {
|
|
2149
|
+
case ZodIssueCode.invalid_type:
|
|
2150
|
+
if (issue.received === ZodParsedType.undefined) {
|
|
2151
|
+
message = "Required";
|
|
2152
|
+
}
|
|
2153
|
+
else {
|
|
2154
|
+
message = `Expected ${issue.expected}, received ${issue.received}`;
|
|
2155
|
+
}
|
|
2156
|
+
break;
|
|
2157
|
+
case ZodIssueCode.invalid_literal:
|
|
2158
|
+
message = `Invalid literal value, expected ${JSON.stringify(issue.expected, util.jsonStringifyReplacer)}`;
|
|
2159
|
+
break;
|
|
2160
|
+
case ZodIssueCode.unrecognized_keys:
|
|
2161
|
+
message = `Unrecognized key(s) in object: ${util.joinValues(issue.keys, ", ")}`;
|
|
2162
|
+
break;
|
|
2163
|
+
case ZodIssueCode.invalid_union:
|
|
2164
|
+
message = `Invalid input`;
|
|
2165
|
+
break;
|
|
2166
|
+
case ZodIssueCode.invalid_union_discriminator:
|
|
2167
|
+
message = `Invalid discriminator value. Expected ${util.joinValues(issue.options)}`;
|
|
2168
|
+
break;
|
|
2169
|
+
case ZodIssueCode.invalid_enum_value:
|
|
2170
|
+
message = `Invalid enum value. Expected ${util.joinValues(issue.options)}, received '${issue.received}'`;
|
|
2171
|
+
break;
|
|
2172
|
+
case ZodIssueCode.invalid_arguments:
|
|
2173
|
+
message = `Invalid function arguments`;
|
|
2174
|
+
break;
|
|
2175
|
+
case ZodIssueCode.invalid_return_type:
|
|
2176
|
+
message = `Invalid function return type`;
|
|
2177
|
+
break;
|
|
2178
|
+
case ZodIssueCode.invalid_date:
|
|
2179
|
+
message = `Invalid date`;
|
|
2180
|
+
break;
|
|
2181
|
+
case ZodIssueCode.invalid_string:
|
|
2182
|
+
if (typeof issue.validation === "object") {
|
|
2183
|
+
if ("includes" in issue.validation) {
|
|
2184
|
+
message = `Invalid input: must include "${issue.validation.includes}"`;
|
|
2185
|
+
if (typeof issue.validation.position === "number") {
|
|
2186
|
+
message = `${message} at one or more positions greater than or equal to ${issue.validation.position}`;
|
|
2187
|
+
}
|
|
2188
|
+
}
|
|
2189
|
+
else if ("startsWith" in issue.validation) {
|
|
2190
|
+
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
|
|
2191
|
+
}
|
|
2192
|
+
else if ("endsWith" in issue.validation) {
|
|
2193
|
+
message = `Invalid input: must end with "${issue.validation.endsWith}"`;
|
|
2194
|
+
}
|
|
2195
|
+
else {
|
|
2196
|
+
util.assertNever(issue.validation);
|
|
2197
|
+
}
|
|
2198
|
+
}
|
|
2199
|
+
else if (issue.validation !== "regex") {
|
|
2200
|
+
message = `Invalid ${issue.validation}`;
|
|
2201
|
+
}
|
|
2202
|
+
else {
|
|
2203
|
+
message = "Invalid";
|
|
2204
|
+
}
|
|
2205
|
+
break;
|
|
2206
|
+
case ZodIssueCode.too_small:
|
|
2207
|
+
if (issue.type === "array")
|
|
2208
|
+
message = `Array must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `more than`} ${issue.minimum} element(s)`;
|
|
2209
|
+
else if (issue.type === "string")
|
|
2210
|
+
message = `String must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
|
|
2211
|
+
else if (issue.type === "number")
|
|
2212
|
+
message = `Number must be ${issue.exact
|
|
2213
|
+
? `exactly equal to `
|
|
2214
|
+
: issue.inclusive
|
|
2215
|
+
? `greater than or equal to `
|
|
2216
|
+
: `greater than `}${issue.minimum}`;
|
|
2217
|
+
else if (issue.type === "date")
|
|
2218
|
+
message = `Date must be ${issue.exact
|
|
2219
|
+
? `exactly equal to `
|
|
2220
|
+
: issue.inclusive
|
|
2221
|
+
? `greater than or equal to `
|
|
2222
|
+
: `greater than `}${new Date(Number(issue.minimum))}`;
|
|
2223
|
+
else
|
|
2224
|
+
message = "Invalid input";
|
|
2225
|
+
break;
|
|
2226
|
+
case ZodIssueCode.too_big:
|
|
2227
|
+
if (issue.type === "array")
|
|
2228
|
+
message = `Array must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `less than`} ${issue.maximum} element(s)`;
|
|
2229
|
+
else if (issue.type === "string")
|
|
2230
|
+
message = `String must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
|
|
2231
|
+
else if (issue.type === "number")
|
|
2232
|
+
message = `Number must be ${issue.exact
|
|
2233
|
+
? `exactly`
|
|
2234
|
+
: issue.inclusive
|
|
2235
|
+
? `less than or equal to`
|
|
2236
|
+
: `less than`} ${issue.maximum}`;
|
|
2237
|
+
else if (issue.type === "bigint")
|
|
2238
|
+
message = `BigInt must be ${issue.exact
|
|
2239
|
+
? `exactly`
|
|
2240
|
+
: issue.inclusive
|
|
2241
|
+
? `less than or equal to`
|
|
2242
|
+
: `less than`} ${issue.maximum}`;
|
|
2243
|
+
else if (issue.type === "date")
|
|
2244
|
+
message = `Date must be ${issue.exact
|
|
2245
|
+
? `exactly`
|
|
2246
|
+
: issue.inclusive
|
|
2247
|
+
? `smaller than or equal to`
|
|
2248
|
+
: `smaller than`} ${new Date(Number(issue.maximum))}`;
|
|
2249
|
+
else
|
|
2250
|
+
message = "Invalid input";
|
|
2251
|
+
break;
|
|
2252
|
+
case ZodIssueCode.custom:
|
|
2253
|
+
message = `Invalid input`;
|
|
2254
|
+
break;
|
|
2255
|
+
case ZodIssueCode.invalid_intersection_types:
|
|
2256
|
+
message = `Intersection results could not be merged`;
|
|
2257
|
+
break;
|
|
2258
|
+
case ZodIssueCode.not_multiple_of:
|
|
2259
|
+
message = `Number must be a multiple of ${issue.multipleOf}`;
|
|
2260
|
+
break;
|
|
2261
|
+
case ZodIssueCode.not_finite:
|
|
2262
|
+
message = "Number must be finite";
|
|
2263
|
+
break;
|
|
2264
|
+
default:
|
|
2265
|
+
message = _ctx.defaultError;
|
|
2266
|
+
util.assertNever(issue);
|
|
2267
|
+
}
|
|
2268
|
+
return { message };
|
|
2269
|
+
};
|
|
2270
|
+
|
|
2271
|
+
let overrideErrorMap = errorMap;
|
|
2272
|
+
function setErrorMap(map) {
|
|
2273
|
+
overrideErrorMap = map;
|
|
2274
|
+
}
|
|
2275
|
+
function getErrorMap() {
|
|
2276
|
+
return overrideErrorMap;
|
|
2277
|
+
}
|
|
2278
|
+
|
|
2279
|
+
const makeIssue = (params) => {
|
|
2280
|
+
const { data, path, errorMaps, issueData } = params;
|
|
2281
|
+
const fullPath = [...path, ...(issueData.path || [])];
|
|
2282
|
+
const fullIssue = {
|
|
2283
|
+
...issueData,
|
|
2284
|
+
path: fullPath,
|
|
2285
|
+
};
|
|
2286
|
+
if (issueData.message !== undefined) {
|
|
2287
|
+
return {
|
|
2288
|
+
...issueData,
|
|
2289
|
+
path: fullPath,
|
|
2290
|
+
message: issueData.message,
|
|
2291
|
+
};
|
|
2292
|
+
}
|
|
2293
|
+
let errorMessage = "";
|
|
2294
|
+
const maps = errorMaps
|
|
2295
|
+
.filter((m) => !!m)
|
|
2296
|
+
.slice()
|
|
2297
|
+
.reverse();
|
|
2298
|
+
for (const map of maps) {
|
|
2299
|
+
errorMessage = map(fullIssue, { data, defaultError: errorMessage }).message;
|
|
2300
|
+
}
|
|
2301
|
+
return {
|
|
2302
|
+
...issueData,
|
|
2303
|
+
path: fullPath,
|
|
2304
|
+
message: errorMessage,
|
|
2305
|
+
};
|
|
2306
|
+
};
|
|
2307
|
+
const EMPTY_PATH = [];
|
|
2308
|
+
function addIssueToContext(ctx, issueData) {
|
|
2309
|
+
const overrideMap = getErrorMap();
|
|
2310
|
+
const issue = makeIssue({
|
|
2311
|
+
issueData: issueData,
|
|
2312
|
+
data: ctx.data,
|
|
2313
|
+
path: ctx.path,
|
|
2314
|
+
errorMaps: [
|
|
2315
|
+
ctx.common.contextualErrorMap,
|
|
2316
|
+
ctx.schemaErrorMap,
|
|
2317
|
+
overrideMap,
|
|
2318
|
+
overrideMap === errorMap ? undefined : errorMap, // then global default map
|
|
2319
|
+
].filter((x) => !!x),
|
|
2320
|
+
});
|
|
2321
|
+
ctx.common.issues.push(issue);
|
|
2322
|
+
}
|
|
2323
|
+
class ParseStatus {
|
|
2324
|
+
constructor() {
|
|
2325
|
+
this.value = "valid";
|
|
2326
|
+
}
|
|
2327
|
+
dirty() {
|
|
2328
|
+
if (this.value === "valid")
|
|
2329
|
+
this.value = "dirty";
|
|
2330
|
+
}
|
|
2331
|
+
abort() {
|
|
2332
|
+
if (this.value !== "aborted")
|
|
2333
|
+
this.value = "aborted";
|
|
2334
|
+
}
|
|
2335
|
+
static mergeArray(status, results) {
|
|
2336
|
+
const arrayValue = [];
|
|
2337
|
+
for (const s of results) {
|
|
2338
|
+
if (s.status === "aborted")
|
|
2339
|
+
return INVALID;
|
|
2340
|
+
if (s.status === "dirty")
|
|
2341
|
+
status.dirty();
|
|
2342
|
+
arrayValue.push(s.value);
|
|
2343
|
+
}
|
|
2344
|
+
return { status: status.value, value: arrayValue };
|
|
2345
|
+
}
|
|
2346
|
+
static async mergeObjectAsync(status, pairs) {
|
|
2347
|
+
const syncPairs = [];
|
|
2348
|
+
for (const pair of pairs) {
|
|
2349
|
+
const key = await pair.key;
|
|
2350
|
+
const value = await pair.value;
|
|
2351
|
+
syncPairs.push({
|
|
2352
|
+
key,
|
|
2353
|
+
value,
|
|
2354
|
+
});
|
|
2355
|
+
}
|
|
2356
|
+
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
2357
|
+
}
|
|
2358
|
+
static mergeObjectSync(status, pairs) {
|
|
2359
|
+
const finalObject = {};
|
|
2360
|
+
for (const pair of pairs) {
|
|
2361
|
+
const { key, value } = pair;
|
|
2362
|
+
if (key.status === "aborted")
|
|
2363
|
+
return INVALID;
|
|
2364
|
+
if (value.status === "aborted")
|
|
2365
|
+
return INVALID;
|
|
2366
|
+
if (key.status === "dirty")
|
|
2367
|
+
status.dirty();
|
|
2368
|
+
if (value.status === "dirty")
|
|
2369
|
+
status.dirty();
|
|
2370
|
+
if (key.value !== "__proto__" &&
|
|
2371
|
+
(typeof value.value !== "undefined" || pair.alwaysSet)) {
|
|
2372
|
+
finalObject[key.value] = value.value;
|
|
2373
|
+
}
|
|
2374
|
+
}
|
|
2375
|
+
return { status: status.value, value: finalObject };
|
|
2376
|
+
}
|
|
2377
|
+
}
|
|
2378
|
+
const INVALID = Object.freeze({
|
|
2379
|
+
status: "aborted",
|
|
2380
|
+
});
|
|
2381
|
+
const DIRTY = (value) => ({ status: "dirty", value });
|
|
2382
|
+
const OK = (value) => ({ status: "valid", value });
|
|
2383
|
+
const isAborted = (x) => x.status === "aborted";
|
|
2384
|
+
const isDirty = (x) => x.status === "dirty";
|
|
2385
|
+
const isValid = (x) => x.status === "valid";
|
|
2386
|
+
const isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
|
|
2387
|
+
|
|
2388
|
+
/******************************************************************************
|
|
2389
|
+
Copyright (c) Microsoft Corporation.
|
|
2390
|
+
|
|
2391
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
2392
|
+
purpose with or without fee is hereby granted.
|
|
2393
|
+
|
|
2394
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
2395
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
2396
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
2397
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
2398
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
2399
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
2400
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
2401
|
+
***************************************************************************** */
|
|
2402
|
+
|
|
2403
|
+
function __classPrivateFieldGet(receiver, state, kind, f) {
|
|
2404
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
2405
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
2406
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
2407
|
+
}
|
|
2408
|
+
|
|
2409
|
+
function __classPrivateFieldSet(receiver, state, value, kind, f) {
|
|
2410
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
2411
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
2412
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
2413
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
2414
|
+
}
|
|
2415
|
+
|
|
2416
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
2417
|
+
var e = new Error(message);
|
|
2418
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
2419
|
+
};
|
|
2420
|
+
|
|
2421
|
+
var errorUtil;
|
|
2422
|
+
(function (errorUtil) {
|
|
2423
|
+
errorUtil.errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
2424
|
+
errorUtil.toString = (message) => typeof message === "string" ? message : message === null || message === void 0 ? void 0 : message.message;
|
|
2425
|
+
})(errorUtil || (errorUtil = {}));
|
|
2426
|
+
|
|
2427
|
+
var _ZodEnum_cache, _ZodNativeEnum_cache;
|
|
2428
|
+
class ParseInputLazyPath {
|
|
2429
|
+
constructor(parent, value, path, key) {
|
|
2430
|
+
this._cachedPath = [];
|
|
2431
|
+
this.parent = parent;
|
|
2432
|
+
this.data = value;
|
|
2433
|
+
this._path = path;
|
|
2434
|
+
this._key = key;
|
|
2435
|
+
}
|
|
2436
|
+
get path() {
|
|
2437
|
+
if (!this._cachedPath.length) {
|
|
2438
|
+
if (this._key instanceof Array) {
|
|
2439
|
+
this._cachedPath.push(...this._path, ...this._key);
|
|
2440
|
+
}
|
|
2441
|
+
else {
|
|
2442
|
+
this._cachedPath.push(...this._path, this._key);
|
|
2443
|
+
}
|
|
2444
|
+
}
|
|
2445
|
+
return this._cachedPath;
|
|
2446
|
+
}
|
|
2447
|
+
}
|
|
2448
|
+
const handleResult$1 = (ctx, result) => {
|
|
2449
|
+
if (isValid(result)) {
|
|
2450
|
+
return { success: true, data: result.value };
|
|
2451
|
+
}
|
|
2452
|
+
else {
|
|
2453
|
+
if (!ctx.common.issues.length) {
|
|
2454
|
+
throw new Error("Validation failed but no issues detected.");
|
|
2455
|
+
}
|
|
2456
|
+
return {
|
|
2457
|
+
success: false,
|
|
2458
|
+
get error() {
|
|
2459
|
+
if (this._error)
|
|
2460
|
+
return this._error;
|
|
2461
|
+
const error = new ZodError(ctx.common.issues);
|
|
2462
|
+
this._error = error;
|
|
2463
|
+
return this._error;
|
|
2464
|
+
},
|
|
2465
|
+
};
|
|
2466
|
+
}
|
|
2467
|
+
};
|
|
2468
|
+
function processCreateParams(params) {
|
|
2469
|
+
if (!params)
|
|
2470
|
+
return {};
|
|
2471
|
+
const { errorMap, invalid_type_error, required_error, description } = params;
|
|
2472
|
+
if (errorMap && (invalid_type_error || required_error)) {
|
|
2473
|
+
throw new Error(`Can't use "invalid_type_error" or "required_error" in conjunction with custom error map.`);
|
|
2474
|
+
}
|
|
2475
|
+
if (errorMap)
|
|
2476
|
+
return { errorMap: errorMap, description };
|
|
2477
|
+
const customMap = (iss, ctx) => {
|
|
2478
|
+
var _a, _b;
|
|
2479
|
+
const { message } = params;
|
|
2480
|
+
if (iss.code === "invalid_enum_value") {
|
|
2481
|
+
return { message: message !== null && message !== void 0 ? message : ctx.defaultError };
|
|
2482
|
+
}
|
|
2483
|
+
if (typeof ctx.data === "undefined") {
|
|
2484
|
+
return { message: (_a = message !== null && message !== void 0 ? message : required_error) !== null && _a !== void 0 ? _a : ctx.defaultError };
|
|
2485
|
+
}
|
|
2486
|
+
if (iss.code !== "invalid_type")
|
|
2487
|
+
return { message: ctx.defaultError };
|
|
2488
|
+
return { message: (_b = message !== null && message !== void 0 ? message : invalid_type_error) !== null && _b !== void 0 ? _b : ctx.defaultError };
|
|
2489
|
+
};
|
|
2490
|
+
return { errorMap: customMap, description };
|
|
2491
|
+
}
|
|
2492
|
+
class ZodType {
|
|
2493
|
+
constructor(def) {
|
|
2494
|
+
/** Alias of safeParseAsync */
|
|
2495
|
+
this.spa = this.safeParseAsync;
|
|
2496
|
+
this._def = def;
|
|
2497
|
+
this.parse = this.parse.bind(this);
|
|
2498
|
+
this.safeParse = this.safeParse.bind(this);
|
|
2499
|
+
this.parseAsync = this.parseAsync.bind(this);
|
|
2500
|
+
this.safeParseAsync = this.safeParseAsync.bind(this);
|
|
2501
|
+
this.spa = this.spa.bind(this);
|
|
2502
|
+
this.refine = this.refine.bind(this);
|
|
2503
|
+
this.refinement = this.refinement.bind(this);
|
|
2504
|
+
this.superRefine = this.superRefine.bind(this);
|
|
2505
|
+
this.optional = this.optional.bind(this);
|
|
2506
|
+
this.nullable = this.nullable.bind(this);
|
|
2507
|
+
this.nullish = this.nullish.bind(this);
|
|
2508
|
+
this.array = this.array.bind(this);
|
|
2509
|
+
this.promise = this.promise.bind(this);
|
|
2510
|
+
this.or = this.or.bind(this);
|
|
2511
|
+
this.and = this.and.bind(this);
|
|
2512
|
+
this.transform = this.transform.bind(this);
|
|
2513
|
+
this.brand = this.brand.bind(this);
|
|
2514
|
+
this.default = this.default.bind(this);
|
|
2515
|
+
this.catch = this.catch.bind(this);
|
|
2516
|
+
this.describe = this.describe.bind(this);
|
|
2517
|
+
this.pipe = this.pipe.bind(this);
|
|
2518
|
+
this.readonly = this.readonly.bind(this);
|
|
2519
|
+
this.isNullable = this.isNullable.bind(this);
|
|
2520
|
+
this.isOptional = this.isOptional.bind(this);
|
|
2521
|
+
}
|
|
2522
|
+
get description() {
|
|
2523
|
+
return this._def.description;
|
|
2524
|
+
}
|
|
2525
|
+
_getType(input) {
|
|
2526
|
+
return getParsedType(input.data);
|
|
2527
|
+
}
|
|
2528
|
+
_getOrReturnCtx(input, ctx) {
|
|
2529
|
+
return (ctx || {
|
|
2530
|
+
common: input.parent.common,
|
|
2531
|
+
data: input.data,
|
|
2532
|
+
parsedType: getParsedType(input.data),
|
|
2533
|
+
schemaErrorMap: this._def.errorMap,
|
|
2534
|
+
path: input.path,
|
|
2535
|
+
parent: input.parent,
|
|
2536
|
+
});
|
|
2537
|
+
}
|
|
2538
|
+
_processInputParams(input) {
|
|
2539
|
+
return {
|
|
2540
|
+
status: new ParseStatus(),
|
|
2541
|
+
ctx: {
|
|
2542
|
+
common: input.parent.common,
|
|
2543
|
+
data: input.data,
|
|
2544
|
+
parsedType: getParsedType(input.data),
|
|
2545
|
+
schemaErrorMap: this._def.errorMap,
|
|
2546
|
+
path: input.path,
|
|
2547
|
+
parent: input.parent,
|
|
2548
|
+
},
|
|
2549
|
+
};
|
|
2550
|
+
}
|
|
2551
|
+
_parseSync(input) {
|
|
2552
|
+
const result = this._parse(input);
|
|
2553
|
+
if (isAsync(result)) {
|
|
2554
|
+
throw new Error("Synchronous parse encountered promise.");
|
|
2555
|
+
}
|
|
2556
|
+
return result;
|
|
2557
|
+
}
|
|
2558
|
+
_parseAsync(input) {
|
|
2559
|
+
const result = this._parse(input);
|
|
2560
|
+
return Promise.resolve(result);
|
|
2561
|
+
}
|
|
2562
|
+
parse(data, params) {
|
|
2563
|
+
const result = this.safeParse(data, params);
|
|
2564
|
+
if (result.success)
|
|
2565
|
+
return result.data;
|
|
2566
|
+
throw result.error;
|
|
2567
|
+
}
|
|
2568
|
+
safeParse(data, params) {
|
|
2569
|
+
var _a;
|
|
2570
|
+
const ctx = {
|
|
2571
|
+
common: {
|
|
2572
|
+
issues: [],
|
|
2573
|
+
async: (_a = params === null || params === void 0 ? void 0 : params.async) !== null && _a !== void 0 ? _a : false,
|
|
2574
|
+
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
|
|
2575
|
+
},
|
|
2576
|
+
path: (params === null || params === void 0 ? void 0 : params.path) || [],
|
|
2577
|
+
schemaErrorMap: this._def.errorMap,
|
|
2578
|
+
parent: null,
|
|
2579
|
+
data,
|
|
2580
|
+
parsedType: getParsedType(data),
|
|
2581
|
+
};
|
|
2582
|
+
const result = this._parseSync({ data, path: ctx.path, parent: ctx });
|
|
2583
|
+
return handleResult$1(ctx, result);
|
|
2584
|
+
}
|
|
2585
|
+
async parseAsync(data, params) {
|
|
2586
|
+
const result = await this.safeParseAsync(data, params);
|
|
2587
|
+
if (result.success)
|
|
2588
|
+
return result.data;
|
|
2589
|
+
throw result.error;
|
|
2590
|
+
}
|
|
2591
|
+
async safeParseAsync(data, params) {
|
|
2592
|
+
const ctx = {
|
|
2593
|
+
common: {
|
|
2594
|
+
issues: [],
|
|
2595
|
+
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
|
|
2596
|
+
async: true,
|
|
2597
|
+
},
|
|
2598
|
+
path: (params === null || params === void 0 ? void 0 : params.path) || [],
|
|
2599
|
+
schemaErrorMap: this._def.errorMap,
|
|
2600
|
+
parent: null,
|
|
2601
|
+
data,
|
|
2602
|
+
parsedType: getParsedType(data),
|
|
2603
|
+
};
|
|
2604
|
+
const maybeAsyncResult = this._parse({ data, path: ctx.path, parent: ctx });
|
|
2605
|
+
const result = await (isAsync(maybeAsyncResult)
|
|
2606
|
+
? maybeAsyncResult
|
|
2607
|
+
: Promise.resolve(maybeAsyncResult));
|
|
2608
|
+
return handleResult$1(ctx, result);
|
|
2609
|
+
}
|
|
2610
|
+
refine(check, message) {
|
|
2611
|
+
const getIssueProperties = (val) => {
|
|
2612
|
+
if (typeof message === "string" || typeof message === "undefined") {
|
|
2613
|
+
return { message };
|
|
2614
|
+
}
|
|
2615
|
+
else if (typeof message === "function") {
|
|
2616
|
+
return message(val);
|
|
2617
|
+
}
|
|
2618
|
+
else {
|
|
2619
|
+
return message;
|
|
2620
|
+
}
|
|
2621
|
+
};
|
|
2622
|
+
return this._refinement((val, ctx) => {
|
|
2623
|
+
const result = check(val);
|
|
2624
|
+
const setError = () => ctx.addIssue({
|
|
2625
|
+
code: ZodIssueCode.custom,
|
|
2626
|
+
...getIssueProperties(val),
|
|
2627
|
+
});
|
|
2628
|
+
if (typeof Promise !== "undefined" && result instanceof Promise) {
|
|
2629
|
+
return result.then((data) => {
|
|
2630
|
+
if (!data) {
|
|
2631
|
+
setError();
|
|
2632
|
+
return false;
|
|
2633
|
+
}
|
|
2634
|
+
else {
|
|
2635
|
+
return true;
|
|
2636
|
+
}
|
|
2637
|
+
});
|
|
2638
|
+
}
|
|
2639
|
+
if (!result) {
|
|
2640
|
+
setError();
|
|
2641
|
+
return false;
|
|
2642
|
+
}
|
|
2643
|
+
else {
|
|
2644
|
+
return true;
|
|
2645
|
+
}
|
|
2646
|
+
});
|
|
2647
|
+
}
|
|
2648
|
+
refinement(check, refinementData) {
|
|
2649
|
+
return this._refinement((val, ctx) => {
|
|
2650
|
+
if (!check(val)) {
|
|
2651
|
+
ctx.addIssue(typeof refinementData === "function"
|
|
2652
|
+
? refinementData(val, ctx)
|
|
2653
|
+
: refinementData);
|
|
2654
|
+
return false;
|
|
2655
|
+
}
|
|
2656
|
+
else {
|
|
2657
|
+
return true;
|
|
2658
|
+
}
|
|
2659
|
+
});
|
|
2660
|
+
}
|
|
2661
|
+
_refinement(refinement) {
|
|
2662
|
+
return new ZodEffects({
|
|
2663
|
+
schema: this,
|
|
2664
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
2665
|
+
effect: { type: "refinement", refinement },
|
|
2666
|
+
});
|
|
2667
|
+
}
|
|
2668
|
+
superRefine(refinement) {
|
|
2669
|
+
return this._refinement(refinement);
|
|
2670
|
+
}
|
|
2671
|
+
optional() {
|
|
2672
|
+
return ZodOptional.create(this, this._def);
|
|
2673
|
+
}
|
|
2674
|
+
nullable() {
|
|
2675
|
+
return ZodNullable.create(this, this._def);
|
|
2676
|
+
}
|
|
2677
|
+
nullish() {
|
|
2678
|
+
return this.nullable().optional();
|
|
2679
|
+
}
|
|
2680
|
+
array() {
|
|
2681
|
+
return ZodArray.create(this, this._def);
|
|
2682
|
+
}
|
|
2683
|
+
promise() {
|
|
2684
|
+
return ZodPromise.create(this, this._def);
|
|
2685
|
+
}
|
|
2686
|
+
or(option) {
|
|
2687
|
+
return ZodUnion.create([this, option], this._def);
|
|
2688
|
+
}
|
|
2689
|
+
and(incoming) {
|
|
2690
|
+
return ZodIntersection.create(this, incoming, this._def);
|
|
2691
|
+
}
|
|
2692
|
+
transform(transform) {
|
|
2693
|
+
return new ZodEffects({
|
|
2694
|
+
...processCreateParams(this._def),
|
|
2695
|
+
schema: this,
|
|
2696
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
2697
|
+
effect: { type: "transform", transform },
|
|
2698
|
+
});
|
|
2699
|
+
}
|
|
2700
|
+
default(def) {
|
|
2701
|
+
const defaultValueFunc = typeof def === "function" ? def : () => def;
|
|
2702
|
+
return new ZodDefault({
|
|
2703
|
+
...processCreateParams(this._def),
|
|
2704
|
+
innerType: this,
|
|
2705
|
+
defaultValue: defaultValueFunc,
|
|
2706
|
+
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
2707
|
+
});
|
|
2708
|
+
}
|
|
2709
|
+
brand() {
|
|
2710
|
+
return new ZodBranded({
|
|
2711
|
+
typeName: ZodFirstPartyTypeKind.ZodBranded,
|
|
2712
|
+
type: this,
|
|
2713
|
+
...processCreateParams(this._def),
|
|
2714
|
+
});
|
|
2715
|
+
}
|
|
2716
|
+
catch(def) {
|
|
2717
|
+
const catchValueFunc = typeof def === "function" ? def : () => def;
|
|
2718
|
+
return new ZodCatch({
|
|
2719
|
+
...processCreateParams(this._def),
|
|
2720
|
+
innerType: this,
|
|
2721
|
+
catchValue: catchValueFunc,
|
|
2722
|
+
typeName: ZodFirstPartyTypeKind.ZodCatch,
|
|
2723
|
+
});
|
|
2724
|
+
}
|
|
2725
|
+
describe(description) {
|
|
2726
|
+
const This = this.constructor;
|
|
2727
|
+
return new This({
|
|
2728
|
+
...this._def,
|
|
2729
|
+
description,
|
|
2730
|
+
});
|
|
2731
|
+
}
|
|
2732
|
+
pipe(target) {
|
|
2733
|
+
return ZodPipeline.create(this, target);
|
|
2734
|
+
}
|
|
2735
|
+
readonly() {
|
|
2736
|
+
return ZodReadonly.create(this);
|
|
2737
|
+
}
|
|
2738
|
+
isOptional() {
|
|
2739
|
+
return this.safeParse(undefined).success;
|
|
2740
|
+
}
|
|
2741
|
+
isNullable() {
|
|
2742
|
+
return this.safeParse(null).success;
|
|
2743
|
+
}
|
|
2744
|
+
}
|
|
2745
|
+
const cuidRegex = /^c[^\s-]{8,}$/i;
|
|
2746
|
+
const cuid2Regex = /^[0-9a-z]+$/;
|
|
2747
|
+
const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
2748
|
+
// const uuidRegex =
|
|
2749
|
+
// /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i;
|
|
2750
|
+
const uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
|
|
2751
|
+
const nanoidRegex = /^[a-z0-9_-]{21}$/i;
|
|
2752
|
+
const durationRegex = /^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;
|
|
2753
|
+
// from https://stackoverflow.com/a/46181/1550155
|
|
2754
|
+
// old version: too slow, didn't support unicode
|
|
2755
|
+
// const emailRegex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
|
|
2756
|
+
//old email regex
|
|
2757
|
+
// const emailRegex = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@((?!-)([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{1,})[^-<>()[\].,;:\s@"]$/i;
|
|
2758
|
+
// eslint-disable-next-line
|
|
2759
|
+
// const emailRegex =
|
|
2760
|
+
// /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\])|(\[IPv6:(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))\])|([A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])*(\.[A-Za-z]{2,})+))$/;
|
|
2761
|
+
// const emailRegex =
|
|
2762
|
+
// /^[a-zA-Z0-9\.\!\#\$\%\&\'\*\+\/\=\?\^\_\`\{\|\}\~\-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
|
2763
|
+
// const emailRegex =
|
|
2764
|
+
// /^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/i;
|
|
2765
|
+
const emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
|
|
2766
|
+
// const emailRegex =
|
|
2767
|
+
// /^[a-z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9\-]+)*$/i;
|
|
2768
|
+
// from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression
|
|
2769
|
+
const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
|
|
2770
|
+
let emojiRegex$1;
|
|
2771
|
+
// faster, simpler, safer
|
|
2772
|
+
const ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
|
|
2773
|
+
const ipv6Regex = /^(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))$/;
|
|
2774
|
+
// https://stackoverflow.com/questions/7860392/determine-if-string-is-in-base64-using-javascript
|
|
2775
|
+
const base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
|
2776
|
+
// simple
|
|
2777
|
+
// const dateRegexSource = `\\d{4}-\\d{2}-\\d{2}`;
|
|
2778
|
+
// no leap year validation
|
|
2779
|
+
// const dateRegexSource = `\\d{4}-((0[13578]|10|12)-31|(0[13-9]|1[0-2])-30|(0[1-9]|1[0-2])-(0[1-9]|1\\d|2\\d))`;
|
|
2780
|
+
// with leap year validation
|
|
2781
|
+
const dateRegexSource = `((\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-((0[13578]|1[02])-(0[1-9]|[12]\\d|3[01])|(0[469]|11)-(0[1-9]|[12]\\d|30)|(02)-(0[1-9]|1\\d|2[0-8])))`;
|
|
2782
|
+
const dateRegex = new RegExp(`^${dateRegexSource}$`);
|
|
2783
|
+
function timeRegexSource(args) {
|
|
2784
|
+
// let regex = `\\d{2}:\\d{2}:\\d{2}`;
|
|
2785
|
+
let regex = `([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d`;
|
|
2786
|
+
if (args.precision) {
|
|
2787
|
+
regex = `${regex}\\.\\d{${args.precision}}`;
|
|
2788
|
+
}
|
|
2789
|
+
else if (args.precision == null) {
|
|
2790
|
+
regex = `${regex}(\\.\\d+)?`;
|
|
2791
|
+
}
|
|
2792
|
+
return regex;
|
|
2793
|
+
}
|
|
2794
|
+
function timeRegex(args) {
|
|
2795
|
+
return new RegExp(`^${timeRegexSource(args)}$`);
|
|
2796
|
+
}
|
|
2797
|
+
// Adapted from https://stackoverflow.com/a/3143231
|
|
2798
|
+
function datetimeRegex(args) {
|
|
2799
|
+
let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
|
|
2800
|
+
const opts = [];
|
|
2801
|
+
opts.push(args.local ? `Z?` : `Z`);
|
|
2802
|
+
if (args.offset)
|
|
2803
|
+
opts.push(`([+-]\\d{2}:?\\d{2})`);
|
|
2804
|
+
regex = `${regex}(${opts.join("|")})`;
|
|
2805
|
+
return new RegExp(`^${regex}$`);
|
|
2806
|
+
}
|
|
2807
|
+
function isValidIP(ip, version) {
|
|
2808
|
+
if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
|
|
2809
|
+
return true;
|
|
2810
|
+
}
|
|
2811
|
+
if ((version === "v6" || !version) && ipv6Regex.test(ip)) {
|
|
2812
|
+
return true;
|
|
2813
|
+
}
|
|
2814
|
+
return false;
|
|
2815
|
+
}
|
|
2816
|
+
class ZodString extends ZodType {
|
|
2817
|
+
_parse(input) {
|
|
2818
|
+
if (this._def.coerce) {
|
|
2819
|
+
input.data = String(input.data);
|
|
2820
|
+
}
|
|
2821
|
+
const parsedType = this._getType(input);
|
|
2822
|
+
if (parsedType !== ZodParsedType.string) {
|
|
2823
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2824
|
+
addIssueToContext(ctx, {
|
|
2825
|
+
code: ZodIssueCode.invalid_type,
|
|
2826
|
+
expected: ZodParsedType.string,
|
|
2827
|
+
received: ctx.parsedType,
|
|
2828
|
+
});
|
|
2829
|
+
return INVALID;
|
|
2830
|
+
}
|
|
2831
|
+
const status = new ParseStatus();
|
|
2832
|
+
let ctx = undefined;
|
|
2833
|
+
for (const check of this._def.checks) {
|
|
2834
|
+
if (check.kind === "min") {
|
|
2835
|
+
if (input.data.length < check.value) {
|
|
2836
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2837
|
+
addIssueToContext(ctx, {
|
|
2838
|
+
code: ZodIssueCode.too_small,
|
|
2839
|
+
minimum: check.value,
|
|
2840
|
+
type: "string",
|
|
2841
|
+
inclusive: true,
|
|
2842
|
+
exact: false,
|
|
2843
|
+
message: check.message,
|
|
2844
|
+
});
|
|
2845
|
+
status.dirty();
|
|
2846
|
+
}
|
|
2847
|
+
}
|
|
2848
|
+
else if (check.kind === "max") {
|
|
2849
|
+
if (input.data.length > check.value) {
|
|
2850
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2851
|
+
addIssueToContext(ctx, {
|
|
2852
|
+
code: ZodIssueCode.too_big,
|
|
2853
|
+
maximum: check.value,
|
|
2854
|
+
type: "string",
|
|
2855
|
+
inclusive: true,
|
|
2856
|
+
exact: false,
|
|
2857
|
+
message: check.message,
|
|
2858
|
+
});
|
|
2859
|
+
status.dirty();
|
|
2860
|
+
}
|
|
2861
|
+
}
|
|
2862
|
+
else if (check.kind === "length") {
|
|
2863
|
+
const tooBig = input.data.length > check.value;
|
|
2864
|
+
const tooSmall = input.data.length < check.value;
|
|
2865
|
+
if (tooBig || tooSmall) {
|
|
2866
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2867
|
+
if (tooBig) {
|
|
2868
|
+
addIssueToContext(ctx, {
|
|
2869
|
+
code: ZodIssueCode.too_big,
|
|
2870
|
+
maximum: check.value,
|
|
2871
|
+
type: "string",
|
|
2872
|
+
inclusive: true,
|
|
2873
|
+
exact: true,
|
|
2874
|
+
message: check.message,
|
|
2875
|
+
});
|
|
2876
|
+
}
|
|
2877
|
+
else if (tooSmall) {
|
|
2878
|
+
addIssueToContext(ctx, {
|
|
2879
|
+
code: ZodIssueCode.too_small,
|
|
2880
|
+
minimum: check.value,
|
|
2881
|
+
type: "string",
|
|
2882
|
+
inclusive: true,
|
|
2883
|
+
exact: true,
|
|
2884
|
+
message: check.message,
|
|
2885
|
+
});
|
|
2886
|
+
}
|
|
2887
|
+
status.dirty();
|
|
2888
|
+
}
|
|
2889
|
+
}
|
|
2890
|
+
else if (check.kind === "email") {
|
|
2891
|
+
if (!emailRegex.test(input.data)) {
|
|
2892
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2893
|
+
addIssueToContext(ctx, {
|
|
2894
|
+
validation: "email",
|
|
2895
|
+
code: ZodIssueCode.invalid_string,
|
|
2896
|
+
message: check.message,
|
|
2897
|
+
});
|
|
2898
|
+
status.dirty();
|
|
2899
|
+
}
|
|
2900
|
+
}
|
|
2901
|
+
else if (check.kind === "emoji") {
|
|
2902
|
+
if (!emojiRegex$1) {
|
|
2903
|
+
emojiRegex$1 = new RegExp(_emojiRegex, "u");
|
|
2904
|
+
}
|
|
2905
|
+
if (!emojiRegex$1.test(input.data)) {
|
|
2906
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2907
|
+
addIssueToContext(ctx, {
|
|
2908
|
+
validation: "emoji",
|
|
2909
|
+
code: ZodIssueCode.invalid_string,
|
|
2910
|
+
message: check.message,
|
|
2911
|
+
});
|
|
2912
|
+
status.dirty();
|
|
2913
|
+
}
|
|
2914
|
+
}
|
|
2915
|
+
else if (check.kind === "uuid") {
|
|
2916
|
+
if (!uuidRegex.test(input.data)) {
|
|
2917
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2918
|
+
addIssueToContext(ctx, {
|
|
2919
|
+
validation: "uuid",
|
|
2920
|
+
code: ZodIssueCode.invalid_string,
|
|
2921
|
+
message: check.message,
|
|
2922
|
+
});
|
|
2923
|
+
status.dirty();
|
|
2924
|
+
}
|
|
2925
|
+
}
|
|
2926
|
+
else if (check.kind === "nanoid") {
|
|
2927
|
+
if (!nanoidRegex.test(input.data)) {
|
|
2928
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2929
|
+
addIssueToContext(ctx, {
|
|
2930
|
+
validation: "nanoid",
|
|
2931
|
+
code: ZodIssueCode.invalid_string,
|
|
2932
|
+
message: check.message,
|
|
2933
|
+
});
|
|
2934
|
+
status.dirty();
|
|
2935
|
+
}
|
|
2936
|
+
}
|
|
2937
|
+
else if (check.kind === "cuid") {
|
|
2938
|
+
if (!cuidRegex.test(input.data)) {
|
|
2939
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2940
|
+
addIssueToContext(ctx, {
|
|
2941
|
+
validation: "cuid",
|
|
2942
|
+
code: ZodIssueCode.invalid_string,
|
|
2943
|
+
message: check.message,
|
|
2944
|
+
});
|
|
2945
|
+
status.dirty();
|
|
2946
|
+
}
|
|
2947
|
+
}
|
|
2948
|
+
else if (check.kind === "cuid2") {
|
|
2949
|
+
if (!cuid2Regex.test(input.data)) {
|
|
2950
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2951
|
+
addIssueToContext(ctx, {
|
|
2952
|
+
validation: "cuid2",
|
|
2953
|
+
code: ZodIssueCode.invalid_string,
|
|
2954
|
+
message: check.message,
|
|
2955
|
+
});
|
|
2956
|
+
status.dirty();
|
|
2957
|
+
}
|
|
2958
|
+
}
|
|
2959
|
+
else if (check.kind === "ulid") {
|
|
2960
|
+
if (!ulidRegex.test(input.data)) {
|
|
2961
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2962
|
+
addIssueToContext(ctx, {
|
|
2963
|
+
validation: "ulid",
|
|
2964
|
+
code: ZodIssueCode.invalid_string,
|
|
2965
|
+
message: check.message,
|
|
2966
|
+
});
|
|
2967
|
+
status.dirty();
|
|
2968
|
+
}
|
|
2969
|
+
}
|
|
2970
|
+
else if (check.kind === "url") {
|
|
2971
|
+
try {
|
|
2972
|
+
new URL(input.data);
|
|
2973
|
+
}
|
|
2974
|
+
catch (_a) {
|
|
2975
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2976
|
+
addIssueToContext(ctx, {
|
|
2977
|
+
validation: "url",
|
|
2978
|
+
code: ZodIssueCode.invalid_string,
|
|
2979
|
+
message: check.message,
|
|
2980
|
+
});
|
|
2981
|
+
status.dirty();
|
|
2982
|
+
}
|
|
2983
|
+
}
|
|
2984
|
+
else if (check.kind === "regex") {
|
|
2985
|
+
check.regex.lastIndex = 0;
|
|
2986
|
+
const testResult = check.regex.test(input.data);
|
|
2987
|
+
if (!testResult) {
|
|
2988
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2989
|
+
addIssueToContext(ctx, {
|
|
2990
|
+
validation: "regex",
|
|
2991
|
+
code: ZodIssueCode.invalid_string,
|
|
2992
|
+
message: check.message,
|
|
2993
|
+
});
|
|
2994
|
+
status.dirty();
|
|
2995
|
+
}
|
|
2996
|
+
}
|
|
2997
|
+
else if (check.kind === "trim") {
|
|
2998
|
+
input.data = input.data.trim();
|
|
2999
|
+
}
|
|
3000
|
+
else if (check.kind === "includes") {
|
|
3001
|
+
if (!input.data.includes(check.value, check.position)) {
|
|
3002
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3003
|
+
addIssueToContext(ctx, {
|
|
3004
|
+
code: ZodIssueCode.invalid_string,
|
|
3005
|
+
validation: { includes: check.value, position: check.position },
|
|
3006
|
+
message: check.message,
|
|
3007
|
+
});
|
|
3008
|
+
status.dirty();
|
|
3009
|
+
}
|
|
3010
|
+
}
|
|
3011
|
+
else if (check.kind === "toLowerCase") {
|
|
3012
|
+
input.data = input.data.toLowerCase();
|
|
3013
|
+
}
|
|
3014
|
+
else if (check.kind === "toUpperCase") {
|
|
3015
|
+
input.data = input.data.toUpperCase();
|
|
3016
|
+
}
|
|
3017
|
+
else if (check.kind === "startsWith") {
|
|
3018
|
+
if (!input.data.startsWith(check.value)) {
|
|
3019
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3020
|
+
addIssueToContext(ctx, {
|
|
3021
|
+
code: ZodIssueCode.invalid_string,
|
|
3022
|
+
validation: { startsWith: check.value },
|
|
3023
|
+
message: check.message,
|
|
3024
|
+
});
|
|
3025
|
+
status.dirty();
|
|
3026
|
+
}
|
|
3027
|
+
}
|
|
3028
|
+
else if (check.kind === "endsWith") {
|
|
3029
|
+
if (!input.data.endsWith(check.value)) {
|
|
3030
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3031
|
+
addIssueToContext(ctx, {
|
|
3032
|
+
code: ZodIssueCode.invalid_string,
|
|
3033
|
+
validation: { endsWith: check.value },
|
|
3034
|
+
message: check.message,
|
|
3035
|
+
});
|
|
3036
|
+
status.dirty();
|
|
3037
|
+
}
|
|
3038
|
+
}
|
|
3039
|
+
else if (check.kind === "datetime") {
|
|
3040
|
+
const regex = datetimeRegex(check);
|
|
3041
|
+
if (!regex.test(input.data)) {
|
|
3042
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3043
|
+
addIssueToContext(ctx, {
|
|
3044
|
+
code: ZodIssueCode.invalid_string,
|
|
3045
|
+
validation: "datetime",
|
|
3046
|
+
message: check.message,
|
|
3047
|
+
});
|
|
3048
|
+
status.dirty();
|
|
3049
|
+
}
|
|
3050
|
+
}
|
|
3051
|
+
else if (check.kind === "date") {
|
|
3052
|
+
const regex = dateRegex;
|
|
3053
|
+
if (!regex.test(input.data)) {
|
|
3054
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3055
|
+
addIssueToContext(ctx, {
|
|
3056
|
+
code: ZodIssueCode.invalid_string,
|
|
3057
|
+
validation: "date",
|
|
3058
|
+
message: check.message,
|
|
3059
|
+
});
|
|
3060
|
+
status.dirty();
|
|
3061
|
+
}
|
|
3062
|
+
}
|
|
3063
|
+
else if (check.kind === "time") {
|
|
3064
|
+
const regex = timeRegex(check);
|
|
3065
|
+
if (!regex.test(input.data)) {
|
|
3066
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3067
|
+
addIssueToContext(ctx, {
|
|
3068
|
+
code: ZodIssueCode.invalid_string,
|
|
3069
|
+
validation: "time",
|
|
3070
|
+
message: check.message,
|
|
3071
|
+
});
|
|
3072
|
+
status.dirty();
|
|
3073
|
+
}
|
|
3074
|
+
}
|
|
3075
|
+
else if (check.kind === "duration") {
|
|
3076
|
+
if (!durationRegex.test(input.data)) {
|
|
3077
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3078
|
+
addIssueToContext(ctx, {
|
|
3079
|
+
validation: "duration",
|
|
3080
|
+
code: ZodIssueCode.invalid_string,
|
|
3081
|
+
message: check.message,
|
|
3082
|
+
});
|
|
3083
|
+
status.dirty();
|
|
3084
|
+
}
|
|
3085
|
+
}
|
|
3086
|
+
else if (check.kind === "ip") {
|
|
3087
|
+
if (!isValidIP(input.data, check.version)) {
|
|
3088
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3089
|
+
addIssueToContext(ctx, {
|
|
3090
|
+
validation: "ip",
|
|
3091
|
+
code: ZodIssueCode.invalid_string,
|
|
3092
|
+
message: check.message,
|
|
3093
|
+
});
|
|
3094
|
+
status.dirty();
|
|
3095
|
+
}
|
|
3096
|
+
}
|
|
3097
|
+
else if (check.kind === "base64") {
|
|
3098
|
+
if (!base64Regex.test(input.data)) {
|
|
3099
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3100
|
+
addIssueToContext(ctx, {
|
|
3101
|
+
validation: "base64",
|
|
3102
|
+
code: ZodIssueCode.invalid_string,
|
|
3103
|
+
message: check.message,
|
|
3104
|
+
});
|
|
3105
|
+
status.dirty();
|
|
3106
|
+
}
|
|
3107
|
+
}
|
|
3108
|
+
else {
|
|
3109
|
+
util.assertNever(check);
|
|
3110
|
+
}
|
|
3111
|
+
}
|
|
3112
|
+
return { status: status.value, value: input.data };
|
|
3113
|
+
}
|
|
3114
|
+
_regex(regex, validation, message) {
|
|
3115
|
+
return this.refinement((data) => regex.test(data), {
|
|
3116
|
+
validation,
|
|
3117
|
+
code: ZodIssueCode.invalid_string,
|
|
3118
|
+
...errorUtil.errToObj(message),
|
|
3119
|
+
});
|
|
3120
|
+
}
|
|
3121
|
+
_addCheck(check) {
|
|
3122
|
+
return new ZodString({
|
|
3123
|
+
...this._def,
|
|
3124
|
+
checks: [...this._def.checks, check],
|
|
3125
|
+
});
|
|
3126
|
+
}
|
|
3127
|
+
email(message) {
|
|
3128
|
+
return this._addCheck({ kind: "email", ...errorUtil.errToObj(message) });
|
|
3129
|
+
}
|
|
3130
|
+
url(message) {
|
|
3131
|
+
return this._addCheck({ kind: "url", ...errorUtil.errToObj(message) });
|
|
3132
|
+
}
|
|
3133
|
+
emoji(message) {
|
|
3134
|
+
return this._addCheck({ kind: "emoji", ...errorUtil.errToObj(message) });
|
|
3135
|
+
}
|
|
3136
|
+
uuid(message) {
|
|
3137
|
+
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
|
|
3138
|
+
}
|
|
3139
|
+
nanoid(message) {
|
|
3140
|
+
return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) });
|
|
3141
|
+
}
|
|
3142
|
+
cuid(message) {
|
|
3143
|
+
return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
|
|
3144
|
+
}
|
|
3145
|
+
cuid2(message) {
|
|
3146
|
+
return this._addCheck({ kind: "cuid2", ...errorUtil.errToObj(message) });
|
|
3147
|
+
}
|
|
3148
|
+
ulid(message) {
|
|
3149
|
+
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
|
|
3150
|
+
}
|
|
3151
|
+
base64(message) {
|
|
3152
|
+
return this._addCheck({ kind: "base64", ...errorUtil.errToObj(message) });
|
|
3153
|
+
}
|
|
3154
|
+
ip(options) {
|
|
3155
|
+
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
|
|
3156
|
+
}
|
|
3157
|
+
datetime(options) {
|
|
3158
|
+
var _a, _b;
|
|
3159
|
+
if (typeof options === "string") {
|
|
3160
|
+
return this._addCheck({
|
|
3161
|
+
kind: "datetime",
|
|
3162
|
+
precision: null,
|
|
3163
|
+
offset: false,
|
|
3164
|
+
local: false,
|
|
3165
|
+
message: options,
|
|
3166
|
+
});
|
|
3167
|
+
}
|
|
3168
|
+
return this._addCheck({
|
|
3169
|
+
kind: "datetime",
|
|
3170
|
+
precision: typeof (options === null || options === void 0 ? void 0 : options.precision) === "undefined" ? null : options === null || options === void 0 ? void 0 : options.precision,
|
|
3171
|
+
offset: (_a = options === null || options === void 0 ? void 0 : options.offset) !== null && _a !== void 0 ? _a : false,
|
|
3172
|
+
local: (_b = options === null || options === void 0 ? void 0 : options.local) !== null && _b !== void 0 ? _b : false,
|
|
3173
|
+
...errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message),
|
|
3174
|
+
});
|
|
3175
|
+
}
|
|
3176
|
+
date(message) {
|
|
3177
|
+
return this._addCheck({ kind: "date", message });
|
|
3178
|
+
}
|
|
3179
|
+
time(options) {
|
|
3180
|
+
if (typeof options === "string") {
|
|
3181
|
+
return this._addCheck({
|
|
3182
|
+
kind: "time",
|
|
3183
|
+
precision: null,
|
|
3184
|
+
message: options,
|
|
3185
|
+
});
|
|
3186
|
+
}
|
|
3187
|
+
return this._addCheck({
|
|
3188
|
+
kind: "time",
|
|
3189
|
+
precision: typeof (options === null || options === void 0 ? void 0 : options.precision) === "undefined" ? null : options === null || options === void 0 ? void 0 : options.precision,
|
|
3190
|
+
...errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message),
|
|
3191
|
+
});
|
|
3192
|
+
}
|
|
3193
|
+
duration(message) {
|
|
3194
|
+
return this._addCheck({ kind: "duration", ...errorUtil.errToObj(message) });
|
|
3195
|
+
}
|
|
3196
|
+
regex(regex, message) {
|
|
3197
|
+
return this._addCheck({
|
|
3198
|
+
kind: "regex",
|
|
3199
|
+
regex: regex,
|
|
3200
|
+
...errorUtil.errToObj(message),
|
|
3201
|
+
});
|
|
3202
|
+
}
|
|
3203
|
+
includes(value, options) {
|
|
3204
|
+
return this._addCheck({
|
|
3205
|
+
kind: "includes",
|
|
3206
|
+
value: value,
|
|
3207
|
+
position: options === null || options === void 0 ? void 0 : options.position,
|
|
3208
|
+
...errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message),
|
|
3209
|
+
});
|
|
3210
|
+
}
|
|
3211
|
+
startsWith(value, message) {
|
|
3212
|
+
return this._addCheck({
|
|
3213
|
+
kind: "startsWith",
|
|
3214
|
+
value: value,
|
|
3215
|
+
...errorUtil.errToObj(message),
|
|
3216
|
+
});
|
|
3217
|
+
}
|
|
3218
|
+
endsWith(value, message) {
|
|
3219
|
+
return this._addCheck({
|
|
3220
|
+
kind: "endsWith",
|
|
3221
|
+
value: value,
|
|
3222
|
+
...errorUtil.errToObj(message),
|
|
3223
|
+
});
|
|
3224
|
+
}
|
|
3225
|
+
min(minLength, message) {
|
|
3226
|
+
return this._addCheck({
|
|
3227
|
+
kind: "min",
|
|
3228
|
+
value: minLength,
|
|
3229
|
+
...errorUtil.errToObj(message),
|
|
3230
|
+
});
|
|
3231
|
+
}
|
|
3232
|
+
max(maxLength, message) {
|
|
3233
|
+
return this._addCheck({
|
|
3234
|
+
kind: "max",
|
|
3235
|
+
value: maxLength,
|
|
3236
|
+
...errorUtil.errToObj(message),
|
|
3237
|
+
});
|
|
3238
|
+
}
|
|
3239
|
+
length(len, message) {
|
|
3240
|
+
return this._addCheck({
|
|
3241
|
+
kind: "length",
|
|
3242
|
+
value: len,
|
|
3243
|
+
...errorUtil.errToObj(message),
|
|
3244
|
+
});
|
|
3245
|
+
}
|
|
3246
|
+
/**
|
|
3247
|
+
* @deprecated Use z.string().min(1) instead.
|
|
3248
|
+
* @see {@link ZodString.min}
|
|
3249
|
+
*/
|
|
3250
|
+
nonempty(message) {
|
|
3251
|
+
return this.min(1, errorUtil.errToObj(message));
|
|
3252
|
+
}
|
|
3253
|
+
trim() {
|
|
3254
|
+
return new ZodString({
|
|
3255
|
+
...this._def,
|
|
3256
|
+
checks: [...this._def.checks, { kind: "trim" }],
|
|
3257
|
+
});
|
|
3258
|
+
}
|
|
3259
|
+
toLowerCase() {
|
|
3260
|
+
return new ZodString({
|
|
3261
|
+
...this._def,
|
|
3262
|
+
checks: [...this._def.checks, { kind: "toLowerCase" }],
|
|
3263
|
+
});
|
|
3264
|
+
}
|
|
3265
|
+
toUpperCase() {
|
|
3266
|
+
return new ZodString({
|
|
3267
|
+
...this._def,
|
|
3268
|
+
checks: [...this._def.checks, { kind: "toUpperCase" }],
|
|
3269
|
+
});
|
|
3270
|
+
}
|
|
3271
|
+
get isDatetime() {
|
|
3272
|
+
return !!this._def.checks.find((ch) => ch.kind === "datetime");
|
|
3273
|
+
}
|
|
3274
|
+
get isDate() {
|
|
3275
|
+
return !!this._def.checks.find((ch) => ch.kind === "date");
|
|
3276
|
+
}
|
|
3277
|
+
get isTime() {
|
|
3278
|
+
return !!this._def.checks.find((ch) => ch.kind === "time");
|
|
3279
|
+
}
|
|
3280
|
+
get isDuration() {
|
|
3281
|
+
return !!this._def.checks.find((ch) => ch.kind === "duration");
|
|
3282
|
+
}
|
|
3283
|
+
get isEmail() {
|
|
3284
|
+
return !!this._def.checks.find((ch) => ch.kind === "email");
|
|
3285
|
+
}
|
|
3286
|
+
get isURL() {
|
|
3287
|
+
return !!this._def.checks.find((ch) => ch.kind === "url");
|
|
3288
|
+
}
|
|
3289
|
+
get isEmoji() {
|
|
3290
|
+
return !!this._def.checks.find((ch) => ch.kind === "emoji");
|
|
3291
|
+
}
|
|
3292
|
+
get isUUID() {
|
|
3293
|
+
return !!this._def.checks.find((ch) => ch.kind === "uuid");
|
|
3294
|
+
}
|
|
3295
|
+
get isNANOID() {
|
|
3296
|
+
return !!this._def.checks.find((ch) => ch.kind === "nanoid");
|
|
3297
|
+
}
|
|
3298
|
+
get isCUID() {
|
|
3299
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid");
|
|
3300
|
+
}
|
|
3301
|
+
get isCUID2() {
|
|
3302
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid2");
|
|
3303
|
+
}
|
|
3304
|
+
get isULID() {
|
|
3305
|
+
return !!this._def.checks.find((ch) => ch.kind === "ulid");
|
|
3306
|
+
}
|
|
3307
|
+
get isIP() {
|
|
3308
|
+
return !!this._def.checks.find((ch) => ch.kind === "ip");
|
|
3309
|
+
}
|
|
3310
|
+
get isBase64() {
|
|
3311
|
+
return !!this._def.checks.find((ch) => ch.kind === "base64");
|
|
3312
|
+
}
|
|
3313
|
+
get minLength() {
|
|
3314
|
+
let min = null;
|
|
3315
|
+
for (const ch of this._def.checks) {
|
|
3316
|
+
if (ch.kind === "min") {
|
|
3317
|
+
if (min === null || ch.value > min)
|
|
3318
|
+
min = ch.value;
|
|
3319
|
+
}
|
|
3320
|
+
}
|
|
3321
|
+
return min;
|
|
3322
|
+
}
|
|
3323
|
+
get maxLength() {
|
|
3324
|
+
let max = null;
|
|
3325
|
+
for (const ch of this._def.checks) {
|
|
3326
|
+
if (ch.kind === "max") {
|
|
3327
|
+
if (max === null || ch.value < max)
|
|
3328
|
+
max = ch.value;
|
|
3329
|
+
}
|
|
3330
|
+
}
|
|
3331
|
+
return max;
|
|
3332
|
+
}
|
|
3333
|
+
}
|
|
3334
|
+
ZodString.create = (params) => {
|
|
3335
|
+
var _a;
|
|
3336
|
+
return new ZodString({
|
|
3337
|
+
checks: [],
|
|
3338
|
+
typeName: ZodFirstPartyTypeKind.ZodString,
|
|
3339
|
+
coerce: (_a = params === null || params === void 0 ? void 0 : params.coerce) !== null && _a !== void 0 ? _a : false,
|
|
3340
|
+
...processCreateParams(params),
|
|
3341
|
+
});
|
|
3342
|
+
};
|
|
3343
|
+
// https://stackoverflow.com/questions/3966484/why-does-modulus-operator-return-fractional-number-in-javascript/31711034#31711034
|
|
3344
|
+
function floatSafeRemainder(val, step) {
|
|
3345
|
+
const valDecCount = (val.toString().split(".")[1] || "").length;
|
|
3346
|
+
const stepDecCount = (step.toString().split(".")[1] || "").length;
|
|
3347
|
+
const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
|
|
3348
|
+
const valInt = parseInt(val.toFixed(decCount).replace(".", ""));
|
|
3349
|
+
const stepInt = parseInt(step.toFixed(decCount).replace(".", ""));
|
|
3350
|
+
return (valInt % stepInt) / Math.pow(10, decCount);
|
|
3351
|
+
}
|
|
3352
|
+
class ZodNumber extends ZodType {
|
|
3353
|
+
constructor() {
|
|
3354
|
+
super(...arguments);
|
|
3355
|
+
this.min = this.gte;
|
|
3356
|
+
this.max = this.lte;
|
|
3357
|
+
this.step = this.multipleOf;
|
|
3358
|
+
}
|
|
3359
|
+
_parse(input) {
|
|
3360
|
+
if (this._def.coerce) {
|
|
3361
|
+
input.data = Number(input.data);
|
|
3362
|
+
}
|
|
3363
|
+
const parsedType = this._getType(input);
|
|
3364
|
+
if (parsedType !== ZodParsedType.number) {
|
|
3365
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3366
|
+
addIssueToContext(ctx, {
|
|
3367
|
+
code: ZodIssueCode.invalid_type,
|
|
3368
|
+
expected: ZodParsedType.number,
|
|
3369
|
+
received: ctx.parsedType,
|
|
3370
|
+
});
|
|
3371
|
+
return INVALID;
|
|
3372
|
+
}
|
|
3373
|
+
let ctx = undefined;
|
|
3374
|
+
const status = new ParseStatus();
|
|
3375
|
+
for (const check of this._def.checks) {
|
|
3376
|
+
if (check.kind === "int") {
|
|
3377
|
+
if (!util.isInteger(input.data)) {
|
|
3378
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3379
|
+
addIssueToContext(ctx, {
|
|
3380
|
+
code: ZodIssueCode.invalid_type,
|
|
3381
|
+
expected: "integer",
|
|
3382
|
+
received: "float",
|
|
3383
|
+
message: check.message,
|
|
3384
|
+
});
|
|
3385
|
+
status.dirty();
|
|
3386
|
+
}
|
|
3387
|
+
}
|
|
3388
|
+
else if (check.kind === "min") {
|
|
3389
|
+
const tooSmall = check.inclusive
|
|
3390
|
+
? input.data < check.value
|
|
3391
|
+
: input.data <= check.value;
|
|
3392
|
+
if (tooSmall) {
|
|
3393
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3394
|
+
addIssueToContext(ctx, {
|
|
3395
|
+
code: ZodIssueCode.too_small,
|
|
3396
|
+
minimum: check.value,
|
|
3397
|
+
type: "number",
|
|
3398
|
+
inclusive: check.inclusive,
|
|
3399
|
+
exact: false,
|
|
3400
|
+
message: check.message,
|
|
3401
|
+
});
|
|
3402
|
+
status.dirty();
|
|
3403
|
+
}
|
|
3404
|
+
}
|
|
3405
|
+
else if (check.kind === "max") {
|
|
3406
|
+
const tooBig = check.inclusive
|
|
3407
|
+
? input.data > check.value
|
|
3408
|
+
: input.data >= check.value;
|
|
3409
|
+
if (tooBig) {
|
|
3410
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3411
|
+
addIssueToContext(ctx, {
|
|
3412
|
+
code: ZodIssueCode.too_big,
|
|
3413
|
+
maximum: check.value,
|
|
3414
|
+
type: "number",
|
|
3415
|
+
inclusive: check.inclusive,
|
|
3416
|
+
exact: false,
|
|
3417
|
+
message: check.message,
|
|
3418
|
+
});
|
|
3419
|
+
status.dirty();
|
|
3420
|
+
}
|
|
3421
|
+
}
|
|
3422
|
+
else if (check.kind === "multipleOf") {
|
|
3423
|
+
if (floatSafeRemainder(input.data, check.value) !== 0) {
|
|
3424
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3425
|
+
addIssueToContext(ctx, {
|
|
3426
|
+
code: ZodIssueCode.not_multiple_of,
|
|
3427
|
+
multipleOf: check.value,
|
|
3428
|
+
message: check.message,
|
|
3429
|
+
});
|
|
3430
|
+
status.dirty();
|
|
3431
|
+
}
|
|
3432
|
+
}
|
|
3433
|
+
else if (check.kind === "finite") {
|
|
3434
|
+
if (!Number.isFinite(input.data)) {
|
|
3435
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3436
|
+
addIssueToContext(ctx, {
|
|
3437
|
+
code: ZodIssueCode.not_finite,
|
|
3438
|
+
message: check.message,
|
|
3439
|
+
});
|
|
3440
|
+
status.dirty();
|
|
3441
|
+
}
|
|
3442
|
+
}
|
|
3443
|
+
else {
|
|
3444
|
+
util.assertNever(check);
|
|
3445
|
+
}
|
|
3446
|
+
}
|
|
3447
|
+
return { status: status.value, value: input.data };
|
|
3448
|
+
}
|
|
3449
|
+
gte(value, message) {
|
|
3450
|
+
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
3451
|
+
}
|
|
3452
|
+
gt(value, message) {
|
|
3453
|
+
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
3454
|
+
}
|
|
3455
|
+
lte(value, message) {
|
|
3456
|
+
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
3457
|
+
}
|
|
3458
|
+
lt(value, message) {
|
|
3459
|
+
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
3460
|
+
}
|
|
3461
|
+
setLimit(kind, value, inclusive, message) {
|
|
3462
|
+
return new ZodNumber({
|
|
3463
|
+
...this._def,
|
|
3464
|
+
checks: [
|
|
3465
|
+
...this._def.checks,
|
|
3466
|
+
{
|
|
3467
|
+
kind,
|
|
3468
|
+
value,
|
|
3469
|
+
inclusive,
|
|
3470
|
+
message: errorUtil.toString(message),
|
|
3471
|
+
},
|
|
3472
|
+
],
|
|
3473
|
+
});
|
|
3474
|
+
}
|
|
3475
|
+
_addCheck(check) {
|
|
3476
|
+
return new ZodNumber({
|
|
3477
|
+
...this._def,
|
|
3478
|
+
checks: [...this._def.checks, check],
|
|
3479
|
+
});
|
|
3480
|
+
}
|
|
3481
|
+
int(message) {
|
|
3482
|
+
return this._addCheck({
|
|
3483
|
+
kind: "int",
|
|
3484
|
+
message: errorUtil.toString(message),
|
|
3485
|
+
});
|
|
3486
|
+
}
|
|
3487
|
+
positive(message) {
|
|
3488
|
+
return this._addCheck({
|
|
3489
|
+
kind: "min",
|
|
3490
|
+
value: 0,
|
|
3491
|
+
inclusive: false,
|
|
3492
|
+
message: errorUtil.toString(message),
|
|
3493
|
+
});
|
|
3494
|
+
}
|
|
3495
|
+
negative(message) {
|
|
3496
|
+
return this._addCheck({
|
|
3497
|
+
kind: "max",
|
|
3498
|
+
value: 0,
|
|
3499
|
+
inclusive: false,
|
|
3500
|
+
message: errorUtil.toString(message),
|
|
3501
|
+
});
|
|
3502
|
+
}
|
|
3503
|
+
nonpositive(message) {
|
|
3504
|
+
return this._addCheck({
|
|
3505
|
+
kind: "max",
|
|
3506
|
+
value: 0,
|
|
3507
|
+
inclusive: true,
|
|
3508
|
+
message: errorUtil.toString(message),
|
|
3509
|
+
});
|
|
3510
|
+
}
|
|
3511
|
+
nonnegative(message) {
|
|
3512
|
+
return this._addCheck({
|
|
3513
|
+
kind: "min",
|
|
3514
|
+
value: 0,
|
|
3515
|
+
inclusive: true,
|
|
3516
|
+
message: errorUtil.toString(message),
|
|
3517
|
+
});
|
|
3518
|
+
}
|
|
3519
|
+
multipleOf(value, message) {
|
|
3520
|
+
return this._addCheck({
|
|
3521
|
+
kind: "multipleOf",
|
|
3522
|
+
value: value,
|
|
3523
|
+
message: errorUtil.toString(message),
|
|
3524
|
+
});
|
|
3525
|
+
}
|
|
3526
|
+
finite(message) {
|
|
3527
|
+
return this._addCheck({
|
|
3528
|
+
kind: "finite",
|
|
3529
|
+
message: errorUtil.toString(message),
|
|
3530
|
+
});
|
|
3531
|
+
}
|
|
3532
|
+
safe(message) {
|
|
3533
|
+
return this._addCheck({
|
|
3534
|
+
kind: "min",
|
|
3535
|
+
inclusive: true,
|
|
3536
|
+
value: Number.MIN_SAFE_INTEGER,
|
|
3537
|
+
message: errorUtil.toString(message),
|
|
3538
|
+
})._addCheck({
|
|
3539
|
+
kind: "max",
|
|
3540
|
+
inclusive: true,
|
|
3541
|
+
value: Number.MAX_SAFE_INTEGER,
|
|
3542
|
+
message: errorUtil.toString(message),
|
|
3543
|
+
});
|
|
3544
|
+
}
|
|
3545
|
+
get minValue() {
|
|
3546
|
+
let min = null;
|
|
3547
|
+
for (const ch of this._def.checks) {
|
|
3548
|
+
if (ch.kind === "min") {
|
|
3549
|
+
if (min === null || ch.value > min)
|
|
3550
|
+
min = ch.value;
|
|
3551
|
+
}
|
|
3552
|
+
}
|
|
3553
|
+
return min;
|
|
3554
|
+
}
|
|
3555
|
+
get maxValue() {
|
|
3556
|
+
let max = null;
|
|
3557
|
+
for (const ch of this._def.checks) {
|
|
3558
|
+
if (ch.kind === "max") {
|
|
3559
|
+
if (max === null || ch.value < max)
|
|
3560
|
+
max = ch.value;
|
|
3561
|
+
}
|
|
3562
|
+
}
|
|
3563
|
+
return max;
|
|
3564
|
+
}
|
|
3565
|
+
get isInt() {
|
|
3566
|
+
return !!this._def.checks.find((ch) => ch.kind === "int" ||
|
|
3567
|
+
(ch.kind === "multipleOf" && util.isInteger(ch.value)));
|
|
3568
|
+
}
|
|
3569
|
+
get isFinite() {
|
|
3570
|
+
let max = null, min = null;
|
|
3571
|
+
for (const ch of this._def.checks) {
|
|
3572
|
+
if (ch.kind === "finite" ||
|
|
3573
|
+
ch.kind === "int" ||
|
|
3574
|
+
ch.kind === "multipleOf") {
|
|
3575
|
+
return true;
|
|
3576
|
+
}
|
|
3577
|
+
else if (ch.kind === "min") {
|
|
3578
|
+
if (min === null || ch.value > min)
|
|
3579
|
+
min = ch.value;
|
|
3580
|
+
}
|
|
3581
|
+
else if (ch.kind === "max") {
|
|
3582
|
+
if (max === null || ch.value < max)
|
|
3583
|
+
max = ch.value;
|
|
3584
|
+
}
|
|
3585
|
+
}
|
|
3586
|
+
return Number.isFinite(min) && Number.isFinite(max);
|
|
3587
|
+
}
|
|
3588
|
+
}
|
|
3589
|
+
ZodNumber.create = (params) => {
|
|
3590
|
+
return new ZodNumber({
|
|
3591
|
+
checks: [],
|
|
3592
|
+
typeName: ZodFirstPartyTypeKind.ZodNumber,
|
|
3593
|
+
coerce: (params === null || params === void 0 ? void 0 : params.coerce) || false,
|
|
3594
|
+
...processCreateParams(params),
|
|
3595
|
+
});
|
|
3596
|
+
};
|
|
3597
|
+
class ZodBigInt extends ZodType {
|
|
3598
|
+
constructor() {
|
|
3599
|
+
super(...arguments);
|
|
3600
|
+
this.min = this.gte;
|
|
3601
|
+
this.max = this.lte;
|
|
3602
|
+
}
|
|
3603
|
+
_parse(input) {
|
|
3604
|
+
if (this._def.coerce) {
|
|
3605
|
+
input.data = BigInt(input.data);
|
|
3606
|
+
}
|
|
3607
|
+
const parsedType = this._getType(input);
|
|
3608
|
+
if (parsedType !== ZodParsedType.bigint) {
|
|
3609
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3610
|
+
addIssueToContext(ctx, {
|
|
3611
|
+
code: ZodIssueCode.invalid_type,
|
|
3612
|
+
expected: ZodParsedType.bigint,
|
|
3613
|
+
received: ctx.parsedType,
|
|
3614
|
+
});
|
|
3615
|
+
return INVALID;
|
|
3616
|
+
}
|
|
3617
|
+
let ctx = undefined;
|
|
3618
|
+
const status = new ParseStatus();
|
|
3619
|
+
for (const check of this._def.checks) {
|
|
3620
|
+
if (check.kind === "min") {
|
|
3621
|
+
const tooSmall = check.inclusive
|
|
3622
|
+
? input.data < check.value
|
|
3623
|
+
: input.data <= check.value;
|
|
3624
|
+
if (tooSmall) {
|
|
3625
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3626
|
+
addIssueToContext(ctx, {
|
|
3627
|
+
code: ZodIssueCode.too_small,
|
|
3628
|
+
type: "bigint",
|
|
3629
|
+
minimum: check.value,
|
|
3630
|
+
inclusive: check.inclusive,
|
|
3631
|
+
message: check.message,
|
|
3632
|
+
});
|
|
3633
|
+
status.dirty();
|
|
3634
|
+
}
|
|
3635
|
+
}
|
|
3636
|
+
else if (check.kind === "max") {
|
|
3637
|
+
const tooBig = check.inclusive
|
|
3638
|
+
? input.data > check.value
|
|
3639
|
+
: input.data >= check.value;
|
|
3640
|
+
if (tooBig) {
|
|
3641
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3642
|
+
addIssueToContext(ctx, {
|
|
3643
|
+
code: ZodIssueCode.too_big,
|
|
3644
|
+
type: "bigint",
|
|
3645
|
+
maximum: check.value,
|
|
3646
|
+
inclusive: check.inclusive,
|
|
3647
|
+
message: check.message,
|
|
3648
|
+
});
|
|
3649
|
+
status.dirty();
|
|
3650
|
+
}
|
|
3651
|
+
}
|
|
3652
|
+
else if (check.kind === "multipleOf") {
|
|
3653
|
+
if (input.data % check.value !== BigInt(0)) {
|
|
3654
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3655
|
+
addIssueToContext(ctx, {
|
|
3656
|
+
code: ZodIssueCode.not_multiple_of,
|
|
3657
|
+
multipleOf: check.value,
|
|
3658
|
+
message: check.message,
|
|
3659
|
+
});
|
|
3660
|
+
status.dirty();
|
|
3661
|
+
}
|
|
3662
|
+
}
|
|
3663
|
+
else {
|
|
3664
|
+
util.assertNever(check);
|
|
3665
|
+
}
|
|
3666
|
+
}
|
|
3667
|
+
return { status: status.value, value: input.data };
|
|
3668
|
+
}
|
|
3669
|
+
gte(value, message) {
|
|
3670
|
+
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
3671
|
+
}
|
|
3672
|
+
gt(value, message) {
|
|
3673
|
+
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
3674
|
+
}
|
|
3675
|
+
lte(value, message) {
|
|
3676
|
+
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
3677
|
+
}
|
|
3678
|
+
lt(value, message) {
|
|
3679
|
+
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
3680
|
+
}
|
|
3681
|
+
setLimit(kind, value, inclusive, message) {
|
|
3682
|
+
return new ZodBigInt({
|
|
3683
|
+
...this._def,
|
|
3684
|
+
checks: [
|
|
3685
|
+
...this._def.checks,
|
|
3686
|
+
{
|
|
3687
|
+
kind,
|
|
3688
|
+
value,
|
|
3689
|
+
inclusive,
|
|
3690
|
+
message: errorUtil.toString(message),
|
|
3691
|
+
},
|
|
3692
|
+
],
|
|
3693
|
+
});
|
|
3694
|
+
}
|
|
3695
|
+
_addCheck(check) {
|
|
3696
|
+
return new ZodBigInt({
|
|
3697
|
+
...this._def,
|
|
3698
|
+
checks: [...this._def.checks, check],
|
|
3699
|
+
});
|
|
3700
|
+
}
|
|
3701
|
+
positive(message) {
|
|
3702
|
+
return this._addCheck({
|
|
3703
|
+
kind: "min",
|
|
3704
|
+
value: BigInt(0),
|
|
3705
|
+
inclusive: false,
|
|
3706
|
+
message: errorUtil.toString(message),
|
|
3707
|
+
});
|
|
3708
|
+
}
|
|
3709
|
+
negative(message) {
|
|
3710
|
+
return this._addCheck({
|
|
3711
|
+
kind: "max",
|
|
3712
|
+
value: BigInt(0),
|
|
3713
|
+
inclusive: false,
|
|
3714
|
+
message: errorUtil.toString(message),
|
|
3715
|
+
});
|
|
3716
|
+
}
|
|
3717
|
+
nonpositive(message) {
|
|
3718
|
+
return this._addCheck({
|
|
3719
|
+
kind: "max",
|
|
3720
|
+
value: BigInt(0),
|
|
3721
|
+
inclusive: true,
|
|
3722
|
+
message: errorUtil.toString(message),
|
|
3723
|
+
});
|
|
3724
|
+
}
|
|
3725
|
+
nonnegative(message) {
|
|
3726
|
+
return this._addCheck({
|
|
3727
|
+
kind: "min",
|
|
3728
|
+
value: BigInt(0),
|
|
3729
|
+
inclusive: true,
|
|
3730
|
+
message: errorUtil.toString(message),
|
|
3731
|
+
});
|
|
3732
|
+
}
|
|
3733
|
+
multipleOf(value, message) {
|
|
3734
|
+
return this._addCheck({
|
|
3735
|
+
kind: "multipleOf",
|
|
3736
|
+
value,
|
|
3737
|
+
message: errorUtil.toString(message),
|
|
3738
|
+
});
|
|
3739
|
+
}
|
|
3740
|
+
get minValue() {
|
|
3741
|
+
let min = null;
|
|
3742
|
+
for (const ch of this._def.checks) {
|
|
3743
|
+
if (ch.kind === "min") {
|
|
3744
|
+
if (min === null || ch.value > min)
|
|
3745
|
+
min = ch.value;
|
|
3746
|
+
}
|
|
3747
|
+
}
|
|
3748
|
+
return min;
|
|
3749
|
+
}
|
|
3750
|
+
get maxValue() {
|
|
3751
|
+
let max = null;
|
|
3752
|
+
for (const ch of this._def.checks) {
|
|
3753
|
+
if (ch.kind === "max") {
|
|
3754
|
+
if (max === null || ch.value < max)
|
|
3755
|
+
max = ch.value;
|
|
3756
|
+
}
|
|
3757
|
+
}
|
|
3758
|
+
return max;
|
|
3759
|
+
}
|
|
3760
|
+
}
|
|
3761
|
+
ZodBigInt.create = (params) => {
|
|
3762
|
+
var _a;
|
|
3763
|
+
return new ZodBigInt({
|
|
3764
|
+
checks: [],
|
|
3765
|
+
typeName: ZodFirstPartyTypeKind.ZodBigInt,
|
|
3766
|
+
coerce: (_a = params === null || params === void 0 ? void 0 : params.coerce) !== null && _a !== void 0 ? _a : false,
|
|
3767
|
+
...processCreateParams(params),
|
|
3768
|
+
});
|
|
3769
|
+
};
|
|
3770
|
+
class ZodBoolean extends ZodType {
|
|
3771
|
+
_parse(input) {
|
|
3772
|
+
if (this._def.coerce) {
|
|
3773
|
+
input.data = Boolean(input.data);
|
|
3774
|
+
}
|
|
3775
|
+
const parsedType = this._getType(input);
|
|
3776
|
+
if (parsedType !== ZodParsedType.boolean) {
|
|
3777
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3778
|
+
addIssueToContext(ctx, {
|
|
3779
|
+
code: ZodIssueCode.invalid_type,
|
|
3780
|
+
expected: ZodParsedType.boolean,
|
|
3781
|
+
received: ctx.parsedType,
|
|
3782
|
+
});
|
|
3783
|
+
return INVALID;
|
|
3784
|
+
}
|
|
3785
|
+
return OK(input.data);
|
|
3786
|
+
}
|
|
3787
|
+
}
|
|
3788
|
+
ZodBoolean.create = (params) => {
|
|
3789
|
+
return new ZodBoolean({
|
|
3790
|
+
typeName: ZodFirstPartyTypeKind.ZodBoolean,
|
|
3791
|
+
coerce: (params === null || params === void 0 ? void 0 : params.coerce) || false,
|
|
3792
|
+
...processCreateParams(params),
|
|
3793
|
+
});
|
|
3794
|
+
};
|
|
3795
|
+
class ZodDate extends ZodType {
|
|
3796
|
+
_parse(input) {
|
|
3797
|
+
if (this._def.coerce) {
|
|
3798
|
+
input.data = new Date(input.data);
|
|
3799
|
+
}
|
|
3800
|
+
const parsedType = this._getType(input);
|
|
3801
|
+
if (parsedType !== ZodParsedType.date) {
|
|
3802
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3803
|
+
addIssueToContext(ctx, {
|
|
3804
|
+
code: ZodIssueCode.invalid_type,
|
|
3805
|
+
expected: ZodParsedType.date,
|
|
3806
|
+
received: ctx.parsedType,
|
|
3807
|
+
});
|
|
3808
|
+
return INVALID;
|
|
3809
|
+
}
|
|
3810
|
+
if (isNaN(input.data.getTime())) {
|
|
3811
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3812
|
+
addIssueToContext(ctx, {
|
|
3813
|
+
code: ZodIssueCode.invalid_date,
|
|
3814
|
+
});
|
|
3815
|
+
return INVALID;
|
|
3816
|
+
}
|
|
3817
|
+
const status = new ParseStatus();
|
|
3818
|
+
let ctx = undefined;
|
|
3819
|
+
for (const check of this._def.checks) {
|
|
3820
|
+
if (check.kind === "min") {
|
|
3821
|
+
if (input.data.getTime() < check.value) {
|
|
3822
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3823
|
+
addIssueToContext(ctx, {
|
|
3824
|
+
code: ZodIssueCode.too_small,
|
|
3825
|
+
message: check.message,
|
|
3826
|
+
inclusive: true,
|
|
3827
|
+
exact: false,
|
|
3828
|
+
minimum: check.value,
|
|
3829
|
+
type: "date",
|
|
3830
|
+
});
|
|
3831
|
+
status.dirty();
|
|
3832
|
+
}
|
|
3833
|
+
}
|
|
3834
|
+
else if (check.kind === "max") {
|
|
3835
|
+
if (input.data.getTime() > check.value) {
|
|
3836
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3837
|
+
addIssueToContext(ctx, {
|
|
3838
|
+
code: ZodIssueCode.too_big,
|
|
3839
|
+
message: check.message,
|
|
3840
|
+
inclusive: true,
|
|
3841
|
+
exact: false,
|
|
3842
|
+
maximum: check.value,
|
|
3843
|
+
type: "date",
|
|
3844
|
+
});
|
|
3845
|
+
status.dirty();
|
|
3846
|
+
}
|
|
3847
|
+
}
|
|
3848
|
+
else {
|
|
3849
|
+
util.assertNever(check);
|
|
3850
|
+
}
|
|
3851
|
+
}
|
|
3852
|
+
return {
|
|
3853
|
+
status: status.value,
|
|
3854
|
+
value: new Date(input.data.getTime()),
|
|
3855
|
+
};
|
|
3856
|
+
}
|
|
3857
|
+
_addCheck(check) {
|
|
3858
|
+
return new ZodDate({
|
|
3859
|
+
...this._def,
|
|
3860
|
+
checks: [...this._def.checks, check],
|
|
3861
|
+
});
|
|
3862
|
+
}
|
|
3863
|
+
min(minDate, message) {
|
|
3864
|
+
return this._addCheck({
|
|
3865
|
+
kind: "min",
|
|
3866
|
+
value: minDate.getTime(),
|
|
3867
|
+
message: errorUtil.toString(message),
|
|
3868
|
+
});
|
|
3869
|
+
}
|
|
3870
|
+
max(maxDate, message) {
|
|
3871
|
+
return this._addCheck({
|
|
3872
|
+
kind: "max",
|
|
3873
|
+
value: maxDate.getTime(),
|
|
3874
|
+
message: errorUtil.toString(message),
|
|
3875
|
+
});
|
|
3876
|
+
}
|
|
3877
|
+
get minDate() {
|
|
3878
|
+
let min = null;
|
|
3879
|
+
for (const ch of this._def.checks) {
|
|
3880
|
+
if (ch.kind === "min") {
|
|
3881
|
+
if (min === null || ch.value > min)
|
|
3882
|
+
min = ch.value;
|
|
3883
|
+
}
|
|
3884
|
+
}
|
|
3885
|
+
return min != null ? new Date(min) : null;
|
|
3886
|
+
}
|
|
3887
|
+
get maxDate() {
|
|
3888
|
+
let max = null;
|
|
3889
|
+
for (const ch of this._def.checks) {
|
|
3890
|
+
if (ch.kind === "max") {
|
|
3891
|
+
if (max === null || ch.value < max)
|
|
3892
|
+
max = ch.value;
|
|
3893
|
+
}
|
|
3894
|
+
}
|
|
3895
|
+
return max != null ? new Date(max) : null;
|
|
3896
|
+
}
|
|
3897
|
+
}
|
|
3898
|
+
ZodDate.create = (params) => {
|
|
3899
|
+
return new ZodDate({
|
|
3900
|
+
checks: [],
|
|
3901
|
+
coerce: (params === null || params === void 0 ? void 0 : params.coerce) || false,
|
|
3902
|
+
typeName: ZodFirstPartyTypeKind.ZodDate,
|
|
3903
|
+
...processCreateParams(params),
|
|
3904
|
+
});
|
|
3905
|
+
};
|
|
3906
|
+
class ZodSymbol extends ZodType {
|
|
3907
|
+
_parse(input) {
|
|
3908
|
+
const parsedType = this._getType(input);
|
|
3909
|
+
if (parsedType !== ZodParsedType.symbol) {
|
|
3910
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3911
|
+
addIssueToContext(ctx, {
|
|
3912
|
+
code: ZodIssueCode.invalid_type,
|
|
3913
|
+
expected: ZodParsedType.symbol,
|
|
3914
|
+
received: ctx.parsedType,
|
|
3915
|
+
});
|
|
3916
|
+
return INVALID;
|
|
3917
|
+
}
|
|
3918
|
+
return OK(input.data);
|
|
3919
|
+
}
|
|
3920
|
+
}
|
|
3921
|
+
ZodSymbol.create = (params) => {
|
|
3922
|
+
return new ZodSymbol({
|
|
3923
|
+
typeName: ZodFirstPartyTypeKind.ZodSymbol,
|
|
3924
|
+
...processCreateParams(params),
|
|
3925
|
+
});
|
|
3926
|
+
};
|
|
3927
|
+
class ZodUndefined extends ZodType {
|
|
3928
|
+
_parse(input) {
|
|
3929
|
+
const parsedType = this._getType(input);
|
|
3930
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
3931
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3932
|
+
addIssueToContext(ctx, {
|
|
3933
|
+
code: ZodIssueCode.invalid_type,
|
|
3934
|
+
expected: ZodParsedType.undefined,
|
|
3935
|
+
received: ctx.parsedType,
|
|
3936
|
+
});
|
|
3937
|
+
return INVALID;
|
|
3938
|
+
}
|
|
3939
|
+
return OK(input.data);
|
|
3940
|
+
}
|
|
3941
|
+
}
|
|
3942
|
+
ZodUndefined.create = (params) => {
|
|
3943
|
+
return new ZodUndefined({
|
|
3944
|
+
typeName: ZodFirstPartyTypeKind.ZodUndefined,
|
|
3945
|
+
...processCreateParams(params),
|
|
3946
|
+
});
|
|
3947
|
+
};
|
|
3948
|
+
class ZodNull extends ZodType {
|
|
3949
|
+
_parse(input) {
|
|
3950
|
+
const parsedType = this._getType(input);
|
|
3951
|
+
if (parsedType !== ZodParsedType.null) {
|
|
3952
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3953
|
+
addIssueToContext(ctx, {
|
|
3954
|
+
code: ZodIssueCode.invalid_type,
|
|
3955
|
+
expected: ZodParsedType.null,
|
|
3956
|
+
received: ctx.parsedType,
|
|
3957
|
+
});
|
|
3958
|
+
return INVALID;
|
|
3959
|
+
}
|
|
3960
|
+
return OK(input.data);
|
|
3961
|
+
}
|
|
3962
|
+
}
|
|
3963
|
+
ZodNull.create = (params) => {
|
|
3964
|
+
return new ZodNull({
|
|
3965
|
+
typeName: ZodFirstPartyTypeKind.ZodNull,
|
|
3966
|
+
...processCreateParams(params),
|
|
3967
|
+
});
|
|
3968
|
+
};
|
|
3969
|
+
class ZodAny extends ZodType {
|
|
3970
|
+
constructor() {
|
|
3971
|
+
super(...arguments);
|
|
3972
|
+
// to prevent instances of other classes from extending ZodAny. this causes issues with catchall in ZodObject.
|
|
3973
|
+
this._any = true;
|
|
3974
|
+
}
|
|
3975
|
+
_parse(input) {
|
|
3976
|
+
return OK(input.data);
|
|
3977
|
+
}
|
|
3978
|
+
}
|
|
3979
|
+
ZodAny.create = (params) => {
|
|
3980
|
+
return new ZodAny({
|
|
3981
|
+
typeName: ZodFirstPartyTypeKind.ZodAny,
|
|
3982
|
+
...processCreateParams(params),
|
|
3983
|
+
});
|
|
3984
|
+
};
|
|
3985
|
+
class ZodUnknown extends ZodType {
|
|
3986
|
+
constructor() {
|
|
3987
|
+
super(...arguments);
|
|
3988
|
+
// required
|
|
3989
|
+
this._unknown = true;
|
|
3990
|
+
}
|
|
3991
|
+
_parse(input) {
|
|
3992
|
+
return OK(input.data);
|
|
3993
|
+
}
|
|
3994
|
+
}
|
|
3995
|
+
ZodUnknown.create = (params) => {
|
|
3996
|
+
return new ZodUnknown({
|
|
3997
|
+
typeName: ZodFirstPartyTypeKind.ZodUnknown,
|
|
3998
|
+
...processCreateParams(params),
|
|
3999
|
+
});
|
|
4000
|
+
};
|
|
4001
|
+
class ZodNever extends ZodType {
|
|
4002
|
+
_parse(input) {
|
|
4003
|
+
const ctx = this._getOrReturnCtx(input);
|
|
4004
|
+
addIssueToContext(ctx, {
|
|
4005
|
+
code: ZodIssueCode.invalid_type,
|
|
4006
|
+
expected: ZodParsedType.never,
|
|
4007
|
+
received: ctx.parsedType,
|
|
4008
|
+
});
|
|
4009
|
+
return INVALID;
|
|
4010
|
+
}
|
|
4011
|
+
}
|
|
4012
|
+
ZodNever.create = (params) => {
|
|
4013
|
+
return new ZodNever({
|
|
4014
|
+
typeName: ZodFirstPartyTypeKind.ZodNever,
|
|
4015
|
+
...processCreateParams(params),
|
|
4016
|
+
});
|
|
4017
|
+
};
|
|
4018
|
+
class ZodVoid extends ZodType {
|
|
4019
|
+
_parse(input) {
|
|
4020
|
+
const parsedType = this._getType(input);
|
|
4021
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
4022
|
+
const ctx = this._getOrReturnCtx(input);
|
|
4023
|
+
addIssueToContext(ctx, {
|
|
4024
|
+
code: ZodIssueCode.invalid_type,
|
|
4025
|
+
expected: ZodParsedType.void,
|
|
4026
|
+
received: ctx.parsedType,
|
|
4027
|
+
});
|
|
4028
|
+
return INVALID;
|
|
4029
|
+
}
|
|
4030
|
+
return OK(input.data);
|
|
4031
|
+
}
|
|
4032
|
+
}
|
|
4033
|
+
ZodVoid.create = (params) => {
|
|
4034
|
+
return new ZodVoid({
|
|
4035
|
+
typeName: ZodFirstPartyTypeKind.ZodVoid,
|
|
4036
|
+
...processCreateParams(params),
|
|
4037
|
+
});
|
|
4038
|
+
};
|
|
4039
|
+
class ZodArray extends ZodType {
|
|
4040
|
+
_parse(input) {
|
|
4041
|
+
const { ctx, status } = this._processInputParams(input);
|
|
4042
|
+
const def = this._def;
|
|
4043
|
+
if (ctx.parsedType !== ZodParsedType.array) {
|
|
4044
|
+
addIssueToContext(ctx, {
|
|
4045
|
+
code: ZodIssueCode.invalid_type,
|
|
4046
|
+
expected: ZodParsedType.array,
|
|
4047
|
+
received: ctx.parsedType,
|
|
4048
|
+
});
|
|
4049
|
+
return INVALID;
|
|
4050
|
+
}
|
|
4051
|
+
if (def.exactLength !== null) {
|
|
4052
|
+
const tooBig = ctx.data.length > def.exactLength.value;
|
|
4053
|
+
const tooSmall = ctx.data.length < def.exactLength.value;
|
|
4054
|
+
if (tooBig || tooSmall) {
|
|
4055
|
+
addIssueToContext(ctx, {
|
|
4056
|
+
code: tooBig ? ZodIssueCode.too_big : ZodIssueCode.too_small,
|
|
4057
|
+
minimum: (tooSmall ? def.exactLength.value : undefined),
|
|
4058
|
+
maximum: (tooBig ? def.exactLength.value : undefined),
|
|
4059
|
+
type: "array",
|
|
4060
|
+
inclusive: true,
|
|
4061
|
+
exact: true,
|
|
4062
|
+
message: def.exactLength.message,
|
|
4063
|
+
});
|
|
4064
|
+
status.dirty();
|
|
4065
|
+
}
|
|
4066
|
+
}
|
|
4067
|
+
if (def.minLength !== null) {
|
|
4068
|
+
if (ctx.data.length < def.minLength.value) {
|
|
4069
|
+
addIssueToContext(ctx, {
|
|
4070
|
+
code: ZodIssueCode.too_small,
|
|
4071
|
+
minimum: def.minLength.value,
|
|
4072
|
+
type: "array",
|
|
4073
|
+
inclusive: true,
|
|
4074
|
+
exact: false,
|
|
4075
|
+
message: def.minLength.message,
|
|
4076
|
+
});
|
|
4077
|
+
status.dirty();
|
|
4078
|
+
}
|
|
4079
|
+
}
|
|
4080
|
+
if (def.maxLength !== null) {
|
|
4081
|
+
if (ctx.data.length > def.maxLength.value) {
|
|
4082
|
+
addIssueToContext(ctx, {
|
|
4083
|
+
code: ZodIssueCode.too_big,
|
|
4084
|
+
maximum: def.maxLength.value,
|
|
4085
|
+
type: "array",
|
|
4086
|
+
inclusive: true,
|
|
4087
|
+
exact: false,
|
|
4088
|
+
message: def.maxLength.message,
|
|
4089
|
+
});
|
|
4090
|
+
status.dirty();
|
|
4091
|
+
}
|
|
4092
|
+
}
|
|
4093
|
+
if (ctx.common.async) {
|
|
4094
|
+
return Promise.all([...ctx.data].map((item, i) => {
|
|
4095
|
+
return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
4096
|
+
})).then((result) => {
|
|
4097
|
+
return ParseStatus.mergeArray(status, result);
|
|
4098
|
+
});
|
|
4099
|
+
}
|
|
4100
|
+
const result = [...ctx.data].map((item, i) => {
|
|
4101
|
+
return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
4102
|
+
});
|
|
4103
|
+
return ParseStatus.mergeArray(status, result);
|
|
4104
|
+
}
|
|
4105
|
+
get element() {
|
|
4106
|
+
return this._def.type;
|
|
4107
|
+
}
|
|
4108
|
+
min(minLength, message) {
|
|
4109
|
+
return new ZodArray({
|
|
4110
|
+
...this._def,
|
|
4111
|
+
minLength: { value: minLength, message: errorUtil.toString(message) },
|
|
4112
|
+
});
|
|
4113
|
+
}
|
|
4114
|
+
max(maxLength, message) {
|
|
4115
|
+
return new ZodArray({
|
|
4116
|
+
...this._def,
|
|
4117
|
+
maxLength: { value: maxLength, message: errorUtil.toString(message) },
|
|
4118
|
+
});
|
|
4119
|
+
}
|
|
4120
|
+
length(len, message) {
|
|
4121
|
+
return new ZodArray({
|
|
4122
|
+
...this._def,
|
|
4123
|
+
exactLength: { value: len, message: errorUtil.toString(message) },
|
|
4124
|
+
});
|
|
4125
|
+
}
|
|
4126
|
+
nonempty(message) {
|
|
4127
|
+
return this.min(1, message);
|
|
4128
|
+
}
|
|
4129
|
+
}
|
|
4130
|
+
ZodArray.create = (schema, params) => {
|
|
4131
|
+
return new ZodArray({
|
|
4132
|
+
type: schema,
|
|
4133
|
+
minLength: null,
|
|
4134
|
+
maxLength: null,
|
|
4135
|
+
exactLength: null,
|
|
4136
|
+
typeName: ZodFirstPartyTypeKind.ZodArray,
|
|
4137
|
+
...processCreateParams(params),
|
|
4138
|
+
});
|
|
4139
|
+
};
|
|
4140
|
+
function deepPartialify(schema) {
|
|
4141
|
+
if (schema instanceof ZodObject) {
|
|
4142
|
+
const newShape = {};
|
|
4143
|
+
for (const key in schema.shape) {
|
|
4144
|
+
const fieldSchema = schema.shape[key];
|
|
4145
|
+
newShape[key] = ZodOptional.create(deepPartialify(fieldSchema));
|
|
4146
|
+
}
|
|
4147
|
+
return new ZodObject({
|
|
4148
|
+
...schema._def,
|
|
4149
|
+
shape: () => newShape,
|
|
4150
|
+
});
|
|
4151
|
+
}
|
|
4152
|
+
else if (schema instanceof ZodArray) {
|
|
4153
|
+
return new ZodArray({
|
|
4154
|
+
...schema._def,
|
|
4155
|
+
type: deepPartialify(schema.element),
|
|
4156
|
+
});
|
|
4157
|
+
}
|
|
4158
|
+
else if (schema instanceof ZodOptional) {
|
|
4159
|
+
return ZodOptional.create(deepPartialify(schema.unwrap()));
|
|
4160
|
+
}
|
|
4161
|
+
else if (schema instanceof ZodNullable) {
|
|
4162
|
+
return ZodNullable.create(deepPartialify(schema.unwrap()));
|
|
4163
|
+
}
|
|
4164
|
+
else if (schema instanceof ZodTuple) {
|
|
4165
|
+
return ZodTuple.create(schema.items.map((item) => deepPartialify(item)));
|
|
4166
|
+
}
|
|
4167
|
+
else {
|
|
4168
|
+
return schema;
|
|
4169
|
+
}
|
|
4170
|
+
}
|
|
4171
|
+
class ZodObject extends ZodType {
|
|
4172
|
+
constructor() {
|
|
4173
|
+
super(...arguments);
|
|
4174
|
+
this._cached = null;
|
|
4175
|
+
/**
|
|
4176
|
+
* @deprecated In most cases, this is no longer needed - unknown properties are now silently stripped.
|
|
4177
|
+
* If you want to pass through unknown properties, use `.passthrough()` instead.
|
|
4178
|
+
*/
|
|
4179
|
+
this.nonstrict = this.passthrough;
|
|
4180
|
+
// extend<
|
|
4181
|
+
// Augmentation extends ZodRawShape,
|
|
4182
|
+
// NewOutput extends util.flatten<{
|
|
4183
|
+
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
|
|
4184
|
+
// ? Augmentation[k]["_output"]
|
|
4185
|
+
// : k extends keyof Output
|
|
4186
|
+
// ? Output[k]
|
|
4187
|
+
// : never;
|
|
4188
|
+
// }>,
|
|
4189
|
+
// NewInput extends util.flatten<{
|
|
4190
|
+
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
|
|
4191
|
+
// ? Augmentation[k]["_input"]
|
|
4192
|
+
// : k extends keyof Input
|
|
4193
|
+
// ? Input[k]
|
|
4194
|
+
// : never;
|
|
4195
|
+
// }>
|
|
4196
|
+
// >(
|
|
4197
|
+
// augmentation: Augmentation
|
|
4198
|
+
// ): ZodObject<
|
|
4199
|
+
// extendShape<T, Augmentation>,
|
|
4200
|
+
// UnknownKeys,
|
|
4201
|
+
// Catchall,
|
|
4202
|
+
// NewOutput,
|
|
4203
|
+
// NewInput
|
|
4204
|
+
// > {
|
|
4205
|
+
// return new ZodObject({
|
|
4206
|
+
// ...this._def,
|
|
4207
|
+
// shape: () => ({
|
|
4208
|
+
// ...this._def.shape(),
|
|
4209
|
+
// ...augmentation,
|
|
4210
|
+
// }),
|
|
4211
|
+
// }) as any;
|
|
4212
|
+
// }
|
|
4213
|
+
/**
|
|
4214
|
+
* @deprecated Use `.extend` instead
|
|
4215
|
+
* */
|
|
4216
|
+
this.augment = this.extend;
|
|
4217
|
+
}
|
|
4218
|
+
_getCached() {
|
|
4219
|
+
if (this._cached !== null)
|
|
4220
|
+
return this._cached;
|
|
4221
|
+
const shape = this._def.shape();
|
|
4222
|
+
const keys = util.objectKeys(shape);
|
|
4223
|
+
return (this._cached = { shape, keys });
|
|
4224
|
+
}
|
|
4225
|
+
_parse(input) {
|
|
4226
|
+
const parsedType = this._getType(input);
|
|
4227
|
+
if (parsedType !== ZodParsedType.object) {
|
|
4228
|
+
const ctx = this._getOrReturnCtx(input);
|
|
4229
|
+
addIssueToContext(ctx, {
|
|
4230
|
+
code: ZodIssueCode.invalid_type,
|
|
4231
|
+
expected: ZodParsedType.object,
|
|
4232
|
+
received: ctx.parsedType,
|
|
4233
|
+
});
|
|
4234
|
+
return INVALID;
|
|
4235
|
+
}
|
|
4236
|
+
const { status, ctx } = this._processInputParams(input);
|
|
4237
|
+
const { shape, keys: shapeKeys } = this._getCached();
|
|
4238
|
+
const extraKeys = [];
|
|
4239
|
+
if (!(this._def.catchall instanceof ZodNever &&
|
|
4240
|
+
this._def.unknownKeys === "strip")) {
|
|
4241
|
+
for (const key in ctx.data) {
|
|
4242
|
+
if (!shapeKeys.includes(key)) {
|
|
4243
|
+
extraKeys.push(key);
|
|
4244
|
+
}
|
|
4245
|
+
}
|
|
4246
|
+
}
|
|
4247
|
+
const pairs = [];
|
|
4248
|
+
for (const key of shapeKeys) {
|
|
4249
|
+
const keyValidator = shape[key];
|
|
4250
|
+
const value = ctx.data[key];
|
|
4251
|
+
pairs.push({
|
|
4252
|
+
key: { status: "valid", value: key },
|
|
4253
|
+
value: keyValidator._parse(new ParseInputLazyPath(ctx, value, ctx.path, key)),
|
|
4254
|
+
alwaysSet: key in ctx.data,
|
|
4255
|
+
});
|
|
4256
|
+
}
|
|
4257
|
+
if (this._def.catchall instanceof ZodNever) {
|
|
4258
|
+
const unknownKeys = this._def.unknownKeys;
|
|
4259
|
+
if (unknownKeys === "passthrough") {
|
|
4260
|
+
for (const key of extraKeys) {
|
|
4261
|
+
pairs.push({
|
|
4262
|
+
key: { status: "valid", value: key },
|
|
4263
|
+
value: { status: "valid", value: ctx.data[key] },
|
|
4264
|
+
});
|
|
4265
|
+
}
|
|
4266
|
+
}
|
|
4267
|
+
else if (unknownKeys === "strict") {
|
|
4268
|
+
if (extraKeys.length > 0) {
|
|
4269
|
+
addIssueToContext(ctx, {
|
|
4270
|
+
code: ZodIssueCode.unrecognized_keys,
|
|
4271
|
+
keys: extraKeys,
|
|
4272
|
+
});
|
|
4273
|
+
status.dirty();
|
|
4274
|
+
}
|
|
4275
|
+
}
|
|
4276
|
+
else if (unknownKeys === "strip") ;
|
|
4277
|
+
else {
|
|
4278
|
+
throw new Error(`Internal ZodObject error: invalid unknownKeys value.`);
|
|
4279
|
+
}
|
|
4280
|
+
}
|
|
4281
|
+
else {
|
|
4282
|
+
// run catchall validation
|
|
4283
|
+
const catchall = this._def.catchall;
|
|
4284
|
+
for (const key of extraKeys) {
|
|
4285
|
+
const value = ctx.data[key];
|
|
4286
|
+
pairs.push({
|
|
4287
|
+
key: { status: "valid", value: key },
|
|
4288
|
+
value: catchall._parse(new ParseInputLazyPath(ctx, value, ctx.path, key) //, ctx.child(key), value, getParsedType(value)
|
|
4289
|
+
),
|
|
4290
|
+
alwaysSet: key in ctx.data,
|
|
4291
|
+
});
|
|
4292
|
+
}
|
|
4293
|
+
}
|
|
4294
|
+
if (ctx.common.async) {
|
|
4295
|
+
return Promise.resolve()
|
|
4296
|
+
.then(async () => {
|
|
4297
|
+
const syncPairs = [];
|
|
4298
|
+
for (const pair of pairs) {
|
|
4299
|
+
const key = await pair.key;
|
|
4300
|
+
const value = await pair.value;
|
|
4301
|
+
syncPairs.push({
|
|
4302
|
+
key,
|
|
4303
|
+
value,
|
|
4304
|
+
alwaysSet: pair.alwaysSet,
|
|
4305
|
+
});
|
|
4306
|
+
}
|
|
4307
|
+
return syncPairs;
|
|
4308
|
+
})
|
|
4309
|
+
.then((syncPairs) => {
|
|
4310
|
+
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
4311
|
+
});
|
|
4312
|
+
}
|
|
4313
|
+
else {
|
|
4314
|
+
return ParseStatus.mergeObjectSync(status, pairs);
|
|
4315
|
+
}
|
|
4316
|
+
}
|
|
4317
|
+
get shape() {
|
|
4318
|
+
return this._def.shape();
|
|
4319
|
+
}
|
|
4320
|
+
strict(message) {
|
|
4321
|
+
errorUtil.errToObj;
|
|
4322
|
+
return new ZodObject({
|
|
4323
|
+
...this._def,
|
|
4324
|
+
unknownKeys: "strict",
|
|
4325
|
+
...(message !== undefined
|
|
4326
|
+
? {
|
|
4327
|
+
errorMap: (issue, ctx) => {
|
|
4328
|
+
var _a, _b, _c, _d;
|
|
4329
|
+
const defaultError = (_c = (_b = (_a = this._def).errorMap) === null || _b === void 0 ? void 0 : _b.call(_a, issue, ctx).message) !== null && _c !== void 0 ? _c : ctx.defaultError;
|
|
4330
|
+
if (issue.code === "unrecognized_keys")
|
|
4331
|
+
return {
|
|
4332
|
+
message: (_d = errorUtil.errToObj(message).message) !== null && _d !== void 0 ? _d : defaultError,
|
|
4333
|
+
};
|
|
4334
|
+
return {
|
|
4335
|
+
message: defaultError,
|
|
4336
|
+
};
|
|
4337
|
+
},
|
|
4338
|
+
}
|
|
4339
|
+
: {}),
|
|
4340
|
+
});
|
|
4341
|
+
}
|
|
4342
|
+
strip() {
|
|
4343
|
+
return new ZodObject({
|
|
4344
|
+
...this._def,
|
|
4345
|
+
unknownKeys: "strip",
|
|
4346
|
+
});
|
|
4347
|
+
}
|
|
4348
|
+
passthrough() {
|
|
4349
|
+
return new ZodObject({
|
|
4350
|
+
...this._def,
|
|
4351
|
+
unknownKeys: "passthrough",
|
|
4352
|
+
});
|
|
4353
|
+
}
|
|
4354
|
+
// const AugmentFactory =
|
|
4355
|
+
// <Def extends ZodObjectDef>(def: Def) =>
|
|
4356
|
+
// <Augmentation extends ZodRawShape>(
|
|
4357
|
+
// augmentation: Augmentation
|
|
4358
|
+
// ): ZodObject<
|
|
4359
|
+
// extendShape<ReturnType<Def["shape"]>, Augmentation>,
|
|
4360
|
+
// Def["unknownKeys"],
|
|
4361
|
+
// Def["catchall"]
|
|
4362
|
+
// > => {
|
|
4363
|
+
// return new ZodObject({
|
|
4364
|
+
// ...def,
|
|
4365
|
+
// shape: () => ({
|
|
4366
|
+
// ...def.shape(),
|
|
4367
|
+
// ...augmentation,
|
|
4368
|
+
// }),
|
|
4369
|
+
// }) as any;
|
|
4370
|
+
// };
|
|
4371
|
+
extend(augmentation) {
|
|
4372
|
+
return new ZodObject({
|
|
4373
|
+
...this._def,
|
|
4374
|
+
shape: () => ({
|
|
4375
|
+
...this._def.shape(),
|
|
4376
|
+
...augmentation,
|
|
4377
|
+
}),
|
|
4378
|
+
});
|
|
4379
|
+
}
|
|
4380
|
+
/**
|
|
4381
|
+
* Prior to zod@1.0.12 there was a bug in the
|
|
4382
|
+
* inferred type of merged objects. Please
|
|
4383
|
+
* upgrade if you are experiencing issues.
|
|
4384
|
+
*/
|
|
4385
|
+
merge(merging) {
|
|
4386
|
+
const merged = new ZodObject({
|
|
4387
|
+
unknownKeys: merging._def.unknownKeys,
|
|
4388
|
+
catchall: merging._def.catchall,
|
|
4389
|
+
shape: () => ({
|
|
4390
|
+
...this._def.shape(),
|
|
4391
|
+
...merging._def.shape(),
|
|
4392
|
+
}),
|
|
4393
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
4394
|
+
});
|
|
4395
|
+
return merged;
|
|
4396
|
+
}
|
|
4397
|
+
// merge<
|
|
4398
|
+
// Incoming extends AnyZodObject,
|
|
4399
|
+
// Augmentation extends Incoming["shape"],
|
|
4400
|
+
// NewOutput extends {
|
|
4401
|
+
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
|
|
4402
|
+
// ? Augmentation[k]["_output"]
|
|
4403
|
+
// : k extends keyof Output
|
|
4404
|
+
// ? Output[k]
|
|
4405
|
+
// : never;
|
|
4406
|
+
// },
|
|
4407
|
+
// NewInput extends {
|
|
4408
|
+
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
|
|
4409
|
+
// ? Augmentation[k]["_input"]
|
|
4410
|
+
// : k extends keyof Input
|
|
4411
|
+
// ? Input[k]
|
|
4412
|
+
// : never;
|
|
4413
|
+
// }
|
|
4414
|
+
// >(
|
|
4415
|
+
// merging: Incoming
|
|
4416
|
+
// ): ZodObject<
|
|
4417
|
+
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
4418
|
+
// Incoming["_def"]["unknownKeys"],
|
|
4419
|
+
// Incoming["_def"]["catchall"],
|
|
4420
|
+
// NewOutput,
|
|
4421
|
+
// NewInput
|
|
4422
|
+
// > {
|
|
4423
|
+
// const merged: any = new ZodObject({
|
|
4424
|
+
// unknownKeys: merging._def.unknownKeys,
|
|
4425
|
+
// catchall: merging._def.catchall,
|
|
4426
|
+
// shape: () =>
|
|
4427
|
+
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
4428
|
+
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
4429
|
+
// }) as any;
|
|
4430
|
+
// return merged;
|
|
4431
|
+
// }
|
|
4432
|
+
setKey(key, schema) {
|
|
4433
|
+
return this.augment({ [key]: schema });
|
|
4434
|
+
}
|
|
4435
|
+
// merge<Incoming extends AnyZodObject>(
|
|
4436
|
+
// merging: Incoming
|
|
4437
|
+
// ): //ZodObject<T & Incoming["_shape"], UnknownKeys, Catchall> = (merging) => {
|
|
4438
|
+
// ZodObject<
|
|
4439
|
+
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
4440
|
+
// Incoming["_def"]["unknownKeys"],
|
|
4441
|
+
// Incoming["_def"]["catchall"]
|
|
4442
|
+
// > {
|
|
4443
|
+
// // const mergedShape = objectUtil.mergeShapes(
|
|
4444
|
+
// // this._def.shape(),
|
|
4445
|
+
// // merging._def.shape()
|
|
4446
|
+
// // );
|
|
4447
|
+
// const merged: any = new ZodObject({
|
|
4448
|
+
// unknownKeys: merging._def.unknownKeys,
|
|
4449
|
+
// catchall: merging._def.catchall,
|
|
4450
|
+
// shape: () =>
|
|
4451
|
+
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
4452
|
+
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
4453
|
+
// }) as any;
|
|
4454
|
+
// return merged;
|
|
4455
|
+
// }
|
|
4456
|
+
catchall(index) {
|
|
4457
|
+
return new ZodObject({
|
|
4458
|
+
...this._def,
|
|
4459
|
+
catchall: index,
|
|
4460
|
+
});
|
|
4461
|
+
}
|
|
4462
|
+
pick(mask) {
|
|
4463
|
+
const shape = {};
|
|
4464
|
+
util.objectKeys(mask).forEach((key) => {
|
|
4465
|
+
if (mask[key] && this.shape[key]) {
|
|
4466
|
+
shape[key] = this.shape[key];
|
|
4467
|
+
}
|
|
4468
|
+
});
|
|
4469
|
+
return new ZodObject({
|
|
4470
|
+
...this._def,
|
|
4471
|
+
shape: () => shape,
|
|
4472
|
+
});
|
|
4473
|
+
}
|
|
4474
|
+
omit(mask) {
|
|
4475
|
+
const shape = {};
|
|
4476
|
+
util.objectKeys(this.shape).forEach((key) => {
|
|
4477
|
+
if (!mask[key]) {
|
|
4478
|
+
shape[key] = this.shape[key];
|
|
4479
|
+
}
|
|
4480
|
+
});
|
|
4481
|
+
return new ZodObject({
|
|
4482
|
+
...this._def,
|
|
4483
|
+
shape: () => shape,
|
|
4484
|
+
});
|
|
4485
|
+
}
|
|
4486
|
+
/**
|
|
4487
|
+
* @deprecated
|
|
4488
|
+
*/
|
|
4489
|
+
deepPartial() {
|
|
4490
|
+
return deepPartialify(this);
|
|
4491
|
+
}
|
|
4492
|
+
partial(mask) {
|
|
4493
|
+
const newShape = {};
|
|
4494
|
+
util.objectKeys(this.shape).forEach((key) => {
|
|
4495
|
+
const fieldSchema = this.shape[key];
|
|
4496
|
+
if (mask && !mask[key]) {
|
|
4497
|
+
newShape[key] = fieldSchema;
|
|
4498
|
+
}
|
|
4499
|
+
else {
|
|
4500
|
+
newShape[key] = fieldSchema.optional();
|
|
4501
|
+
}
|
|
4502
|
+
});
|
|
4503
|
+
return new ZodObject({
|
|
4504
|
+
...this._def,
|
|
4505
|
+
shape: () => newShape,
|
|
4506
|
+
});
|
|
4507
|
+
}
|
|
4508
|
+
required(mask) {
|
|
4509
|
+
const newShape = {};
|
|
4510
|
+
util.objectKeys(this.shape).forEach((key) => {
|
|
4511
|
+
if (mask && !mask[key]) {
|
|
4512
|
+
newShape[key] = this.shape[key];
|
|
4513
|
+
}
|
|
4514
|
+
else {
|
|
4515
|
+
const fieldSchema = this.shape[key];
|
|
4516
|
+
let newField = fieldSchema;
|
|
4517
|
+
while (newField instanceof ZodOptional) {
|
|
4518
|
+
newField = newField._def.innerType;
|
|
4519
|
+
}
|
|
4520
|
+
newShape[key] = newField;
|
|
4521
|
+
}
|
|
4522
|
+
});
|
|
4523
|
+
return new ZodObject({
|
|
4524
|
+
...this._def,
|
|
4525
|
+
shape: () => newShape,
|
|
4526
|
+
});
|
|
4527
|
+
}
|
|
4528
|
+
keyof() {
|
|
4529
|
+
return createZodEnum(util.objectKeys(this.shape));
|
|
4530
|
+
}
|
|
4531
|
+
}
|
|
4532
|
+
ZodObject.create = (shape, params) => {
|
|
4533
|
+
return new ZodObject({
|
|
4534
|
+
shape: () => shape,
|
|
4535
|
+
unknownKeys: "strip",
|
|
4536
|
+
catchall: ZodNever.create(),
|
|
4537
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
4538
|
+
...processCreateParams(params),
|
|
4539
|
+
});
|
|
4540
|
+
};
|
|
4541
|
+
ZodObject.strictCreate = (shape, params) => {
|
|
4542
|
+
return new ZodObject({
|
|
4543
|
+
shape: () => shape,
|
|
4544
|
+
unknownKeys: "strict",
|
|
4545
|
+
catchall: ZodNever.create(),
|
|
4546
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
4547
|
+
...processCreateParams(params),
|
|
4548
|
+
});
|
|
4549
|
+
};
|
|
4550
|
+
ZodObject.lazycreate = (shape, params) => {
|
|
4551
|
+
return new ZodObject({
|
|
4552
|
+
shape,
|
|
4553
|
+
unknownKeys: "strip",
|
|
4554
|
+
catchall: ZodNever.create(),
|
|
4555
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
4556
|
+
...processCreateParams(params),
|
|
4557
|
+
});
|
|
4558
|
+
};
|
|
4559
|
+
class ZodUnion extends ZodType {
|
|
4560
|
+
_parse(input) {
|
|
4561
|
+
const { ctx } = this._processInputParams(input);
|
|
4562
|
+
const options = this._def.options;
|
|
4563
|
+
function handleResults(results) {
|
|
4564
|
+
// return first issue-free validation if it exists
|
|
4565
|
+
for (const result of results) {
|
|
4566
|
+
if (result.result.status === "valid") {
|
|
4567
|
+
return result.result;
|
|
4568
|
+
}
|
|
4569
|
+
}
|
|
4570
|
+
for (const result of results) {
|
|
4571
|
+
if (result.result.status === "dirty") {
|
|
4572
|
+
// add issues from dirty option
|
|
4573
|
+
ctx.common.issues.push(...result.ctx.common.issues);
|
|
4574
|
+
return result.result;
|
|
4575
|
+
}
|
|
4576
|
+
}
|
|
4577
|
+
// return invalid
|
|
4578
|
+
const unionErrors = results.map((result) => new ZodError(result.ctx.common.issues));
|
|
4579
|
+
addIssueToContext(ctx, {
|
|
4580
|
+
code: ZodIssueCode.invalid_union,
|
|
4581
|
+
unionErrors,
|
|
4582
|
+
});
|
|
4583
|
+
return INVALID;
|
|
4584
|
+
}
|
|
4585
|
+
if (ctx.common.async) {
|
|
4586
|
+
return Promise.all(options.map(async (option) => {
|
|
4587
|
+
const childCtx = {
|
|
4588
|
+
...ctx,
|
|
4589
|
+
common: {
|
|
4590
|
+
...ctx.common,
|
|
4591
|
+
issues: [],
|
|
4592
|
+
},
|
|
4593
|
+
parent: null,
|
|
4594
|
+
};
|
|
4595
|
+
return {
|
|
4596
|
+
result: await option._parseAsync({
|
|
4597
|
+
data: ctx.data,
|
|
4598
|
+
path: ctx.path,
|
|
4599
|
+
parent: childCtx,
|
|
4600
|
+
}),
|
|
4601
|
+
ctx: childCtx,
|
|
4602
|
+
};
|
|
4603
|
+
})).then(handleResults);
|
|
4604
|
+
}
|
|
4605
|
+
else {
|
|
4606
|
+
let dirty = undefined;
|
|
4607
|
+
const issues = [];
|
|
4608
|
+
for (const option of options) {
|
|
4609
|
+
const childCtx = {
|
|
4610
|
+
...ctx,
|
|
4611
|
+
common: {
|
|
4612
|
+
...ctx.common,
|
|
4613
|
+
issues: [],
|
|
4614
|
+
},
|
|
4615
|
+
parent: null,
|
|
4616
|
+
};
|
|
4617
|
+
const result = option._parseSync({
|
|
4618
|
+
data: ctx.data,
|
|
4619
|
+
path: ctx.path,
|
|
4620
|
+
parent: childCtx,
|
|
4621
|
+
});
|
|
4622
|
+
if (result.status === "valid") {
|
|
4623
|
+
return result;
|
|
4624
|
+
}
|
|
4625
|
+
else if (result.status === "dirty" && !dirty) {
|
|
4626
|
+
dirty = { result, ctx: childCtx };
|
|
4627
|
+
}
|
|
4628
|
+
if (childCtx.common.issues.length) {
|
|
4629
|
+
issues.push(childCtx.common.issues);
|
|
4630
|
+
}
|
|
4631
|
+
}
|
|
4632
|
+
if (dirty) {
|
|
4633
|
+
ctx.common.issues.push(...dirty.ctx.common.issues);
|
|
4634
|
+
return dirty.result;
|
|
4635
|
+
}
|
|
4636
|
+
const unionErrors = issues.map((issues) => new ZodError(issues));
|
|
4637
|
+
addIssueToContext(ctx, {
|
|
4638
|
+
code: ZodIssueCode.invalid_union,
|
|
4639
|
+
unionErrors,
|
|
4640
|
+
});
|
|
4641
|
+
return INVALID;
|
|
4642
|
+
}
|
|
4643
|
+
}
|
|
4644
|
+
get options() {
|
|
4645
|
+
return this._def.options;
|
|
4646
|
+
}
|
|
4647
|
+
}
|
|
4648
|
+
ZodUnion.create = (types, params) => {
|
|
4649
|
+
return new ZodUnion({
|
|
4650
|
+
options: types,
|
|
4651
|
+
typeName: ZodFirstPartyTypeKind.ZodUnion,
|
|
4652
|
+
...processCreateParams(params),
|
|
4653
|
+
});
|
|
4654
|
+
};
|
|
4655
|
+
/////////////////////////////////////////////////////
|
|
4656
|
+
/////////////////////////////////////////////////////
|
|
4657
|
+
////////// //////////
|
|
4658
|
+
////////// ZodDiscriminatedUnion //////////
|
|
4659
|
+
////////// //////////
|
|
4660
|
+
/////////////////////////////////////////////////////
|
|
4661
|
+
/////////////////////////////////////////////////////
|
|
4662
|
+
const getDiscriminator = (type) => {
|
|
4663
|
+
if (type instanceof ZodLazy) {
|
|
4664
|
+
return getDiscriminator(type.schema);
|
|
4665
|
+
}
|
|
4666
|
+
else if (type instanceof ZodEffects) {
|
|
4667
|
+
return getDiscriminator(type.innerType());
|
|
4668
|
+
}
|
|
4669
|
+
else if (type instanceof ZodLiteral) {
|
|
4670
|
+
return [type.value];
|
|
4671
|
+
}
|
|
4672
|
+
else if (type instanceof ZodEnum) {
|
|
4673
|
+
return type.options;
|
|
4674
|
+
}
|
|
4675
|
+
else if (type instanceof ZodNativeEnum) {
|
|
4676
|
+
// eslint-disable-next-line ban/ban
|
|
4677
|
+
return util.objectValues(type.enum);
|
|
4678
|
+
}
|
|
4679
|
+
else if (type instanceof ZodDefault) {
|
|
4680
|
+
return getDiscriminator(type._def.innerType);
|
|
4681
|
+
}
|
|
4682
|
+
else if (type instanceof ZodUndefined) {
|
|
4683
|
+
return [undefined];
|
|
4684
|
+
}
|
|
4685
|
+
else if (type instanceof ZodNull) {
|
|
4686
|
+
return [null];
|
|
4687
|
+
}
|
|
4688
|
+
else if (type instanceof ZodOptional) {
|
|
4689
|
+
return [undefined, ...getDiscriminator(type.unwrap())];
|
|
4690
|
+
}
|
|
4691
|
+
else if (type instanceof ZodNullable) {
|
|
4692
|
+
return [null, ...getDiscriminator(type.unwrap())];
|
|
4693
|
+
}
|
|
4694
|
+
else if (type instanceof ZodBranded) {
|
|
4695
|
+
return getDiscriminator(type.unwrap());
|
|
4696
|
+
}
|
|
4697
|
+
else if (type instanceof ZodReadonly) {
|
|
4698
|
+
return getDiscriminator(type.unwrap());
|
|
4699
|
+
}
|
|
4700
|
+
else if (type instanceof ZodCatch) {
|
|
4701
|
+
return getDiscriminator(type._def.innerType);
|
|
4702
|
+
}
|
|
4703
|
+
else {
|
|
4704
|
+
return [];
|
|
4705
|
+
}
|
|
4706
|
+
};
|
|
4707
|
+
class ZodDiscriminatedUnion extends ZodType {
|
|
4708
|
+
_parse(input) {
|
|
4709
|
+
const { ctx } = this._processInputParams(input);
|
|
4710
|
+
if (ctx.parsedType !== ZodParsedType.object) {
|
|
4711
|
+
addIssueToContext(ctx, {
|
|
4712
|
+
code: ZodIssueCode.invalid_type,
|
|
4713
|
+
expected: ZodParsedType.object,
|
|
4714
|
+
received: ctx.parsedType,
|
|
4715
|
+
});
|
|
4716
|
+
return INVALID;
|
|
4717
|
+
}
|
|
4718
|
+
const discriminator = this.discriminator;
|
|
4719
|
+
const discriminatorValue = ctx.data[discriminator];
|
|
4720
|
+
const option = this.optionsMap.get(discriminatorValue);
|
|
4721
|
+
if (!option) {
|
|
4722
|
+
addIssueToContext(ctx, {
|
|
4723
|
+
code: ZodIssueCode.invalid_union_discriminator,
|
|
4724
|
+
options: Array.from(this.optionsMap.keys()),
|
|
4725
|
+
path: [discriminator],
|
|
4726
|
+
});
|
|
4727
|
+
return INVALID;
|
|
4728
|
+
}
|
|
4729
|
+
if (ctx.common.async) {
|
|
4730
|
+
return option._parseAsync({
|
|
4731
|
+
data: ctx.data,
|
|
4732
|
+
path: ctx.path,
|
|
4733
|
+
parent: ctx,
|
|
4734
|
+
});
|
|
4735
|
+
}
|
|
4736
|
+
else {
|
|
4737
|
+
return option._parseSync({
|
|
4738
|
+
data: ctx.data,
|
|
4739
|
+
path: ctx.path,
|
|
4740
|
+
parent: ctx,
|
|
4741
|
+
});
|
|
4742
|
+
}
|
|
4743
|
+
}
|
|
4744
|
+
get discriminator() {
|
|
4745
|
+
return this._def.discriminator;
|
|
4746
|
+
}
|
|
4747
|
+
get options() {
|
|
4748
|
+
return this._def.options;
|
|
4749
|
+
}
|
|
4750
|
+
get optionsMap() {
|
|
4751
|
+
return this._def.optionsMap;
|
|
4752
|
+
}
|
|
4753
|
+
/**
|
|
4754
|
+
* The constructor of the discriminated union schema. Its behaviour is very similar to that of the normal z.union() constructor.
|
|
4755
|
+
* However, it only allows a union of objects, all of which need to share a discriminator property. This property must
|
|
4756
|
+
* have a different value for each object in the union.
|
|
4757
|
+
* @param discriminator the name of the discriminator property
|
|
4758
|
+
* @param types an array of object schemas
|
|
4759
|
+
* @param params
|
|
4760
|
+
*/
|
|
4761
|
+
static create(discriminator, options, params) {
|
|
4762
|
+
// Get all the valid discriminator values
|
|
4763
|
+
const optionsMap = new Map();
|
|
4764
|
+
// try {
|
|
4765
|
+
for (const type of options) {
|
|
4766
|
+
const discriminatorValues = getDiscriminator(type.shape[discriminator]);
|
|
4767
|
+
if (!discriminatorValues.length) {
|
|
4768
|
+
throw new Error(`A discriminator value for key \`${discriminator}\` could not be extracted from all schema options`);
|
|
4769
|
+
}
|
|
4770
|
+
for (const value of discriminatorValues) {
|
|
4771
|
+
if (optionsMap.has(value)) {
|
|
4772
|
+
throw new Error(`Discriminator property ${String(discriminator)} has duplicate value ${String(value)}`);
|
|
4773
|
+
}
|
|
4774
|
+
optionsMap.set(value, type);
|
|
4775
|
+
}
|
|
4776
|
+
}
|
|
4777
|
+
return new ZodDiscriminatedUnion({
|
|
4778
|
+
typeName: ZodFirstPartyTypeKind.ZodDiscriminatedUnion,
|
|
4779
|
+
discriminator,
|
|
4780
|
+
options,
|
|
4781
|
+
optionsMap,
|
|
4782
|
+
...processCreateParams(params),
|
|
4783
|
+
});
|
|
4784
|
+
}
|
|
4785
|
+
}
|
|
4786
|
+
function mergeValues(a, b) {
|
|
4787
|
+
const aType = getParsedType(a);
|
|
4788
|
+
const bType = getParsedType(b);
|
|
4789
|
+
if (a === b) {
|
|
4790
|
+
return { valid: true, data: a };
|
|
4791
|
+
}
|
|
4792
|
+
else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
|
|
4793
|
+
const bKeys = util.objectKeys(b);
|
|
4794
|
+
const sharedKeys = util
|
|
4795
|
+
.objectKeys(a)
|
|
4796
|
+
.filter((key) => bKeys.indexOf(key) !== -1);
|
|
4797
|
+
const newObj = { ...a, ...b };
|
|
4798
|
+
for (const key of sharedKeys) {
|
|
4799
|
+
const sharedValue = mergeValues(a[key], b[key]);
|
|
4800
|
+
if (!sharedValue.valid) {
|
|
4801
|
+
return { valid: false };
|
|
4802
|
+
}
|
|
4803
|
+
newObj[key] = sharedValue.data;
|
|
4804
|
+
}
|
|
4805
|
+
return { valid: true, data: newObj };
|
|
4806
|
+
}
|
|
4807
|
+
else if (aType === ZodParsedType.array && bType === ZodParsedType.array) {
|
|
4808
|
+
if (a.length !== b.length) {
|
|
4809
|
+
return { valid: false };
|
|
4810
|
+
}
|
|
4811
|
+
const newArray = [];
|
|
4812
|
+
for (let index = 0; index < a.length; index++) {
|
|
4813
|
+
const itemA = a[index];
|
|
4814
|
+
const itemB = b[index];
|
|
4815
|
+
const sharedValue = mergeValues(itemA, itemB);
|
|
4816
|
+
if (!sharedValue.valid) {
|
|
4817
|
+
return { valid: false };
|
|
4818
|
+
}
|
|
4819
|
+
newArray.push(sharedValue.data);
|
|
4820
|
+
}
|
|
4821
|
+
return { valid: true, data: newArray };
|
|
4822
|
+
}
|
|
4823
|
+
else if (aType === ZodParsedType.date &&
|
|
4824
|
+
bType === ZodParsedType.date &&
|
|
4825
|
+
+a === +b) {
|
|
4826
|
+
return { valid: true, data: a };
|
|
4827
|
+
}
|
|
4828
|
+
else {
|
|
4829
|
+
return { valid: false };
|
|
4830
|
+
}
|
|
4831
|
+
}
|
|
4832
|
+
class ZodIntersection extends ZodType {
|
|
4833
|
+
_parse(input) {
|
|
4834
|
+
const { status, ctx } = this._processInputParams(input);
|
|
4835
|
+
const handleParsed = (parsedLeft, parsedRight) => {
|
|
4836
|
+
if (isAborted(parsedLeft) || isAborted(parsedRight)) {
|
|
4837
|
+
return INVALID;
|
|
4838
|
+
}
|
|
4839
|
+
const merged = mergeValues(parsedLeft.value, parsedRight.value);
|
|
4840
|
+
if (!merged.valid) {
|
|
4841
|
+
addIssueToContext(ctx, {
|
|
4842
|
+
code: ZodIssueCode.invalid_intersection_types,
|
|
4843
|
+
});
|
|
4844
|
+
return INVALID;
|
|
4845
|
+
}
|
|
4846
|
+
if (isDirty(parsedLeft) || isDirty(parsedRight)) {
|
|
4847
|
+
status.dirty();
|
|
4848
|
+
}
|
|
4849
|
+
return { status: status.value, value: merged.data };
|
|
4850
|
+
};
|
|
4851
|
+
if (ctx.common.async) {
|
|
4852
|
+
return Promise.all([
|
|
4853
|
+
this._def.left._parseAsync({
|
|
4854
|
+
data: ctx.data,
|
|
4855
|
+
path: ctx.path,
|
|
4856
|
+
parent: ctx,
|
|
4857
|
+
}),
|
|
4858
|
+
this._def.right._parseAsync({
|
|
4859
|
+
data: ctx.data,
|
|
4860
|
+
path: ctx.path,
|
|
4861
|
+
parent: ctx,
|
|
4862
|
+
}),
|
|
4863
|
+
]).then(([left, right]) => handleParsed(left, right));
|
|
4864
|
+
}
|
|
4865
|
+
else {
|
|
4866
|
+
return handleParsed(this._def.left._parseSync({
|
|
4867
|
+
data: ctx.data,
|
|
4868
|
+
path: ctx.path,
|
|
4869
|
+
parent: ctx,
|
|
4870
|
+
}), this._def.right._parseSync({
|
|
4871
|
+
data: ctx.data,
|
|
4872
|
+
path: ctx.path,
|
|
4873
|
+
parent: ctx,
|
|
4874
|
+
}));
|
|
4875
|
+
}
|
|
4876
|
+
}
|
|
4877
|
+
}
|
|
4878
|
+
ZodIntersection.create = (left, right, params) => {
|
|
4879
|
+
return new ZodIntersection({
|
|
4880
|
+
left: left,
|
|
4881
|
+
right: right,
|
|
4882
|
+
typeName: ZodFirstPartyTypeKind.ZodIntersection,
|
|
4883
|
+
...processCreateParams(params),
|
|
4884
|
+
});
|
|
4885
|
+
};
|
|
4886
|
+
class ZodTuple extends ZodType {
|
|
4887
|
+
_parse(input) {
|
|
4888
|
+
const { status, ctx } = this._processInputParams(input);
|
|
4889
|
+
if (ctx.parsedType !== ZodParsedType.array) {
|
|
4890
|
+
addIssueToContext(ctx, {
|
|
4891
|
+
code: ZodIssueCode.invalid_type,
|
|
4892
|
+
expected: ZodParsedType.array,
|
|
4893
|
+
received: ctx.parsedType,
|
|
4894
|
+
});
|
|
4895
|
+
return INVALID;
|
|
4896
|
+
}
|
|
4897
|
+
if (ctx.data.length < this._def.items.length) {
|
|
4898
|
+
addIssueToContext(ctx, {
|
|
4899
|
+
code: ZodIssueCode.too_small,
|
|
4900
|
+
minimum: this._def.items.length,
|
|
4901
|
+
inclusive: true,
|
|
4902
|
+
exact: false,
|
|
4903
|
+
type: "array",
|
|
4904
|
+
});
|
|
4905
|
+
return INVALID;
|
|
4906
|
+
}
|
|
4907
|
+
const rest = this._def.rest;
|
|
4908
|
+
if (!rest && ctx.data.length > this._def.items.length) {
|
|
4909
|
+
addIssueToContext(ctx, {
|
|
4910
|
+
code: ZodIssueCode.too_big,
|
|
4911
|
+
maximum: this._def.items.length,
|
|
4912
|
+
inclusive: true,
|
|
4913
|
+
exact: false,
|
|
4914
|
+
type: "array",
|
|
4915
|
+
});
|
|
4916
|
+
status.dirty();
|
|
4917
|
+
}
|
|
4918
|
+
const items = [...ctx.data]
|
|
4919
|
+
.map((item, itemIndex) => {
|
|
4920
|
+
const schema = this._def.items[itemIndex] || this._def.rest;
|
|
4921
|
+
if (!schema)
|
|
4922
|
+
return null;
|
|
4923
|
+
return schema._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
|
|
4924
|
+
})
|
|
4925
|
+
.filter((x) => !!x); // filter nulls
|
|
4926
|
+
if (ctx.common.async) {
|
|
4927
|
+
return Promise.all(items).then((results) => {
|
|
4928
|
+
return ParseStatus.mergeArray(status, results);
|
|
4929
|
+
});
|
|
4930
|
+
}
|
|
4931
|
+
else {
|
|
4932
|
+
return ParseStatus.mergeArray(status, items);
|
|
4933
|
+
}
|
|
4934
|
+
}
|
|
4935
|
+
get items() {
|
|
4936
|
+
return this._def.items;
|
|
4937
|
+
}
|
|
4938
|
+
rest(rest) {
|
|
4939
|
+
return new ZodTuple({
|
|
4940
|
+
...this._def,
|
|
4941
|
+
rest,
|
|
4942
|
+
});
|
|
4943
|
+
}
|
|
4944
|
+
}
|
|
4945
|
+
ZodTuple.create = (schemas, params) => {
|
|
4946
|
+
if (!Array.isArray(schemas)) {
|
|
4947
|
+
throw new Error("You must pass an array of schemas to z.tuple([ ... ])");
|
|
4948
|
+
}
|
|
4949
|
+
return new ZodTuple({
|
|
4950
|
+
items: schemas,
|
|
4951
|
+
typeName: ZodFirstPartyTypeKind.ZodTuple,
|
|
4952
|
+
rest: null,
|
|
4953
|
+
...processCreateParams(params),
|
|
4954
|
+
});
|
|
4955
|
+
};
|
|
4956
|
+
class ZodRecord extends ZodType {
|
|
4957
|
+
get keySchema() {
|
|
4958
|
+
return this._def.keyType;
|
|
4959
|
+
}
|
|
4960
|
+
get valueSchema() {
|
|
4961
|
+
return this._def.valueType;
|
|
4962
|
+
}
|
|
4963
|
+
_parse(input) {
|
|
4964
|
+
const { status, ctx } = this._processInputParams(input);
|
|
4965
|
+
if (ctx.parsedType !== ZodParsedType.object) {
|
|
4966
|
+
addIssueToContext(ctx, {
|
|
4967
|
+
code: ZodIssueCode.invalid_type,
|
|
4968
|
+
expected: ZodParsedType.object,
|
|
4969
|
+
received: ctx.parsedType,
|
|
4970
|
+
});
|
|
4971
|
+
return INVALID;
|
|
4972
|
+
}
|
|
4973
|
+
const pairs = [];
|
|
4974
|
+
const keyType = this._def.keyType;
|
|
4975
|
+
const valueType = this._def.valueType;
|
|
4976
|
+
for (const key in ctx.data) {
|
|
4977
|
+
pairs.push({
|
|
4978
|
+
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
|
|
4979
|
+
value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
|
|
4980
|
+
alwaysSet: key in ctx.data,
|
|
4981
|
+
});
|
|
4982
|
+
}
|
|
4983
|
+
if (ctx.common.async) {
|
|
4984
|
+
return ParseStatus.mergeObjectAsync(status, pairs);
|
|
4985
|
+
}
|
|
4986
|
+
else {
|
|
4987
|
+
return ParseStatus.mergeObjectSync(status, pairs);
|
|
4988
|
+
}
|
|
4989
|
+
}
|
|
4990
|
+
get element() {
|
|
4991
|
+
return this._def.valueType;
|
|
4992
|
+
}
|
|
4993
|
+
static create(first, second, third) {
|
|
4994
|
+
if (second instanceof ZodType) {
|
|
4995
|
+
return new ZodRecord({
|
|
4996
|
+
keyType: first,
|
|
4997
|
+
valueType: second,
|
|
4998
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
4999
|
+
...processCreateParams(third),
|
|
5000
|
+
});
|
|
5001
|
+
}
|
|
5002
|
+
return new ZodRecord({
|
|
5003
|
+
keyType: ZodString.create(),
|
|
5004
|
+
valueType: first,
|
|
5005
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
5006
|
+
...processCreateParams(second),
|
|
5007
|
+
});
|
|
5008
|
+
}
|
|
5009
|
+
}
|
|
5010
|
+
class ZodMap extends ZodType {
|
|
5011
|
+
get keySchema() {
|
|
5012
|
+
return this._def.keyType;
|
|
5013
|
+
}
|
|
5014
|
+
get valueSchema() {
|
|
5015
|
+
return this._def.valueType;
|
|
5016
|
+
}
|
|
5017
|
+
_parse(input) {
|
|
5018
|
+
const { status, ctx } = this._processInputParams(input);
|
|
5019
|
+
if (ctx.parsedType !== ZodParsedType.map) {
|
|
5020
|
+
addIssueToContext(ctx, {
|
|
5021
|
+
code: ZodIssueCode.invalid_type,
|
|
5022
|
+
expected: ZodParsedType.map,
|
|
5023
|
+
received: ctx.parsedType,
|
|
5024
|
+
});
|
|
5025
|
+
return INVALID;
|
|
5026
|
+
}
|
|
5027
|
+
const keyType = this._def.keyType;
|
|
5028
|
+
const valueType = this._def.valueType;
|
|
5029
|
+
const pairs = [...ctx.data.entries()].map(([key, value], index) => {
|
|
5030
|
+
return {
|
|
5031
|
+
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, [index, "key"])),
|
|
5032
|
+
value: valueType._parse(new ParseInputLazyPath(ctx, value, ctx.path, [index, "value"])),
|
|
5033
|
+
};
|
|
5034
|
+
});
|
|
5035
|
+
if (ctx.common.async) {
|
|
5036
|
+
const finalMap = new Map();
|
|
5037
|
+
return Promise.resolve().then(async () => {
|
|
5038
|
+
for (const pair of pairs) {
|
|
5039
|
+
const key = await pair.key;
|
|
5040
|
+
const value = await pair.value;
|
|
5041
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
5042
|
+
return INVALID;
|
|
5043
|
+
}
|
|
5044
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
5045
|
+
status.dirty();
|
|
5046
|
+
}
|
|
5047
|
+
finalMap.set(key.value, value.value);
|
|
5048
|
+
}
|
|
5049
|
+
return { status: status.value, value: finalMap };
|
|
5050
|
+
});
|
|
5051
|
+
}
|
|
5052
|
+
else {
|
|
5053
|
+
const finalMap = new Map();
|
|
5054
|
+
for (const pair of pairs) {
|
|
5055
|
+
const key = pair.key;
|
|
5056
|
+
const value = pair.value;
|
|
5057
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
5058
|
+
return INVALID;
|
|
5059
|
+
}
|
|
5060
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
5061
|
+
status.dirty();
|
|
5062
|
+
}
|
|
5063
|
+
finalMap.set(key.value, value.value);
|
|
5064
|
+
}
|
|
5065
|
+
return { status: status.value, value: finalMap };
|
|
5066
|
+
}
|
|
5067
|
+
}
|
|
5068
|
+
}
|
|
5069
|
+
ZodMap.create = (keyType, valueType, params) => {
|
|
5070
|
+
return new ZodMap({
|
|
5071
|
+
valueType,
|
|
5072
|
+
keyType,
|
|
5073
|
+
typeName: ZodFirstPartyTypeKind.ZodMap,
|
|
5074
|
+
...processCreateParams(params),
|
|
5075
|
+
});
|
|
5076
|
+
};
|
|
5077
|
+
class ZodSet extends ZodType {
|
|
5078
|
+
_parse(input) {
|
|
5079
|
+
const { status, ctx } = this._processInputParams(input);
|
|
5080
|
+
if (ctx.parsedType !== ZodParsedType.set) {
|
|
5081
|
+
addIssueToContext(ctx, {
|
|
5082
|
+
code: ZodIssueCode.invalid_type,
|
|
5083
|
+
expected: ZodParsedType.set,
|
|
5084
|
+
received: ctx.parsedType,
|
|
5085
|
+
});
|
|
5086
|
+
return INVALID;
|
|
5087
|
+
}
|
|
5088
|
+
const def = this._def;
|
|
5089
|
+
if (def.minSize !== null) {
|
|
5090
|
+
if (ctx.data.size < def.minSize.value) {
|
|
5091
|
+
addIssueToContext(ctx, {
|
|
5092
|
+
code: ZodIssueCode.too_small,
|
|
5093
|
+
minimum: def.minSize.value,
|
|
5094
|
+
type: "set",
|
|
5095
|
+
inclusive: true,
|
|
5096
|
+
exact: false,
|
|
5097
|
+
message: def.minSize.message,
|
|
5098
|
+
});
|
|
5099
|
+
status.dirty();
|
|
5100
|
+
}
|
|
5101
|
+
}
|
|
5102
|
+
if (def.maxSize !== null) {
|
|
5103
|
+
if (ctx.data.size > def.maxSize.value) {
|
|
5104
|
+
addIssueToContext(ctx, {
|
|
5105
|
+
code: ZodIssueCode.too_big,
|
|
5106
|
+
maximum: def.maxSize.value,
|
|
5107
|
+
type: "set",
|
|
5108
|
+
inclusive: true,
|
|
5109
|
+
exact: false,
|
|
5110
|
+
message: def.maxSize.message,
|
|
5111
|
+
});
|
|
5112
|
+
status.dirty();
|
|
5113
|
+
}
|
|
5114
|
+
}
|
|
5115
|
+
const valueType = this._def.valueType;
|
|
5116
|
+
function finalizeSet(elements) {
|
|
5117
|
+
const parsedSet = new Set();
|
|
5118
|
+
for (const element of elements) {
|
|
5119
|
+
if (element.status === "aborted")
|
|
5120
|
+
return INVALID;
|
|
5121
|
+
if (element.status === "dirty")
|
|
5122
|
+
status.dirty();
|
|
5123
|
+
parsedSet.add(element.value);
|
|
5124
|
+
}
|
|
5125
|
+
return { status: status.value, value: parsedSet };
|
|
5126
|
+
}
|
|
5127
|
+
const elements = [...ctx.data.values()].map((item, i) => valueType._parse(new ParseInputLazyPath(ctx, item, ctx.path, i)));
|
|
5128
|
+
if (ctx.common.async) {
|
|
5129
|
+
return Promise.all(elements).then((elements) => finalizeSet(elements));
|
|
5130
|
+
}
|
|
5131
|
+
else {
|
|
5132
|
+
return finalizeSet(elements);
|
|
5133
|
+
}
|
|
5134
|
+
}
|
|
5135
|
+
min(minSize, message) {
|
|
5136
|
+
return new ZodSet({
|
|
5137
|
+
...this._def,
|
|
5138
|
+
minSize: { value: minSize, message: errorUtil.toString(message) },
|
|
5139
|
+
});
|
|
5140
|
+
}
|
|
5141
|
+
max(maxSize, message) {
|
|
5142
|
+
return new ZodSet({
|
|
5143
|
+
...this._def,
|
|
5144
|
+
maxSize: { value: maxSize, message: errorUtil.toString(message) },
|
|
5145
|
+
});
|
|
5146
|
+
}
|
|
5147
|
+
size(size, message) {
|
|
5148
|
+
return this.min(size, message).max(size, message);
|
|
5149
|
+
}
|
|
5150
|
+
nonempty(message) {
|
|
5151
|
+
return this.min(1, message);
|
|
5152
|
+
}
|
|
5153
|
+
}
|
|
5154
|
+
ZodSet.create = (valueType, params) => {
|
|
5155
|
+
return new ZodSet({
|
|
5156
|
+
valueType,
|
|
5157
|
+
minSize: null,
|
|
5158
|
+
maxSize: null,
|
|
5159
|
+
typeName: ZodFirstPartyTypeKind.ZodSet,
|
|
5160
|
+
...processCreateParams(params),
|
|
5161
|
+
});
|
|
5162
|
+
};
|
|
5163
|
+
class ZodFunction extends ZodType {
|
|
5164
|
+
constructor() {
|
|
5165
|
+
super(...arguments);
|
|
5166
|
+
this.validate = this.implement;
|
|
5167
|
+
}
|
|
5168
|
+
_parse(input) {
|
|
5169
|
+
const { ctx } = this._processInputParams(input);
|
|
5170
|
+
if (ctx.parsedType !== ZodParsedType.function) {
|
|
5171
|
+
addIssueToContext(ctx, {
|
|
5172
|
+
code: ZodIssueCode.invalid_type,
|
|
5173
|
+
expected: ZodParsedType.function,
|
|
5174
|
+
received: ctx.parsedType,
|
|
5175
|
+
});
|
|
5176
|
+
return INVALID;
|
|
5177
|
+
}
|
|
5178
|
+
function makeArgsIssue(args, error) {
|
|
5179
|
+
return makeIssue({
|
|
5180
|
+
data: args,
|
|
5181
|
+
path: ctx.path,
|
|
5182
|
+
errorMaps: [
|
|
5183
|
+
ctx.common.contextualErrorMap,
|
|
5184
|
+
ctx.schemaErrorMap,
|
|
5185
|
+
getErrorMap(),
|
|
5186
|
+
errorMap,
|
|
5187
|
+
].filter((x) => !!x),
|
|
5188
|
+
issueData: {
|
|
5189
|
+
code: ZodIssueCode.invalid_arguments,
|
|
5190
|
+
argumentsError: error,
|
|
5191
|
+
},
|
|
5192
|
+
});
|
|
5193
|
+
}
|
|
5194
|
+
function makeReturnsIssue(returns, error) {
|
|
5195
|
+
return makeIssue({
|
|
5196
|
+
data: returns,
|
|
5197
|
+
path: ctx.path,
|
|
5198
|
+
errorMaps: [
|
|
5199
|
+
ctx.common.contextualErrorMap,
|
|
5200
|
+
ctx.schemaErrorMap,
|
|
5201
|
+
getErrorMap(),
|
|
5202
|
+
errorMap,
|
|
5203
|
+
].filter((x) => !!x),
|
|
5204
|
+
issueData: {
|
|
5205
|
+
code: ZodIssueCode.invalid_return_type,
|
|
5206
|
+
returnTypeError: error,
|
|
5207
|
+
},
|
|
5208
|
+
});
|
|
5209
|
+
}
|
|
5210
|
+
const params = { errorMap: ctx.common.contextualErrorMap };
|
|
5211
|
+
const fn = ctx.data;
|
|
5212
|
+
if (this._def.returns instanceof ZodPromise) {
|
|
5213
|
+
// Would love a way to avoid disabling this rule, but we need
|
|
5214
|
+
// an alias (using an arrow function was what caused 2651).
|
|
5215
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
5216
|
+
const me = this;
|
|
5217
|
+
return OK(async function (...args) {
|
|
5218
|
+
const error = new ZodError([]);
|
|
5219
|
+
const parsedArgs = await me._def.args
|
|
5220
|
+
.parseAsync(args, params)
|
|
5221
|
+
.catch((e) => {
|
|
5222
|
+
error.addIssue(makeArgsIssue(args, e));
|
|
5223
|
+
throw error;
|
|
5224
|
+
});
|
|
5225
|
+
const result = await Reflect.apply(fn, this, parsedArgs);
|
|
5226
|
+
const parsedReturns = await me._def.returns._def.type
|
|
5227
|
+
.parseAsync(result, params)
|
|
5228
|
+
.catch((e) => {
|
|
5229
|
+
error.addIssue(makeReturnsIssue(result, e));
|
|
5230
|
+
throw error;
|
|
5231
|
+
});
|
|
5232
|
+
return parsedReturns;
|
|
5233
|
+
});
|
|
5234
|
+
}
|
|
5235
|
+
else {
|
|
5236
|
+
// Would love a way to avoid disabling this rule, but we need
|
|
5237
|
+
// an alias (using an arrow function was what caused 2651).
|
|
5238
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
5239
|
+
const me = this;
|
|
5240
|
+
return OK(function (...args) {
|
|
5241
|
+
const parsedArgs = me._def.args.safeParse(args, params);
|
|
5242
|
+
if (!parsedArgs.success) {
|
|
5243
|
+
throw new ZodError([makeArgsIssue(args, parsedArgs.error)]);
|
|
5244
|
+
}
|
|
5245
|
+
const result = Reflect.apply(fn, this, parsedArgs.data);
|
|
5246
|
+
const parsedReturns = me._def.returns.safeParse(result, params);
|
|
5247
|
+
if (!parsedReturns.success) {
|
|
5248
|
+
throw new ZodError([makeReturnsIssue(result, parsedReturns.error)]);
|
|
5249
|
+
}
|
|
5250
|
+
return parsedReturns.data;
|
|
5251
|
+
});
|
|
5252
|
+
}
|
|
5253
|
+
}
|
|
5254
|
+
parameters() {
|
|
5255
|
+
return this._def.args;
|
|
5256
|
+
}
|
|
5257
|
+
returnType() {
|
|
5258
|
+
return this._def.returns;
|
|
5259
|
+
}
|
|
5260
|
+
args(...items) {
|
|
5261
|
+
return new ZodFunction({
|
|
5262
|
+
...this._def,
|
|
5263
|
+
args: ZodTuple.create(items).rest(ZodUnknown.create()),
|
|
5264
|
+
});
|
|
5265
|
+
}
|
|
5266
|
+
returns(returnType) {
|
|
5267
|
+
return new ZodFunction({
|
|
5268
|
+
...this._def,
|
|
5269
|
+
returns: returnType,
|
|
5270
|
+
});
|
|
5271
|
+
}
|
|
5272
|
+
implement(func) {
|
|
5273
|
+
const validatedFunc = this.parse(func);
|
|
5274
|
+
return validatedFunc;
|
|
5275
|
+
}
|
|
5276
|
+
strictImplement(func) {
|
|
5277
|
+
const validatedFunc = this.parse(func);
|
|
5278
|
+
return validatedFunc;
|
|
5279
|
+
}
|
|
5280
|
+
static create(args, returns, params) {
|
|
5281
|
+
return new ZodFunction({
|
|
5282
|
+
args: (args
|
|
5283
|
+
? args
|
|
5284
|
+
: ZodTuple.create([]).rest(ZodUnknown.create())),
|
|
5285
|
+
returns: returns || ZodUnknown.create(),
|
|
5286
|
+
typeName: ZodFirstPartyTypeKind.ZodFunction,
|
|
5287
|
+
...processCreateParams(params),
|
|
5288
|
+
});
|
|
5289
|
+
}
|
|
5290
|
+
}
|
|
5291
|
+
class ZodLazy extends ZodType {
|
|
5292
|
+
get schema() {
|
|
5293
|
+
return this._def.getter();
|
|
5294
|
+
}
|
|
5295
|
+
_parse(input) {
|
|
5296
|
+
const { ctx } = this._processInputParams(input);
|
|
5297
|
+
const lazySchema = this._def.getter();
|
|
5298
|
+
return lazySchema._parse({ data: ctx.data, path: ctx.path, parent: ctx });
|
|
5299
|
+
}
|
|
5300
|
+
}
|
|
5301
|
+
ZodLazy.create = (getter, params) => {
|
|
5302
|
+
return new ZodLazy({
|
|
5303
|
+
getter: getter,
|
|
5304
|
+
typeName: ZodFirstPartyTypeKind.ZodLazy,
|
|
5305
|
+
...processCreateParams(params),
|
|
5306
|
+
});
|
|
5307
|
+
};
|
|
5308
|
+
class ZodLiteral extends ZodType {
|
|
5309
|
+
_parse(input) {
|
|
5310
|
+
if (input.data !== this._def.value) {
|
|
5311
|
+
const ctx = this._getOrReturnCtx(input);
|
|
5312
|
+
addIssueToContext(ctx, {
|
|
5313
|
+
received: ctx.data,
|
|
5314
|
+
code: ZodIssueCode.invalid_literal,
|
|
5315
|
+
expected: this._def.value,
|
|
5316
|
+
});
|
|
5317
|
+
return INVALID;
|
|
5318
|
+
}
|
|
5319
|
+
return { status: "valid", value: input.data };
|
|
5320
|
+
}
|
|
5321
|
+
get value() {
|
|
5322
|
+
return this._def.value;
|
|
5323
|
+
}
|
|
5324
|
+
}
|
|
5325
|
+
ZodLiteral.create = (value, params) => {
|
|
5326
|
+
return new ZodLiteral({
|
|
5327
|
+
value: value,
|
|
5328
|
+
typeName: ZodFirstPartyTypeKind.ZodLiteral,
|
|
5329
|
+
...processCreateParams(params),
|
|
5330
|
+
});
|
|
5331
|
+
};
|
|
5332
|
+
function createZodEnum(values, params) {
|
|
5333
|
+
return new ZodEnum({
|
|
5334
|
+
values,
|
|
5335
|
+
typeName: ZodFirstPartyTypeKind.ZodEnum,
|
|
5336
|
+
...processCreateParams(params),
|
|
5337
|
+
});
|
|
5338
|
+
}
|
|
5339
|
+
class ZodEnum extends ZodType {
|
|
5340
|
+
constructor() {
|
|
5341
|
+
super(...arguments);
|
|
5342
|
+
_ZodEnum_cache.set(this, void 0);
|
|
5343
|
+
}
|
|
5344
|
+
_parse(input) {
|
|
5345
|
+
if (typeof input.data !== "string") {
|
|
5346
|
+
const ctx = this._getOrReturnCtx(input);
|
|
5347
|
+
const expectedValues = this._def.values;
|
|
5348
|
+
addIssueToContext(ctx, {
|
|
5349
|
+
expected: util.joinValues(expectedValues),
|
|
5350
|
+
received: ctx.parsedType,
|
|
5351
|
+
code: ZodIssueCode.invalid_type,
|
|
5352
|
+
});
|
|
5353
|
+
return INVALID;
|
|
5354
|
+
}
|
|
5355
|
+
if (!__classPrivateFieldGet(this, _ZodEnum_cache, "f")) {
|
|
5356
|
+
__classPrivateFieldSet(this, _ZodEnum_cache, new Set(this._def.values), "f");
|
|
5357
|
+
}
|
|
5358
|
+
if (!__classPrivateFieldGet(this, _ZodEnum_cache, "f").has(input.data)) {
|
|
5359
|
+
const ctx = this._getOrReturnCtx(input);
|
|
5360
|
+
const expectedValues = this._def.values;
|
|
5361
|
+
addIssueToContext(ctx, {
|
|
5362
|
+
received: ctx.data,
|
|
5363
|
+
code: ZodIssueCode.invalid_enum_value,
|
|
5364
|
+
options: expectedValues,
|
|
5365
|
+
});
|
|
5366
|
+
return INVALID;
|
|
5367
|
+
}
|
|
5368
|
+
return OK(input.data);
|
|
5369
|
+
}
|
|
5370
|
+
get options() {
|
|
5371
|
+
return this._def.values;
|
|
5372
|
+
}
|
|
5373
|
+
get enum() {
|
|
5374
|
+
const enumValues = {};
|
|
5375
|
+
for (const val of this._def.values) {
|
|
5376
|
+
enumValues[val] = val;
|
|
5377
|
+
}
|
|
5378
|
+
return enumValues;
|
|
5379
|
+
}
|
|
5380
|
+
get Values() {
|
|
5381
|
+
const enumValues = {};
|
|
5382
|
+
for (const val of this._def.values) {
|
|
5383
|
+
enumValues[val] = val;
|
|
5384
|
+
}
|
|
5385
|
+
return enumValues;
|
|
5386
|
+
}
|
|
5387
|
+
get Enum() {
|
|
5388
|
+
const enumValues = {};
|
|
5389
|
+
for (const val of this._def.values) {
|
|
5390
|
+
enumValues[val] = val;
|
|
5391
|
+
}
|
|
5392
|
+
return enumValues;
|
|
5393
|
+
}
|
|
5394
|
+
extract(values, newDef = this._def) {
|
|
5395
|
+
return ZodEnum.create(values, {
|
|
5396
|
+
...this._def,
|
|
5397
|
+
...newDef,
|
|
5398
|
+
});
|
|
5399
|
+
}
|
|
5400
|
+
exclude(values, newDef = this._def) {
|
|
5401
|
+
return ZodEnum.create(this.options.filter((opt) => !values.includes(opt)), {
|
|
5402
|
+
...this._def,
|
|
5403
|
+
...newDef,
|
|
5404
|
+
});
|
|
5405
|
+
}
|
|
5406
|
+
}
|
|
5407
|
+
_ZodEnum_cache = new WeakMap();
|
|
5408
|
+
ZodEnum.create = createZodEnum;
|
|
5409
|
+
class ZodNativeEnum extends ZodType {
|
|
5410
|
+
constructor() {
|
|
5411
|
+
super(...arguments);
|
|
5412
|
+
_ZodNativeEnum_cache.set(this, void 0);
|
|
5413
|
+
}
|
|
5414
|
+
_parse(input) {
|
|
5415
|
+
const nativeEnumValues = util.getValidEnumValues(this._def.values);
|
|
5416
|
+
const ctx = this._getOrReturnCtx(input);
|
|
5417
|
+
if (ctx.parsedType !== ZodParsedType.string &&
|
|
5418
|
+
ctx.parsedType !== ZodParsedType.number) {
|
|
5419
|
+
const expectedValues = util.objectValues(nativeEnumValues);
|
|
5420
|
+
addIssueToContext(ctx, {
|
|
5421
|
+
expected: util.joinValues(expectedValues),
|
|
5422
|
+
received: ctx.parsedType,
|
|
5423
|
+
code: ZodIssueCode.invalid_type,
|
|
5424
|
+
});
|
|
5425
|
+
return INVALID;
|
|
5426
|
+
}
|
|
5427
|
+
if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache, "f")) {
|
|
5428
|
+
__classPrivateFieldSet(this, _ZodNativeEnum_cache, new Set(util.getValidEnumValues(this._def.values)), "f");
|
|
5429
|
+
}
|
|
5430
|
+
if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache, "f").has(input.data)) {
|
|
5431
|
+
const expectedValues = util.objectValues(nativeEnumValues);
|
|
5432
|
+
addIssueToContext(ctx, {
|
|
5433
|
+
received: ctx.data,
|
|
5434
|
+
code: ZodIssueCode.invalid_enum_value,
|
|
5435
|
+
options: expectedValues,
|
|
5436
|
+
});
|
|
5437
|
+
return INVALID;
|
|
5438
|
+
}
|
|
5439
|
+
return OK(input.data);
|
|
5440
|
+
}
|
|
5441
|
+
get enum() {
|
|
5442
|
+
return this._def.values;
|
|
5443
|
+
}
|
|
5444
|
+
}
|
|
5445
|
+
_ZodNativeEnum_cache = new WeakMap();
|
|
5446
|
+
ZodNativeEnum.create = (values, params) => {
|
|
5447
|
+
return new ZodNativeEnum({
|
|
5448
|
+
values: values,
|
|
5449
|
+
typeName: ZodFirstPartyTypeKind.ZodNativeEnum,
|
|
5450
|
+
...processCreateParams(params),
|
|
5451
|
+
});
|
|
5452
|
+
};
|
|
5453
|
+
class ZodPromise extends ZodType {
|
|
5454
|
+
unwrap() {
|
|
5455
|
+
return this._def.type;
|
|
5456
|
+
}
|
|
5457
|
+
_parse(input) {
|
|
5458
|
+
const { ctx } = this._processInputParams(input);
|
|
5459
|
+
if (ctx.parsedType !== ZodParsedType.promise &&
|
|
5460
|
+
ctx.common.async === false) {
|
|
5461
|
+
addIssueToContext(ctx, {
|
|
5462
|
+
code: ZodIssueCode.invalid_type,
|
|
5463
|
+
expected: ZodParsedType.promise,
|
|
5464
|
+
received: ctx.parsedType,
|
|
5465
|
+
});
|
|
5466
|
+
return INVALID;
|
|
5467
|
+
}
|
|
5468
|
+
const promisified = ctx.parsedType === ZodParsedType.promise
|
|
5469
|
+
? ctx.data
|
|
5470
|
+
: Promise.resolve(ctx.data);
|
|
5471
|
+
return OK(promisified.then((data) => {
|
|
5472
|
+
return this._def.type.parseAsync(data, {
|
|
5473
|
+
path: ctx.path,
|
|
5474
|
+
errorMap: ctx.common.contextualErrorMap,
|
|
5475
|
+
});
|
|
5476
|
+
}));
|
|
5477
|
+
}
|
|
5478
|
+
}
|
|
5479
|
+
ZodPromise.create = (schema, params) => {
|
|
5480
|
+
return new ZodPromise({
|
|
5481
|
+
type: schema,
|
|
5482
|
+
typeName: ZodFirstPartyTypeKind.ZodPromise,
|
|
5483
|
+
...processCreateParams(params),
|
|
5484
|
+
});
|
|
5485
|
+
};
|
|
5486
|
+
class ZodEffects extends ZodType {
|
|
5487
|
+
innerType() {
|
|
5488
|
+
return this._def.schema;
|
|
5489
|
+
}
|
|
5490
|
+
sourceType() {
|
|
5491
|
+
return this._def.schema._def.typeName === ZodFirstPartyTypeKind.ZodEffects
|
|
5492
|
+
? this._def.schema.sourceType()
|
|
5493
|
+
: this._def.schema;
|
|
5494
|
+
}
|
|
5495
|
+
_parse(input) {
|
|
5496
|
+
const { status, ctx } = this._processInputParams(input);
|
|
5497
|
+
const effect = this._def.effect || null;
|
|
5498
|
+
const checkCtx = {
|
|
5499
|
+
addIssue: (arg) => {
|
|
5500
|
+
addIssueToContext(ctx, arg);
|
|
5501
|
+
if (arg.fatal) {
|
|
5502
|
+
status.abort();
|
|
5503
|
+
}
|
|
5504
|
+
else {
|
|
5505
|
+
status.dirty();
|
|
5506
|
+
}
|
|
5507
|
+
},
|
|
5508
|
+
get path() {
|
|
5509
|
+
return ctx.path;
|
|
5510
|
+
},
|
|
5511
|
+
};
|
|
5512
|
+
checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
|
|
5513
|
+
if (effect.type === "preprocess") {
|
|
5514
|
+
const processed = effect.transform(ctx.data, checkCtx);
|
|
5515
|
+
if (ctx.common.async) {
|
|
5516
|
+
return Promise.resolve(processed).then(async (processed) => {
|
|
5517
|
+
if (status.value === "aborted")
|
|
5518
|
+
return INVALID;
|
|
5519
|
+
const result = await this._def.schema._parseAsync({
|
|
5520
|
+
data: processed,
|
|
5521
|
+
path: ctx.path,
|
|
5522
|
+
parent: ctx,
|
|
5523
|
+
});
|
|
5524
|
+
if (result.status === "aborted")
|
|
5525
|
+
return INVALID;
|
|
5526
|
+
if (result.status === "dirty")
|
|
5527
|
+
return DIRTY(result.value);
|
|
5528
|
+
if (status.value === "dirty")
|
|
5529
|
+
return DIRTY(result.value);
|
|
5530
|
+
return result;
|
|
5531
|
+
});
|
|
5532
|
+
}
|
|
5533
|
+
else {
|
|
5534
|
+
if (status.value === "aborted")
|
|
5535
|
+
return INVALID;
|
|
5536
|
+
const result = this._def.schema._parseSync({
|
|
5537
|
+
data: processed,
|
|
5538
|
+
path: ctx.path,
|
|
5539
|
+
parent: ctx,
|
|
5540
|
+
});
|
|
5541
|
+
if (result.status === "aborted")
|
|
5542
|
+
return INVALID;
|
|
5543
|
+
if (result.status === "dirty")
|
|
5544
|
+
return DIRTY(result.value);
|
|
5545
|
+
if (status.value === "dirty")
|
|
5546
|
+
return DIRTY(result.value);
|
|
5547
|
+
return result;
|
|
5548
|
+
}
|
|
5549
|
+
}
|
|
5550
|
+
if (effect.type === "refinement") {
|
|
5551
|
+
const executeRefinement = (acc) => {
|
|
5552
|
+
const result = effect.refinement(acc, checkCtx);
|
|
5553
|
+
if (ctx.common.async) {
|
|
5554
|
+
return Promise.resolve(result);
|
|
5555
|
+
}
|
|
5556
|
+
if (result instanceof Promise) {
|
|
5557
|
+
throw new Error("Async refinement encountered during synchronous parse operation. Use .parseAsync instead.");
|
|
5558
|
+
}
|
|
5559
|
+
return acc;
|
|
5560
|
+
};
|
|
5561
|
+
if (ctx.common.async === false) {
|
|
5562
|
+
const inner = this._def.schema._parseSync({
|
|
5563
|
+
data: ctx.data,
|
|
5564
|
+
path: ctx.path,
|
|
5565
|
+
parent: ctx,
|
|
5566
|
+
});
|
|
5567
|
+
if (inner.status === "aborted")
|
|
5568
|
+
return INVALID;
|
|
5569
|
+
if (inner.status === "dirty")
|
|
5570
|
+
status.dirty();
|
|
5571
|
+
// return value is ignored
|
|
5572
|
+
executeRefinement(inner.value);
|
|
5573
|
+
return { status: status.value, value: inner.value };
|
|
5574
|
+
}
|
|
5575
|
+
else {
|
|
5576
|
+
return this._def.schema
|
|
5577
|
+
._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx })
|
|
5578
|
+
.then((inner) => {
|
|
5579
|
+
if (inner.status === "aborted")
|
|
5580
|
+
return INVALID;
|
|
5581
|
+
if (inner.status === "dirty")
|
|
5582
|
+
status.dirty();
|
|
5583
|
+
return executeRefinement(inner.value).then(() => {
|
|
5584
|
+
return { status: status.value, value: inner.value };
|
|
5585
|
+
});
|
|
5586
|
+
});
|
|
5587
|
+
}
|
|
5588
|
+
}
|
|
5589
|
+
if (effect.type === "transform") {
|
|
5590
|
+
if (ctx.common.async === false) {
|
|
5591
|
+
const base = this._def.schema._parseSync({
|
|
5592
|
+
data: ctx.data,
|
|
5593
|
+
path: ctx.path,
|
|
5594
|
+
parent: ctx,
|
|
5595
|
+
});
|
|
5596
|
+
if (!isValid(base))
|
|
5597
|
+
return base;
|
|
5598
|
+
const result = effect.transform(base.value, checkCtx);
|
|
5599
|
+
if (result instanceof Promise) {
|
|
5600
|
+
throw new Error(`Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.`);
|
|
5601
|
+
}
|
|
5602
|
+
return { status: status.value, value: result };
|
|
5603
|
+
}
|
|
5604
|
+
else {
|
|
5605
|
+
return this._def.schema
|
|
5606
|
+
._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx })
|
|
5607
|
+
.then((base) => {
|
|
5608
|
+
if (!isValid(base))
|
|
5609
|
+
return base;
|
|
5610
|
+
return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({ status: status.value, value: result }));
|
|
5611
|
+
});
|
|
5612
|
+
}
|
|
5613
|
+
}
|
|
5614
|
+
util.assertNever(effect);
|
|
5615
|
+
}
|
|
5616
|
+
}
|
|
5617
|
+
ZodEffects.create = (schema, effect, params) => {
|
|
5618
|
+
return new ZodEffects({
|
|
5619
|
+
schema,
|
|
5620
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
5621
|
+
effect,
|
|
5622
|
+
...processCreateParams(params),
|
|
5623
|
+
});
|
|
5624
|
+
};
|
|
5625
|
+
ZodEffects.createWithPreprocess = (preprocess, schema, params) => {
|
|
5626
|
+
return new ZodEffects({
|
|
5627
|
+
schema,
|
|
5628
|
+
effect: { type: "preprocess", transform: preprocess },
|
|
5629
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
5630
|
+
...processCreateParams(params),
|
|
5631
|
+
});
|
|
5632
|
+
};
|
|
5633
|
+
class ZodOptional extends ZodType {
|
|
5634
|
+
_parse(input) {
|
|
5635
|
+
const parsedType = this._getType(input);
|
|
5636
|
+
if (parsedType === ZodParsedType.undefined) {
|
|
5637
|
+
return OK(undefined);
|
|
1804
5638
|
}
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
5639
|
+
return this._def.innerType._parse(input);
|
|
5640
|
+
}
|
|
5641
|
+
unwrap() {
|
|
5642
|
+
return this._def.innerType;
|
|
5643
|
+
}
|
|
5644
|
+
}
|
|
5645
|
+
ZodOptional.create = (type, params) => {
|
|
5646
|
+
return new ZodOptional({
|
|
5647
|
+
innerType: type,
|
|
5648
|
+
typeName: ZodFirstPartyTypeKind.ZodOptional,
|
|
5649
|
+
...processCreateParams(params),
|
|
5650
|
+
});
|
|
5651
|
+
};
|
|
5652
|
+
class ZodNullable extends ZodType {
|
|
5653
|
+
_parse(input) {
|
|
5654
|
+
const parsedType = this._getType(input);
|
|
5655
|
+
if (parsedType === ZodParsedType.null) {
|
|
5656
|
+
return OK(null);
|
|
1808
5657
|
}
|
|
1809
|
-
|
|
1810
|
-
return this;
|
|
5658
|
+
return this._def.innerType._parse(input);
|
|
1811
5659
|
}
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
5660
|
+
unwrap() {
|
|
5661
|
+
return this._def.innerType;
|
|
5662
|
+
}
|
|
5663
|
+
}
|
|
5664
|
+
ZodNullable.create = (type, params) => {
|
|
5665
|
+
return new ZodNullable({
|
|
5666
|
+
innerType: type,
|
|
5667
|
+
typeName: ZodFirstPartyTypeKind.ZodNullable,
|
|
5668
|
+
...processCreateParams(params),
|
|
5669
|
+
});
|
|
5670
|
+
};
|
|
5671
|
+
class ZodDefault extends ZodType {
|
|
5672
|
+
_parse(input) {
|
|
5673
|
+
const { ctx } = this._processInputParams(input);
|
|
5674
|
+
let data = ctx.data;
|
|
5675
|
+
if (ctx.parsedType === ZodParsedType.undefined) {
|
|
5676
|
+
data = this._def.defaultValue();
|
|
1815
5677
|
}
|
|
1816
|
-
this.
|
|
1817
|
-
|
|
5678
|
+
return this._def.innerType._parse({
|
|
5679
|
+
data,
|
|
5680
|
+
path: ctx.path,
|
|
5681
|
+
parent: ctx,
|
|
5682
|
+
});
|
|
1818
5683
|
}
|
|
1819
|
-
|
|
1820
|
-
this.
|
|
1821
|
-
return this;
|
|
5684
|
+
removeDefault() {
|
|
5685
|
+
return this._def.innerType;
|
|
1822
5686
|
}
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
5687
|
+
}
|
|
5688
|
+
ZodDefault.create = (type, params) => {
|
|
5689
|
+
return new ZodDefault({
|
|
5690
|
+
innerType: type,
|
|
5691
|
+
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
5692
|
+
defaultValue: typeof params.default === "function"
|
|
5693
|
+
? params.default
|
|
5694
|
+
: () => params.default,
|
|
5695
|
+
...processCreateParams(params),
|
|
5696
|
+
});
|
|
5697
|
+
};
|
|
5698
|
+
class ZodCatch extends ZodType {
|
|
5699
|
+
_parse(input) {
|
|
5700
|
+
const { ctx } = this._processInputParams(input);
|
|
5701
|
+
// newCtx is used to not collect issues from inner types in ctx
|
|
5702
|
+
const newCtx = {
|
|
5703
|
+
...ctx,
|
|
5704
|
+
common: {
|
|
5705
|
+
...ctx.common,
|
|
5706
|
+
issues: [],
|
|
5707
|
+
},
|
|
5708
|
+
};
|
|
5709
|
+
const result = this._def.innerType._parse({
|
|
5710
|
+
data: newCtx.data,
|
|
5711
|
+
path: newCtx.path,
|
|
5712
|
+
parent: {
|
|
5713
|
+
...newCtx,
|
|
5714
|
+
},
|
|
5715
|
+
});
|
|
5716
|
+
if (isAsync(result)) {
|
|
5717
|
+
return result.then((result) => {
|
|
5718
|
+
return {
|
|
5719
|
+
status: "valid",
|
|
5720
|
+
value: result.status === "valid"
|
|
5721
|
+
? result.value
|
|
5722
|
+
: this._def.catchValue({
|
|
5723
|
+
get error() {
|
|
5724
|
+
return new ZodError(newCtx.common.issues);
|
|
5725
|
+
},
|
|
5726
|
+
input: newCtx.data,
|
|
5727
|
+
}),
|
|
5728
|
+
};
|
|
5729
|
+
});
|
|
1826
5730
|
}
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
5731
|
+
else {
|
|
5732
|
+
return {
|
|
5733
|
+
status: "valid",
|
|
5734
|
+
value: result.status === "valid"
|
|
5735
|
+
? result.value
|
|
5736
|
+
: this._def.catchValue({
|
|
5737
|
+
get error() {
|
|
5738
|
+
return new ZodError(newCtx.common.issues);
|
|
5739
|
+
},
|
|
5740
|
+
input: newCtx.data,
|
|
5741
|
+
}),
|
|
5742
|
+
};
|
|
1831
5743
|
}
|
|
1832
|
-
console.log(outputMessage);
|
|
1833
|
-
return this;
|
|
1834
5744
|
}
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
return this;
|
|
1838
|
-
}
|
|
1839
|
-
const spinnerMessage = options.color ? chalk[options.color](message) : message;
|
|
1840
|
-
this.spinner = ora(spinnerMessage).start();
|
|
1841
|
-
return this;
|
|
5745
|
+
removeCatch() {
|
|
5746
|
+
return this._def.innerType;
|
|
1842
5747
|
}
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
5748
|
+
}
|
|
5749
|
+
ZodCatch.create = (type, params) => {
|
|
5750
|
+
return new ZodCatch({
|
|
5751
|
+
innerType: type,
|
|
5752
|
+
typeName: ZodFirstPartyTypeKind.ZodCatch,
|
|
5753
|
+
catchValue: typeof params.catch === "function" ? params.catch : () => params.catch,
|
|
5754
|
+
...processCreateParams(params),
|
|
5755
|
+
});
|
|
5756
|
+
};
|
|
5757
|
+
class ZodNaN extends ZodType {
|
|
5758
|
+
_parse(input) {
|
|
5759
|
+
const parsedType = this._getType(input);
|
|
5760
|
+
if (parsedType !== ZodParsedType.nan) {
|
|
5761
|
+
const ctx = this._getOrReturnCtx(input);
|
|
5762
|
+
addIssueToContext(ctx, {
|
|
5763
|
+
code: ZodIssueCode.invalid_type,
|
|
5764
|
+
expected: ZodParsedType.nan,
|
|
5765
|
+
received: ctx.parsedType,
|
|
5766
|
+
});
|
|
5767
|
+
return INVALID;
|
|
1846
5768
|
}
|
|
1847
|
-
|
|
1848
|
-
this.spinner?.[options.mode || 'succeed'](spinnerMessage);
|
|
1849
|
-
this.spinner = null;
|
|
1850
|
-
return this;
|
|
5769
|
+
return { status: "valid", value: input.data };
|
|
1851
5770
|
}
|
|
1852
5771
|
}
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
5772
|
+
ZodNaN.create = (params) => {
|
|
5773
|
+
return new ZodNaN({
|
|
5774
|
+
typeName: ZodFirstPartyTypeKind.ZodNaN,
|
|
5775
|
+
...processCreateParams(params),
|
|
5776
|
+
});
|
|
5777
|
+
};
|
|
5778
|
+
const BRAND = Symbol("zod_brand");
|
|
5779
|
+
class ZodBranded extends ZodType {
|
|
5780
|
+
_parse(input) {
|
|
5781
|
+
const { ctx } = this._processInputParams(input);
|
|
5782
|
+
const data = ctx.data;
|
|
5783
|
+
return this._def.type._parse({
|
|
5784
|
+
data,
|
|
5785
|
+
path: ctx.path,
|
|
5786
|
+
parent: ctx,
|
|
5787
|
+
});
|
|
5788
|
+
}
|
|
5789
|
+
unwrap() {
|
|
5790
|
+
return this._def.type;
|
|
5791
|
+
}
|
|
5792
|
+
}
|
|
5793
|
+
class ZodPipeline extends ZodType {
|
|
5794
|
+
_parse(input) {
|
|
5795
|
+
const { status, ctx } = this._processInputParams(input);
|
|
5796
|
+
if (ctx.common.async) {
|
|
5797
|
+
const handleAsync = async () => {
|
|
5798
|
+
const inResult = await this._def.in._parseAsync({
|
|
5799
|
+
data: ctx.data,
|
|
5800
|
+
path: ctx.path,
|
|
5801
|
+
parent: ctx,
|
|
5802
|
+
});
|
|
5803
|
+
if (inResult.status === "aborted")
|
|
5804
|
+
return INVALID;
|
|
5805
|
+
if (inResult.status === "dirty") {
|
|
5806
|
+
status.dirty();
|
|
5807
|
+
return DIRTY(inResult.value);
|
|
5808
|
+
}
|
|
5809
|
+
else {
|
|
5810
|
+
return this._def.out._parseAsync({
|
|
5811
|
+
data: inResult.value,
|
|
5812
|
+
path: ctx.path,
|
|
5813
|
+
parent: ctx,
|
|
5814
|
+
});
|
|
5815
|
+
}
|
|
5816
|
+
};
|
|
5817
|
+
return handleAsync();
|
|
1860
5818
|
}
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
5819
|
+
else {
|
|
5820
|
+
const inResult = this._def.in._parseSync({
|
|
5821
|
+
data: ctx.data,
|
|
5822
|
+
path: ctx.path,
|
|
5823
|
+
parent: ctx,
|
|
5824
|
+
});
|
|
5825
|
+
if (inResult.status === "aborted")
|
|
5826
|
+
return INVALID;
|
|
5827
|
+
if (inResult.status === "dirty") {
|
|
5828
|
+
status.dirty();
|
|
5829
|
+
return {
|
|
5830
|
+
status: "dirty",
|
|
5831
|
+
value: inResult.value,
|
|
5832
|
+
};
|
|
5833
|
+
}
|
|
5834
|
+
else {
|
|
5835
|
+
return this._def.out._parseSync({
|
|
5836
|
+
data: inResult.value,
|
|
5837
|
+
path: ctx.path,
|
|
5838
|
+
parent: ctx,
|
|
5839
|
+
});
|
|
5840
|
+
}
|
|
1866
5841
|
}
|
|
1867
|
-
}
|
|
5842
|
+
}
|
|
5843
|
+
static create(a, b) {
|
|
5844
|
+
return new ZodPipeline({
|
|
5845
|
+
in: a,
|
|
5846
|
+
out: b,
|
|
5847
|
+
typeName: ZodFirstPartyTypeKind.ZodPipeline,
|
|
5848
|
+
});
|
|
5849
|
+
}
|
|
5850
|
+
}
|
|
5851
|
+
class ZodReadonly extends ZodType {
|
|
5852
|
+
_parse(input) {
|
|
5853
|
+
const result = this._def.innerType._parse(input);
|
|
5854
|
+
const freeze = (data) => {
|
|
5855
|
+
if (isValid(data)) {
|
|
5856
|
+
data.value = Object.freeze(data.value);
|
|
5857
|
+
}
|
|
5858
|
+
return data;
|
|
5859
|
+
};
|
|
5860
|
+
return isAsync(result)
|
|
5861
|
+
? result.then((data) => freeze(data))
|
|
5862
|
+
: freeze(result);
|
|
5863
|
+
}
|
|
5864
|
+
unwrap() {
|
|
5865
|
+
return this._def.innerType;
|
|
5866
|
+
}
|
|
5867
|
+
}
|
|
5868
|
+
ZodReadonly.create = (type, params) => {
|
|
5869
|
+
return new ZodReadonly({
|
|
5870
|
+
innerType: type,
|
|
5871
|
+
typeName: ZodFirstPartyTypeKind.ZodReadonly,
|
|
5872
|
+
...processCreateParams(params),
|
|
5873
|
+
});
|
|
5874
|
+
};
|
|
5875
|
+
function custom(check, params = {},
|
|
5876
|
+
/**
|
|
5877
|
+
* @deprecated
|
|
5878
|
+
*
|
|
5879
|
+
* Pass `fatal` into the params object instead:
|
|
5880
|
+
*
|
|
5881
|
+
* ```ts
|
|
5882
|
+
* z.string().custom((val) => val.length > 5, { fatal: false })
|
|
5883
|
+
* ```
|
|
5884
|
+
*
|
|
5885
|
+
*/
|
|
5886
|
+
fatal) {
|
|
5887
|
+
if (check)
|
|
5888
|
+
return ZodAny.create().superRefine((data, ctx) => {
|
|
5889
|
+
var _a, _b;
|
|
5890
|
+
if (!check(data)) {
|
|
5891
|
+
const p = typeof params === "function"
|
|
5892
|
+
? params(data)
|
|
5893
|
+
: typeof params === "string"
|
|
5894
|
+
? { message: params }
|
|
5895
|
+
: params;
|
|
5896
|
+
const _fatal = (_b = (_a = p.fatal) !== null && _a !== void 0 ? _a : fatal) !== null && _b !== void 0 ? _b : true;
|
|
5897
|
+
const p2 = typeof p === "string" ? { message: p } : p;
|
|
5898
|
+
ctx.addIssue({ code: "custom", ...p2, fatal: _fatal });
|
|
5899
|
+
}
|
|
5900
|
+
});
|
|
5901
|
+
return ZodAny.create();
|
|
1868
5902
|
}
|
|
5903
|
+
const late = {
|
|
5904
|
+
object: ZodObject.lazycreate,
|
|
5905
|
+
};
|
|
5906
|
+
var ZodFirstPartyTypeKind;
|
|
5907
|
+
(function (ZodFirstPartyTypeKind) {
|
|
5908
|
+
ZodFirstPartyTypeKind["ZodString"] = "ZodString";
|
|
5909
|
+
ZodFirstPartyTypeKind["ZodNumber"] = "ZodNumber";
|
|
5910
|
+
ZodFirstPartyTypeKind["ZodNaN"] = "ZodNaN";
|
|
5911
|
+
ZodFirstPartyTypeKind["ZodBigInt"] = "ZodBigInt";
|
|
5912
|
+
ZodFirstPartyTypeKind["ZodBoolean"] = "ZodBoolean";
|
|
5913
|
+
ZodFirstPartyTypeKind["ZodDate"] = "ZodDate";
|
|
5914
|
+
ZodFirstPartyTypeKind["ZodSymbol"] = "ZodSymbol";
|
|
5915
|
+
ZodFirstPartyTypeKind["ZodUndefined"] = "ZodUndefined";
|
|
5916
|
+
ZodFirstPartyTypeKind["ZodNull"] = "ZodNull";
|
|
5917
|
+
ZodFirstPartyTypeKind["ZodAny"] = "ZodAny";
|
|
5918
|
+
ZodFirstPartyTypeKind["ZodUnknown"] = "ZodUnknown";
|
|
5919
|
+
ZodFirstPartyTypeKind["ZodNever"] = "ZodNever";
|
|
5920
|
+
ZodFirstPartyTypeKind["ZodVoid"] = "ZodVoid";
|
|
5921
|
+
ZodFirstPartyTypeKind["ZodArray"] = "ZodArray";
|
|
5922
|
+
ZodFirstPartyTypeKind["ZodObject"] = "ZodObject";
|
|
5923
|
+
ZodFirstPartyTypeKind["ZodUnion"] = "ZodUnion";
|
|
5924
|
+
ZodFirstPartyTypeKind["ZodDiscriminatedUnion"] = "ZodDiscriminatedUnion";
|
|
5925
|
+
ZodFirstPartyTypeKind["ZodIntersection"] = "ZodIntersection";
|
|
5926
|
+
ZodFirstPartyTypeKind["ZodTuple"] = "ZodTuple";
|
|
5927
|
+
ZodFirstPartyTypeKind["ZodRecord"] = "ZodRecord";
|
|
5928
|
+
ZodFirstPartyTypeKind["ZodMap"] = "ZodMap";
|
|
5929
|
+
ZodFirstPartyTypeKind["ZodSet"] = "ZodSet";
|
|
5930
|
+
ZodFirstPartyTypeKind["ZodFunction"] = "ZodFunction";
|
|
5931
|
+
ZodFirstPartyTypeKind["ZodLazy"] = "ZodLazy";
|
|
5932
|
+
ZodFirstPartyTypeKind["ZodLiteral"] = "ZodLiteral";
|
|
5933
|
+
ZodFirstPartyTypeKind["ZodEnum"] = "ZodEnum";
|
|
5934
|
+
ZodFirstPartyTypeKind["ZodEffects"] = "ZodEffects";
|
|
5935
|
+
ZodFirstPartyTypeKind["ZodNativeEnum"] = "ZodNativeEnum";
|
|
5936
|
+
ZodFirstPartyTypeKind["ZodOptional"] = "ZodOptional";
|
|
5937
|
+
ZodFirstPartyTypeKind["ZodNullable"] = "ZodNullable";
|
|
5938
|
+
ZodFirstPartyTypeKind["ZodDefault"] = "ZodDefault";
|
|
5939
|
+
ZodFirstPartyTypeKind["ZodCatch"] = "ZodCatch";
|
|
5940
|
+
ZodFirstPartyTypeKind["ZodPromise"] = "ZodPromise";
|
|
5941
|
+
ZodFirstPartyTypeKind["ZodBranded"] = "ZodBranded";
|
|
5942
|
+
ZodFirstPartyTypeKind["ZodPipeline"] = "ZodPipeline";
|
|
5943
|
+
ZodFirstPartyTypeKind["ZodReadonly"] = "ZodReadonly";
|
|
5944
|
+
})(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
|
|
5945
|
+
const instanceOfType = (
|
|
5946
|
+
// const instanceOfType = <T extends new (...args: any[]) => any>(
|
|
5947
|
+
cls, params = {
|
|
5948
|
+
message: `Input not instance of ${cls.name}`,
|
|
5949
|
+
}) => custom((data) => data instanceof cls, params);
|
|
5950
|
+
const stringType = ZodString.create;
|
|
5951
|
+
const numberType = ZodNumber.create;
|
|
5952
|
+
const nanType = ZodNaN.create;
|
|
5953
|
+
const bigIntType = ZodBigInt.create;
|
|
5954
|
+
const booleanType = ZodBoolean.create;
|
|
5955
|
+
const dateType = ZodDate.create;
|
|
5956
|
+
const symbolType = ZodSymbol.create;
|
|
5957
|
+
const undefinedType = ZodUndefined.create;
|
|
5958
|
+
const nullType = ZodNull.create;
|
|
5959
|
+
const anyType = ZodAny.create;
|
|
5960
|
+
const unknownType = ZodUnknown.create;
|
|
5961
|
+
const neverType = ZodNever.create;
|
|
5962
|
+
const voidType = ZodVoid.create;
|
|
5963
|
+
const arrayType = ZodArray.create;
|
|
5964
|
+
const objectType = ZodObject.create;
|
|
5965
|
+
const strictObjectType = ZodObject.strictCreate;
|
|
5966
|
+
const unionType = ZodUnion.create;
|
|
5967
|
+
const discriminatedUnionType = ZodDiscriminatedUnion.create;
|
|
5968
|
+
const intersectionType = ZodIntersection.create;
|
|
5969
|
+
const tupleType = ZodTuple.create;
|
|
5970
|
+
const recordType = ZodRecord.create;
|
|
5971
|
+
const mapType = ZodMap.create;
|
|
5972
|
+
const setType = ZodSet.create;
|
|
5973
|
+
const functionType = ZodFunction.create;
|
|
5974
|
+
const lazyType = ZodLazy.create;
|
|
5975
|
+
const literalType = ZodLiteral.create;
|
|
5976
|
+
const enumType = ZodEnum.create;
|
|
5977
|
+
const nativeEnumType = ZodNativeEnum.create;
|
|
5978
|
+
const promiseType = ZodPromise.create;
|
|
5979
|
+
const effectsType = ZodEffects.create;
|
|
5980
|
+
const optionalType = ZodOptional.create;
|
|
5981
|
+
const nullableType = ZodNullable.create;
|
|
5982
|
+
const preprocessType = ZodEffects.createWithPreprocess;
|
|
5983
|
+
const pipelineType = ZodPipeline.create;
|
|
5984
|
+
const ostring = () => stringType().optional();
|
|
5985
|
+
const onumber = () => numberType().optional();
|
|
5986
|
+
const oboolean = () => booleanType().optional();
|
|
5987
|
+
const coerce = {
|
|
5988
|
+
string: ((arg) => ZodString.create({ ...arg, coerce: true })),
|
|
5989
|
+
number: ((arg) => ZodNumber.create({ ...arg, coerce: true })),
|
|
5990
|
+
boolean: ((arg) => ZodBoolean.create({
|
|
5991
|
+
...arg,
|
|
5992
|
+
coerce: true,
|
|
5993
|
+
})),
|
|
5994
|
+
bigint: ((arg) => ZodBigInt.create({ ...arg, coerce: true })),
|
|
5995
|
+
date: ((arg) => ZodDate.create({ ...arg, coerce: true })),
|
|
5996
|
+
};
|
|
5997
|
+
const NEVER = INVALID;
|
|
5998
|
+
|
|
5999
|
+
var z = /*#__PURE__*/Object.freeze({
|
|
6000
|
+
__proto__: null,
|
|
6001
|
+
defaultErrorMap: errorMap,
|
|
6002
|
+
setErrorMap: setErrorMap,
|
|
6003
|
+
getErrorMap: getErrorMap,
|
|
6004
|
+
makeIssue: makeIssue,
|
|
6005
|
+
EMPTY_PATH: EMPTY_PATH,
|
|
6006
|
+
addIssueToContext: addIssueToContext,
|
|
6007
|
+
ParseStatus: ParseStatus,
|
|
6008
|
+
INVALID: INVALID,
|
|
6009
|
+
DIRTY: DIRTY,
|
|
6010
|
+
OK: OK,
|
|
6011
|
+
isAborted: isAborted,
|
|
6012
|
+
isDirty: isDirty,
|
|
6013
|
+
isValid: isValid,
|
|
6014
|
+
isAsync: isAsync,
|
|
6015
|
+
get util () { return util; },
|
|
6016
|
+
get objectUtil () { return objectUtil; },
|
|
6017
|
+
ZodParsedType: ZodParsedType,
|
|
6018
|
+
getParsedType: getParsedType,
|
|
6019
|
+
ZodType: ZodType,
|
|
6020
|
+
datetimeRegex: datetimeRegex,
|
|
6021
|
+
ZodString: ZodString,
|
|
6022
|
+
ZodNumber: ZodNumber,
|
|
6023
|
+
ZodBigInt: ZodBigInt,
|
|
6024
|
+
ZodBoolean: ZodBoolean,
|
|
6025
|
+
ZodDate: ZodDate,
|
|
6026
|
+
ZodSymbol: ZodSymbol,
|
|
6027
|
+
ZodUndefined: ZodUndefined,
|
|
6028
|
+
ZodNull: ZodNull,
|
|
6029
|
+
ZodAny: ZodAny,
|
|
6030
|
+
ZodUnknown: ZodUnknown,
|
|
6031
|
+
ZodNever: ZodNever,
|
|
6032
|
+
ZodVoid: ZodVoid,
|
|
6033
|
+
ZodArray: ZodArray,
|
|
6034
|
+
ZodObject: ZodObject,
|
|
6035
|
+
ZodUnion: ZodUnion,
|
|
6036
|
+
ZodDiscriminatedUnion: ZodDiscriminatedUnion,
|
|
6037
|
+
ZodIntersection: ZodIntersection,
|
|
6038
|
+
ZodTuple: ZodTuple,
|
|
6039
|
+
ZodRecord: ZodRecord,
|
|
6040
|
+
ZodMap: ZodMap,
|
|
6041
|
+
ZodSet: ZodSet,
|
|
6042
|
+
ZodFunction: ZodFunction,
|
|
6043
|
+
ZodLazy: ZodLazy,
|
|
6044
|
+
ZodLiteral: ZodLiteral,
|
|
6045
|
+
ZodEnum: ZodEnum,
|
|
6046
|
+
ZodNativeEnum: ZodNativeEnum,
|
|
6047
|
+
ZodPromise: ZodPromise,
|
|
6048
|
+
ZodEffects: ZodEffects,
|
|
6049
|
+
ZodTransformer: ZodEffects,
|
|
6050
|
+
ZodOptional: ZodOptional,
|
|
6051
|
+
ZodNullable: ZodNullable,
|
|
6052
|
+
ZodDefault: ZodDefault,
|
|
6053
|
+
ZodCatch: ZodCatch,
|
|
6054
|
+
ZodNaN: ZodNaN,
|
|
6055
|
+
BRAND: BRAND,
|
|
6056
|
+
ZodBranded: ZodBranded,
|
|
6057
|
+
ZodPipeline: ZodPipeline,
|
|
6058
|
+
ZodReadonly: ZodReadonly,
|
|
6059
|
+
custom: custom,
|
|
6060
|
+
Schema: ZodType,
|
|
6061
|
+
ZodSchema: ZodType,
|
|
6062
|
+
late: late,
|
|
6063
|
+
get ZodFirstPartyTypeKind () { return ZodFirstPartyTypeKind; },
|
|
6064
|
+
coerce: coerce,
|
|
6065
|
+
any: anyType,
|
|
6066
|
+
array: arrayType,
|
|
6067
|
+
bigint: bigIntType,
|
|
6068
|
+
boolean: booleanType,
|
|
6069
|
+
date: dateType,
|
|
6070
|
+
discriminatedUnion: discriminatedUnionType,
|
|
6071
|
+
effect: effectsType,
|
|
6072
|
+
'enum': enumType,
|
|
6073
|
+
'function': functionType,
|
|
6074
|
+
'instanceof': instanceOfType,
|
|
6075
|
+
intersection: intersectionType,
|
|
6076
|
+
lazy: lazyType,
|
|
6077
|
+
literal: literalType,
|
|
6078
|
+
map: mapType,
|
|
6079
|
+
nan: nanType,
|
|
6080
|
+
nativeEnum: nativeEnumType,
|
|
6081
|
+
never: neverType,
|
|
6082
|
+
'null': nullType,
|
|
6083
|
+
nullable: nullableType,
|
|
6084
|
+
number: numberType,
|
|
6085
|
+
object: objectType,
|
|
6086
|
+
oboolean: oboolean,
|
|
6087
|
+
onumber: onumber,
|
|
6088
|
+
optional: optionalType,
|
|
6089
|
+
ostring: ostring,
|
|
6090
|
+
pipeline: pipelineType,
|
|
6091
|
+
preprocess: preprocessType,
|
|
6092
|
+
promise: promiseType,
|
|
6093
|
+
record: recordType,
|
|
6094
|
+
set: setType,
|
|
6095
|
+
strictObject: strictObjectType,
|
|
6096
|
+
string: stringType,
|
|
6097
|
+
symbol: symbolType,
|
|
6098
|
+
transformer: effectsType,
|
|
6099
|
+
tuple: tupleType,
|
|
6100
|
+
'undefined': undefinedType,
|
|
6101
|
+
union: unionType,
|
|
6102
|
+
unknown: unknownType,
|
|
6103
|
+
'void': voidType,
|
|
6104
|
+
NEVER: NEVER,
|
|
6105
|
+
ZodIssueCode: ZodIssueCode,
|
|
6106
|
+
quotelessJson: quotelessJson,
|
|
6107
|
+
ZodError: ZodError
|
|
6108
|
+
});
|
|
1869
6109
|
|
|
6110
|
+
const ChangelogResponseSchema = z.object({
|
|
6111
|
+
title: z.string(),
|
|
6112
|
+
content: z.string(),
|
|
6113
|
+
});
|
|
1870
6114
|
const command$4 = 'changelog';
|
|
1871
6115
|
/**
|
|
1872
6116
|
* Command line options via yargs
|
|
@@ -1875,13 +6119,19 @@ const options$4 = {
|
|
|
1875
6119
|
range: {
|
|
1876
6120
|
type: 'string',
|
|
1877
6121
|
alias: 'r',
|
|
1878
|
-
description: 'Commit range e.g `HEAD~2:HEAD`',
|
|
6122
|
+
description: 'Commit range e.g `HEAD~2:HEAD^1` or `abc1234:def5678` (commit hashes)',
|
|
1879
6123
|
},
|
|
1880
6124
|
branch: {
|
|
1881
6125
|
type: 'string',
|
|
1882
6126
|
alias: 'b',
|
|
1883
6127
|
description: 'Target branch to compare against',
|
|
1884
6128
|
},
|
|
6129
|
+
sinceLastTag: {
|
|
6130
|
+
type: 'boolean',
|
|
6131
|
+
alias: 't',
|
|
6132
|
+
description: 'Generate changelog for all commits since the last tag',
|
|
6133
|
+
default: false,
|
|
6134
|
+
},
|
|
1885
6135
|
i: {
|
|
1886
6136
|
type: 'boolean',
|
|
1887
6137
|
alias: 'interactive',
|
|
@@ -1934,6 +6184,7 @@ function getPrompt({ template, variables, fallback }) {
|
|
|
1934
6184
|
? new prompts$1.PromptTemplate({
|
|
1935
6185
|
template,
|
|
1936
6186
|
inputVariables: variables,
|
|
6187
|
+
templateFormat: 'mustache',
|
|
1937
6188
|
})
|
|
1938
6189
|
: fallback);
|
|
1939
6190
|
}
|
|
@@ -1964,18 +6215,52 @@ function extractTicketIdFromBranchName(branchName) {
|
|
|
1964
6215
|
return match ? match[0] : null;
|
|
1965
6216
|
}
|
|
1966
6217
|
|
|
6218
|
+
/**
|
|
6219
|
+
* Formats a commit log into a readable string format.
|
|
6220
|
+
*
|
|
6221
|
+
* @param commitLog - The commit log result containing an array of commit details.
|
|
6222
|
+
* @returns An array of formatted commit log strings.
|
|
6223
|
+
*
|
|
6224
|
+
* Each formatted string includes:
|
|
6225
|
+
* - The date of the commit in square brackets.
|
|
6226
|
+
* - The commit message.
|
|
6227
|
+
* - The commit body.
|
|
6228
|
+
* - The commit hash in parentheses.
|
|
6229
|
+
* - The author's name and email in angle brackets.
|
|
6230
|
+
*/
|
|
6231
|
+
const formatCommitLog = (commitLog) => {
|
|
6232
|
+
return commitLog.all.map(({ message, date, body, author_name, hash, author_email }) => `[${date}] ${message}\n${body}\n(${hash}) - ${author_name}<${author_email}>`);
|
|
6233
|
+
};
|
|
6234
|
+
|
|
6235
|
+
const getChangesSinceLastTag = async ({ git }) => {
|
|
6236
|
+
const tags = await git.tags();
|
|
6237
|
+
if (tags.all.length > 0) {
|
|
6238
|
+
const lastTag = tags.latest;
|
|
6239
|
+
const commitLog = await git.log({ from: lastTag });
|
|
6240
|
+
return formatCommitLog(commitLog);
|
|
6241
|
+
}
|
|
6242
|
+
else {
|
|
6243
|
+
return ['No tags found in the repository.'];
|
|
6244
|
+
}
|
|
6245
|
+
};
|
|
6246
|
+
|
|
1967
6247
|
/**
|
|
1968
6248
|
* Retrieves the commit log range between two specified commits.
|
|
1969
6249
|
*
|
|
1970
|
-
* @param from - The starting commit.
|
|
1971
|
-
* @param to - The ending commit.
|
|
6250
|
+
* @param from - The starting commit (can be a commit hash, HEAD reference, or branch name).
|
|
6251
|
+
* @param to - The ending commit (can be a commit hash, HEAD reference, or branch name).
|
|
1972
6252
|
* @param options - Additional options for retrieving the commit log range.
|
|
1973
6253
|
* @returns A promise that resolves to an array of commit log messages.
|
|
1974
6254
|
* @throws If there is an error retrieving the commit log range.
|
|
1975
6255
|
*/
|
|
1976
6256
|
async function getCommitLogRange(from, to, { noMerges, git }) {
|
|
1977
6257
|
try {
|
|
1978
|
-
|
|
6258
|
+
// Use the git range syntax directly (from..to) which works with both commit hashes and references
|
|
6259
|
+
const logOptions = {
|
|
6260
|
+
from,
|
|
6261
|
+
to,
|
|
6262
|
+
'--no-merges': noMerges
|
|
6263
|
+
};
|
|
1979
6264
|
const commitLog = await git.log(logOptions);
|
|
1980
6265
|
return commitLog.all.map(({ message, date, body, author_name, hash, author_email }) => `[${date}] ${message}\n${body}\n(${hash}) - ${author_name}<${author_email}>`);
|
|
1981
6266
|
}
|
|
@@ -2097,8 +6382,17 @@ const getRepo = () => {
|
|
|
2097
6382
|
return git;
|
|
2098
6383
|
};
|
|
2099
6384
|
|
|
2100
|
-
|
|
2101
|
-
|
|
6385
|
+
/**
|
|
6386
|
+
* Template for generating git commit messages based on code changes
|
|
6387
|
+
*
|
|
6388
|
+
* Variables:
|
|
6389
|
+
* - summary: Contains the diff summary of staged changes
|
|
6390
|
+
* - format_instructions: Instructions for the output format (JSON with title and body)
|
|
6391
|
+
* - additional_context: Optional user-provided context to guide the commit message generation
|
|
6392
|
+
* - commit_history: Optional history of previous commits for context
|
|
6393
|
+
*/
|
|
6394
|
+
const template$4 = `Write informative git commit message, in the imperative, based on the diffs & file changes provided in the "Diff Summary" section.
|
|
6395
|
+
Commit Messages must have a short description that is less than 50 characters and a longer detailed summary around 300 characters, the shorter and more concise the better.
|
|
2102
6396
|
|
|
2103
6397
|
Please follow the guidelines below when writing your commit message:
|
|
2104
6398
|
|
|
@@ -2108,15 +6402,18 @@ Please follow the guidelines below when writing your commit message:
|
|
|
2108
6402
|
- DO NOT include any diffs or file changes in the commit message
|
|
2109
6403
|
- Wrap variable, class, function, components, and dependency names in back ticks e.g. \`variable\`
|
|
2110
6404
|
|
|
2111
|
-
{format_instructions}
|
|
6405
|
+
{{format_instructions}}
|
|
6406
|
+
|
|
6407
|
+
{{commit_history}}
|
|
2112
6408
|
|
|
2113
6409
|
""""""
|
|
2114
|
-
{summary}
|
|
6410
|
+
{{summary}}
|
|
2115
6411
|
""""""
|
|
2116
6412
|
|
|
2117
|
-
{
|
|
6413
|
+
{{additional_context}}
|
|
2118
6414
|
`;
|
|
2119
|
-
|
|
6415
|
+
// Define the variables that will be passed to the prompt template
|
|
6416
|
+
const inputVariables$3 = ['summary', 'format_instructions', 'additional_context', 'commit_history'];
|
|
2120
6417
|
const COMMIT_PROMPT = new prompts$1.PromptTemplate({
|
|
2121
6418
|
template: template$4,
|
|
2122
6419
|
inputVariables: inputVariables$3,
|
|
@@ -2282,9 +6579,16 @@ async function generateAndReviewLoop({ label, factory, parser, noResult, agent,
|
|
|
2282
6579
|
result = '';
|
|
2283
6580
|
continue;
|
|
2284
6581
|
}
|
|
6582
|
+
// Only edit the result in interactive mode if approved
|
|
6583
|
+
result = await editResult(result, options);
|
|
6584
|
+
}
|
|
6585
|
+
else {
|
|
6586
|
+
// In non-interactive mode, we return the result as is to be output to stdout by the caller.
|
|
6587
|
+
const displayResult = reviewParser ? reviewParser(result, options) : result;
|
|
6588
|
+
// In non-interactive mode, ensure we return the properly formatted result
|
|
6589
|
+
result = displayResult;
|
|
2285
6590
|
}
|
|
2286
6591
|
// if we're here, we're done.
|
|
2287
|
-
result = await editResult(result, options);
|
|
2288
6592
|
continueLoop = false;
|
|
2289
6593
|
}
|
|
2290
6594
|
return result;
|
|
@@ -2307,7 +6611,8 @@ async function handleResult({ result, mode, interactiveModeCallback }) {
|
|
|
2307
6611
|
break;
|
|
2308
6612
|
case 'stdout':
|
|
2309
6613
|
default:
|
|
2310
|
-
|
|
6614
|
+
// Ensure we write the result to stdout in non-interactive mode
|
|
6615
|
+
process.stdout.write(result + '\n', 'utf8');
|
|
2311
6616
|
break;
|
|
2312
6617
|
}
|
|
2313
6618
|
process.exit(0);
|
|
@@ -2321,9 +6626,9 @@ const template$3 = `Write informative git changelog, in the imperative, based on
|
|
|
2321
6626
|
- Depending on the size of the changes, consider breaking the changelog into sections
|
|
2322
6627
|
- Avoid generlizations like "various bug fixes" or "improvements" or "enhancements"
|
|
2323
6628
|
|
|
2324
|
-
{format_instructions}
|
|
6629
|
+
{{format_instructions}}
|
|
2325
6630
|
|
|
2326
|
-
"""{summary}"""`;
|
|
6631
|
+
"""{{summary}}"""`;
|
|
2327
6632
|
const inputVariables$2 = ['format_instructions', 'summary'];
|
|
2328
6633
|
const CHANGELOG_PROMPT = new prompts$1.PromptTemplate({
|
|
2329
6634
|
template: template$3,
|
|
@@ -2346,6 +6651,13 @@ const handler$4 = async (argv, logger) => {
|
|
|
2346
6651
|
}
|
|
2347
6652
|
async function factory() {
|
|
2348
6653
|
const branchName = await getCurrentBranchName({ git });
|
|
6654
|
+
if (config.sinceLastTag) {
|
|
6655
|
+
logger.verbose(`Generating commit log since the last tag`, { color: 'yellow' });
|
|
6656
|
+
return {
|
|
6657
|
+
branch: branchName,
|
|
6658
|
+
commits: await getChangesSinceLastTag({ git, logger }),
|
|
6659
|
+
};
|
|
6660
|
+
}
|
|
2349
6661
|
if (config.range && config.range.includes(':')) {
|
|
2350
6662
|
const [from, to] = config.range.split(':');
|
|
2351
6663
|
if (!from || !to) {
|
|
@@ -2364,7 +6676,9 @@ const handler$4 = async (argv, logger) => {
|
|
|
2364
6676
|
commits: await getCommitLogAgainstBranch({ git, logger, targetBranch: argv.branch }),
|
|
2365
6677
|
};
|
|
2366
6678
|
}
|
|
2367
|
-
logger.verbose(`No range or
|
|
6679
|
+
logger.verbose(`No range, branch, or tag option provided. Defaulting to current branch`, {
|
|
6680
|
+
color: 'yellow',
|
|
6681
|
+
});
|
|
2368
6682
|
return {
|
|
2369
6683
|
branch: branchName,
|
|
2370
6684
|
commits: await getCommitLogCurrentBranch({ git, logger }),
|
|
@@ -2394,7 +6708,7 @@ const handler$4 = async (argv, logger) => {
|
|
|
2394
6708
|
factory,
|
|
2395
6709
|
parser,
|
|
2396
6710
|
agent: async (context, options) => {
|
|
2397
|
-
const parser = new output_parsers.
|
|
6711
|
+
const parser = new output_parsers.StructuredOutputParser(ChangelogResponseSchema);
|
|
2398
6712
|
const prompt = getPrompt({
|
|
2399
6713
|
template: options.prompt,
|
|
2400
6714
|
variables: CHANGELOG_PROMPT.inputVariables,
|
|
@@ -2436,12 +6750,16 @@ const handler$4 = async (argv, logger) => {
|
|
|
2436
6750
|
|
|
2437
6751
|
var changelog = {
|
|
2438
6752
|
command: command$4,
|
|
2439
|
-
desc: 'Generate a changelog from current or target branch
|
|
6753
|
+
desc: 'Generate a changelog from current or target branch, provided commit range, or since the last tag.',
|
|
2440
6754
|
builder: builder$4,
|
|
2441
6755
|
handler: commandExecutor(handler$4),
|
|
2442
6756
|
options: options$4,
|
|
2443
6757
|
};
|
|
2444
6758
|
|
|
6759
|
+
const CommitMessageResponseSchema = z.object({
|
|
6760
|
+
title: z.string(),
|
|
6761
|
+
body: z.string(),
|
|
6762
|
+
});
|
|
2445
6763
|
const command$3 = 'commit';
|
|
2446
6764
|
/**
|
|
2447
6765
|
* Command line options via yargs
|
|
@@ -2474,6 +6792,12 @@ const options$3 = {
|
|
|
2474
6792
|
type: 'string',
|
|
2475
6793
|
alias: 'a',
|
|
2476
6794
|
},
|
|
6795
|
+
withPreviousCommits: {
|
|
6796
|
+
description: 'Include previous commits as context (specify number of commits, 0 for none)',
|
|
6797
|
+
type: 'number',
|
|
6798
|
+
default: 0,
|
|
6799
|
+
alias: 'p',
|
|
6800
|
+
},
|
|
2477
6801
|
};
|
|
2478
6802
|
const builder$3 = (yargs) => {
|
|
2479
6803
|
return yargs.options(options$3).usage(getCommandUsageHeader(command$3));
|
|
@@ -6211,6 +10535,48 @@ async function getChanges({ git, options }) {
|
|
|
6211
10535
|
};
|
|
6212
10536
|
}
|
|
6213
10537
|
|
|
10538
|
+
/**
|
|
10539
|
+
* Format a single commit log entry into a readable string
|
|
10540
|
+
* @param commit - The commit log entry
|
|
10541
|
+
* @returns Formatted commit log string
|
|
10542
|
+
*/
|
|
10543
|
+
function formatSingleCommit(commit) {
|
|
10544
|
+
const { hash, date, message, body, author_name } = commit;
|
|
10545
|
+
const shortHash = hash.substring(0, 7);
|
|
10546
|
+
return `Commit: ${shortHash}
|
|
10547
|
+
Author: ${author_name}
|
|
10548
|
+
Date: ${date}
|
|
10549
|
+
Message: ${message}
|
|
10550
|
+
${body ? `\nDetails: ${body}` : ''}`;
|
|
10551
|
+
}
|
|
10552
|
+
|
|
10553
|
+
/**
|
|
10554
|
+
* Get the specified number of previous commits
|
|
10555
|
+
* @param options - Options for getting previous commits
|
|
10556
|
+
* @returns Formatted commit logs
|
|
10557
|
+
*/
|
|
10558
|
+
async function getPreviousCommits(options) {
|
|
10559
|
+
const { git, count = 1 } = options;
|
|
10560
|
+
if (count <= 0) {
|
|
10561
|
+
return '';
|
|
10562
|
+
}
|
|
10563
|
+
try {
|
|
10564
|
+
const logs = await git.log({ maxCount: count });
|
|
10565
|
+
if (!logs || logs.total === 0) {
|
|
10566
|
+
return '';
|
|
10567
|
+
}
|
|
10568
|
+
// Format the commit logs
|
|
10569
|
+
const formattedLogs = logs.all.map((commit) => {
|
|
10570
|
+
return formatSingleCommit(commit);
|
|
10571
|
+
}).join('\n\n');
|
|
10572
|
+
return formattedLogs;
|
|
10573
|
+
}
|
|
10574
|
+
catch (error) {
|
|
10575
|
+
console.error(`Error getting previous commits: ${error.message}`);
|
|
10576
|
+
return '';
|
|
10577
|
+
}
|
|
10578
|
+
}
|
|
10579
|
+
|
|
6214
10580
|
/**
|
|
6215
10581
|
* Retrieves a TikToken for the specified model.
|
|
6216
10582
|
*
|
|
@@ -6317,21 +10683,37 @@ const handler$3 = async (argv, logger) => {
|
|
|
6317
10683
|
factory,
|
|
6318
10684
|
parser,
|
|
6319
10685
|
agent: async (context, options) => {
|
|
6320
|
-
const parser = new output_parsers.
|
|
10686
|
+
const parser = new output_parsers.StructuredOutputParser(CommitMessageResponseSchema);
|
|
6321
10687
|
const prompt = getPrompt({
|
|
6322
10688
|
template: options.prompt,
|
|
6323
10689
|
variables: COMMIT_PROMPT.inputVariables,
|
|
6324
10690
|
fallback: COMMIT_PROMPT,
|
|
6325
10691
|
});
|
|
6326
10692
|
const formatInstructions = "Respond with a valid JSON object, containing two fields: 'title' and 'body', both strings.";
|
|
6327
|
-
|
|
10693
|
+
// Get additional context if provided
|
|
10694
|
+
let additional_context = '';
|
|
10695
|
+
if (argv.additional) {
|
|
10696
|
+
additional_context = `## Additional Context\n${argv.additional}`;
|
|
10697
|
+
}
|
|
10698
|
+
// Get commit history if requested
|
|
10699
|
+
let commit_history = '';
|
|
10700
|
+
if (argv.withPreviousCommits > 0) {
|
|
10701
|
+
const commitHistoryData = await getPreviousCommits({
|
|
10702
|
+
git,
|
|
10703
|
+
count: argv.withPreviousCommits
|
|
10704
|
+
});
|
|
10705
|
+
if (commitHistoryData) {
|
|
10706
|
+
commit_history = `## Commit History\n${commitHistoryData}`;
|
|
10707
|
+
}
|
|
10708
|
+
}
|
|
6328
10709
|
const commitMsg = await executeChain({
|
|
6329
10710
|
llm,
|
|
6330
10711
|
prompt,
|
|
6331
10712
|
variables: {
|
|
6332
10713
|
summary: context,
|
|
6333
10714
|
format_instructions: formatInstructions,
|
|
6334
|
-
|
|
10715
|
+
additional_context: additional_context,
|
|
10716
|
+
commit_history: commit_history,
|
|
6335
10717
|
},
|
|
6336
10718
|
parser,
|
|
6337
10719
|
});
|
|
@@ -6821,6 +11203,10 @@ var init = {
|
|
|
6821
11203
|
options: options$2,
|
|
6822
11204
|
};
|
|
6823
11205
|
|
|
11206
|
+
const RecapLlmResponseSchema = z.object({
|
|
11207
|
+
title: z.string().optional(),
|
|
11208
|
+
summary: z.string().optional(),
|
|
11209
|
+
});
|
|
6824
11210
|
const command$1 = 'recap';
|
|
6825
11211
|
/**
|
|
6826
11212
|
* Command line options via yargs
|
|
@@ -6855,40 +11241,11 @@ const builder$1 = (yargs) => {
|
|
|
6855
11241
|
return yargs.options(options$1).usage(getCommandUsageHeader(command$1));
|
|
6856
11242
|
};
|
|
6857
11243
|
|
|
6858
|
-
/**
|
|
6859
|
-
* Formats a commit log into a readable string format.
|
|
6860
|
-
*
|
|
6861
|
-
* @param commitLog - The commit log result containing an array of commit details.
|
|
6862
|
-
* @returns An array of formatted commit log strings.
|
|
6863
|
-
*
|
|
6864
|
-
* Each formatted string includes:
|
|
6865
|
-
* - The date of the commit in square brackets.
|
|
6866
|
-
* - The commit message.
|
|
6867
|
-
* - The commit body.
|
|
6868
|
-
* - The commit hash in parentheses.
|
|
6869
|
-
* - The author's name and email in angle brackets.
|
|
6870
|
-
*/
|
|
6871
|
-
const formatCommitLog = (commitLog) => {
|
|
6872
|
-
return commitLog.all.map(({ message, date, body, author_name, hash, author_email }) => `[${date}] ${message}\n${body}\n(${hash}) - ${author_name}<${author_email}>`);
|
|
6873
|
-
};
|
|
6874
|
-
|
|
6875
11244
|
const getChangesByTimestamp = async ({ since, git }) => {
|
|
6876
11245
|
const commitLog = await git.log({ '--since': since });
|
|
6877
11246
|
return formatCommitLog(commitLog);
|
|
6878
11247
|
};
|
|
6879
11248
|
|
|
6880
|
-
const getChangesSinceLastTag = async ({ git }) => {
|
|
6881
|
-
const tags = await git.tags();
|
|
6882
|
-
if (tags.all.length > 0) {
|
|
6883
|
-
const lastTag = tags.latest;
|
|
6884
|
-
const commitLog = await git.log({ from: lastTag });
|
|
6885
|
-
return formatCommitLog(commitLog);
|
|
6886
|
-
}
|
|
6887
|
-
else {
|
|
6888
|
-
return ['No tags found in the repository.'];
|
|
6889
|
-
}
|
|
6890
|
-
};
|
|
6891
|
-
|
|
6892
11249
|
async function noResult$1({ logger }) {
|
|
6893
11250
|
logger.log('No repo changes detected. 👀', { color: 'blue' });
|
|
6894
11251
|
}
|
|
@@ -6898,12 +11255,12 @@ The summarization should descibe in a general sense what has changed in the repo
|
|
|
6898
11255
|
|
|
6899
11256
|
Breaking down the changes into categories (e.g. bug fixes, new features, etc.) with markdown headings is encouraged.
|
|
6900
11257
|
|
|
6901
|
-
{timeframe}
|
|
11258
|
+
{{timeframe}}
|
|
6902
11259
|
|
|
6903
|
-
{format_instructions}
|
|
11260
|
+
{{format_instructions}}
|
|
6904
11261
|
|
|
6905
|
-
"""{changes}"""`;
|
|
6906
|
-
const inputVariables$1 = ['
|
|
11262
|
+
"""{{changes}}"""`;
|
|
11263
|
+
const inputVariables$1 = ['timeframe', 'format_instructions', 'changes'];
|
|
6907
11264
|
const RECAP_PROMPT = new prompts$1.PromptTemplate({
|
|
6908
11265
|
template: template$1,
|
|
6909
11266
|
inputVariables: inputVariables$1,
|
|
@@ -6920,10 +11277,13 @@ const handler$1 = async (argv, logger) => {
|
|
|
6920
11277
|
}
|
|
6921
11278
|
const tokenizer = await getTokenCounter(provider === 'openai' ? model : 'gpt-4o');
|
|
6922
11279
|
const llm = getLlm(provider, model, config);
|
|
6923
|
-
const INTERACTIVE = isInteractive(config);
|
|
11280
|
+
const INTERACTIVE = argv.interactive || isInteractive(config);
|
|
6924
11281
|
if (INTERACTIVE) {
|
|
6925
11282
|
logger.log(LOGO);
|
|
6926
11283
|
}
|
|
11284
|
+
else {
|
|
11285
|
+
logger.setConfig({ silent: true });
|
|
11286
|
+
}
|
|
6927
11287
|
const { 'last-month': lastMonth, 'last-tag': lastTag, yesterday, 'last-week': lastWeek } = argv;
|
|
6928
11288
|
const timeframe = lastMonth
|
|
6929
11289
|
? 'last-month'
|
|
@@ -6982,7 +11342,7 @@ const handler$1 = async (argv, logger) => {
|
|
|
6982
11342
|
async function parser(changes) {
|
|
6983
11343
|
return changes.join('\n');
|
|
6984
11344
|
}
|
|
6985
|
-
await generateAndReviewLoop({
|
|
11345
|
+
const recapResult = await generateAndReviewLoop({
|
|
6986
11346
|
label: 'recap',
|
|
6987
11347
|
options: {
|
|
6988
11348
|
...config,
|
|
@@ -7007,30 +11367,57 @@ const handler$1 = async (argv, logger) => {
|
|
|
7007
11367
|
factory,
|
|
7008
11368
|
parser,
|
|
7009
11369
|
agent: async (context, options) => {
|
|
7010
|
-
const
|
|
7011
|
-
const formatInstructions = "Respond with a valid JSON object, containing one field: 'summary', a string.";
|
|
11370
|
+
const formatInstructions = "Respond with a valid JSON object, containing two fields: 'title' and 'summary', both strings.";
|
|
7012
11371
|
const prompt = getPrompt({
|
|
7013
11372
|
template: options.prompt,
|
|
7014
11373
|
variables: RECAP_PROMPT.inputVariables,
|
|
7015
11374
|
fallback: RECAP_PROMPT,
|
|
7016
11375
|
});
|
|
7017
|
-
|
|
7018
|
-
|
|
7019
|
-
|
|
7020
|
-
|
|
7021
|
-
|
|
7022
|
-
|
|
7023
|
-
|
|
7024
|
-
|
|
7025
|
-
|
|
7026
|
-
|
|
7027
|
-
|
|
11376
|
+
try {
|
|
11377
|
+
const parser = new output_parsers.StructuredOutputParser(RecapLlmResponseSchema);
|
|
11378
|
+
const response = await executeChain({
|
|
11379
|
+
llm,
|
|
11380
|
+
prompt,
|
|
11381
|
+
variables: {
|
|
11382
|
+
changes: context,
|
|
11383
|
+
format_instructions: formatInstructions,
|
|
11384
|
+
timeframe,
|
|
11385
|
+
},
|
|
11386
|
+
parser,
|
|
11387
|
+
});
|
|
11388
|
+
return response ? `${response.title}\n\n${response.summary}` : 'no response';
|
|
11389
|
+
}
|
|
11390
|
+
catch (error) {
|
|
11391
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
11392
|
+
// Log the error but don't exit
|
|
11393
|
+
logger.log(`Error parsing LLM response: ${errorMessage}`, { color: 'red' });
|
|
11394
|
+
// Always return a fallback message instead of exiting
|
|
11395
|
+
const fallbackMessage = `
|
|
11396
|
+
## Failed to parse the response [timeframe: ${timeframe}]
|
|
11397
|
+
- There are changes in the codebase that couldn't be properly summarized due to a technical issue.
|
|
11398
|
+
- LLM encountered issues when parsing the changes.
|
|
11399
|
+
|
|
11400
|
+
### Error encountered
|
|
11401
|
+
|
|
11402
|
+
${errorMessage}
|
|
11403
|
+
`;
|
|
11404
|
+
return fallbackMessage;
|
|
11405
|
+
}
|
|
7028
11406
|
},
|
|
7029
11407
|
noResult: async () => {
|
|
7030
11408
|
await noResult$1({ git, logger });
|
|
7031
11409
|
process.exit(0);
|
|
7032
11410
|
},
|
|
7033
11411
|
});
|
|
11412
|
+
// Handle the result based on the mode (interactive or stdout)
|
|
11413
|
+
const MODE = (INTERACTIVE && 'interactive') || (config.recap && 'interactive') || config?.mode || 'stdout'; // Default to stdout
|
|
11414
|
+
handleResult({
|
|
11415
|
+
result: recapResult,
|
|
11416
|
+
interactiveModeCallback: async () => {
|
|
11417
|
+
logSuccess();
|
|
11418
|
+
},
|
|
11419
|
+
mode: MODE,
|
|
11420
|
+
});
|
|
7034
11421
|
};
|
|
7035
11422
|
|
|
7036
11423
|
var recap = {
|
|
@@ -7041,6 +11428,26 @@ var recap = {
|
|
|
7041
11428
|
options: options$1,
|
|
7042
11429
|
};
|
|
7043
11430
|
|
|
11431
|
+
const ReviewFeedbackItemSchema = z.object({
|
|
11432
|
+
title: z.string(),
|
|
11433
|
+
summary: z.string(),
|
|
11434
|
+
severity: z.union([
|
|
11435
|
+
z.literal(1),
|
|
11436
|
+
z.literal(2),
|
|
11437
|
+
z.literal(3),
|
|
11438
|
+
z.literal(4),
|
|
11439
|
+
z.literal(5),
|
|
11440
|
+
z.literal(6),
|
|
11441
|
+
z.literal(7),
|
|
11442
|
+
z.literal(8),
|
|
11443
|
+
z.literal(9),
|
|
11444
|
+
z.literal(10),
|
|
11445
|
+
]),
|
|
11446
|
+
category: z.string(),
|
|
11447
|
+
filePath: z.string(),
|
|
11448
|
+
});
|
|
11449
|
+
// Array schema for review feedback items
|
|
11450
|
+
const ReviewFeedbackItemArraySchema = z.array(ReviewFeedbackItemSchema);
|
|
7044
11451
|
const command = 'review';
|
|
7045
11452
|
/**
|
|
7046
11453
|
* Command line options via yargs
|
|
@@ -28239,11 +32646,11 @@ const template = `As an experienced software engineer, you are tasked with perfo
|
|
|
28239
32646
|
- Breaking down the changes into categories and ranking by severity is helpful.
|
|
28240
32647
|
- Output the content in a clear and concise manner that will render well in a terminal or CLI.
|
|
28241
32648
|
|
|
28242
|
-
{format_instructions}
|
|
32649
|
+
{{format_instructions}}
|
|
28243
32650
|
|
|
28244
32651
|
Following the formatting instructions, perform a code review on the following changes
|
|
28245
32652
|
|
|
28246
|
-
"""{changes}"""`;
|
|
32653
|
+
"""{{changes}}"""`;
|
|
28247
32654
|
const inputVariables = ['format_instructions', 'changes'];
|
|
28248
32655
|
const REVIEW_PROMPT = new prompts$1.PromptTemplate({
|
|
28249
32656
|
template,
|
|
@@ -28339,7 +32746,7 @@ const handler = async (argv, logger) => {
|
|
|
28339
32746
|
factory,
|
|
28340
32747
|
parser,
|
|
28341
32748
|
agent: async (context, options) => {
|
|
28342
|
-
const parser = new output_parsers.
|
|
32749
|
+
const parser = new output_parsers.StructuredOutputParser(ReviewFeedbackItemArraySchema);
|
|
28343
32750
|
const formatInstructions = "Respond with a valid JSON object, containing four fields:'title' a string, 'summary' a short summary of the problem (include line number if big file), 'severity' a numeric enum up to ten, 'category' an enum string, and 'filePath' a relative filepath to file as string.";
|
|
28344
32751
|
const prompt = getPrompt({
|
|
28345
32752
|
template: options.prompt,
|