@oxog/vld 2.1.0 → 2.2.7

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.
Files changed (228) hide show
  1. package/CHANGELOG.md +113 -0
  2. package/README.md +413 -1235
  3. package/dist/chunks/errors-core-CerMg2rq.js +1 -0
  4. package/dist/chunks/index-CkbfQQ4n.js +1 -0
  5. package/dist/chunks/index-yjpbvFgw.js +1 -0
  6. package/dist/chunks/ip-validation-g5LWgy9t.js +1 -0
  7. package/dist/chunks/security-Dy24T8N7.js +1 -0
  8. package/dist/cjs/chunks/errors-core-DrydF46s.cjs +1 -0
  9. package/dist/cjs/chunks/index-BEV9nG0N.cjs +1 -0
  10. package/dist/cjs/chunks/index-ZjHAxYdA.cjs +1 -0
  11. package/dist/cjs/chunks/ip-validation-BZqPamPC.cjs +1 -0
  12. package/dist/cjs/chunks/security-aXZVI7qW.cjs +1 -0
  13. package/dist/cjs/cli/bin.cjs +1 -505
  14. package/dist/cjs/cli/index.cjs +1 -209
  15. package/dist/cjs/codecs/index.cjs +1 -45
  16. package/dist/cjs/coercion/index.cjs +1 -588
  17. package/dist/cjs/compat/emitter.cjs +1 -184
  18. package/dist/cjs/compat/result.cjs +1 -189
  19. package/dist/cjs/errors.cjs +1 -180
  20. package/dist/cjs/index.cjs +1 -1894
  21. package/dist/cjs/kernel.cjs +1 -415
  22. package/dist/cjs/locales/af.cjs +1 -108
  23. package/dist/cjs/locales/ar.cjs +1 -108
  24. package/dist/cjs/locales/bn.cjs +1 -108
  25. package/dist/cjs/locales/da.cjs +1 -108
  26. package/dist/cjs/locales/de.cjs +1 -108
  27. package/dist/cjs/locales/en.cjs +1 -108
  28. package/dist/cjs/locales/es-MX.cjs +1 -107
  29. package/dist/cjs/locales/es.cjs +1 -96
  30. package/dist/cjs/locales/fi.cjs +1 -108
  31. package/dist/cjs/locales/fr.cjs +1 -96
  32. package/dist/cjs/locales/hi.cjs +1 -108
  33. package/dist/cjs/locales/id.cjs +1 -108
  34. package/dist/cjs/locales/index.cjs +1 -43
  35. package/dist/cjs/locales/it.cjs +1 -108
  36. package/dist/cjs/locales/ja.cjs +1 -108
  37. package/dist/cjs/locales/ko.cjs +1 -108
  38. package/dist/cjs/locales/lazy.cjs +1 -188
  39. package/dist/cjs/locales/nl.cjs +1 -108
  40. package/dist/cjs/locales/no.cjs +1 -108
  41. package/dist/cjs/locales/pl.cjs +1 -108
  42. package/dist/cjs/locales/pt-BR.cjs +1 -108
  43. package/dist/cjs/locales/pt.cjs +1 -108
  44. package/dist/cjs/locales/ru.cjs +1 -108
  45. package/dist/cjs/locales/runtime.cjs +1 -51
  46. package/dist/cjs/locales/sv.cjs +1 -108
  47. package/dist/cjs/locales/sw.cjs +1 -108
  48. package/dist/cjs/locales/th.cjs +1 -108
  49. package/dist/cjs/locales/tr.cjs +1 -108
  50. package/dist/cjs/locales/vi.cjs +1 -108
  51. package/dist/cjs/locales/zh.cjs +1 -108
  52. package/dist/cjs/mini.cjs +1 -242
  53. package/dist/cjs/pigment.cjs +1 -207
  54. package/dist/cjs/plugins/index.cjs +1 -12
  55. package/dist/cjs/registry.cjs +1 -37
  56. package/dist/cjs/v3/index.cjs +1 -425
  57. package/dist/cjs/v4/core/index.cjs +1 -894
  58. package/dist/cjs/v4/index.cjs +1 -425
  59. package/dist/cjs/v4/locales/index.cjs +1 -130
  60. package/dist/cjs/v4/mini/index.cjs +1 -509
  61. package/dist/cjs/v4-mini/index.cjs +1 -532
  62. package/dist/cjs/validators/any.cjs +1 -40
  63. package/dist/cjs/validators/array.cjs +1 -283
  64. package/dist/cjs/validators/base.cjs +1 -887
  65. package/dist/cjs/validators/base64.cjs +1 -73
  66. package/dist/cjs/validators/bigint.cjs +1 -232
  67. package/dist/cjs/validators/boolean.cjs +1 -146
  68. package/dist/cjs/validators/codec.cjs +1 -205
  69. package/dist/cjs/validators/custom.cjs +1 -71
  70. package/dist/cjs/validators/date.cjs +1 -278
  71. package/dist/cjs/validators/discriminated-union.cjs +1 -130
  72. package/dist/cjs/validators/enum.cjs +1 -154
  73. package/dist/cjs/validators/file.cjs +1 -130
  74. package/dist/cjs/validators/function.cjs +1 -63
  75. package/dist/cjs/validators/hex.cjs +1 -70
  76. package/dist/cjs/validators/index.cjs +1 -123
  77. package/dist/cjs/validators/intersection.cjs +1 -120
  78. package/dist/cjs/validators/json.cjs +1 -70
  79. package/dist/cjs/validators/lazy.cjs +1 -53
  80. package/dist/cjs/validators/literal.cjs +1 -64
  81. package/dist/cjs/validators/map.cjs +1 -218
  82. package/dist/cjs/validators/nan.cjs +1 -41
  83. package/dist/cjs/validators/never.cjs +1 -36
  84. package/dist/cjs/validators/null.cjs +1 -43
  85. package/dist/cjs/validators/number.cjs +1 -427
  86. package/dist/cjs/validators/object.cjs +1 -868
  87. package/dist/cjs/validators/promise.cjs +1 -115
  88. package/dist/cjs/validators/record.cjs +1 -262
  89. package/dist/cjs/validators/set.cjs +1 -203
  90. package/dist/cjs/validators/string-bool.cjs +1 -124
  91. package/dist/cjs/validators/string-formats.cjs +1 -168
  92. package/dist/cjs/validators/string.cjs +1 -12
  93. package/dist/cjs/validators/symbol.cjs +1 -62
  94. package/dist/cjs/validators/template-literal.cjs +1 -108
  95. package/dist/cjs/validators/tuple.cjs +1 -187
  96. package/dist/cjs/validators/uint8array.cjs +1 -106
  97. package/dist/cjs/validators/undefined.cjs +1 -43
  98. package/dist/cjs/validators/union.cjs +1 -191
  99. package/dist/cjs/validators/unknown.cjs +1 -40
  100. package/dist/cjs/validators/void.cjs +1 -48
  101. package/dist/cjs/validators/xor.cjs +1 -69
  102. package/dist/cli/bin.js +1 -406
  103. package/dist/cli/index.js +1 -205
  104. package/dist/codecs/index.js +1 -11
  105. package/dist/coercion/index.js +1 -579
  106. package/dist/compat/emitter.js +1 -180
  107. package/dist/compat/result.js +1 -170
  108. package/dist/errors-core.d.ts +32 -2
  109. package/dist/errors.js +1 -171
  110. package/dist/index.d.ts +315 -361
  111. package/dist/index.js +1 -1534
  112. package/dist/kernel.js +1 -409
  113. package/dist/locales/af.js +1 -106
  114. package/dist/locales/ar.js +1 -106
  115. package/dist/locales/bn.js +1 -106
  116. package/dist/locales/da.js +1 -106
  117. package/dist/locales/de.js +1 -106
  118. package/dist/locales/en.js +1 -106
  119. package/dist/locales/es-MX.js +1 -105
  120. package/dist/locales/es.js +1 -94
  121. package/dist/locales/fi.js +1 -106
  122. package/dist/locales/fr.js +1 -94
  123. package/dist/locales/hi.js +1 -106
  124. package/dist/locales/id.js +1 -106
  125. package/dist/locales/index.js +1 -29
  126. package/dist/locales/it.js +1 -106
  127. package/dist/locales/ja.js +1 -106
  128. package/dist/locales/ko.js +1 -106
  129. package/dist/locales/lazy.js +1 -177
  130. package/dist/locales/nl.js +1 -106
  131. package/dist/locales/no.js +1 -106
  132. package/dist/locales/pl.js +1 -106
  133. package/dist/locales/pt-BR.js +1 -106
  134. package/dist/locales/pt.js +1 -106
  135. package/dist/locales/ru.js +1 -106
  136. package/dist/locales/runtime.js +1 -41
  137. package/dist/locales/sv.js +1 -106
  138. package/dist/locales/sw.js +1 -106
  139. package/dist/locales/th.js +1 -106
  140. package/dist/locales/tr.js +1 -106
  141. package/dist/locales/vi.js +1 -106
  142. package/dist/locales/zh.js +1 -106
  143. package/dist/mini.d.ts +3 -3
  144. package/dist/mini.js +1 -156
  145. package/dist/pigment.js +1 -180
  146. package/dist/plugins/index.js +1 -1
  147. package/dist/registry.js +1 -34
  148. package/dist/utils/json-schema.d.ts +10 -1
  149. package/dist/v3/index.js +1 -46
  150. package/dist/v4/core/index.d.ts +28 -16
  151. package/dist/v4/core/index.js +1 -277
  152. package/dist/v4/index.js +1 -50
  153. package/dist/v4/locales/index.d.ts +4 -0
  154. package/dist/v4/locales/index.js +1 -77
  155. package/dist/v4/mini/index.js +1 -47
  156. package/dist/v4-mini/index.d.ts +1 -1
  157. package/dist/v4-mini/index.js +1 -75
  158. package/dist/validators/any.js +1 -37
  159. package/dist/validators/array.d.ts +7 -0
  160. package/dist/validators/array.js +1 -279
  161. package/dist/validators/base.d.ts +210 -17
  162. package/dist/validators/base.js +1 -868
  163. package/dist/validators/base64.js +1 -68
  164. package/dist/validators/bigint.d.ts +7 -0
  165. package/dist/validators/bigint.js +1 -228
  166. package/dist/validators/boolean.js +1 -142
  167. package/dist/validators/codec.js +1 -200
  168. package/dist/validators/custom.d.ts +20 -10
  169. package/dist/validators/custom.js +1 -67
  170. package/dist/validators/date.d.ts +2 -0
  171. package/dist/validators/date.js +1 -274
  172. package/dist/validators/discriminated-union.d.ts +4 -2
  173. package/dist/validators/discriminated-union.js +1 -123
  174. package/dist/validators/enum.d.ts +8 -2
  175. package/dist/validators/enum.js +1 -150
  176. package/dist/validators/file.js +1 -125
  177. package/dist/validators/function.js +1 -58
  178. package/dist/validators/hex.js +1 -65
  179. package/dist/validators/index.d.ts +1 -1
  180. package/dist/validators/index.js +1 -35
  181. package/dist/validators/intersection.js +1 -116
  182. package/dist/validators/json.js +1 -66
  183. package/dist/validators/lazy.js +1 -50
  184. package/dist/validators/literal.d.ts +8 -3
  185. package/dist/validators/literal.js +1 -60
  186. package/dist/validators/map.d.ts +7 -0
  187. package/dist/validators/map.js +1 -214
  188. package/dist/validators/nan.js +1 -38
  189. package/dist/validators/never.js +1 -32
  190. package/dist/validators/null.js +1 -40
  191. package/dist/validators/number.d.ts +25 -1
  192. package/dist/validators/number.js +1 -423
  193. package/dist/validators/object.d.ts +18 -2
  194. package/dist/validators/object.js +1 -864
  195. package/dist/validators/promise.d.ts +6 -7
  196. package/dist/validators/promise.js +1 -111
  197. package/dist/validators/record.d.ts +13 -8
  198. package/dist/validators/record.js +1 -258
  199. package/dist/validators/set.d.ts +5 -0
  200. package/dist/validators/set.js +1 -199
  201. package/dist/validators/string-bool.js +1 -120
  202. package/dist/validators/string-formats.d.ts +58 -62
  203. package/dist/validators/string-formats.js +1 -135
  204. package/dist/validators/string.d.ts +49 -10
  205. package/dist/validators/string.js +1 -4
  206. package/dist/validators/symbol.js +1 -58
  207. package/dist/validators/template-literal.js +1 -103
  208. package/dist/validators/tuple.d.ts +17 -13
  209. package/dist/validators/tuple.js +1 -183
  210. package/dist/validators/uint8array.js +1 -101
  211. package/dist/validators/undefined.js +1 -40
  212. package/dist/validators/union.d.ts +1 -0
  213. package/dist/validators/union.js +1 -187
  214. package/dist/validators/unknown.js +1 -37
  215. package/dist/validators/void.js +1 -44
  216. package/dist/validators/xor.d.ts +2 -2
  217. package/dist/validators/xor.js +1 -66
  218. package/package.json +240 -238
  219. package/dist/chunks/errors-core-BeiFUtZM.js +0 -81
  220. package/dist/chunks/index-0R2ntSud.js +0 -508
  221. package/dist/chunks/index-BvH403Yy.js +0 -88
  222. package/dist/chunks/security-B0Pd_vSY.js +0 -35
  223. package/dist/chunks/string-BJRQod3t.js +0 -530
  224. package/dist/cjs/chunks/errors-core-DWxLFs2f.cjs +0 -83
  225. package/dist/cjs/chunks/index-BkI37108.cjs +0 -538
  226. package/dist/cjs/chunks/index-Cc3bjUMX.cjs +0 -91
  227. package/dist/cjs/chunks/security-Dc3Jh13T.cjs +0 -37
  228. package/dist/cjs/chunks/string-BKZrc4hs.cjs +0 -533
