checks.cjs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. "use strict";
  2. // import { $ZodType } from "./schemas.js";
  3. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  4. if (k2 === undefined) k2 = k;
  5. var desc = Object.getOwnPropertyDescriptor(m, k);
  6. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  7. desc = { enumerable: true, get: function() { return m[k]; } };
  8. }
  9. Object.defineProperty(o, k2, desc);
  10. }) : (function(o, m, k, k2) {
  11. if (k2 === undefined) k2 = k;
  12. o[k2] = m[k];
  13. }));
  14. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  15. Object.defineProperty(o, "default", { enumerable: true, value: v });
  16. }) : function(o, v) {
  17. o["default"] = v;
  18. });
  19. var __importStar = (this && this.__importStar) || function (mod) {
  20. if (mod && mod.__esModule) return mod;
  21. var result = {};
  22. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  23. __setModuleDefault(result, mod);
  24. return result;
  25. };
  26. Object.defineProperty(exports, "__esModule", { value: true });
  27. exports.$ZodCheckOverwrite = exports.$ZodCheckMimeType = exports.$ZodCheckProperty = exports.$ZodCheckEndsWith = exports.$ZodCheckStartsWith = exports.$ZodCheckIncludes = exports.$ZodCheckUpperCase = exports.$ZodCheckLowerCase = exports.$ZodCheckRegex = exports.$ZodCheckStringFormat = exports.$ZodCheckLengthEquals = exports.$ZodCheckMinLength = exports.$ZodCheckMaxLength = exports.$ZodCheckSizeEquals = exports.$ZodCheckMinSize = exports.$ZodCheckMaxSize = exports.$ZodCheckBigIntFormat = exports.$ZodCheckNumberFormat = exports.$ZodCheckMultipleOf = exports.$ZodCheckGreaterThan = exports.$ZodCheckLessThan = exports.$ZodCheck = void 0;
  28. const core = __importStar(require("./core.cjs"));
  29. const regexes = __importStar(require("./regexes.cjs"));
  30. const util = __importStar(require("./util.cjs"));
  31. exports.$ZodCheck = core.$constructor("$ZodCheck", (inst, def) => {
  32. var _a;
  33. inst._zod ?? (inst._zod = {});
  34. inst._zod.def = def;
  35. (_a = inst._zod).onattach ?? (_a.onattach = []);
  36. });
  37. const numericOriginMap = {
  38. number: "number",
  39. bigint: "bigint",
  40. object: "date",
  41. };
  42. exports.$ZodCheckLessThan = core.$constructor("$ZodCheckLessThan", (inst, def) => {
  43. exports.$ZodCheck.init(inst, def);
  44. const origin = numericOriginMap[typeof def.value];
  45. inst._zod.onattach.push((inst) => {
  46. const bag = inst._zod.bag;
  47. const curr = (def.inclusive ? bag.maximum : bag.exclusiveMaximum) ?? Number.POSITIVE_INFINITY;
  48. if (def.value < curr) {
  49. if (def.inclusive)
  50. bag.maximum = def.value;
  51. else
  52. bag.exclusiveMaximum = def.value;
  53. }
  54. });
  55. inst._zod.check = (payload) => {
  56. if (def.inclusive ? payload.value <= def.value : payload.value < def.value) {
  57. return;
  58. }
  59. payload.issues.push({
  60. origin,
  61. code: "too_big",
  62. maximum: typeof def.value === "object" ? def.value.getTime() : def.value,
  63. input: payload.value,
  64. inclusive: def.inclusive,
  65. inst,
  66. continue: !def.abort,
  67. });
  68. };
  69. });
  70. exports.$ZodCheckGreaterThan = core.$constructor("$ZodCheckGreaterThan", (inst, def) => {
  71. exports.$ZodCheck.init(inst, def);
  72. const origin = numericOriginMap[typeof def.value];
  73. inst._zod.onattach.push((inst) => {
  74. const bag = inst._zod.bag;
  75. const curr = (def.inclusive ? bag.minimum : bag.exclusiveMinimum) ?? Number.NEGATIVE_INFINITY;
  76. if (def.value > curr) {
  77. if (def.inclusive)
  78. bag.minimum = def.value;
  79. else
  80. bag.exclusiveMinimum = def.value;
  81. }
  82. });
  83. inst._zod.check = (payload) => {
  84. if (def.inclusive ? payload.value >= def.value : payload.value > def.value) {
  85. return;
  86. }
  87. payload.issues.push({
  88. origin,
  89. code: "too_small",
  90. minimum: typeof def.value === "object" ? def.value.getTime() : def.value,
  91. input: payload.value,
  92. inclusive: def.inclusive,
  93. inst,
  94. continue: !def.abort,
  95. });
  96. };
  97. });
  98. exports.$ZodCheckMultipleOf =
  99. /*@__PURE__*/ core.$constructor("$ZodCheckMultipleOf", (inst, def) => {
  100. exports.$ZodCheck.init(inst, def);
  101. inst._zod.onattach.push((inst) => {
  102. var _a;
  103. (_a = inst._zod.bag).multipleOf ?? (_a.multipleOf = def.value);
  104. });
  105. inst._zod.check = (payload) => {
  106. if (typeof payload.value !== typeof def.value)
  107. throw new Error("Cannot mix number and bigint in multiple_of check.");
  108. const isMultiple = typeof payload.value === "bigint"
  109. ? payload.value % def.value === BigInt(0)
  110. : util.floatSafeRemainder(payload.value, def.value) === 0;
  111. if (isMultiple)
  112. return;
  113. payload.issues.push({
  114. origin: typeof payload.value,
  115. code: "not_multiple_of",
  116. divisor: def.value,
  117. input: payload.value,
  118. inst,
  119. continue: !def.abort,
  120. });
  121. };
  122. });
  123. exports.$ZodCheckNumberFormat = core.$constructor("$ZodCheckNumberFormat", (inst, def) => {
  124. exports.$ZodCheck.init(inst, def); // no format checks
  125. def.format = def.format || "float64";
  126. const isInt = def.format?.includes("int");
  127. const origin = isInt ? "int" : "number";
  128. const [minimum, maximum] = util.NUMBER_FORMAT_RANGES[def.format];
  129. inst._zod.onattach.push((inst) => {
  130. const bag = inst._zod.bag;
  131. bag.format = def.format;
  132. bag.minimum = minimum;
  133. bag.maximum = maximum;
  134. if (isInt)
  135. bag.pattern = regexes.integer;
  136. });
  137. inst._zod.check = (payload) => {
  138. const input = payload.value;
  139. if (isInt) {
  140. if (!Number.isInteger(input)) {
  141. // invalid_format issue
  142. // payload.issues.push({
  143. // expected: def.format,
  144. // format: def.format,
  145. // code: "invalid_format",
  146. // input,
  147. // inst,
  148. // });
  149. // invalid_type issue
  150. payload.issues.push({
  151. expected: origin,
  152. format: def.format,
  153. code: "invalid_type",
  154. continue: false,
  155. input,
  156. inst,
  157. });
  158. return;
  159. // not_multiple_of issue
  160. // payload.issues.push({
  161. // code: "not_multiple_of",
  162. // origin: "number",
  163. // input,
  164. // inst,
  165. // divisor: 1,
  166. // });
  167. }
  168. if (!Number.isSafeInteger(input)) {
  169. if (input > 0) {
  170. // too_big
  171. payload.issues.push({
  172. input,
  173. code: "too_big",
  174. maximum: Number.MAX_SAFE_INTEGER,
  175. note: "Integers must be within the safe integer range.",
  176. inst,
  177. origin,
  178. inclusive: true,
  179. continue: !def.abort,
  180. });
  181. }
  182. else {
  183. // too_small
  184. payload.issues.push({
  185. input,
  186. code: "too_small",
  187. minimum: Number.MIN_SAFE_INTEGER,
  188. note: "Integers must be within the safe integer range.",
  189. inst,
  190. origin,
  191. inclusive: true,
  192. continue: !def.abort,
  193. });
  194. }
  195. return;
  196. }
  197. }
  198. if (input < minimum) {
  199. payload.issues.push({
  200. origin: "number",
  201. input,
  202. code: "too_small",
  203. minimum,
  204. inclusive: true,
  205. inst,
  206. continue: !def.abort,
  207. });
  208. }
  209. if (input > maximum) {
  210. payload.issues.push({
  211. origin: "number",
  212. input,
  213. code: "too_big",
  214. maximum,
  215. inclusive: true,
  216. inst,
  217. continue: !def.abort,
  218. });
  219. }
  220. };
  221. });
  222. exports.$ZodCheckBigIntFormat = core.$constructor("$ZodCheckBigIntFormat", (inst, def) => {
  223. exports.$ZodCheck.init(inst, def); // no format checks
  224. const [minimum, maximum] = util.BIGINT_FORMAT_RANGES[def.format];
  225. inst._zod.onattach.push((inst) => {
  226. const bag = inst._zod.bag;
  227. bag.format = def.format;
  228. bag.minimum = minimum;
  229. bag.maximum = maximum;
  230. });
  231. inst._zod.check = (payload) => {
  232. const input = payload.value;
  233. if (input < minimum) {
  234. payload.issues.push({
  235. origin: "bigint",
  236. input,
  237. code: "too_small",
  238. minimum: minimum,
  239. inclusive: true,
  240. inst,
  241. continue: !def.abort,
  242. });
  243. }
  244. if (input > maximum) {
  245. payload.issues.push({
  246. origin: "bigint",
  247. input,
  248. code: "too_big",
  249. maximum,
  250. inclusive: true,
  251. inst,
  252. continue: !def.abort,
  253. });
  254. }
  255. };
  256. });
  257. exports.$ZodCheckMaxSize = core.$constructor("$ZodCheckMaxSize", (inst, def) => {
  258. var _a;
  259. exports.$ZodCheck.init(inst, def);
  260. (_a = inst._zod.def).when ?? (_a.when = (payload) => {
  261. const val = payload.value;
  262. return !util.nullish(val) && val.size !== undefined;
  263. });
  264. inst._zod.onattach.push((inst) => {
  265. const curr = (inst._zod.bag.maximum ?? Number.POSITIVE_INFINITY);
  266. if (def.maximum < curr)
  267. inst._zod.bag.maximum = def.maximum;
  268. });
  269. inst._zod.check = (payload) => {
  270. const input = payload.value;
  271. const size = input.size;
  272. if (size <= def.maximum)
  273. return;
  274. payload.issues.push({
  275. origin: util.getSizableOrigin(input),
  276. code: "too_big",
  277. maximum: def.maximum,
  278. inclusive: true,
  279. input,
  280. inst,
  281. continue: !def.abort,
  282. });
  283. };
  284. });
  285. exports.$ZodCheckMinSize = core.$constructor("$ZodCheckMinSize", (inst, def) => {
  286. var _a;
  287. exports.$ZodCheck.init(inst, def);
  288. (_a = inst._zod.def).when ?? (_a.when = (payload) => {
  289. const val = payload.value;
  290. return !util.nullish(val) && val.size !== undefined;
  291. });
  292. inst._zod.onattach.push((inst) => {
  293. const curr = (inst._zod.bag.minimum ?? Number.NEGATIVE_INFINITY);
  294. if (def.minimum > curr)
  295. inst._zod.bag.minimum = def.minimum;
  296. });
  297. inst._zod.check = (payload) => {
  298. const input = payload.value;
  299. const size = input.size;
  300. if (size >= def.minimum)
  301. return;
  302. payload.issues.push({
  303. origin: util.getSizableOrigin(input),
  304. code: "too_small",
  305. minimum: def.minimum,
  306. inclusive: true,
  307. input,
  308. inst,
  309. continue: !def.abort,
  310. });
  311. };
  312. });
  313. exports.$ZodCheckSizeEquals = core.$constructor("$ZodCheckSizeEquals", (inst, def) => {
  314. var _a;
  315. exports.$ZodCheck.init(inst, def);
  316. (_a = inst._zod.def).when ?? (_a.when = (payload) => {
  317. const val = payload.value;
  318. return !util.nullish(val) && val.size !== undefined;
  319. });
  320. inst._zod.onattach.push((inst) => {
  321. const bag = inst._zod.bag;
  322. bag.minimum = def.size;
  323. bag.maximum = def.size;
  324. bag.size = def.size;
  325. });
  326. inst._zod.check = (payload) => {
  327. const input = payload.value;
  328. const size = input.size;
  329. if (size === def.size)
  330. return;
  331. const tooBig = size > def.size;
  332. payload.issues.push({
  333. origin: util.getSizableOrigin(input),
  334. ...(tooBig ? { code: "too_big", maximum: def.size } : { code: "too_small", minimum: def.size }),
  335. inclusive: true,
  336. exact: true,
  337. input: payload.value,
  338. inst,
  339. continue: !def.abort,
  340. });
  341. };
  342. });
  343. exports.$ZodCheckMaxLength = core.$constructor("$ZodCheckMaxLength", (inst, def) => {
  344. var _a;
  345. exports.$ZodCheck.init(inst, def);
  346. (_a = inst._zod.def).when ?? (_a.when = (payload) => {
  347. const val = payload.value;
  348. return !util.nullish(val) && val.length !== undefined;
  349. });
  350. inst._zod.onattach.push((inst) => {
  351. const curr = (inst._zod.bag.maximum ?? Number.POSITIVE_INFINITY);
  352. if (def.maximum < curr)
  353. inst._zod.bag.maximum = def.maximum;
  354. });
  355. inst._zod.check = (payload) => {
  356. const input = payload.value;
  357. const length = input.length;
  358. if (length <= def.maximum)
  359. return;
  360. const origin = util.getLengthableOrigin(input);
  361. payload.issues.push({
  362. origin,
  363. code: "too_big",
  364. maximum: def.maximum,
  365. inclusive: true,
  366. input,
  367. inst,
  368. continue: !def.abort,
  369. });
  370. };
  371. });
  372. exports.$ZodCheckMinLength = core.$constructor("$ZodCheckMinLength", (inst, def) => {
  373. var _a;
  374. exports.$ZodCheck.init(inst, def);
  375. (_a = inst._zod.def).when ?? (_a.when = (payload) => {
  376. const val = payload.value;
  377. return !util.nullish(val) && val.length !== undefined;
  378. });
  379. inst._zod.onattach.push((inst) => {
  380. const curr = (inst._zod.bag.minimum ?? Number.NEGATIVE_INFINITY);
  381. if (def.minimum > curr)
  382. inst._zod.bag.minimum = def.minimum;
  383. });
  384. inst._zod.check = (payload) => {
  385. const input = payload.value;
  386. const length = input.length;
  387. if (length >= def.minimum)
  388. return;
  389. const origin = util.getLengthableOrigin(input);
  390. payload.issues.push({
  391. origin,
  392. code: "too_small",
  393. minimum: def.minimum,
  394. inclusive: true,
  395. input,
  396. inst,
  397. continue: !def.abort,
  398. });
  399. };
  400. });
  401. exports.$ZodCheckLengthEquals = core.$constructor("$ZodCheckLengthEquals", (inst, def) => {
  402. var _a;
  403. exports.$ZodCheck.init(inst, def);
  404. (_a = inst._zod.def).when ?? (_a.when = (payload) => {
  405. const val = payload.value;
  406. return !util.nullish(val) && val.length !== undefined;
  407. });
  408. inst._zod.onattach.push((inst) => {
  409. const bag = inst._zod.bag;
  410. bag.minimum = def.length;
  411. bag.maximum = def.length;
  412. bag.length = def.length;
  413. });
  414. inst._zod.check = (payload) => {
  415. const input = payload.value;
  416. const length = input.length;
  417. if (length === def.length)
  418. return;
  419. const origin = util.getLengthableOrigin(input);
  420. const tooBig = length > def.length;
  421. payload.issues.push({
  422. origin,
  423. ...(tooBig ? { code: "too_big", maximum: def.length } : { code: "too_small", minimum: def.length }),
  424. inclusive: true,
  425. exact: true,
  426. input: payload.value,
  427. inst,
  428. continue: !def.abort,
  429. });
  430. };
  431. });
  432. exports.$ZodCheckStringFormat = core.$constructor("$ZodCheckStringFormat", (inst, def) => {
  433. var _a, _b;
  434. exports.$ZodCheck.init(inst, def);
  435. inst._zod.onattach.push((inst) => {
  436. const bag = inst._zod.bag;
  437. bag.format = def.format;
  438. if (def.pattern) {
  439. bag.patterns ?? (bag.patterns = new Set());
  440. bag.patterns.add(def.pattern);
  441. }
  442. });
  443. if (def.pattern)
  444. (_a = inst._zod).check ?? (_a.check = (payload) => {
  445. def.pattern.lastIndex = 0;
  446. if (def.pattern.test(payload.value))
  447. return;
  448. payload.issues.push({
  449. origin: "string",
  450. code: "invalid_format",
  451. format: def.format,
  452. input: payload.value,
  453. ...(def.pattern ? { pattern: def.pattern.toString() } : {}),
  454. inst,
  455. continue: !def.abort,
  456. });
  457. });
  458. else
  459. (_b = inst._zod).check ?? (_b.check = () => { });
  460. });
  461. exports.$ZodCheckRegex = core.$constructor("$ZodCheckRegex", (inst, def) => {
  462. exports.$ZodCheckStringFormat.init(inst, def);
  463. inst._zod.check = (payload) => {
  464. def.pattern.lastIndex = 0;
  465. if (def.pattern.test(payload.value))
  466. return;
  467. payload.issues.push({
  468. origin: "string",
  469. code: "invalid_format",
  470. format: "regex",
  471. input: payload.value,
  472. pattern: def.pattern.toString(),
  473. inst,
  474. continue: !def.abort,
  475. });
  476. };
  477. });
  478. exports.$ZodCheckLowerCase = core.$constructor("$ZodCheckLowerCase", (inst, def) => {
  479. def.pattern ?? (def.pattern = regexes.lowercase);
  480. exports.$ZodCheckStringFormat.init(inst, def);
  481. });
  482. exports.$ZodCheckUpperCase = core.$constructor("$ZodCheckUpperCase", (inst, def) => {
  483. def.pattern ?? (def.pattern = regexes.uppercase);
  484. exports.$ZodCheckStringFormat.init(inst, def);
  485. });
  486. exports.$ZodCheckIncludes = core.$constructor("$ZodCheckIncludes", (inst, def) => {
  487. exports.$ZodCheck.init(inst, def);
  488. const escapedRegex = util.escapeRegex(def.includes);
  489. const pattern = new RegExp(typeof def.position === "number" ? `^.{${def.position}}${escapedRegex}` : escapedRegex);
  490. def.pattern = pattern;
  491. inst._zod.onattach.push((inst) => {
  492. const bag = inst._zod.bag;
  493. bag.patterns ?? (bag.patterns = new Set());
  494. bag.patterns.add(pattern);
  495. });
  496. inst._zod.check = (payload) => {
  497. if (payload.value.includes(def.includes, def.position))
  498. return;
  499. payload.issues.push({
  500. origin: "string",
  501. code: "invalid_format",
  502. format: "includes",
  503. includes: def.includes,
  504. input: payload.value,
  505. inst,
  506. continue: !def.abort,
  507. });
  508. };
  509. });
  510. exports.$ZodCheckStartsWith = core.$constructor("$ZodCheckStartsWith", (inst, def) => {
  511. exports.$ZodCheck.init(inst, def);
  512. const pattern = new RegExp(`^${util.escapeRegex(def.prefix)}.*`);
  513. def.pattern ?? (def.pattern = pattern);
  514. inst._zod.onattach.push((inst) => {
  515. const bag = inst._zod.bag;
  516. bag.patterns ?? (bag.patterns = new Set());
  517. bag.patterns.add(pattern);
  518. });
  519. inst._zod.check = (payload) => {
  520. if (payload.value.startsWith(def.prefix))
  521. return;
  522. payload.issues.push({
  523. origin: "string",
  524. code: "invalid_format",
  525. format: "starts_with",
  526. prefix: def.prefix,
  527. input: payload.value,
  528. inst,
  529. continue: !def.abort,
  530. });
  531. };
  532. });
  533. exports.$ZodCheckEndsWith = core.$constructor("$ZodCheckEndsWith", (inst, def) => {
  534. exports.$ZodCheck.init(inst, def);
  535. const pattern = new RegExp(`.*${util.escapeRegex(def.suffix)}$`);
  536. def.pattern ?? (def.pattern = pattern);
  537. inst._zod.onattach.push((inst) => {
  538. const bag = inst._zod.bag;
  539. bag.patterns ?? (bag.patterns = new Set());
  540. bag.patterns.add(pattern);
  541. });
  542. inst._zod.check = (payload) => {
  543. if (payload.value.endsWith(def.suffix))
  544. return;
  545. payload.issues.push({
  546. origin: "string",
  547. code: "invalid_format",
  548. format: "ends_with",
  549. suffix: def.suffix,
  550. input: payload.value,
  551. inst,
  552. continue: !def.abort,
  553. });
  554. };
  555. });
  556. ///////////////////////////////////
  557. ///// $ZodCheckProperty /////
  558. ///////////////////////////////////
  559. function handleCheckPropertyResult(result, payload, property) {
  560. if (result.issues.length) {
  561. payload.issues.push(...util.prefixIssues(property, result.issues));
  562. }
  563. }
  564. exports.$ZodCheckProperty = core.$constructor("$ZodCheckProperty", (inst, def) => {
  565. exports.$ZodCheck.init(inst, def);
  566. inst._zod.check = (payload) => {
  567. const result = def.schema._zod.run({
  568. value: payload.value[def.property],
  569. issues: [],
  570. }, {});
  571. if (result instanceof Promise) {
  572. return result.then((result) => handleCheckPropertyResult(result, payload, def.property));
  573. }
  574. handleCheckPropertyResult(result, payload, def.property);
  575. return;
  576. };
  577. });
  578. exports.$ZodCheckMimeType = core.$constructor("$ZodCheckMimeType", (inst, def) => {
  579. exports.$ZodCheck.init(inst, def);
  580. const mimeSet = new Set(def.mime);
  581. inst._zod.onattach.push((inst) => {
  582. inst._zod.bag.mime = def.mime;
  583. });
  584. inst._zod.check = (payload) => {
  585. if (mimeSet.has(payload.value.type))
  586. return;
  587. payload.issues.push({
  588. code: "invalid_value",
  589. values: def.mime,
  590. input: payload.value.type,
  591. inst,
  592. continue: !def.abort,
  593. });
  594. };
  595. });
  596. exports.$ZodCheckOverwrite = core.$constructor("$ZodCheckOverwrite", (inst, def) => {
  597. exports.$ZodCheck.init(inst, def);
  598. inst._zod.check = (payload) => {
  599. payload.value = def.tx(payload.value);
  600. };
  601. });