@o3co/ts.hocon 1.14.0 → 1.14.2

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.
@@ -11,7 +11,7 @@
11
11
 
12
12
 
13
13
 
14
- var _chunkRO4D6BH7cjs = require('./chunk-RO4D6BH7.cjs');
14
+ var _chunkL3D2RE2Ucjs = require('./chunk-L3D2RE2U.cjs');
15
15
 
16
16
  // src/parse.ts
17
17
  var _fs = require('fs'); var fs = _interopRequireWildcard(_fs);
@@ -34,11 +34,11 @@ async function defaultReadFile(filePath) {
34
34
  return fs.promises.readFile(filePath, "utf-8");
35
35
  }
36
36
  function buildResolveContext(input, opts) {
37
- const tokens = _chunkRO4D6BH7cjs.tokenize.call(void 0, input);
38
- const ast = _chunkRO4D6BH7cjs.parseTokens.call(void 0, tokens);
37
+ const tokens = _chunkL3D2RE2Ucjs.tokenize.call(void 0, input);
38
+ const ast = _chunkL3D2RE2Ucjs.parseTokens.call(void 0, tokens);
39
39
  if (ast.kind === "array") {
40
40
  const origin = _nullishCoalesce(opts.originDescription, () => ( "input"));
41
- throw new (0, _chunkRO4D6BH7cjs.ConfigError)(
41
+ throw new (0, _chunkL3D2RE2Ucjs.ConfigError)(
42
42
  `${origin}: ${ast.pos.line}:${ast.pos.col}: document has type array rather than object at file root (HOCON.md L989-991); the Config API requires an object root`,
43
43
  ""
44
44
  );
@@ -59,22 +59,22 @@ function parse(input, opts = {}) {
59
59
  }
60
60
  var parseString = parse;
61
61
  function parseStringWithOptions(input, opts = {}) {
62
- return _chunkRO4D6BH7cjs.guardStackDepth.call(void 0,
62
+ return _chunkL3D2RE2Ucjs.guardStackDepth.call(void 0,
63
63
  () => parseStringInner(input, opts),
64
- (msg) => new (0, _chunkRO4D6BH7cjs.ParseError)(msg, 1, 1, opts.originDescription)
64
+ (msg) => new (0, _chunkL3D2RE2Ucjs.ParseError)(msg, 1, 1, opts.originDescription)
65
65
  );
66
66
  }
67
67
  function parseStringInner(input, opts) {
68
68
  const resolveSubstitutions = _nullishCoalesce(opts.resolveSubstitutions, () => ( true));
69
69
  const { ast, resolveOpts } = buildResolveContext(input, opts);
70
70
  if (resolveSubstitutions) {
71
- const value = _chunkRO4D6BH7cjs.resolve.call(void 0, ast, resolveOpts);
71
+ const value = _chunkL3D2RE2Ucjs.resolve.call(void 0, ast, resolveOpts);
72
72
  if (value.kind !== "object") throw new Error("resolved value is not an object");
73
- return new (0, _chunkRO4D6BH7cjs.Config)(value, { resolved: true, originDescription: opts.originDescription });
73
+ return new (0, _chunkL3D2RE2Ucjs.Config)(value, { resolved: true, originDescription: opts.originDescription });
74
74
  }
75
- const tree = _chunkRO4D6BH7cjs.buildTree.call(void 0, ast, resolveOpts);
76
- const hasPlaceholders = _chunkRO4D6BH7cjs.containsPlaceholders.call(void 0, tree);
77
- return _chunkRO4D6BH7cjs.Config._fromUnresolvedResObj(tree, {
75
+ const tree = _chunkL3D2RE2Ucjs.buildTree.call(void 0, ast, resolveOpts);
76
+ const hasPlaceholders = _chunkL3D2RE2Ucjs.containsPlaceholders.call(void 0, tree);
77
+ return _chunkL3D2RE2Ucjs.Config._fromUnresolvedResObj(tree, {
78
78
  parseBaseDir: opts.baseDir,
79
79
  originDescription: opts.originDescription,
80
80
  resolved: !hasPlaceholders,
@@ -95,22 +95,22 @@ function parseFileWithOptions(filePath, opts = {}) {
95
95
  });
96
96
  }
97
97
  async function parseAsync(input, opts = {}) {
98
- return _chunkRO4D6BH7cjs.guardStackDepthAsync.call(void 0,
98
+ return _chunkL3D2RE2Ucjs.guardStackDepthAsync.call(void 0,
99
99
  () => parseStringInnerAsync(input, opts),
100
- (msg) => new (0, _chunkRO4D6BH7cjs.ParseError)(msg, 1, 1, opts.originDescription)
100
+ (msg) => new (0, _chunkL3D2RE2Ucjs.ParseError)(msg, 1, 1, opts.originDescription)
101
101
  );
102
102
  }
103
103
  async function parseStringInnerAsync(input, opts) {
104
104
  const resolveSubstitutions = _nullishCoalesce(opts.resolveSubstitutions, () => ( true));
105
105
  const { ast, resolveOpts } = buildResolveContext(input, opts);
106
106
  if (resolveSubstitutions) {
107
- const value = await _chunkRO4D6BH7cjs.resolveAsync.call(void 0, ast, resolveOpts);
107
+ const value = await _chunkL3D2RE2Ucjs.resolveAsync.call(void 0, ast, resolveOpts);
108
108
  if (value.kind !== "object") throw new Error("resolved value is not an object");
109
- return new (0, _chunkRO4D6BH7cjs.Config)(value, { resolved: true, originDescription: opts.originDescription });
109
+ return new (0, _chunkL3D2RE2Ucjs.Config)(value, { resolved: true, originDescription: opts.originDescription });
110
110
  }
111
- const tree = await _chunkRO4D6BH7cjs.buildTreeAsync.call(void 0, ast, resolveOpts);
112
- const hasPlaceholders = _chunkRO4D6BH7cjs.containsPlaceholders.call(void 0, tree);
113
- return _chunkRO4D6BH7cjs.Config._fromUnresolvedResObj(tree, {
111
+ const tree = await _chunkL3D2RE2Ucjs.buildTreeAsync.call(void 0, ast, resolveOpts);
112
+ const hasPlaceholders = _chunkL3D2RE2Ucjs.containsPlaceholders.call(void 0, tree);
113
+ return _chunkL3D2RE2Ucjs.Config._fromUnresolvedResObj(tree, {
114
114
  parseBaseDir: opts.baseDir,
115
115
  originDescription: opts.originDescription,
116
116
  resolved: !hasPlaceholders,
@@ -3,20 +3,20 @@
3
3
 
4
4
 
5
5
 
6
- var _chunkRO4D6BH7cjs = require('./chunk-RO4D6BH7.cjs');
6
+ var _chunkL3D2RE2Ucjs = require('./chunk-L3D2RE2U.cjs');
7
7
 
8
8
  // src/value-factory.ts
9
9
  function fromMap(values, originDescription) {
10
- const fields = _chunkRO4D6BH7cjs.guardStackDepth.call(void 0,
10
+ const fields = _chunkL3D2RE2Ucjs.guardStackDepth.call(void 0,
11
11
  () => coerceObject(values, ""),
12
- (msg) => _chunkRO4D6BH7cjs.depthError.call(void 0, `fromMap: ${msg}`)
12
+ (msg) => _chunkL3D2RE2Ucjs.depthError.call(void 0, `fromMap: ${msg}`)
13
13
  );
14
14
  const root = { kind: "object", fields };
15
- return _chunkRO4D6BH7cjs.Config._fromResolvedValue(root, { originDescription });
15
+ return _chunkL3D2RE2Ucjs.Config._fromResolvedValue(root, { originDescription });
16
16
  }
17
17
  function empty(originDescription) {
18
18
  const root = { kind: "object", fields: /* @__PURE__ */ new Map() };
19
- return _chunkRO4D6BH7cjs.Config._fromResolvedValue(root, { originDescription });
19
+ return _chunkL3D2RE2Ucjs.Config._fromResolvedValue(root, { originDescription });
20
20
  }
21
21
  var INT64_MAX = 9223372036854775807n;
22
22
  var INT64_MIN = -9223372036854775808n;
@@ -33,7 +33,7 @@ function coerceValue(v, atPath) {
33
33
  return { kind: "scalar", raw: "null", valueType: "null" };
34
34
  }
35
35
  if (v === void 0) {
36
- throw new (0, _chunkRO4D6BH7cjs.ConfigError)(
36
+ throw new (0, _chunkL3D2RE2Ucjs.ConfigError)(
37
37
  `fromMap: undefined is not a valid HOCON value at path "${atPath}"`,
38
38
  atPath
39
39
  );
@@ -43,7 +43,7 @@ function coerceValue(v, atPath) {
43
43
  return { kind: "scalar", raw: v, valueType: "string" };
44
44
  case "number": {
45
45
  if (Number.isNaN(v) || !Number.isFinite(v)) {
46
- throw new (0, _chunkRO4D6BH7cjs.ConfigError)(
46
+ throw new (0, _chunkL3D2RE2Ucjs.ConfigError)(
47
47
  `fromMap: ${v} is not representable in HOCON (NaN/Infinity not allowed) at path "${atPath}"`,
48
48
  atPath
49
49
  );
@@ -54,7 +54,7 @@ function coerceValue(v, atPath) {
54
54
  return { kind: "scalar", raw: v ? "true" : "false", valueType: "boolean" };
55
55
  case "bigint": {
56
56
  if (v > INT64_MAX || v < INT64_MIN) {
57
- throw new (0, _chunkRO4D6BH7cjs.ConfigError)(
57
+ throw new (0, _chunkL3D2RE2Ucjs.ConfigError)(
58
58
  `fromMap: integer ${v} is out of int64 range (HOCON integers are int64; spec F0.5) at path "${atPath}"`,
59
59
  atPath
60
60
  );
@@ -70,7 +70,7 @@ function coerceValue(v, atPath) {
70
70
  return { kind: "object", fields };
71
71
  }
72
72
  default:
73
- throw new (0, _chunkRO4D6BH7cjs.ConfigError)(
73
+ throw new (0, _chunkL3D2RE2Ucjs.ConfigError)(
74
74
  `fromMap: unsupported value type "${typeof v}" at path "${atPath}"`,
75
75
  atPath
76
76
  );
@@ -3,7 +3,7 @@ import {
3
3
  ConfigError,
4
4
  depthError,
5
5
  guardStackDepth
6
- } from "./chunk-WTXULUZS.js";
6
+ } from "./chunk-GOXMKYWS.js";
7
7
 
8
8
  // src/value-factory.ts
9
9
  function fromMap(values, originDescription) {
package/dist/index.cjs CHANGED
@@ -8,11 +8,11 @@
8
8
 
9
9
 
10
10
 
11
- var _chunkIMJ7QHIKcjs = require('./chunk-IMJ7QHIK.cjs');
11
+ var _chunkOUPKYWL3cjs = require('./chunk-OUPKYWL3.cjs');
12
12
 
13
13
 
14
14
 
15
- var _chunkGBNQADHTcjs = require('./chunk-GBNQADHT.cjs');
15
+ var _chunkPCAMDXJ4cjs = require('./chunk-PCAMDXJ4.cjs');
16
16
 
17
17
 
18
18
 
@@ -22,17 +22,17 @@ var _chunkGBNQADHTcjs = require('./chunk-GBNQADHT.cjs');
22
22
 
23
23
 
24
24
 
25
- var _chunkRO4D6BH7cjs = require('./chunk-RO4D6BH7.cjs');
25
+ var _chunkL3D2RE2Ucjs = require('./chunk-L3D2RE2U.cjs');
26
26
 
27
27
  // src/value.ts
28
28
  function asString(v) {
29
29
  return v.kind === "scalar" && v.valueType === "string" ? v.raw : void 0;
30
30
  }
31
31
  function asNumber(v) {
32
- return v.kind === "scalar" ? _chunkRO4D6BH7cjs.coerceNumber.call(void 0, v.raw) : void 0;
32
+ return v.kind === "scalar" ? _chunkL3D2RE2Ucjs.coerceNumber.call(void 0, v.raw) : void 0;
33
33
  }
34
34
  function asBoolean(v) {
35
- return v.kind === "scalar" ? _chunkRO4D6BH7cjs.coerceBoolean.call(void 0, v.raw) : void 0;
35
+ return v.kind === "scalar" ? _chunkL3D2RE2Ucjs.coerceBoolean.call(void 0, v.raw) : void 0;
36
36
  }
37
37
  function asObject(v) {
38
38
  return v.kind === "object" ? v.fields : void 0;
@@ -79,4 +79,4 @@ function isNull(v) {
79
79
 
80
80
 
81
81
 
82
- exports.Config = _chunkRO4D6BH7cjs.Config; exports.ConfigError = _chunkRO4D6BH7cjs.ConfigError; exports.NotResolvedError = _chunkRO4D6BH7cjs.NotResolvedError; exports.PackageLookupError = _chunkRO4D6BH7cjs.PackageLookupError; exports.ParseError = _chunkRO4D6BH7cjs.ParseError; exports.ResolveError = _chunkRO4D6BH7cjs.ResolveError; exports.asArray = asArray; exports.asBoolean = asBoolean; exports.asNumber = asNumber; exports.asObject = asObject; exports.asString = asString; exports.defaultParseOptions = _chunkIMJ7QHIKcjs.defaultParseOptions; exports.defaultResolveOptions = _chunkIMJ7QHIKcjs.defaultResolveOptions; exports.empty = _chunkGBNQADHTcjs.empty; exports.fromMap = _chunkGBNQADHTcjs.fromMap; exports.isArray = isArray; exports.isNull = isNull; exports.isObject = isObject; exports.isScalar = isScalar; exports.parse = _chunkIMJ7QHIKcjs.parse; exports.parseAsync = _chunkIMJ7QHIKcjs.parseAsync; exports.parseFile = _chunkIMJ7QHIKcjs.parseFile; exports.parseFileAsync = _chunkIMJ7QHIKcjs.parseFileAsync; exports.parseFileWithOptions = _chunkIMJ7QHIKcjs.parseFileWithOptions; exports.parseString = _chunkIMJ7QHIKcjs.parseString; exports.parseStringWithOptions = _chunkIMJ7QHIKcjs.parseStringWithOptions;
82
+ exports.Config = _chunkL3D2RE2Ucjs.Config; exports.ConfigError = _chunkL3D2RE2Ucjs.ConfigError; exports.NotResolvedError = _chunkL3D2RE2Ucjs.NotResolvedError; exports.PackageLookupError = _chunkL3D2RE2Ucjs.PackageLookupError; exports.ParseError = _chunkL3D2RE2Ucjs.ParseError; exports.ResolveError = _chunkL3D2RE2Ucjs.ResolveError; exports.asArray = asArray; exports.asBoolean = asBoolean; exports.asNumber = asNumber; exports.asObject = asObject; exports.asString = asString; exports.defaultParseOptions = _chunkOUPKYWL3cjs.defaultParseOptions; exports.defaultResolveOptions = _chunkOUPKYWL3cjs.defaultResolveOptions; exports.empty = _chunkPCAMDXJ4cjs.empty; exports.fromMap = _chunkPCAMDXJ4cjs.fromMap; exports.isArray = isArray; exports.isNull = isNull; exports.isObject = isObject; exports.isScalar = isScalar; exports.parse = _chunkOUPKYWL3cjs.parse; exports.parseAsync = _chunkOUPKYWL3cjs.parseAsync; exports.parseFile = _chunkOUPKYWL3cjs.parseFile; exports.parseFileAsync = _chunkOUPKYWL3cjs.parseFileAsync; exports.parseFileWithOptions = _chunkOUPKYWL3cjs.parseFileWithOptions; exports.parseString = _chunkOUPKYWL3cjs.parseString; exports.parseStringWithOptions = _chunkOUPKYWL3cjs.parseStringWithOptions;
package/dist/index.js CHANGED
@@ -8,11 +8,11 @@ import {
8
8
  parseFileWithOptions,
9
9
  parseString,
10
10
  parseStringWithOptions
11
- } from "./chunk-XWKO4JM3.js";
11
+ } from "./chunk-JVABBNF5.js";
12
12
  import {
13
13
  empty,
14
14
  fromMap
15
- } from "./chunk-QY7ECGS6.js";
15
+ } from "./chunk-YRNDXYGQ.js";
16
16
  import {
17
17
  Config,
18
18
  ConfigError,
@@ -22,7 +22,7 @@ import {
22
22
  ResolveError,
23
23
  coerceBoolean,
24
24
  coerceNumber
25
- } from "./chunk-WTXULUZS.js";
25
+ } from "./chunk-GOXMKYWS.js";
26
26
 
27
27
  // src/value.ts
28
28
  function asString(v) {
package/dist/zod.cjs CHANGED
@@ -1,57 +1,67 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
2
 
3
3
 
4
- var _chunkIMJ7QHIKcjs = require('./chunk-IMJ7QHIK.cjs');
4
+ var _chunkOUPKYWL3cjs = require('./chunk-OUPKYWL3.cjs');
5
5
 
6
6
 
7
7
 
8
8
 
9
9
 
10
10
 
11
- var _chunkRO4D6BH7cjs = require('./chunk-RO4D6BH7.cjs');
11
+ var _chunkL3D2RE2Ucjs = require('./chunk-L3D2RE2U.cjs');
12
12
 
13
13
  // src/zod.ts
14
14
  var _zod2 = require('zod');
15
15
  function validate(config, schema) {
16
16
  requireResolved(config, "");
17
- const plain = config.toObject();
18
- refusePrototypeKeys(plain);
19
- const coerced = coerceValue(plain, schema);
20
- return schema.parse(coerced);
17
+ return parseValue(config.toObject(), schema);
21
18
  }
22
19
  function getValidated(config, path, schema) {
23
20
  requireResolved(config, path);
24
- const val = config.get(path);
25
- refusePrototypeKeys(val);
26
- const coerced = coerceValue(val, schema);
27
- return schema.parse(coerced);
21
+ return parseValue(config.get(path), schema);
22
+ }
23
+ function parseValue(value, schema) {
24
+ const refused = [];
25
+ const coerced = coerceValue(dropPrototypeKeys(value, [], refused), schema);
26
+ if (refused.length === 0) return schema.parse(coerced);
27
+ let result;
28
+ try {
29
+ result = schema.safeParse(coerced);
30
+ } catch (err) {
31
+ if (err instanceof _zod2.z.core.$ZodAsyncError || _optionalChain([err, 'optionalAccess', _ => _.constructor, 'optionalAccess', _2 => _2.name]) === "$ZodAsyncError") {
32
+ throw new _zod2.z.ZodError(refused);
33
+ }
34
+ throw err;
35
+ }
36
+ if (result.success) throw new _zod2.z.ZodError(refused);
37
+ throw new _zod2.z.ZodError([...refused, ...withoutPrototypeKeyIssues(result.error.issues)]);
38
+ }
39
+ function withoutPrototypeKeyIssues(issues) {
40
+ return issues.filter((issue) => !issue.path.some((seg) => typeof seg === "string" && PROTOTYPE_KEYS.has(seg))).map(
41
+ (issue) => issue.code === "invalid_union" ? { ...issue, errors: issue.errors.map(withoutPrototypeKeyIssues) } : issue
42
+ );
28
43
  }
29
44
  function requireResolved(config, path) {
30
45
  config.getValue(path);
31
46
  }
32
47
  var PROTOTYPE_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
33
- function refusePrototypeKeys(value) {
34
- const issues = [];
35
- collectPrototypeKeys(value, [], issues);
36
- if (issues.length > 0) throw new _zod2.z.ZodError(issues);
37
- }
38
- function collectPrototypeKeys(value, path, issues) {
39
- if (Array.isArray(value)) {
40
- value.forEach((item, i) => collectPrototypeKeys(item, [...path, i], issues));
41
- } else if (typeof value === "object" && value !== null) {
42
- for (const [key, item] of Object.entries(value)) {
43
- if (PROTOTYPE_KEYS.has(key)) {
44
- issues.push({
45
- code: "custom",
46
- path,
47
- message: `key ${JSON.stringify(key)} names an object's prototype`,
48
- params: { key }
49
- });
50
- } else {
51
- collectPrototypeKeys(item, [...path, key], issues);
52
- }
48
+ function dropPrototypeKeys(value, path, issues) {
49
+ if (Array.isArray(value)) return value.map((item, i) => dropPrototypeKeys(item, [...path, i], issues));
50
+ if (typeof value !== "object" || value === null) return value;
51
+ const kept = {};
52
+ for (const [key, item] of Object.entries(value)) {
53
+ if (PROTOTYPE_KEYS.has(key)) {
54
+ issues.push({
55
+ code: "custom",
56
+ path,
57
+ message: `key ${JSON.stringify(key)} names an object's prototype`,
58
+ params: { key }
59
+ });
60
+ } else {
61
+ kept[key] = dropPrototypeKeys(item, [...path, key], issues);
53
62
  }
54
63
  }
64
+ return kept;
55
65
  }
56
66
  function unitMarker(what, read) {
57
67
  return _zod2.z.transform((input, ctx) => {
@@ -62,18 +72,23 @@ function unitMarker(what, read) {
62
72
  const text = typeof input === "string" ? input : String(input);
63
73
  const reading = read(text);
64
74
  if (reading.ok) return reading.value;
65
- ctx.issues.push({ code: "custom", message: unitProblemMessage(what, reading.problem, text), input });
75
+ ctx.issues.push({
76
+ code: "custom",
77
+ message: unitProblemMessage(what, reading.problem),
78
+ input,
79
+ params: { problem: reading.problem }
80
+ });
66
81
  return _zod2.z.NEVER;
67
82
  });
68
83
  }
69
- function unitProblemMessage(what, problem, text) {
84
+ function unitProblemMessage(what, problem) {
70
85
  switch (problem) {
71
86
  case "invalid":
72
- return `invalid ${what}: ${JSON.stringify(text)}`;
87
+ return `not a ${what}`;
73
88
  case "negative":
74
- return `${what} must be non-negative: ${JSON.stringify(text)}`;
89
+ return `${what} must be non-negative`;
75
90
  case "overflow":
76
- return `${what} overflows the representable range: ${JSON.stringify(text)}`;
91
+ return `${what} overflows the representable range`;
77
92
  }
78
93
  }
79
94
  function describeInput(input) {
@@ -90,7 +105,7 @@ var hocon = {
90
105
  * 1000.0000000000001 through the getter, 1000 here).
91
106
  */
92
107
  duration(unit) {
93
- return unitMarker("duration", (text) => _chunkRO4D6BH7cjs.readDuration.call(void 0, text, unit));
108
+ return unitMarker("duration", (text) => _chunkL3D2RE2Ucjs.readDuration.call(void 0, text, unit));
94
109
  },
95
110
  /**
96
111
  * A byte size, read as {@link Config.getBytes} reads it (default unit `B`),
@@ -100,7 +115,7 @@ var hocon = {
100
115
  * the getter, 2 here).
101
116
  */
102
117
  bytes(unit) {
103
- return unitMarker("byte size", (text) => _chunkRO4D6BH7cjs.readBytes.call(void 0, text, unit));
118
+ return unitMarker("byte size", (text) => _chunkL3D2RE2Ucjs.readBytes.call(void 0, text, unit));
104
119
  },
105
120
  /**
106
121
  * A calendar period, read as {@link Config.getPeriod} reads it (a bare
@@ -109,30 +124,36 @@ var hocon = {
109
124
  * the marker reads the JS number.
110
125
  */
111
126
  period() {
112
- return unitMarker("period", _chunkRO4D6BH7cjs.readPeriod);
127
+ return unitMarker("period", _chunkL3D2RE2Ucjs.readPeriod);
113
128
  }
114
129
  };
115
130
  function getDefType(schema) {
116
131
  const zod = schema._zod;
117
- return _optionalChain([zod, 'optionalAccess', _ => _.def, 'optionalAccess', _2 => _2.type]);
132
+ return _optionalChain([zod, 'optionalAccess', _3 => _3.def, 'optionalAccess', _4 => _4.type]);
133
+ }
134
+ function getDef(schema) {
135
+ return _optionalChain([schema, 'access', _5 => _5._zod, 'optionalAccess', _6 => _6.def]);
118
136
  }
119
137
  function getInnerSchema(schema) {
120
- const def = _optionalChain([schema, 'access', _3 => _3._zod, 'optionalAccess', _4 => _4.def]);
121
- return _nullishCoalesce(_optionalChain([def, 'optionalAccess', _5 => _5.innerType]), () => ( _optionalChain([def, 'optionalAccess', _6 => _6.schema])));
138
+ const def = _optionalChain([schema, 'access', _7 => _7._zod, 'optionalAccess', _8 => _8.def]);
139
+ return _nullishCoalesce(_optionalChain([def, 'optionalAccess', _9 => _9.innerType]), () => ( _optionalChain([def, 'optionalAccess', _10 => _10.schema])));
122
140
  }
123
141
  function getObjectShape(schema) {
124
- const shape = _optionalChain([schema, 'access', _7 => _7._zod, 'optionalAccess', _8 => _8.def, 'optionalAccess', _9 => _9.shape]);
142
+ const shape = _optionalChain([schema, 'access', _11 => _11._zod, 'optionalAccess', _12 => _12.def, 'optionalAccess', _13 => _13.shape]);
125
143
  return typeof shape === "object" && shape !== null ? shape : void 0;
126
144
  }
145
+ function getPropValues(schema) {
146
+ return _optionalChain([schema, 'access', _14 => _14._zod, 'optionalAccess', _15 => _15.propValues]);
147
+ }
127
148
  function getArrayElement(schema) {
128
- return _optionalChain([schema, 'access', _10 => _10._zod, 'optionalAccess', _11 => _11.def, 'optionalAccess', _12 => _12.element]);
149
+ return _optionalChain([schema, 'access', _16 => _16._zod, 'optionalAccess', _17 => _17.def, 'optionalAccess', _18 => _18.element]);
129
150
  }
130
151
  function parseWithSchema(input, schema, opts) {
131
- const config = _chunkIMJ7QHIKcjs.parse.call(void 0, input, opts);
152
+ const config = _chunkOUPKYWL3cjs.parse.call(void 0, input, opts);
132
153
  return validate(config, schema);
133
154
  }
134
155
  function parseFileWithSchema(filePath, schema, opts) {
135
- const config = _chunkIMJ7QHIKcjs.parseFile.call(void 0, filePath, opts);
156
+ const config = _chunkOUPKYWL3cjs.parseFile.call(void 0, filePath, opts);
136
157
  return validate(config, schema);
137
158
  }
138
159
  function coerceValue(value, schema) {
@@ -142,16 +163,24 @@ function coerceValue(value, schema) {
142
163
  switch (defType) {
143
164
  case "boolean":
144
165
  if (typeof value === "string") {
145
- const coerced = _chunkRO4D6BH7cjs.coerceBoolean.call(void 0, value);
166
+ const coerced = _chunkL3D2RE2Ucjs.coerceBoolean.call(void 0, value);
146
167
  return coerced !== void 0 ? coerced : value;
147
168
  }
148
169
  return value;
149
170
  case "number":
150
171
  if (typeof value === "string") {
151
- const coerced = _chunkRO4D6BH7cjs.coerceNumber.call(void 0, value);
172
+ const coerced = _chunkL3D2RE2Ucjs.coerceNumber.call(void 0, value);
152
173
  return coerced !== void 0 ? coerced : value;
153
174
  }
154
175
  return value;
176
+ case "enum": {
177
+ const values = schema._zod.values;
178
+ return coerceToLiteral(value, values);
179
+ }
180
+ case "literal": {
181
+ const values = _optionalChain([getDef, 'call', _19 => _19(schema), 'optionalAccess', _20 => _20.values]);
182
+ return values ? coerceToLiteral(value, new Set(values)) : value;
183
+ }
155
184
  case "object": {
156
185
  if (typeof value !== "object" || Array.isArray(value)) return value;
157
186
  const shape = getObjectShape(schema);
@@ -163,17 +192,63 @@ function coerceValue(value, schema) {
163
192
  result[key] = coerceValue(obj[key], shape[key]);
164
193
  }
165
194
  }
195
+ const catchall = _optionalChain([getDef, 'call', _21 => _21(schema), 'optionalAccess', _22 => _22.catchall]);
196
+ if (catchall) {
197
+ for (const key of Object.keys(obj)) {
198
+ if (!Object.prototype.hasOwnProperty.call(shape, key)) result[key] = coerceValue(obj[key], catchall);
199
+ }
200
+ }
166
201
  return result;
167
202
  }
203
+ case "intersection": {
204
+ if (accepts(schema, value)) return value;
205
+ const def = getDef(schema);
206
+ const forLeft = coerceValue(value, def.left);
207
+ if (accepts(schema, forLeft)) return forLeft;
208
+ const forRight = coerceValue(value, def.right);
209
+ if (accepts(schema, forRight)) return forRight;
210
+ return coerceValue(forLeft, def.right);
211
+ }
168
212
  case "array": {
169
213
  if (!Array.isArray(value)) return value;
170
214
  const elementSchema = getArrayElement(schema);
171
215
  if (!elementSchema) return value;
172
216
  return value.map((item) => coerceValue(item, elementSchema));
173
217
  }
218
+ case "record": {
219
+ if (typeof value !== "object" || Array.isArray(value)) return value;
220
+ const valueSchema = _optionalChain([getDef, 'call', _23 => _23(schema), 'optionalAccess', _24 => _24.valueType]);
221
+ if (!valueSchema) return value;
222
+ const result = {};
223
+ for (const [key, item] of Object.entries(value)) result[key] = coerceValue(item, valueSchema);
224
+ return result;
225
+ }
226
+ case "tuple": {
227
+ if (!Array.isArray(value)) return value;
228
+ const def = getDef(schema);
229
+ const items = _nullishCoalesce(_optionalChain([def, 'optionalAccess', _25 => _25.items]), () => ( []));
230
+ const rest = _optionalChain([def, 'optionalAccess', _26 => _26.rest]);
231
+ return value.map((item, i) => {
232
+ const itemSchema = i < items.length ? items[i] : rest;
233
+ return itemSchema ? coerceValue(item, itemSchema) : item;
234
+ });
235
+ }
236
+ case "union":
237
+ return coerceUnion(value, schema);
238
+ case "pipe": {
239
+ const input = _optionalChain([getDef, 'call', _27 => _27(schema), 'optionalAccess', _28 => _28.in]);
240
+ return input ? coerceValue(value, input) : value;
241
+ }
242
+ case "lazy": {
243
+ const getter = _optionalChain([getDef, 'call', _29 => _29(schema), 'optionalAccess', _30 => _30.getter]);
244
+ return getter ? coerceValue(value, getter()) : value;
245
+ }
174
246
  case "optional":
175
247
  case "nullable":
176
248
  case "default":
249
+ case "prefault":
250
+ case "nonoptional":
251
+ case "success":
177
252
  case "catch":
178
253
  case "readonly": {
179
254
  const inner = getInnerSchema(schema);
@@ -183,6 +258,42 @@ function coerceValue(value, schema) {
183
258
  return value;
184
259
  }
185
260
  }
261
+ function coerceUnion(value, schema) {
262
+ const def = getDef(schema);
263
+ const options = _nullishCoalesce(_optionalChain([def, 'optionalAccess', _31 => _31.options]), () => ( []));
264
+ const discriminator = _optionalChain([def, 'optionalAccess', _32 => _32.discriminator]);
265
+ if (discriminator !== void 0) {
266
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return value;
267
+ const tag = Object.prototype.hasOwnProperty.call(value, discriminator) ? value[discriminator] : void 0;
268
+ const tagsOf = (o) => _optionalChain([getPropValues, 'call', _33 => _33(o), 'optionalAccess', _34 => _34[discriminator]]);
269
+ const option = _nullishCoalesce(options.find((o) => _optionalChain([tagsOf, 'call', _35 => _35(o), 'optionalAccess', _36 => _36.has, 'call', _37 => _37(tag)])), () => ( options.find((o) => {
270
+ const tags = tagsOf(o);
271
+ return tags !== void 0 && tags.has(coerceToLiteral(tag, tags));
272
+ })));
273
+ return option ? coerceValue(value, option) : value;
274
+ }
275
+ if (options.some((option) => accepts(option, value))) return value;
276
+ for (const option of options) {
277
+ const coerced = coerceValue(value, option);
278
+ if (accepts(option, coerced)) return coerced;
279
+ }
280
+ return value;
281
+ }
282
+ function coerceToLiteral(value, literals) {
283
+ if (typeof value !== "string" || literals.has(value)) return value;
284
+ const n = _chunkL3D2RE2Ucjs.coerceNumber.call(void 0, value);
285
+ if (n !== void 0 && literals.has(n)) return n;
286
+ const b = _chunkL3D2RE2Ucjs.coerceBoolean.call(void 0, value);
287
+ if (b !== void 0 && literals.has(b)) return b;
288
+ return value;
289
+ }
290
+ function accepts(schema, value) {
291
+ try {
292
+ return schema.safeParse(value).success;
293
+ } catch (e) {
294
+ return false;
295
+ }
296
+ }
186
297
 
187
298
 
188
299
 
package/dist/zod.d.cts CHANGED
@@ -4,13 +4,17 @@ import { C as Config, D as DurationUnit, B as ByteUnit, b as Period, a as ParseO
4
4
  /**
5
5
  * The whole config, validated against `schema`. HOCON strings are coerced
6
6
  * where the schema expects a number or a boolean (`"8080"` → 8080), through
7
- * objects, arrays and optional / nullable / default / catch / readonly
8
- * wrappers (not unions, records, tuples or pipes); unit values go through the
9
- * {@link hocon} markers.
7
+ * objects (and their catchall), arrays, records, tuples, unions,
8
+ * intersections, pipes (by their input schema), lazy schemas, numeric literals
9
+ * and enums, and optional / nullable / default / prefault / nonoptional /
10
+ * success / catch / readonly wrappers; a union coerces by the option that
11
+ * takes the value (see the README). Unit values go through the {@link hocon}
12
+ * markers.
10
13
  *
11
14
  * An object key named `__proto__`, `constructor` or `prototype` anywhere in
12
15
  * the value is refused with a `ZodError` issue at the object that holds it
13
- * (#202); read such a key with the plain getters instead.
16
+ * (#202), reported together with the schema's issues for the rest of the value
17
+ * (#227); read such a key with the plain getters instead.
14
18
  *
15
19
  * @throws {@link NotResolvedError} when the config still holds an unresolved
16
20
  * substitution (#210); call `resolve()` first.
@@ -23,7 +27,8 @@ declare function validate<T>(config: Config, schema: ZodType<T>): T;
23
27
  *
24
28
  * An object key named `__proto__`, `constructor` or `prototype` anywhere in
25
29
  * the value is refused with a `ZodError` issue at the object that holds it
26
- * (#202); read such a key with the plain getters instead.
30
+ * (#202), reported together with the schema's issues for the rest of the value
31
+ * (#227); read such a key with the plain getters instead.
27
32
  *
28
33
  * @throws {@link NotResolvedError} when the value at `path` still holds an
29
34
  * unresolved substitution (#210); a resolved subtree of an otherwise
package/dist/zod.d.ts CHANGED
@@ -4,13 +4,17 @@ import { C as Config, D as DurationUnit, B as ByteUnit, b as Period, a as ParseO
4
4
  /**
5
5
  * The whole config, validated against `schema`. HOCON strings are coerced
6
6
  * where the schema expects a number or a boolean (`"8080"` → 8080), through
7
- * objects, arrays and optional / nullable / default / catch / readonly
8
- * wrappers (not unions, records, tuples or pipes); unit values go through the
9
- * {@link hocon} markers.
7
+ * objects (and their catchall), arrays, records, tuples, unions,
8
+ * intersections, pipes (by their input schema), lazy schemas, numeric literals
9
+ * and enums, and optional / nullable / default / prefault / nonoptional /
10
+ * success / catch / readonly wrappers; a union coerces by the option that
11
+ * takes the value (see the README). Unit values go through the {@link hocon}
12
+ * markers.
10
13
  *
11
14
  * An object key named `__proto__`, `constructor` or `prototype` anywhere in
12
15
  * the value is refused with a `ZodError` issue at the object that holds it
13
- * (#202); read such a key with the plain getters instead.
16
+ * (#202), reported together with the schema's issues for the rest of the value
17
+ * (#227); read such a key with the plain getters instead.
14
18
  *
15
19
  * @throws {@link NotResolvedError} when the config still holds an unresolved
16
20
  * substitution (#210); call `resolve()` first.
@@ -23,7 +27,8 @@ declare function validate<T>(config: Config, schema: ZodType<T>): T;
23
27
  *
24
28
  * An object key named `__proto__`, `constructor` or `prototype` anywhere in
25
29
  * the value is refused with a `ZodError` issue at the object that holds it
26
- * (#202); read such a key with the plain getters instead.
30
+ * (#202), reported together with the schema's issues for the rest of the value
31
+ * (#227); read such a key with the plain getters instead.
27
32
  *
28
33
  * @throws {@link NotResolvedError} when the value at `path` still holds an
29
34
  * unresolved substitution (#210); a resolved subtree of an otherwise