@o3co/ts.hocon 1.14.0 → 1.14.1

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,64 @@
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 (e) {
31
+ throw new _zod2.z.ZodError(refused);
32
+ }
33
+ if (result.success) throw new _zod2.z.ZodError(refused);
34
+ throw new _zod2.z.ZodError([...refused, ...withoutPrototypeKeyIssues(result.error.issues)]);
35
+ }
36
+ function withoutPrototypeKeyIssues(issues) {
37
+ return issues.filter((issue) => !issue.path.some((seg) => typeof seg === "string" && PROTOTYPE_KEYS.has(seg))).map(
38
+ (issue) => issue.code === "invalid_union" ? { ...issue, errors: issue.errors.map(withoutPrototypeKeyIssues) } : issue
39
+ );
28
40
  }
29
41
  function requireResolved(config, path) {
30
42
  config.getValue(path);
31
43
  }
32
44
  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
- }
45
+ function dropPrototypeKeys(value, path, issues) {
46
+ if (Array.isArray(value)) return value.map((item, i) => dropPrototypeKeys(item, [...path, i], issues));
47
+ if (typeof value !== "object" || value === null) return value;
48
+ const kept = {};
49
+ for (const [key, item] of Object.entries(value)) {
50
+ if (PROTOTYPE_KEYS.has(key)) {
51
+ issues.push({
52
+ code: "custom",
53
+ path,
54
+ message: `key ${JSON.stringify(key)} names an object's prototype`,
55
+ params: { key }
56
+ });
57
+ } else {
58
+ kept[key] = dropPrototypeKeys(item, [...path, key], issues);
53
59
  }
54
60
  }
61
+ return kept;
55
62
  }
56
63
  function unitMarker(what, read) {
57
64
  return _zod2.z.transform((input, ctx) => {
@@ -62,18 +69,23 @@ function unitMarker(what, read) {
62
69
  const text = typeof input === "string" ? input : String(input);
63
70
  const reading = read(text);
64
71
  if (reading.ok) return reading.value;
65
- ctx.issues.push({ code: "custom", message: unitProblemMessage(what, reading.problem, text), input });
72
+ ctx.issues.push({
73
+ code: "custom",
74
+ message: unitProblemMessage(what, reading.problem),
75
+ input,
76
+ params: { problem: reading.problem }
77
+ });
66
78
  return _zod2.z.NEVER;
67
79
  });
68
80
  }
69
- function unitProblemMessage(what, problem, text) {
81
+ function unitProblemMessage(what, problem) {
70
82
  switch (problem) {
71
83
  case "invalid":
72
- return `invalid ${what}: ${JSON.stringify(text)}`;
84
+ return `not a ${what}`;
73
85
  case "negative":
74
- return `${what} must be non-negative: ${JSON.stringify(text)}`;
86
+ return `${what} must be non-negative`;
75
87
  case "overflow":
76
- return `${what} overflows the representable range: ${JSON.stringify(text)}`;
88
+ return `${what} overflows the representable range`;
77
89
  }
78
90
  }
79
91
  function describeInput(input) {
@@ -90,7 +102,7 @@ var hocon = {
90
102
  * 1000.0000000000001 through the getter, 1000 here).
91
103
  */
92
104
  duration(unit) {
93
- return unitMarker("duration", (text) => _chunkRO4D6BH7cjs.readDuration.call(void 0, text, unit));
105
+ return unitMarker("duration", (text) => _chunkL3D2RE2Ucjs.readDuration.call(void 0, text, unit));
94
106
  },
95
107
  /**
96
108
  * A byte size, read as {@link Config.getBytes} reads it (default unit `B`),
@@ -100,7 +112,7 @@ var hocon = {
100
112
  * the getter, 2 here).
101
113
  */
102
114
  bytes(unit) {
103
- return unitMarker("byte size", (text) => _chunkRO4D6BH7cjs.readBytes.call(void 0, text, unit));
115
+ return unitMarker("byte size", (text) => _chunkL3D2RE2Ucjs.readBytes.call(void 0, text, unit));
104
116
  },
105
117
  /**
106
118
  * A calendar period, read as {@link Config.getPeriod} reads it (a bare
@@ -109,30 +121,36 @@ var hocon = {
109
121
  * the marker reads the JS number.
110
122
  */
111
123
  period() {
112
- return unitMarker("period", _chunkRO4D6BH7cjs.readPeriod);
124
+ return unitMarker("period", _chunkL3D2RE2Ucjs.readPeriod);
113
125
  }
114
126
  };
115
127
  function getDefType(schema) {
116
128
  const zod = schema._zod;
117
129
  return _optionalChain([zod, 'optionalAccess', _ => _.def, 'optionalAccess', _2 => _2.type]);
118
130
  }
131
+ function getDef(schema) {
132
+ return _optionalChain([schema, 'access', _3 => _3._zod, 'optionalAccess', _4 => _4.def]);
133
+ }
119
134
  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])));
