parse.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import * as core from "./core.js";
  2. import * as errors from "./errors.js";
  3. import * as util from "./util.js";
  4. export const _parse = (_Err) => (schema, value, _ctx, _params) => {
  5. const ctx = _ctx ? Object.assign(_ctx, { async: false }) : { async: false };
  6. const result = schema._zod.run({ value, issues: [] }, ctx);
  7. if (result instanceof Promise) {
  8. throw new core.$ZodAsyncError();
  9. }
  10. if (result.issues.length) {
  11. const e = new (_params?.Err ?? _Err)(result.issues.map((iss) => util.finalizeIssue(iss, ctx, core.config())));
  12. util.captureStackTrace(e, _params?.callee);
  13. throw e;
  14. }
  15. return result.value;
  16. };
  17. export const parse = /* @__PURE__*/ _parse(errors.$ZodRealError);
  18. export const _parseAsync = (_Err) => async (schema, value, _ctx, params) => {
  19. const ctx = _ctx ? Object.assign(_ctx, { async: true }) : { async: true };
  20. let result = schema._zod.run({ value, issues: [] }, ctx);
  21. if (result instanceof Promise)
  22. result = await result;
  23. if (result.issues.length) {
  24. const e = new (params?.Err ?? _Err)(result.issues.map((iss) => util.finalizeIssue(iss, ctx, core.config())));
  25. util.captureStackTrace(e, params?.callee);
  26. throw e;
  27. }
  28. return result.value;
  29. };
  30. export const parseAsync = /* @__PURE__*/ _parseAsync(errors.$ZodRealError);
  31. export const _safeParse = (_Err) => (schema, value, _ctx) => {
  32. const ctx = _ctx ? { ..._ctx, async: false } : { async: false };
  33. const result = schema._zod.run({ value, issues: [] }, ctx);
  34. if (result instanceof Promise) {
  35. throw new core.$ZodAsyncError();
  36. }
  37. return result.issues.length
  38. ? {
  39. success: false,
  40. error: new (_Err ?? errors.$ZodError)(result.issues.map((iss) => util.finalizeIssue(iss, ctx, core.config()))),
  41. }
  42. : { success: true, data: result.value };
  43. };
  44. export const safeParse = /* @__PURE__*/ _safeParse(errors.$ZodRealError);
  45. export const _safeParseAsync = (_Err) => async (schema, value, _ctx) => {
  46. const ctx = _ctx ? Object.assign(_ctx, { async: true }) : { async: true };
  47. let result = schema._zod.run({ value, issues: [] }, ctx);
  48. if (result instanceof Promise)
  49. result = await result;
  50. return result.issues.length
  51. ? {
  52. success: false,
  53. error: new _Err(result.issues.map((iss) => util.finalizeIssue(iss, ctx, core.config()))),
  54. }
  55. : { success: true, data: result.value };
  56. };
  57. export const safeParseAsync = /* @__PURE__*/ _safeParseAsync(errors.$ZodRealError);
  58. export const _encode = (_Err) => (schema, value, _ctx) => {
  59. const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
  60. return _parse(_Err)(schema, value, ctx);
  61. };
  62. export const encode = /* @__PURE__*/ _encode(errors.$ZodRealError);
  63. export const _decode = (_Err) => (schema, value, _ctx) => {
  64. return _parse(_Err)(schema, value, _ctx);
  65. };
  66. export const decode = /* @__PURE__*/ _decode(errors.$ZodRealError);
  67. export const _encodeAsync = (_Err) => async (schema, value, _ctx) => {
  68. const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
  69. return _parseAsync(_Err)(schema, value, ctx);
  70. };
  71. export const encodeAsync = /* @__PURE__*/ _encodeAsync(errors.$ZodRealError);
  72. export const _decodeAsync = (_Err) => async (schema, value, _ctx) => {
  73. return _parseAsync(_Err)(schema, value, _ctx);
  74. };
  75. export const decodeAsync = /* @__PURE__*/ _decodeAsync(errors.$ZodRealError);
  76. export const _safeEncode = (_Err) => (schema, value, _ctx) => {
  77. const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
  78. return _safeParse(_Err)(schema, value, ctx);
  79. };
  80. export const safeEncode = /* @__PURE__*/ _safeEncode(errors.$ZodRealError);
  81. export const _safeDecode = (_Err) => (schema, value, _ctx) => {
  82. return _safeParse(_Err)(schema, value, _ctx);
  83. };
  84. export const safeDecode = /* @__PURE__*/ _safeDecode(errors.$ZodRealError);
  85. export const _safeEncodeAsync = (_Err) => async (schema, value, _ctx) => {
  86. const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
  87. return _safeParseAsync(_Err)(schema, value, ctx);
  88. };
  89. export const safeEncodeAsync = /* @__PURE__*/ _safeEncodeAsync(errors.$ZodRealError);
  90. export const _safeDecodeAsync = (_Err) => async (schema, value, _ctx) => {
  91. return _safeParseAsync(_Err)(schema, value, _ctx);
  92. };
  93. export const safeDecodeAsync = /* @__PURE__*/ _safeDecodeAsync(errors.$ZodRealError);