@@ -1,868 +1 @@
1
- 'use strict';
2
-
3
- var validators_base = require('./base.cjs');
4
- var locales_runtime = require('../locales/runtime.cjs');
5
- var validators_enum = require('./enum.cjs');
6
- var security = require('../chunks/security-Dc3Jh13T.cjs');
7
- var errorsCore = require('../chunks/errors-core-DWxLFs2f.cjs');
8
- require('../registry.cjs');
9
- require('../locales/en.cjs');
10
-
11
- /**
12
- * Optimized immutable object validator with chainable methods
13
- * Features pre-computed keys and Set-based lookups for better performance
14
- */
15
- class VldObject extends validators_base.VldBase {
16
- createSafeParseError(message) {
17
- return new errorsCore.VldError([{ code: 'invalid_object', path: [], message }]);
18
- }
19
- /**
20
- * Private constructor to enforce immutability
21
- */
22
- constructor(config) {
23
- super(validators_base.VLD_VALIDATOR_TYPES.OBJECT);
24
- this._config = config;
25
- // Pre-compute shape keys for faster access
26
- this._shapeKeys = Object.keys(config.shape);
27
- this._shapeKeysSet = new Set(this._shapeKeys);
28
- this._validators = this._shapeKeys.map(k => this.tryGetFieldValidator(k));
29
- // Pre-compute validator types when possible. Getter-based recursive schemas
30
- // may reference the object being constructed, so unresolved getters fall
31
- // back to the generic path and are resolved at parse time.
32
- this._validatorTypes = this._shapeKeys.map((k, i) => this._validators[i]?.validatorType || this.getValidatorType(k));
33
- this._simpleFieldModes = this._validators.map((validator, i) => this.getSimpleFieldMode(validator, this._validatorTypes[i]));
34
- this._simpleFieldValues = this._validators.map((validator, i) => this._simpleFieldModes[i] === 'literal' ? validator.literal : undefined);
35
- this._canUseSimpleObjectFastPath =
36
- !config.strict &&
37
- !config.passthrough &&
38
- !config.catchall &&
39
- this._shapeKeys.length > 0 &&
40
- this._simpleFieldModes.every(mode => mode !== undefined);
41
- this._canUseSafeParseFastPath =
42
- this._validators.every(validator => validator instanceof validators_base.VldBase) &&
43
- (config.catchall === undefined || config.catchall instanceof validators_base.VldBase);
44
- }
45
- tryGetFieldValidator(key) {
46
- try {
47
- const descriptor = Object.getOwnPropertyDescriptor(this._config.shape, key);
48
- if (descriptor?.get) {
49
- return undefined;
50
- }
51
- return this.resolveFieldValidator(key);
52
- }
53
- catch {
54
- return undefined;
55
- }
56
- }
57
- resolveFieldValidator(key) {
58
- try {
59
- const validator = this._config.shape[key];
60
- if (!validator || typeof validator.safeParse !== 'function') {
61
- return undefined;
62
- }
63
- return validator;
64
- }
65
- catch {
66
- return undefined;
67
- }
68
- }
69
- getFieldValidator(key, index) {
70
- const cached = index === undefined ? undefined : this._validators[index];
71
- const validator = cached || this.resolveFieldValidator(key);
72
- if (!validator) {
73
- throw new Error(`Invalid validator for field "${key}"`);
74
- }
75
- return validator;
76
- }
77
- getValidatorType(key) {
78
- try {
79
- const validator = this._config.shape[key];
80
- return validator?.validatorType || validators_base.VLD_VALIDATOR_TYPES.UNKNOWN;
81
- }
82
- catch {
83
- return validators_base.VLD_VALIDATOR_TYPES.UNKNOWN;
84
- }
85
- }
86
- getSimpleFieldMode(validator, type) {
87
- if (!validator || validator.isSimple !== true) {
88
- return undefined;
89
- }
90
- switch (type) {
91
- case validators_base.VLD_VALIDATOR_TYPES.STRING:
92
- return 'string';
93
- case validators_base.VLD_VALIDATOR_TYPES.NUMBER:
94
- return 'number';
95
- case validators_base.VLD_VALIDATOR_TYPES.BOOLEAN:
96
- return 'boolean';
97
- case validators_base.VLD_VALIDATOR_TYPES.BIGINT:
98
- return 'bigint';
99
- case validators_base.VLD_VALIDATOR_TYPES.SYMBOL:
100
- return 'symbol';
101
- case validators_base.VLD_VALIDATOR_TYPES.NULL:
102
- return 'null';
103
- case validators_base.VLD_VALIDATOR_TYPES.UNDEFINED:
104
- case validators_base.VLD_VALIDATOR_TYPES.VOID:
105
- return 'undefinedValue';
106
- case validators_base.VLD_VALIDATOR_TYPES.LITERAL:
107
- return 'literal';
108
- case validators_base.VLD_VALIDATOR_TYPES.ANY:
109
- case validators_base.VLD_VALIDATOR_TYPES.UNKNOWN:
110
- return 'passthrough';
111
- default:
112
- return undefined;
113
- }
114
- }
115
- getSimpleFieldError(mode, expected, received) {
116
- switch (mode) {
117
- case 'string':
118
- return locales_runtime.getMessages().invalidString;
119
- case 'number':
120
- return locales_runtime.getMessages().invalidNumber;
121
- case 'boolean':
122
- return locales_runtime.getMessages().invalidBoolean;
123
- case 'bigint':
124
- return locales_runtime.getMessages().invalidBigint;
125
- case 'symbol':
126
- return locales_runtime.getMessages().invalidSymbol;
127
- case 'null':
128
- return `Expected null, received ${typeof received}`;
129
- case 'undefinedValue':
130
- return locales_runtime.getMessages().expectedUndefined;
131
- case 'literal':
132
- return locales_runtime.getMessages().literalExpected(JSON.stringify(expected), JSON.stringify(received));
133
- case 'passthrough':
134
- return locales_runtime.getMessages().invalidObject;
135
- default:
136
- return locales_runtime.getMessages().invalidObject;
137
- }
138
- }
139
- parseCheckedField(validator, type, value) {
140
- if (type === validators_base.VLD_VALIDATOR_TYPES.STRING && typeof value === 'string') {
141
- const parseKnownString = validator.parseKnownString;
142
- if (typeof parseKnownString === 'function') {
143
- return parseKnownString.call(validator, value);
144
- }
145
- }
146
- if (type === validators_base.VLD_VALIDATOR_TYPES.NUMBER && typeof value === 'number' && !isNaN(value)) {
147
- const parseKnownNumber = validator.parseKnownNumber;
148
- if (typeof parseKnownNumber === 'function') {
149
- return parseKnownNumber.call(validator, value);
150
- }
151
- }
152
- if (type === validators_base.VLD_VALIDATOR_TYPES.BOOLEAN && typeof value === 'boolean') {
153
- const parseKnownBoolean = validator.parseKnownBoolean;
154
- if (typeof parseKnownBoolean === 'function') {
155
- return parseKnownBoolean.call(validator, value);
156
- }
157
- }
158
- if (type === validators_base.VLD_VALIDATOR_TYPES.BIGINT && typeof value === 'bigint') {
159
- const parseKnownBigInt = validator.parseKnownBigInt;
160
- if (typeof parseKnownBigInt === 'function') {
161
- return parseKnownBigInt.call(validator, value);
162
- }
163
- }
164
- if (type === validators_base.VLD_VALIDATOR_TYPES.SYMBOL && typeof value === 'symbol') {
165
- const parseKnownSymbol = validator.parseKnownSymbol;
166
- if (typeof parseKnownSymbol === 'function') {
167
- return parseKnownSymbol.call(validator, value);
168
- }
169
- }
170
- if (type === validators_base.VLD_VALIDATOR_TYPES.FUNCTION && typeof value === 'function') {
171
- const parseKnownFunction = validator.parseKnownFunction;
172
- if (typeof parseKnownFunction === 'function') {
173
- return parseKnownFunction.call(validator, value);
174
- }
175
- }
176
- if (type === validators_base.VLD_VALIDATOR_TYPES.FILE &&
177
- typeof value === 'object' &&
178
- value !== null &&
179
- (('size' in value && 'type' in value) ||
180
- (typeof File !== 'undefined' && value instanceof File))) {
181
- const parseKnownFile = validator.parseKnownFile;
182
- if (typeof parseKnownFile === 'function') {
183
- return parseKnownFile.call(validator, value);
184
- }
185
- }
186
- if (type === validators_base.VLD_VALIDATOR_TYPES.DATE && value instanceof Date) {
187
- const parseKnownDate = validator.parseKnownDate;
188
- if (typeof parseKnownDate === 'function') {
189
- return parseKnownDate.call(validator, value);
190
- }
191
- }
192
- if (type === validators_base.VLD_VALIDATOR_TYPES.OBJECT &&
193
- typeof value === 'object' &&
194
- value !== null &&
195
- !Array.isArray(value)) {
196
- const parseKnownObject = validator.parseKnownObject;
197
- if (typeof parseKnownObject === 'function') {
198
- return parseKnownObject.call(validator, value);
199
- }
200
- }
201
- if (type === validators_base.VLD_VALIDATOR_TYPES.ARRAY && Array.isArray(value)) {
202
- const parseKnownArray = validator.parseKnownArray;
203
- if (typeof parseKnownArray === 'function') {
204
- return parseKnownArray.call(validator, value);
205
- }
206
- }
207
- if (type === validators_base.VLD_VALIDATOR_TYPES.TUPLE && Array.isArray(value)) {
208
- const parseKnownTuple = validator.parseKnownTuple;
209
- if (typeof parseKnownTuple === 'function') {
210
- return parseKnownTuple.call(validator, value);
211
- }
212
- }
213
- if (type === validators_base.VLD_VALIDATOR_TYPES.RECORD &&
214
- typeof value === 'object' &&
215
- value !== null &&
216
- !Array.isArray(value)) {
217
- const parseKnownRecord = validator.parseKnownRecord;
218
- if (typeof parseKnownRecord === 'function') {
219
- return parseKnownRecord.call(validator, value);
220
- }
221
- }
222
- if (type === validators_base.VLD_VALIDATOR_TYPES.SET && value instanceof Set) {
223
- const parseKnownSet = validator.parseKnownSet;
224
- if (typeof parseKnownSet === 'function') {
225
- return parseKnownSet.call(validator, value);
226
- }
227
- }
228
- if (type === validators_base.VLD_VALIDATOR_TYPES.MAP && value instanceof Map) {
229
- const parseKnownMap = validator.parseKnownMap;
230
- if (typeof parseKnownMap === 'function') {
231
- return parseKnownMap.call(validator, value);
232
- }
233
- }
234
- return validator.parse(value);
235
- }
236
- parseSimpleObjectValue(obj, trustedKey, skipTrustedLiteralCheck = false) {
237
- if (this._shapeKeys.length === 2 &&
238
- this._simpleFieldModes[0] === 'string' &&
239
- this._simpleFieldModes[1] === 'number') {
240
- const key0 = this._shapeKeys[0];
241
- const value0 = obj[key0];
242
- if (typeof value0 !== 'string') {
243
- throw new Error(locales_runtime.getMessages().objectField(key0, locales_runtime.getMessages().invalidString));
244
- }
245
- const key1 = this._shapeKeys[1];
246
- const value1 = obj[key1];
247
- if (typeof value1 !== 'number' || isNaN(value1)) {
248
- throw new Error(locales_runtime.getMessages().objectField(key1, locales_runtime.getMessages().invalidNumber));
249
- }
250
- const result = {};
251
- result[key0] = value0;
252
- result[key1] = value1;
253
- return result;
254
- }
255
- if (this._shapeKeys.length === 3 &&
256
- this._simpleFieldModes[0] === 'literal' &&
257
- this._simpleFieldModes[1] === 'string' &&
258
- this._simpleFieldModes[2] === 'number') {
259
- const key0 = this._shapeKeys[0];
260
- const value0 = obj[key0];
261
- const literal0 = this._simpleFieldValues[0];
262
- if (!(skipTrustedLiteralCheck && key0 === trustedKey) && value0 !== literal0) {
263
- throw new Error(locales_runtime.getMessages().objectField(key0, this.getSimpleFieldError('literal', literal0, value0)));
264
- }
265
- const key1 = this._shapeKeys[1];
266
- const value1 = obj[key1];
267
- if (typeof value1 !== 'string') {
268
- throw new Error(locales_runtime.getMessages().objectField(key1, locales_runtime.getMessages().invalidString));
269
- }
270
- const key2 = this._shapeKeys[2];
271
- const value2 = obj[key2];
272
- if (typeof value2 !== 'number' || isNaN(value2)) {
273
- throw new Error(locales_runtime.getMessages().objectField(key2, locales_runtime.getMessages().invalidNumber));
274
- }
275
- const result = {};
276
- result[key0] = literal0;
277
- result[key1] = value1;
278
- result[key2] = value2;
279
- return result;
280
- }
281
- const result = {};
282
- for (let i = 0; i < this._shapeKeys.length; i++) {
283
- const key = this._shapeKeys[i];
284
- const fieldValue = obj[key];
285
- const simpleMode = this._simpleFieldModes[i];
286
- switch (simpleMode) {
287
- case 'string':
288
- if (typeof fieldValue !== 'string') {
289
- throw new Error(locales_runtime.getMessages().objectField(key, locales_runtime.getMessages().invalidString));
290
- }
291
- result[key] = fieldValue;
292
- break;
293
- case 'number':
294
- if (typeof fieldValue !== 'number' || isNaN(fieldValue)) {
295
- throw new Error(locales_runtime.getMessages().objectField(key, locales_runtime.getMessages().invalidNumber));
296
- }
297
- result[key] = fieldValue;
298
- break;
299
- case 'boolean':
300
- if (typeof fieldValue !== 'boolean') {
301
- throw new Error(locales_runtime.getMessages().objectField(key, locales_runtime.getMessages().invalidBoolean));
302
- }
303
- result[key] = fieldValue;
304
- break;
305
- case 'bigint':
306
- if (typeof fieldValue !== 'bigint') {
307
- throw new Error(locales_runtime.getMessages().objectField(key, locales_runtime.getMessages().invalidBigint));
308
- }
309
- result[key] = fieldValue;
310
- break;
311
- case 'symbol':
312
- if (typeof fieldValue !== 'symbol') {
313
- throw new Error(locales_runtime.getMessages().objectField(key, locales_runtime.getMessages().invalidSymbol));
314
- }
315
- result[key] = fieldValue;
316
- break;
317
- case 'null':
318
- if (fieldValue !== null) {
319
- throw new Error(locales_runtime.getMessages().objectField(key, this.getSimpleFieldError(simpleMode, undefined, fieldValue)));
320
- }
321
- result[key] = null;
322
- break;
323
- case 'undefinedValue':
324
- if (fieldValue !== undefined) {
325
- throw new Error(locales_runtime.getMessages().objectField(key, locales_runtime.getMessages().expectedUndefined));
326
- }
327
- result[key] = undefined;
328
- break;
329
- case 'literal':
330
- if (skipTrustedLiteralCheck && key === trustedKey) {
331
- result[key] = this._simpleFieldValues[i];
332
- break;
333
- }
334
- if (fieldValue !== this._simpleFieldValues[i]) {
335
- throw new Error(locales_runtime.getMessages().objectField(key, this.getSimpleFieldError(simpleMode, this._simpleFieldValues[i], fieldValue)));
336
- }
337
- result[key] = this._simpleFieldValues[i];
338
- break;
339
- case 'passthrough':
340
- result[key] = fieldValue;
341
- break;
342
- default:
343
- throw new Error(locales_runtime.getMessages().objectField(key, locales_runtime.getMessages().invalidObject));
344
- }
345
- }
346
- return result;
347
- }
348
- /**
349
- * Get the validator configuration
350
- * @internal Used by discriminated union validator
351
- */
352
- get config() {
353
- return this._config;
354
- }
355
- /**
356
- * Get the shape keys array
357
- */
358
- get shapeKeys() {
359
- return this._shapeKeys;
360
- }
361
- /**
362
- * Get the shape keys set for O(1) lookups
363
- * @internal Used by discriminated union validator
364
- */
365
- get shapeKeysSet() {
366
- return this._shapeKeysSet;
367
- }
368
- /**
369
- * Create a new object validator
370
- */
371
- static create(shape) {
372
- return new VldObject({ shape });
373
- }
374
- /**
375
- * Parse and validate an object value
376
- * Ultra-optimized with inline type checks and minimal overhead
377
- */
378
- parse(value) {
379
- // Fast type check
380
- if (typeof value !== 'object' || value === null || Array.isArray(value)) {
381
- throw new Error(this._config.errorMessage || locales_runtime.getMessages().invalidObject);
382
- }
383
- return this.parseObjectValue(value);
384
- }
385
- /**
386
- * Parse an object value that already passed the object type guard.
387
- * @internal Used by discriminated unions after discriminator lookup.
388
- */
389
- parseKnownObject(value, trustedKey) {
390
- return this.parseObjectValue(value, trustedKey);
391
- }
392
- /**
393
- * Parse an object after an owning discriminated union has already matched
394
- * the discriminator value to this exact object schema.
395
- * @internal
396
- */
397
- parseTrustedKnownObject(value, trustedKey) {
398
- return this.parseObjectValue(value, trustedKey, true);
399
- }
400
- parseObjectValue(obj, trustedKey, skipTrustedLiteralCheck = false) {
401
- if (this._canUseSimpleObjectFastPath) {
402
- return this.parseSimpleObjectValue(obj, trustedKey, skipTrustedLiteralCheck);
403
- }
404
- const result = {};
405
- // Validate fields directly on parse() to avoid safeParse result allocation
406
- // in the successful hot path.
407
- let currentKey = '';
408
- try {
409
- for (let i = 0; i < this._shapeKeys.length; i++) {
410
- currentKey = this._shapeKeys[i];
411
- if (skipTrustedLiteralCheck &&
412
- currentKey === trustedKey &&
413
- this._simpleFieldModes[i] === 'literal') {
414
- result[currentKey] = this._simpleFieldValues[i];
415
- continue;
416
- }
417
- const simpleMode = this._simpleFieldModes[i];
418
- if (simpleMode !== undefined) {
419
- const fieldValue = obj[currentKey];
420
- switch (simpleMode) {
421
- case 'string':
422
- if (typeof fieldValue !== 'string') {
423
- throw new Error(locales_runtime.getMessages().invalidString);
424
- }
425
- result[currentKey] = fieldValue;
426
- continue;
427
- case 'number':
428
- if (typeof fieldValue !== 'number' || isNaN(fieldValue)) {
429
- throw new Error(locales_runtime.getMessages().invalidNumber);
430
- }
431
- result[currentKey] = fieldValue;
432
- continue;
433
- case 'boolean':
434
- if (typeof fieldValue !== 'boolean') {
435
- throw new Error(locales_runtime.getMessages().invalidBoolean);
436
- }
437
- result[currentKey] = fieldValue;
438
- continue;
439
- case 'bigint':
440
- if (typeof fieldValue !== 'bigint') {
441
- throw new Error(locales_runtime.getMessages().invalidBigint);
442
- }
443
- result[currentKey] = fieldValue;
444
- continue;
445
- case 'symbol':
446
- if (typeof fieldValue !== 'symbol') {
447
- throw new Error(locales_runtime.getMessages().invalidSymbol);
448
- }
449
- result[currentKey] = fieldValue;
450
- continue;
451
- case 'null':
452
- if (fieldValue !== null) {
453
- throw new Error(this.getSimpleFieldError(simpleMode, undefined, fieldValue));
454
- }
455
- result[currentKey] = null;
456
- continue;
457
- case 'undefinedValue':
458
- if (fieldValue !== undefined) {
459
- throw new Error(locales_runtime.getMessages().expectedUndefined);
460
- }
461
- result[currentKey] = undefined;
462
- continue;
463
- case 'literal':
464
- if (fieldValue !== this._simpleFieldValues[i]) {
465
- throw new Error(this.getSimpleFieldError(simpleMode, this._simpleFieldValues[i], fieldValue));
466
- }
467
- result[currentKey] = this._simpleFieldValues[i];
468
- continue;
469
- case 'passthrough':
470
- result[currentKey] = fieldValue;
471
- continue;
472
- }
473
- continue;
474
- }
475
- const validator = this.getFieldValidator(currentKey, i);
476
- result[currentKey] = this.parseCheckedField(validator, this._validatorTypes[i], obj[currentKey]);
477
- }
478
- }
479
- catch (error) {
480
- throw new Error(locales_runtime.getMessages().objectField(currentKey, error.message));
481
- }
482
- // Handle strict/passthrough/catchall modes - optimized single Object.keys() call
483
- if (this._config.strict || this._config.passthrough || this._config.catchall) {
484
- const objKeys = Object.keys(obj);
485
- // Handle strict mode - optimized with Set
486
- if (this._config.strict) {
487
- const extraKeys = [];
488
- for (let i = 0; i < objKeys.length; i++) {
489
- const key = objKeys[i];
490
- if (!this._shapeKeysSet.has(key)) {
491
- extraKeys.push(key);
492
- }
493
- }
494
- if (extraKeys.length > 0) {
495
- throw new Error(locales_runtime.getMessages().unexpectedKeys(extraKeys));
496
- }
497
- }
498
- // Handle passthrough mode - optimized with comprehensive prototype pollution protection
499
- if (this._config.passthrough) {
500
- for (let i = 0; i < objKeys.length; i++) {
501
- const key = objKeys[i];
502
- // Skip dangerous keys to prevent prototype pollution
503
- if (!this._shapeKeysSet.has(key) && !security.isDangerousKey(key)) {
504
- result[key] = obj[key];
505
- }
506
- }
507
- }
508
- // Handle catchall - validate extra keys with catchall validator
509
- if (this._config.catchall) {
510
- for (let i = 0; i < objKeys.length; i++) {
511
- const key = objKeys[i];
512
- // Skip keys already in shape and dangerous keys
513
- if (!this._shapeKeysSet.has(key) && !security.isDangerousKey(key)) {
514
- try {
515
- result[key] = this._config.catchall.parse(obj[key]);
516
- }
517
- catch (error) {
518
- throw new Error(locales_runtime.getMessages().objectField(key, error.message));
519
- }
520
- }
521
- }
522
- }
523
- }
524
- return result;
525
- }
526
- /**
527
- * Safely parse and validate an object value
528
- * Optimized version using pre-computed keys
529
- */
530
- safeParse(value) {
531
- // Fast type check
532
- if (typeof value !== 'object' || value === null || Array.isArray(value)) {
533
- return {
534
- success: false,
535
- error: this.createSafeParseError(this._config.errorMessage || locales_runtime.getMessages().invalidObject)
536
- };
537
- }
538
- const obj = value;
539
- if (this._canUseSimpleObjectFastPath) {
540
- try {
541
- return { success: true, data: this.parseSimpleObjectValue(obj) };
542
- }
543
- catch (error) {
544
- return { success: false, error: this.createSafeParseError(error.message) };
545
- }
546
- }
547
- if (this._canUseSafeParseFastPath) {
548
- try {
549
- return { success: true, data: this.parseObjectValue(obj) };
550
- }
551
- catch (error) {
552
- return { success: false, error: this.createSafeParseError(error.message) };
553
- }
554
- }
555
- const result = {};
556
- // Ultra-optimized field validation - use pre-computed validatorTypes in hot path
557
- for (let i = 0; i < this._shapeKeys.length; i++) {
558
- const key = this._shapeKeys[i];
559
- const fieldValue = obj[key];
560
- const simpleMode = this._simpleFieldModes[i];
561
- if (simpleMode !== undefined) {
562
- if ((simpleMode === 'string' && typeof fieldValue === 'string') ||
563
- (simpleMode === 'number' && typeof fieldValue === 'number' && !isNaN(fieldValue)) ||
564
- (simpleMode === 'boolean' && typeof fieldValue === 'boolean') ||
565
- (simpleMode === 'bigint' && typeof fieldValue === 'bigint') ||
566
- (simpleMode === 'symbol' && typeof fieldValue === 'symbol') ||
567
- (simpleMode === 'null' && fieldValue === null) ||
568
- (simpleMode === 'undefinedValue' && fieldValue === undefined) ||
569
- (simpleMode === 'literal' && fieldValue === this._simpleFieldValues[i]) ||
570
- simpleMode === 'passthrough') {
571
- result[key] = simpleMode === 'literal'
572
- ? this._simpleFieldValues[i]
573
- : simpleMode === 'null'
574
- ? null
575
- : fieldValue;
576
- continue;
577
- }
578
- return {
579
- success: false,
580
- error: this.createSafeParseError(locales_runtime.getMessages().objectField(key, this.getSimpleFieldError(simpleMode, this._simpleFieldValues[i], fieldValue)))
581
- };
582
- }
583
- let validator;
584
- try {
585
- validator = this.getFieldValidator(key, i);
586
- }
587
- catch (error) {
588
- const message = error instanceof Error ? error.message : String(error);
589
- return {
590
- success: false,
591
- error: this.createSafeParseError(locales_runtime.getMessages().objectField(key, message))
592
- };
593
- }
594
- try {
595
- if (validator instanceof validators_base.VldBase) {
596
- result[key] = this.parseCheckedField(validator, this._validatorTypes[i], fieldValue);
597
- }
598
- else {
599
- const parseResult = validator.safeParse(fieldValue);
600
- if (!parseResult.success) {
601
- return {
602
- success: false,
603
- error: this.createSafeParseError(locales_runtime.getMessages().objectField(key, parseResult.error.message))
604
- };
605
- }
606
- result[key] = parseResult.data;
607
- }
608
- }
609
- catch (error) {
610
- const message = error instanceof Error ? error.message : String(error);
611
- return {
612
- success: false,
613
- error: this.createSafeParseError(locales_runtime.getMessages().objectField(key, message))
614
- };
615
- }
616
- }
617
- // Handle strict/passthrough/catchall modes - optimized single Object.keys() call
618
- if (this._config.strict || this._config.passthrough || this._config.catchall) {
619
- const objKeys = Object.keys(obj);
620
- // Handle strict mode
621
- if (this._config.strict) {
622
- const extraKeys = [];
623
- for (let i = 0; i < objKeys.length; i++) {
624
- const key = objKeys[i];
625
- if (!this._shapeKeysSet.has(key)) {
626
- extraKeys.push(key);
627
- }
628
- }
629
- if (extraKeys.length > 0) {
630
- return {
631
- success: false,
632
- error: this.createSafeParseError(locales_runtime.getMessages().unexpectedKeys(extraKeys))
633
- };
634
- }
635
- }
636
- // Handle passthrough mode with comprehensive prototype pollution protection
637
- if (this._config.passthrough) {
638
- for (let i = 0; i < objKeys.length; i++) {
639
- const key = objKeys[i];
640
- // Skip dangerous keys to prevent prototype pollution
641
- if (!this._shapeKeysSet.has(key) && !security.isDangerousKey(key)) {
642
- result[key] = obj[key];
643
- }
644
- }
645
- }
646
- // Handle catchall - validate extra keys with catchall validator
647
- if (this._config.catchall) {
648
- const catchallValidator = this._config.catchall;
649
- for (let i = 0; i < objKeys.length; i++) {
650
- const key = objKeys[i];
651
- // Skip keys already in shape and dangerous keys
652
- if (!this._shapeKeysSet.has(key) && !security.isDangerousKey(key)) {
653
- try {
654
- if (catchallValidator instanceof validators_base.VldBase) {
655
- result[key] = catchallValidator.parse(obj[key]);
656
- }
657
- else {
658
- const catchallResult = catchallValidator.safeParse(obj[key]);
659
- if (!catchallResult.success) {
660
- return {
661
- success: false,
662
- error: this.createSafeParseError(locales_runtime.getMessages().objectField(key, catchallResult.error.message))
663
- };
664
- }
665
- result[key] = catchallResult.data;
666
- }
667
- }
668
- catch (error) {
669
- const message = error instanceof Error ? error.message : String(error);
670
- return {
671
- success: false,
672
- error: this.createSafeParseError(locales_runtime.getMessages().objectField(key, message))
673
- };
674
- }
675
- }
676
- }
677
- }
678
- }
679
- return { success: true, data: result };
680
- }
681
- /**
682
- * Create a new validator in strict mode (no extra keys allowed)
683
- */
684
- strict(message) {
685
- return new VldObject({
686
- ...this._config,
687
- strict: true,
688
- passthrough: false,
689
- errorMessage: message
690
- });
691
- }
692
- /**
693
- * Create a new validator in passthrough mode (extra keys are preserved)
694
- */
695
- passthrough() {
696
- return new VldObject({
697
- ...this._config,
698
- strict: false,
699
- passthrough: true
700
- });
701
- }
702
- /**
703
- * Create a new validator with all fields optional
704
- */
705
- partial() {
706
- const partialShape = {};
707
- for (const key in this._config.shape) {
708
- partialShape[key] = new validators_base.VldOptional(this._config.shape[key]);
709
- }
710
- return new VldObject({
711
- ...this._config,
712
- shape: partialShape
713
- });
714
- }
715
- /**
716
- * Create a new validator with deep partial (nested objects also partial)
717
- */
718
- deepPartial() {
719
- const deepPartialShape = {};
720
- for (const key in this._config.shape) {
721
- const validator = this._config.shape[key];
722
- if (validator instanceof VldObject) {
723
- deepPartialShape[key] = new validators_base.VldOptional(validator.deepPartial());
724
- }
725
- else {
726
- deepPartialShape[key] = new validators_base.VldOptional(validator);
727
- }
728
- }
729
- return new VldObject({
730
- ...this._config,
731
- shape: deepPartialShape
732
- });
733
- }
734
- /**
735
- * Create a new validator with only specified keys
736
- */
737
- pick(...keys) {
738
- const pickedShape = {};
739
- for (const key of keys) {
740
- if (key in this._config.shape) {
741
- pickedShape[key] = this._config.shape[key];
742
- }
743
- }
744
- return new VldObject({
745
- ...this._config,
746
- shape: pickedShape
747
- });
748
- }
749
- /**
750
- * Create a new validator without specified keys
751
- */
752
- omit(...keys) {
753
- const omittedShape = {};
754
- const keysToOmit = new Set(keys);
755
- for (const key in this._config.shape) {
756
- if (!keysToOmit.has(key)) {
757
- omittedShape[key] = this._config.shape[key];
758
- }
759
- }
760
- return new VldObject({
761
- ...this._config,
762
- shape: omittedShape
763
- });
764
- }
765
- /**
766
- * Create a new validator with additional fields
767
- */
768
- extend(extension) {
769
- return new VldObject({
770
- ...this._config,
771
- shape: { ...this._config.shape, ...extension }
772
- });
773
- }
774
- /**
775
- * Create a new validator by merging with another object validator
776
- */
777
- merge(other) {
778
- return new VldObject({
779
- ...this._config,
780
- shape: { ...this._config.shape, ...other.config.shape }
781
- });
782
- }
783
- /**
784
- * Create a new validator with all fields required (removes optional)
785
- */
786
- required() {
787
- const requiredShape = {};
788
- for (const key in this._config.shape) {
789
- const validator = this._config.shape[key];
790
- // If it's optional, unwrap it
791
- if (validator instanceof validators_base.VldOptional) {
792
- // BUG-001 FIX: Add defensive check for baseValidator property
793
- const unwrapped = validator.baseValidator;
794
- if (!unwrapped || typeof unwrapped.parse !== 'function') {
795
- throw new Error(`Invalid VldOptional structure for field "${key}": missing or invalid baseValidator`);
796
- }
797
- requiredShape[key] = unwrapped;
798
- }
799
- else {
800
- requiredShape[key] = validator;
801
- }
802
- }
803
- return new VldObject({
804
- ...this._config,
805
- shape: requiredShape
806
- });
807
- }
808
- /**
809
- * Create a new validator with a catchall validator for extra keys
810
- * Zod 4 API parity - validates unknown keys with provided schema
811
- */
812
- catchall(schema) {
813
- return new VldObject({
814
- ...this._config,
815
- catchall: schema,
816
- passthrough: false // catchall overrides passthrough
817
- });
818
- }
819
- /**
820
- * Access the inner shape schemas
821
- * Zod 4 API parity - returns the shape object
822
- */
823
- get shape() {
824
- return this._config.shape;
825
- }
826
- /**
827
- * Create an enum validator from object keys
828
- * Zod 4 API parity - creates literal union of keys
829
- */
830
- keyof() {
831
- const keys = Object.keys(this._config.shape);
832
- if (keys.length === 0) {
833
- throw new Error('Cannot create keyof enum from empty object');
834
- }
835
- return validators_enum.VldEnum.create(keys);
836
- }
837
- /**
838
- * Type-safe extend that throws an error if any key already exists
839
- * Zod 4 API parity - prevents accidental field override
840
- * @param extension The extension shape to add
841
- * @returns A new validator with extended shape
842
- * @throws {Error} If any extension key already exists in the shape
843
- * @example
844
- * const base = v.object({ name: v.string() });
845
- * const extended = base.safeExtend({ age: v.number() }); // OK
846
- * const invalid = base.safeExtend({ name: v.number() }); // Throws error
847
- */
848
- safeExtend(extension) {
849
- // Check for overlapping keys
850
- const existingKeys = new Set(Object.keys(this._config.shape));
851
- const extensionKeys = Object.keys(extension);
852
- const overlappingKeys = [];
853
- for (const key of extensionKeys) {
854
- if (existingKeys.has(key)) {
855
- overlappingKeys.push(key);
856
- }
857
- }
858
- if (overlappingKeys.length > 0) {
859
- throw new Error(`safeExtend: ${locales_runtime.getMessages().safeExtendOverlap(overlappingKeys)}`);
860
- }
861
- return new VldObject({
862
- ...this._config,
863
- shape: { ...this._config.shape, ...extension }
864
- });
865
- }
866
- }
867
-
868
- exports.VldObject = VldObject;
1
+ "use strict";var e=require("./base.cjs"),s=require("../locales/runtime.cjs"),t=require("./enum.cjs"),i=require("../chunks/security-aXZVI7qW.cjs"),r=require("../chunks/errors-core-DrydF46s.cjs");require("../registry.cjs"),require("../locales/en.cjs");class VldObject extends e.VldBase{createSafeParseError(e,t){if(e instanceof r.VldError)return void 0!==t?new r.VldError(e.issues.map(e=>({...e,path:e.path?[t,...e.path]:[t],message:e.message.startsWith("Invalid field")?e.message:s.getMessages().objectField(String(t),e.message)}))):e;const i=e instanceof Error?e.message:String(e);return new r.VldError([{code:"invalid_type",path:void 0!==t?[t]:[],message:void 0===t||i.startsWith("Invalid field")?i:s.getMessages().objectField(String(t),i)}])}constructor(s){super(e.VLD_VALIDATOR_TYPES.OBJECT),this._config=s,this._shapeKeys=Object.keys(s.shape),this._shapeKeysSet=new Set(this._shapeKeys),this._validators=this._shapeKeys.map(e=>this.tryGetFieldValidator(e)),this._validatorTypes=this._shapeKeys.map((e,s)=>this._validators[s]?.validatorType||this.getValidatorType(e)),this._simpleFieldModes=this._validators.map((e,s)=>this.getSimpleFieldMode(e,this._validatorTypes[s])),this._simpleFieldValues=this._validators.map((e,s)=>"literal"===this._simpleFieldModes[s]?e.literal:void 0),this._canUseSimpleObjectFastPath=!s.strict&&!s.passthrough&&!s.catchall&&this._shapeKeys.length>0&&this._simpleFieldModes.every(e=>void 0!==e),this._canUseSafeParseFastPath=this._validators.every(s=>s instanceof e.VldBase)&&(void 0===s.catchall||s.catchall instanceof e.VldBase)}tryGetFieldValidator(e){try{const s=Object.getOwnPropertyDescriptor(this._config.shape,e);if(s?.get)return;return this.resolveFieldValidator(e)}catch{return}}resolveFieldValidator(e){try{const s=this._config.shape[e];if(!s||"function"!=typeof s.safeParse)return;return s}catch{return}}getFieldValidator(e,s){const t=(void 0===s?void 0:this._validators[s])||this.resolveFieldValidator(e);if(!t)throw new Error(`Invalid validator for field "${e}"`);return t}getValidatorType(s){try{const t=this._config.shape[s];return t?.validatorType||e.VLD_VALIDATOR_TYPES.UNKNOWN}catch{return e.VLD_VALIDATOR_TYPES.UNKNOWN}}getSimpleFieldMode(s,t){if(s&&!0===s.isSimple)switch(t){case e.VLD_VALIDATOR_TYPES.STRING:return"string";case e.VLD_VALIDATOR_TYPES.NUMBER:return"number";case e.VLD_VALIDATOR_TYPES.BOOLEAN:return"boolean";case e.VLD_VALIDATOR_TYPES.BIGINT:return"bigint";case e.VLD_VALIDATOR_TYPES.SYMBOL:return"symbol";case e.VLD_VALIDATOR_TYPES.NULL:return"null";case e.VLD_VALIDATOR_TYPES.UNDEFINED:case e.VLD_VALIDATOR_TYPES.VOID:return"undefinedValue";case e.VLD_VALIDATOR_TYPES.LITERAL:return"literal";case e.VLD_VALIDATOR_TYPES.ANY:case e.VLD_VALIDATOR_TYPES.UNKNOWN:return"passthrough";default:return}}getSimpleFieldError(e,t,i){switch(e){case"string":return s.getMessages().invalidString;case"number":return s.getMessages().invalidNumber;case"boolean":return s.getMessages().invalidBoolean;case"bigint":return s.getMessages().invalidBigint;case"symbol":return s.getMessages().invalidSymbol;case"null":return"Expected null, received "+typeof i;case"undefinedValue":return s.getMessages().expectedUndefined;case"literal":return s.getMessages().literalExpected(JSON.stringify(t),JSON.stringify(i));default:return s.getMessages().invalidObject}}parseCheckedField(s,t,i){if(t===e.VLD_VALIDATOR_TYPES.STRING&&"string"==typeof i){const e=s.parseKnownString;if("function"==typeof e)return e.call(s,i)}if(t===e.VLD_VALIDATOR_TYPES.NUMBER&&"number"==typeof i&&!isNaN(i)){const e=s.parseKnownNumber;if("function"==typeof e)return e.call(s,i)}if(t===e.VLD_VALIDATOR_TYPES.BOOLEAN&&"boolean"==typeof i){const e=s.parseKnownBoolean;if("function"==typeof e)return e.call(s,i)}if(t===e.VLD_VALIDATOR_TYPES.BIGINT&&"bigint"==typeof i){const e=s.parseKnownBigInt;if("function"==typeof e)return e.call(s,i)}if(t===e.VLD_VALIDATOR_TYPES.SYMBOL&&"symbol"==typeof i){const e=s.parseKnownSymbol;if("function"==typeof e)return e.call(s,i)}if(t===e.VLD_VALIDATOR_TYPES.FUNCTION&&"function"==typeof i){const e=s.parseKnownFunction;if("function"==typeof e)return e.call(s,i)}if(t===e.VLD_VALIDATOR_TYPES.FILE&&"object"==typeof i&&null!==i&&("size"in i&&"type"in i||"undefined"!=typeof File&&i instanceof File)){const e=s.parseKnownFile;if("function"==typeof e)return e.call(s,i)}if(t===e.VLD_VALIDATOR_TYPES.DATE&&i instanceof Date){const e=s.parseKnownDate;if("function"==typeof e)return e.call(s,i)}if(t===e.VLD_VALIDATOR_TYPES.OBJECT&&"object"==typeof i&&null!==i&&!Array.isArray(i)){const e=s.parseKnownObject;if("function"==typeof e)return e.call(s,i)}if(t===e.VLD_VALIDATOR_TYPES.ARRAY&&Array.isArray(i)){const e=s.parseKnownArray;if("function"==typeof e)return e.call(s,i)}if(t===e.VLD_VALIDATOR_TYPES.TUPLE&&Array.isArray(i)){const e=s.parseKnownTuple;if("function"==typeof e)return e.call(s,i)}if(t===e.VLD_VALIDATOR_TYPES.RECORD&&"object"==typeof i&&null!==i&&!Array.isArray(i)){const e=s.parseKnownRecord;if("function"==typeof e)return e.call(s,i)}if(t===e.VLD_VALIDATOR_TYPES.SET&&i instanceof Set){const e=s.parseKnownSet;if("function"==typeof e)return e.call(s,i)}if(t===e.VLD_VALIDATOR_TYPES.MAP&&i instanceof Map){const e=s.parseKnownMap;if("function"==typeof e)return e.call(s,i)}return s.parse(i)}parseSimpleObjectValue(e,t,i=!1){if(2===this._shapeKeys.length&&"string"===this._simpleFieldModes[0]&&"number"===this._simpleFieldModes[1]){const t=this._shapeKeys[0],i=e[t];if("string"!=typeof i)throw new Error(s.getMessages().objectField(t,s.getMessages().invalidString));const r=this._shapeKeys[1],n=e[r];if("number"!=typeof n||isNaN(n))throw new Error(s.getMessages().objectField(r,s.getMessages().invalidNumber));const a={};return a[t]=i,a[r]=n,a}if(3===this._shapeKeys.length&&"literal"===this._simpleFieldModes[0]&&"string"===this._simpleFieldModes[1]&&"number"===this._simpleFieldModes[2]){const r=this._shapeKeys[0],n=e[r],a=this._simpleFieldValues[0];if((!i||r!==t)&&n!==a)throw new Error(s.getMessages().objectField(r,this.getSimpleFieldError("literal",a,n)));const o=this._shapeKeys[1],c=e[o];if("string"!=typeof c)throw new Error(s.getMessages().objectField(o,s.getMessages().invalidString));const l=this._shapeKeys[2],h=e[l];if("number"!=typeof h||isNaN(h))throw new Error(s.getMessages().objectField(l,s.getMessages().invalidNumber));const f={};return f[r]=a,f[o]=c,f[l]=h,f}const r={};for(let n=0;n<this._shapeKeys.length;n++){const a=this._shapeKeys[n],o=e[a],c=this._simpleFieldModes[n];switch(c){case"string":if("string"!=typeof o)throw new Error(s.getMessages().objectField(a,s.getMessages().invalidString));r[a]=o;break;case"number":if("number"!=typeof o||isNaN(o))throw new Error(s.getMessages().objectField(a,s.getMessages().invalidNumber));r[a]=o;break;case"boolean":if("boolean"!=typeof o)throw new Error(s.getMessages().objectField(a,s.getMessages().invalidBoolean));r[a]=o;break;case"bigint":if("bigint"!=typeof o)throw new Error(s.getMessages().objectField(a,s.getMessages().invalidBigint));r[a]=o;break;case"symbol":if("symbol"!=typeof o)throw new Error(s.getMessages().objectField(a,s.getMessages().invalidSymbol));r[a]=o;break;case"null":if(null!==o)throw new Error(s.getMessages().objectField(a,this.getSimpleFieldError(c,void 0,o)));r[a]=null;break;case"undefinedValue":if(void 0!==o)throw new Error(s.getMessages().objectField(a,s.getMessages().expectedUndefined));r[a]=void 0;break;case"literal":if(i&&a===t){r[a]=this._simpleFieldValues[n];break}if(o!==this._simpleFieldValues[n])throw new Error(s.getMessages().objectField(a,this.getSimpleFieldError(c,this._simpleFieldValues[n],o)));r[a]=this._simpleFieldValues[n];break;case"passthrough":r[a]=o;break;default:throw new Error(s.getMessages().objectField(a,s.getMessages().invalidObject))}}return r}get config(){return this._config}get shapeKeys(){return this._shapeKeys}get shapeKeysSet(){return this._shapeKeysSet}static create(e){return new VldObject({shape:e})}parse(e){if("object"!=typeof e||null===e||Array.isArray(e))throw new Error(this._config.errorMessage||s.getMessages().invalidObject);return this.parseObjectValue(e)}parseKnownObject(e,s){return this.parseObjectValue(e,s)}parseTrustedKnownObject(e,s){return this.parseObjectValue(e,s,!0)}parseObjectValue(e,t,n=!1){if(this._canUseSimpleObjectFastPath)return this.parseSimpleObjectValue(e,t,n);const a={};let o="";try{for(let i=0;i<this._shapeKeys.length;i++){if(o=this._shapeKeys[i],n&&o===t&&"literal"===this._simpleFieldModes[i]){a[o]=this._simpleFieldValues[i];continue}const r=this._simpleFieldModes[i];if(void 0!==r){const t=e[o];switch(r){case"string":if("string"!=typeof t)throw new Error(s.getMessages().invalidString);a[o]=t;continue;case"number":if("number"!=typeof t||isNaN(t))throw new Error(s.getMessages().invalidNumber);a[o]=t;continue;case"boolean":if("boolean"!=typeof t)throw new Error(s.getMessages().invalidBoolean);a[o]=t;continue;case"bigint":if("bigint"!=typeof t)throw new Error(s.getMessages().invalidBigint);a[o]=t;continue;case"symbol":if("symbol"!=typeof t)throw new Error(s.getMessages().invalidSymbol);a[o]=t;continue;case"null":if(null!==t)throw new Error(this.getSimpleFieldError(r,void 0,t));a[o]=null;continue;case"undefinedValue":if(void 0!==t)throw new Error(s.getMessages().expectedUndefined);a[o]=void 0;continue;case"literal":if(t!==this._simpleFieldValues[i])throw new Error(this.getSimpleFieldError(r,this._simpleFieldValues[i],t));a[o]=this._simpleFieldValues[i];continue;case"passthrough":a[o]=t;continue}continue}const c=this.getFieldValidator(o,i);a[o]=this.parseCheckedField(c,this._validatorTypes[i],e[o])}}catch(e){if(e instanceof r.VldError)throw new r.VldError(e.issues.map(e=>({...e,path:e.path?[o,...e.path]:[o],message:s.getMessages().objectField(o,e.message.startsWith("Invalid field")?e.message.replace(/^Invalid field "[^"]+": /,""):e.message)})));const t=e instanceof Error?e.message:String(e);throw new r.VldError([{code:"invalid_type",path:[o],message:t.startsWith("Invalid field")?t:s.getMessages().objectField(o,t)}])}if(this._config.strict||this._config.passthrough||this._config.catchall){const t=Object.keys(e);if(this._config.strict){const e=[];for(let s=0;s<t.length;s++){const i=t[s];this._shapeKeysSet.has(i)||e.push(i)}if(e.length>0)throw new Error(s.getMessages().unexpectedKeys(e))}if(this._config.passthrough)for(let s=0;s<t.length;s++){const r=t[s];this._shapeKeysSet.has(r)||i.isDangerousKey(r)||(a[r]=e[r])}if(this._config.catchall)for(let r=0;r<t.length;r++){const n=t[r];if(!this._shapeKeysSet.has(n)&&!i.isDangerousKey(n))try{a[n]=this._config.catchall.parse(e[n])}catch(e){throw new Error(s.getMessages().objectField(n,e.message))}}}return a}safeParse(t){if("object"!=typeof t||null===t||Array.isArray(t))return{success:!1,error:this.createSafeParseError(this._config.errorMessage||s.getMessages().invalidObject)};const n=t;if(this._canUseSimpleObjectFastPath)try{return{success:!0,data:this.parseSimpleObjectValue(n)}}catch{}if(this._canUseSafeParseFastPath)try{return{success:!0,data:this.parseObjectValue(n)}}catch{}const a={},o=[];for(let t=0;t<this._shapeKeys.length;t++){const i=this._shapeKeys[t],r=n[i],c=this._simpleFieldModes[t];if(void 0!==c){if("string"===c&&"string"==typeof r||"number"===c&&"number"==typeof r&&!isNaN(r)||"boolean"===c&&"boolean"==typeof r||"bigint"===c&&"bigint"==typeof r||"symbol"===c&&"symbol"==typeof r||"null"===c&&null===r||"undefinedValue"===c&&void 0===r||"literal"===c&&r===this._simpleFieldValues[t]||"passthrough"===c){a[i]="literal"===c?this._simpleFieldValues[t]:"null"===c?null:r;continue}o.push({code:"invalid_type",path:[i],message:s.getMessages().objectField(i,this.getSimpleFieldError(c,this._simpleFieldValues[t],r))});continue}let l;try{l=this.getFieldValidator(i,t)}catch(e){const s=this.createSafeParseError(e,i);o.push(...s.issues);continue}try{if(l instanceof e.VldBase)a[i]=this.parseCheckedField(l,this._validatorTypes[t],r);else{const e=l.safeParse(r);if(e.success)a[i]=e.data;else{const s=this.createSafeParseError(e.error,i);o.push(...s.issues)}}}catch(e){const s=this.createSafeParseError(e,i);o.push(...s.issues)}}if(this._config.strict||this._config.passthrough||this._config.catchall){const t=Object.keys(n);if(this._config.strict){const e=[];for(let s=0;s<t.length;s++){const i=t[s];this._shapeKeysSet.has(i)||e.push(i)}e.length>0&&o.push(...e.map(e=>({code:"unrecognized_keys",path:[e],message:s.getMessages().unexpectedKeys([e])})))}if(this._config.passthrough)for(let e=0;e<t.length;e++){const s=t[e];this._shapeKeysSet.has(s)||i.isDangerousKey(s)||(a[s]=n[s])}if(this._config.catchall){const s=this._config.catchall;for(let r=0;r<t.length;r++){const c=t[r];if(!this._shapeKeysSet.has(c)&&!i.isDangerousKey(c))try{if(s instanceof e.VldBase)a[c]=s.parse(n[c]);else{const e=s.safeParse(n[c]);if(e.success)a[c]=e.data;else{const s=this.createSafeParseError(e.error,c);o.push(...s.issues)}}}catch(e){const s=this.createSafeParseError(e,c);o.push(...s.issues)}}}}return o.length>0?{success:!1,error:new r.VldError(o)}:{success:!0,data:a}}encode(e){if("object"!=typeof e||null===e||Array.isArray(e))throw new Error(this._config.errorMessage||s.getMessages().invalidObject);return this.encodeObjectSync(e)}safeEncode(e){try{return{success:!0,data:this.encode(e)}}catch(e){return{success:!1,error:this.createSafeParseError(e.message)}}}async encodeAsync(e){if("object"!=typeof e||null===e||Array.isArray(e))throw new Error(this._config.errorMessage||s.getMessages().invalidObject);return this.encodeObjectAsync(e)}async safeEncodeAsync(e){try{return{success:!0,data:await this.encodeAsync(e)}}catch(e){return{success:!1,error:this.createSafeParseError(e.message)}}}encodeObjectSync(e){const s={};for(let t=0;t<this._shapeKeys.length;t++){const i=this._shapeKeys[t],r=this.getFieldValidator(i,t).encode(e[i]);(void 0!==r||Object.prototype.hasOwnProperty.call(e,i))&&(s[i]=r)}return this.encodeExtraKeysSync(e,s),s}async encodeObjectAsync(e){const s={};for(let t=0;t<this._shapeKeys.length;t++){const i=this._shapeKeys[t],r=await this.getFieldValidator(i,t).encodeAsync(e[i]);(void 0!==r||Object.prototype.hasOwnProperty.call(e,i))&&(s[i]=r)}const t=this.getEncodedExtraKeys(e);for(const r of t)i.isDangerousKey(r)||(this._config.catchall?s[r]=await this._config.catchall.encodeAsync(e[r]):this._config.passthrough&&(s[r]=e[r]));return s}getEncodedExtraKeys(e){const t=Object.keys(e).filter(e=>!this._shapeKeysSet.has(e));if(this._config.strict&&t.length>0)throw new Error(s.getMessages().unexpectedKeys(t));return t}encodeExtraKeysSync(e,s){for(const t of this.getEncodedExtraKeys(e))i.isDangerousKey(t)||(this._config.catchall?s[t]=this._config.catchall.encode(e[t]):this._config.passthrough&&(s[t]=e[t]))}strict(e){return new VldObject({...this._config,strict:!0,passthrough:!1,errorMessage:e})}passthrough(){return new VldObject({...this._config,strict:!1,passthrough:!0})}loose(){return this.passthrough()}strip(){const e={...this._config,strict:!1,passthrough:!1};return delete e.catchall,new VldObject(e)}partial(){const s={};for(const t in this._config.shape)s[t]=new e.VldOptional(this._config.shape[t]);return new VldObject({...this._config,shape:s})}deepPartial(){const s={};for(const t in this._config.shape){const i=this._config.shape[t];s[t]=i instanceof VldObject?new e.VldOptional(i.deepPartial()):new e.VldOptional(i)}return new VldObject({...this._config,shape:s})}pick(...e){const s={};let t=[];t=1!==e.length||"object"!=typeof e[0]||null===e[0]||Array.isArray(e[0])?e.flat():Object.keys(e[0]).filter(s=>e[0][s]);for(const e of t)e in this._config.shape&&(s[e]=this._config.shape[e]);return new VldObject({...this._config,shape:s})}omit(...e){let s;s=1!==e.length||"object"!=typeof e[0]||null===e[0]||Array.isArray(e[0])?new Set(e.flat()):new Set(Object.keys(e[0]).filter(s=>e[0][s]));const t={};for(const e in this._config.shape)s.has(e)||(t[e]=this._config.shape[e]);return new VldObject({...this._config,shape:t})}extend(e){return new VldObject({...this._config,shape:{...this._config.shape,...e}})}merge(e){return new VldObject({...this._config,shape:{...this._config.shape,...e.config.shape}})}required(){const s={};for(const t in this._config.shape){const i=this._config.shape[t];if(i instanceof e.VldOptional){const e=i.baseValidator;if(!e||"function"!=typeof e.parse)throw new Error(`Invalid VldOptional structure for field "${t}": missing or invalid baseValidator`);s[t]=e}else s[t]=i}return new VldObject({...this._config,shape:s})}catchall(e){return new VldObject({...this._config,catchall:e,passthrough:!1})}get shape(){return this._config.shape}keyof(){const e=Object.keys(this._config.shape);if(0===e.length)throw new Error("Cannot create keyof enum from empty object");return t.VldEnum.create(e)}safeExtend(e){const t=new Set(Object.keys(this._config.shape)),i=Object.keys(e),r=[];for(const e of i)t.has(e)&&r.push(e);if(r.length>0)throw new Error(`safeExtend: ${s.getMessages().safeExtendOverlap(r)}`);return new VldObject({...this._config,shape:{...this._config.shape,...e}})}}exports.VldObject=VldObject;