135
+ const def = _optionalChain([schema, 'access', _5 => _5._zod, 'optionalAccess', _6 => _6.def]);
136
+ return _nullishCoalesce(_optionalChain([def, 'optionalAccess', _7 => _7.innerType]), () => ( _optionalChain([def, 'optionalAccess', _8 => _8.schema])));
122
137
  }
123
138
  function getObjectShape(schema) {
124
- const shape = _optionalChain([schema, 'access', _7 => _7._zod, 'optionalAccess', _8 => _8.def, 'optionalAccess', _9 => _9.shape]);
139
+ const shape = _optionalChain([schema, 'access', _9 => _9._zod, 'optionalAccess', _10 => _10.def, 'optionalAccess', _11 => _11.shape]);
125
140
  return typeof shape === "object" && shape !== null ? shape : void 0;
126
141
  }
142
+ function getPropValues(schema) {
143
+ return _optionalChain([schema, 'access', _12 => _12._zod, 'optionalAccess', _13 => _13.propValues]);
144
+ }
127
145
  function getArrayElement(schema) {
128
- return _optionalChain([schema, 'access', _10 => _10._zod, 'optionalAccess', _11 => _11.def, 'optionalAccess', _12 => _12.element]);
146
+ return _optionalChain([schema, 'access', _14 => _14._zod, 'optionalAccess', _15 => _15.def, 'optionalAccess', _16 => _16.element]);
129
147
  }
130
148
  function parseWithSchema(input, schema, opts) {
131
- const config = _chunkIMJ7QHIKcjs.parse.call(void 0, input, opts);
149
+ const config = _chunkOUPKYWL3cjs.parse.call(void 0, input, opts);
132
150
  return validate(config, schema);
133
151
  }
134
152
  function parseFileWithSchema(filePath, schema, opts) {
135
- const config = _chunkIMJ7QHIKcjs.parseFile.call(void 0, filePath, opts);
153
+ const config = _chunkOUPKYWL3cjs.parseFile.call(void 0, filePath, opts);
136
154
  return validate(config, schema);
137
155
  }
138
156
  function coerceValue(value, schema) {
@@ -142,16 +160,20 @@ function coerceValue(value, schema) {
142
160
  switch (defType) {
143
161
  case "boolean":
144
162
  if (typeof value === "string") {
145
- const coerced = _chunkRO4D6BH7cjs.coerceBoolean.call(void 0, value);
163
+ const coerced = _chunkL3D2RE2Ucjs.coerceBoolean.call(void 0, value);
146
164
  return coerced !== void 0 ? coerced : value;
147
165
  }
148
166
  return value;
149
167
  case "number":
150
168
  if (typeof value === "string") {
151
- const coerced = _chunkRO4D6BH7cjs.coerceNumber.call(void 0, value);
169
+ const coerced = _chunkL3D2RE2Ucjs.coerceNumber.call(void 0, value);
152
170
  return coerced !== void 0 ? coerced : value;
153
171
  }
154
172
  return value;
173
+ case "literal": {
174
+ const values = _optionalChain([getDef, 'call', _17 => _17(schema), 'optionalAccess', _18 => _18.values]);
175
+ return values ? coerceToLiteral(value, new Set(values)) : value;
176
+ }
155
177
  case "object": {
156
178
  if (typeof value !== "object" || Array.isArray(value)) return value;
157
179
  const shape = getObjectShape(schema);
@@ -171,9 +193,39 @@ function coerceValue(value, schema) {
171
193
  if (!elementSchema) return value;
172
194
  return value.map((item) => coerceValue(item, elementSchema));
173
195
  }
196
+ case "record": {
197
+ if (typeof value !== "object" || Array.isArray(value)) return value;
198
+ const valueSchema = _optionalChain([getDef, 'call', _19 => _19(schema), 'optionalAccess', _20 => _20.valueType]);
199
+ if (!valueSchema) return value;
200
+ const result = {};
201
+ for (const [key, item] of Object.entries(value)) result[key] = coerceValue(item, valueSchema);
202
+ return result;
203
+ }
204
+ case "tuple": {
205
+ if (!Array.isArray(value)) return value;
206
+ const def = getDef(schema);
207
+ const items = _nullishCoalesce(_optionalChain([def, 'optionalAccess', _21 => _21.items]), () => ( []));
208
+ const rest = _optionalChain([def, 'optionalAccess', _22 => _22.rest]);
209
+ return value.map((item, i) => {
210
+ const itemSchema = i < items.length ? items[i] : rest;
211
+ return itemSchema ? coerceValue(item, itemSchema) : item;
212
+ });
213
+ }
214
+ case "union":
215
+ return coerceUnion(value, schema);
216
+ case "pipe": {
217
+ const input = _optionalChain([getDef, 'call', _23 => _23(schema), 'optionalAccess', _24 => _24.in]);
218
+ return input ? coerceValue(value, input) : value;
219
+ }
220
+ case "lazy": {
221
+ const getter = _optionalChain([getDef, 'call', _25 => _25(schema), 'optionalAccess', _26 => _26.getter]);
222
+ return getter ? coerceValue(value, getter()) : value;
223
+ }
174
224
  case "optional":
175
225
  case "nullable":
176
226
  case "default":
227
+ case "prefault":
228
+ case "nonoptional":
177
229
  case "catch":
178
230
  case "readonly": {
179
231
  const inner = getInnerSchema(schema);
@@ -183,6 +235,42 @@ function coerceValue(value, schema) {
183
235
  return value;
184
236
  }
185
237
  }
238
+ function coerceUnion(value, schema) {
239
+ const def = getDef(schema);
240
+ const options = _nullishCoalesce(_optionalChain([def, 'optionalAccess', _27 => _27.options]), () => ( []));
241
+ const discriminator = _optionalChain([def, 'optionalAccess', _28 => _28.discriminator]);
242
+ if (discriminator !== void 0) {
243
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return value;
244
+ const tag = Object.prototype.hasOwnProperty.call(value, discriminator) ? value[discriminator] : void 0;
245
+ const tagsOf = (o) => _optionalChain([getPropValues, 'call', _29 => _29(o), 'optionalAccess', _30 => _30[discriminator]]);
246
+ const option = _nullishCoalesce(options.find((o) => _optionalChain([tagsOf, 'call', _31 => _31(o), 'optionalAccess', _32 => _32.has, 'call', _33 => _33(tag)])), () => ( options.find((o) => {
247
+ const tags = tagsOf(o);
248
+ return tags !== void 0 && tags.has(coerceToLiteral(tag, tags));
249
+ })));
250
+ return option ? coerceValue(value, option) : value;
251
+ }
252
+ if (options.some((option) => accepts(option, value))) return value;
253
+ for (const option of options) {
254
+ const coerced = coerceValue(value, option);
255
+ if (accepts(option, coerced)) return coerced;
256
+ }
257
+ return value;
258
+ }
259
+ function coerceToLiteral(value, literals) {
260
+ if (typeof value !== "string" || literals.has(value)) return value;
261
+ const n = _chunkL3D2RE2Ucjs.coerceNumber.call(void 0, value);
262
+ if (n !== void 0 && literals.has(n)) return n;
263
+ const b = _chunkL3D2RE2Ucjs.coerceBoolean.call(void 0, value);
264
+ if (b !== void 0 && literals.has(b)) return b;
265
+ return value;
266
+ }
267
+ function accepts(schema, value) {
268
+ try {
269
+ return schema.safeParse(value).success;
270
+ } catch (e2) {
271
+ return false;
272
+ }
273
+ }
186
274
 
187
275
 
188
276
 
package/dist/zod.d.cts CHANGED
@@ -4,13 +4,15 @@ 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, arrays, records, tuples, unions, pipes (by their input schema), lazy
8
+ * schemas and optional / nullable / default / prefault / nonoptional / catch /
9
+ * readonly wrappers; a union coerces by the option that takes the value (see
10
+ * the README). Unit values go through the {@link hocon} markers.
10
11
  *
11
12
  * An object key named `__proto__`, `constructor` or `prototype` anywhere in
12
13
  * 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.
14
+ * (#202), reported together with the schema's issues for the rest of the value
15
+ * (#227); read such a key with the plain getters instead.
14
16
  *
15
17
  * @throws {@link NotResolvedError} when the config still holds an unresolved
16
18
  * substitution (#210); call `resolve()` first.
@@ -23,7 +25,8 @@ declare function validate<T>(config: Config, schema: ZodType<T>): T;
23
25
  *
24
26
  * An object key named `__proto__`, `constructor` or `prototype` anywhere in
25
27
  * 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.
28
+ * (#202), reported together with the schema's issues for the rest of the value
29
+ * (#227); read such a key with the plain getters instead.
27
30
  *
28
31
  * @throws {@link NotResolvedError} when the value at `path` still holds an
29
32
  * unresolved substitution (#210); a resolved subtree of an otherwise
package/dist/zod.d.ts CHANGED
@@ -4,13 +4,15 @@ 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, arrays, records, tuples, unions, pipes (by their input schema), lazy
8
+ * schemas and optional / nullable / default / prefault / nonoptional / catch /
9
+ * readonly wrappers; a union coerces by the option that takes the value (see
10
+ * the README). Unit values go through the {@link hocon} markers.
10
11
  *
11
12
  * An object key named `__proto__`, `constructor` or `prototype` anywhere in
12
13
  * 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.
14
+ * (#202), reported together with the schema's issues for the rest of the value
15
+ * (#227); read such a key with the plain getters instead.
14
16
  *
15
17
  * @throws {@link NotResolvedError} when the config still holds an unresolved
16
18
  * substitution (#210); call `resolve()` first.
@@ -23,7 +25,8 @@ declare function validate<T>(config: Config, schema: ZodType<T>): T;
23
25
  *
24
26
  * An object key named `__proto__`, `constructor` or `prototype` anywhere in
25
27
  * 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.
28
+ * (#202), reported together with the schema's issues for the rest of the value
29
+ * (#227); read such a key with the plain getters instead.
27
30
  *
28
31
  * @throws {@link NotResolvedError} when the value at `path` still holds an
29
32
  * unresolved substitution (#210); a resolved subtree of an otherwise