@oxog/vld 2.1.0 → 2.2.5

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 (220) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/README.md +1296 -1235
  3. package/dist/chunks/errors-core-Cqi4-9ew.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-DKPohm9I.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 +20 -1
  109. package/dist/errors.js +1 -171
  110. package/dist/index.d.ts +248 -357
  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 +63 -8
  162. package/dist/validators/base.js +1 -868
  163. package/dist/validators/base64.js +1 -68
  164. package/dist/validators/bigint.js +1 -228
  165. package/dist/validators/boolean.js +1 -142
  166. package/dist/validators/codec.js +1 -200
  167. package/dist/validators/custom.js +1 -67
  168. package/dist/validators/date.js +1 -274
  169. package/dist/validators/discriminated-union.d.ts +2 -2
  170. package/dist/validators/discriminated-union.js +1 -123
  171. package/dist/validators/enum.js +1 -150
  172. package/dist/validators/file.js +1 -125
  173. package/dist/validators/function.js +1 -58
  174. package/dist/validators/hex.js +1 -65
  175. package/dist/validators/index.d.ts +1 -1
  176. package/dist/validators/index.js +1 -35
  177. package/dist/validators/intersection.js +1 -116
  178. package/dist/validators/json.js +1 -66
  179. package/dist/validators/lazy.js +1 -50
  180. package/dist/validators/literal.d.ts +7 -3
  181. package/dist/validators/literal.js +1 -60
  182. package/dist/validators/map.js +1 -214
  183. package/dist/validators/nan.js +1 -38
  184. package/dist/validators/never.js +1 -32
  185. package/dist/validators/null.js +1 -40
  186. package/dist/validators/number.d.ts +20 -1
  187. package/dist/validators/number.js +1 -423
  188. package/dist/validators/object.d.ts +12 -2
  189. package/dist/validators/object.js +1 -864
  190. package/dist/validators/promise.js +1 -111
  191. package/dist/validators/record.d.ts +11 -8
  192. package/dist/validators/record.js +1 -258
  193. package/dist/validators/set.js +1 -199
  194. package/dist/validators/string-bool.js +1 -120
  195. package/dist/validators/string-formats.d.ts +58 -62
  196. package/dist/validators/string-formats.js +1 -135
  197. package/dist/validators/string.d.ts +47 -10
  198. package/dist/validators/string.js +1 -4
  199. package/dist/validators/symbol.js +1 -58
  200. package/dist/validators/template-literal.js +1 -103
  201. package/dist/validators/tuple.d.ts +17 -13
  202. package/dist/validators/tuple.js +1 -183
  203. package/dist/validators/uint8array.js +1 -101
  204. package/dist/validators/undefined.js +1 -40
  205. package/dist/validators/union.js +1 -187
  206. package/dist/validators/unknown.js +1 -37
  207. package/dist/validators/void.js +1 -44
  208. package/dist/validators/xor.d.ts +2 -2
  209. package/dist/validators/xor.js +1 -66
  210. package/package.json +239 -238
  211. package/dist/chunks/errors-core-BeiFUtZM.js +0 -81
  212. package/dist/chunks/index-0R2ntSud.js +0 -508
  213. package/dist/chunks/index-BvH403Yy.js +0 -88
  214. package/dist/chunks/security-B0Pd_vSY.js +0 -35
  215. package/dist/chunks/string-BJRQod3t.js +0 -530
  216. package/dist/cjs/chunks/errors-core-DWxLFs2f.cjs +0 -83
  217. package/dist/cjs/chunks/index-BkI37108.cjs +0 -538
  218. package/dist/cjs/chunks/index-Cc3bjUMX.cjs +0 -91
  219. package/dist/cjs/chunks/security-Dc3Jh13T.cjs +0 -37
  220. package/dist/cjs/chunks/string-BKZrc4hs.cjs +0 -533
@@ -1,115 +1 @@
1
- 'use strict';
2
-
3
- var validators_base = require('./base.cjs');
4
- require('../registry.cjs');
5
-
6
- /**
7
- * Promise validator - validates Promise values
8
- * Validates that the resolved value matches the inner schema
9
- */
10
- /**
11
- * Promise validator - validates Promise<T> where T matches inner schema
12
- * Zod 4 API parity - allows async validation with proper type inference
13
- *
14
- * Validates that the input is a Promise, and validates the resolved value
15
- * against the inner schema when parsed.
16
- *
17
- * Use parseAsync() or safeParseAsync() for proper async handling.
18
- */
19
- class VldPromise {
20
- constructor(inner) {
21
- this.inner = inner;
22
- this.validatorType = validators_base.VLD_VALIDATOR_TYPES.PROMISE;
23
- }
24
- get '~standard'() {
25
- return {
26
- version: 1,
27
- vendor: 'vld',
28
- validate: async (value) => {
29
- const result = await this.safeParseAsync(value);
30
- if (result.success) {
31
- return { value: result.data };
32
- }
33
- return { issues: [{ message: result.error.message }] };
34
- },
35
- types: undefined
36
- };
37
- }
38
- /**
39
- * Check if value is thenable (Promise-like)
40
- * Must be called BEFORE wrapping in Promise.resolve
41
- */
42
- _isThenable(value) {
43
- return value !== null && typeof value.then === 'function';
44
- }
45
- unwrap() {
46
- return this.inner;
47
- }
48
- /**
49
- * Parse and validate a Promise value asynchronously
50
- * @param value The Promise to validate
51
- * @returns The validated value
52
- * @throws {Error} If validation fails when Promise resolves
53
- */
54
- async parse(value) {
55
- // Check if value is thenable BEFORE wrapping in Promise.resolve
56
- if (!this._isThenable(value)) {
57
- throw new Error('Expected a Promise value');
58
- }
59
- // Now we can safely await it
60
- const resolved = await Promise.resolve(value);
61
- return this.inner.parseAsync ? this.inner.parseAsync(resolved) : this.inner.parse(resolved);
62
- }
63
- parseAsync(value) {
64
- return this.parse(value);
65
- }
66
- /**
67
- * Safely parse and validate a Promise value asynchronously
68
- * @param value The Promise to validate
69
- * @returns A Promise resolving to ParseResult containing the validated value
70
- */
71
- async safeParse(value) {
72
- // Check if value is thenable BEFORE wrapping in Promise.resolve
73
- if (!this._isThenable(value)) {
74
- return {
75
- success: false,
76
- error: new Error('Expected a Promise value')
77
- };
78
- }
79
- try {
80
- const resolved = await Promise.resolve(value);
81
- if (this.inner.safeParseAsync) {
82
- return await this.inner.safeParseAsync(resolved);
83
- }
84
- const innerResult = this.inner.safeParse(resolved);
85
- if (!innerResult.success) {
86
- return innerResult;
87
- }
88
- return { success: true, data: innerResult.data };
89
- }
90
- catch (err) {
91
- return {
92
- success: false,
93
- error: err instanceof Error ? err : new Error(String(err))
94
- };
95
- }
96
- }
97
- safeParseAsync(value) {
98
- return this.safeParse(value);
99
- }
100
- }
101
- /**
102
- * Create a Promise validator
103
- * @param inner The schema to validate the resolved value against
104
- * @returns A new Promise validator
105
- * @example
106
- * const promiseSchema = v.promise(v.string());
107
- * const result = await promiseSchema.parse(Promise.resolve("hello"));
108
- * // result is string
109
- */
110
- function promise(inner) {
111
- return new VldPromise(inner);
112
- }
113
-
114
- exports.VldPromise = VldPromise;
115
- exports.promise = promise;
1
+ "use strict";var e=require("./base.cjs");require("../registry.cjs");class VldPromise{constructor(s){this.inner=s,this.validatorType=e.VLD_VALIDATOR_TYPES.PROMISE}get"~standard"(){return{version:1,vendor:"vld",validate:async e=>{const s=await this.safeParseAsync(e);return s.success?{value:s.data}:{issues:[{message:s.error.message}]}},types:void 0}}_isThenable(e){return null!==e&&"function"==typeof e.then}unwrap(){return this.inner}async parse(e){if(!this._isThenable(e))throw new Error("Expected a Promise value");const s=await Promise.resolve(e);return this.inner.parseAsync?this.inner.parseAsync(s):this.inner.parse(s)}parseAsync(e){return this.parse(e)}async safeParse(e){if(!this._isThenable(e))return{success:!1,error:new Error("Expected a Promise value")};try{const s=await Promise.resolve(e);if(this.inner.safeParseAsync)return await this.inner.safeParseAsync(s);const r=this.inner.safeParse(s);return r.success?{success:!0,data:r.data}:r}catch(e){return{success:!1,error:e instanceof Error?e:new Error(String(e))}}}safeParseAsync(e){return this.safeParse(e)}}exports.VldPromise=VldPromise,exports.promise=function(e){return new VldPromise(e)};
@@ -1,262 +1 @@
1
- 'use strict';
2
-
3
- var validators_base = require('./base.cjs');
4
- var locales_runtime = require('../locales/runtime.cjs');
5
- var security = require('../chunks/security-Dc3Jh13T.cjs');
6
- var errorsCore = require('../chunks/errors-core-DWxLFs2f.cjs');
7
- require('../registry.cjs');
8
- require('../locales/en.cjs');
9
-
10
- function createRecordError(message) {
11
- return new errorsCore.VldError([{ code: 'invalid_object', path: [], message }]);
12
- }
13
- /**
14
- * Immutable record validator for key-value pairs
15
- * BUG-NEW-018 FIX: Uses comprehensive dangerous key protection
16
- */
17
- class VldRecord extends validators_base.VldBase {
18
- /**
19
- * Private constructor to enforce immutability
20
- */
21
- constructor(valueValidator, errorMessage) {
22
- super(validators_base.VLD_VALIDATOR_TYPES.RECORD);
23
- this.valueValidator = valueValidator;
24
- this.errorMessage = errorMessage;
25
- this._simpleValueMode = this.getSimpleValueMode(valueValidator);
26
- this._simpleValue = this._simpleValueMode === 'literal' ? valueValidator.literal : undefined;
27
- }
28
- get valueSchema() {
29
- return this.valueValidator;
30
- }
31
- /**
32
- * Create a new record validator
33
- */
34
- static create(valueValidator) {
35
- return new VldRecord(valueValidator);
36
- }
37
- getSimpleValueMode(valueValidator) {
38
- if (valueValidator.isSimple !== true) {
39
- return undefined;
40
- }
41
- switch (valueValidator.validatorType) {
42
- case validators_base.VLD_VALIDATOR_TYPES.STRING:
43
- return 'string';
44
- case validators_base.VLD_VALIDATOR_TYPES.NUMBER:
45
- return 'number';
46
- case validators_base.VLD_VALIDATOR_TYPES.BOOLEAN:
47
- return 'boolean';
48
- case validators_base.VLD_VALIDATOR_TYPES.BIGINT:
49
- return 'bigint';
50
- case validators_base.VLD_VALIDATOR_TYPES.SYMBOL:
51
- return 'symbol';
52
- case validators_base.VLD_VALIDATOR_TYPES.NULL:
53
- return 'null';
54
- case validators_base.VLD_VALIDATOR_TYPES.UNDEFINED:
55
- case validators_base.VLD_VALIDATOR_TYPES.VOID:
56
- return 'undefinedValue';
57
- case validators_base.VLD_VALIDATOR_TYPES.LITERAL:
58
- return 'literal';
59
- case validators_base.VLD_VALIDATOR_TYPES.ANY:
60
- case validators_base.VLD_VALIDATOR_TYPES.UNKNOWN:
61
- return 'passthrough';
62
- default:
63
- return undefined;
64
- }
65
- }
66
- getSimpleValueError(received) {
67
- switch (this._simpleValueMode) {
68
- case 'string':
69
- return locales_runtime.getMessages().invalidString;
70
- case 'number':
71
- return locales_runtime.getMessages().invalidNumber;
72
- case 'boolean':
73
- return locales_runtime.getMessages().invalidBoolean;
74
- case 'bigint':
75
- return locales_runtime.getMessages().invalidBigint;
76
- case 'symbol':
77
- return locales_runtime.getMessages().invalidSymbol;
78
- case 'null':
79
- return `Expected null, received ${typeof received}`;
80
- case 'undefinedValue':
81
- return locales_runtime.getMessages().expectedUndefined;
82
- case 'literal':
83
- return locales_runtime.getMessages().literalExpected(JSON.stringify(this._simpleValue), JSON.stringify(received));
84
- default:
85
- return locales_runtime.getMessages().invalidRecord;
86
- }
87
- }
88
- /**
89
- * Parse and validate a record value
90
- * BUG-NEW-018 FIX: Use comprehensive dangerous key protection
91
- */
92
- parse(value) {
93
- if (typeof value !== 'object' || value === null || Array.isArray(value)) {
94
- throw new Error(this.errorMessage || locales_runtime.getMessages().invalidRecord);
95
- }
96
- return this.parseKnownRecord(value);
97
- }
98
- /**
99
- * Parse a value that has already passed the record object type guard.
100
- * @internal Used by object validators to avoid duplicate hot-path checks.
101
- */
102
- parseKnownRecord(obj) {
103
- const result = {};
104
- const keys = Object.keys(obj);
105
- const simpleMode = this._simpleValueMode;
106
- for (let i = 0; i < keys.length; i++) {
107
- const key = keys[i];
108
- // Skip dangerous keys to prevent prototype pollution
109
- // Now using comprehensive protection from shared utility
110
- if (security.isDangerousKey(key)) {
111
- continue;
112
- }
113
- const val = obj[key];
114
- if (simpleMode !== undefined) {
115
- switch (simpleMode) {
116
- case 'string':
117
- if (typeof val !== 'string') {
118
- throw new Error(locales_runtime.getMessages().objectField(key, locales_runtime.getMessages().invalidString));
119
- }
120
- result[key] = val;
121
- continue;
122
- case 'number':
123
- if (typeof val !== 'number' || isNaN(val)) {
124
- throw new Error(locales_runtime.getMessages().objectField(key, locales_runtime.getMessages().invalidNumber));
125
- }
126
- result[key] = val;
127
- continue;
128
- case 'boolean':
129
- if (typeof val !== 'boolean') {
130
- throw new Error(locales_runtime.getMessages().objectField(key, locales_runtime.getMessages().invalidBoolean));
131
- }
132
- result[key] = val;
133
- continue;
134
- case 'bigint':
135
- if (typeof val !== 'bigint') {
136
- throw new Error(locales_runtime.getMessages().objectField(key, locales_runtime.getMessages().invalidBigint));
137
- }
138
- result[key] = val;
139
- continue;
140
- case 'symbol':
141
- if (typeof val !== 'symbol') {
142
- throw new Error(locales_runtime.getMessages().objectField(key, locales_runtime.getMessages().invalidSymbol));
143
- }
144
- result[key] = val;
145
- continue;
146
- case 'null':
147
- if (val !== null) {
148
- throw new Error(locales_runtime.getMessages().objectField(key, this.getSimpleValueError(val)));
149
- }
150
- result[key] = null;
151
- continue;
152
- case 'undefinedValue':
153
- if (val !== undefined) {
154
- throw new Error(locales_runtime.getMessages().objectField(key, locales_runtime.getMessages().expectedUndefined));
155
- }
156
- result[key] = undefined;
157
- continue;
158
- case 'literal':
159
- if (val !== this._simpleValue) {
160
- throw new Error(locales_runtime.getMessages().objectField(key, this.getSimpleValueError(val)));
161
- }
162
- result[key] = this._simpleValue;
163
- continue;
164
- case 'passthrough':
165
- result[key] = val;
166
- continue;
167
- }
168
- }
169
- try {
170
- result[key] = this.valueValidator.parse(val);
171
- }
172
- catch (error) {
173
- throw new Error(locales_runtime.getMessages().objectField(key, error.message));
174
- }
175
- }
176
- return result;
177
- }
178
- /**
179
- * Safely parse and validate a record value
180
- */
181
- safeParse(value) {
182
- try {
183
- return { success: true, data: this.parse(value) };
184
- }
185
- catch (error) {
186
- return { success: false, error: createRecordError(error.message) };
187
- }
188
- }
189
- /**
190
- * Create a partial record variant (all keys optional)
191
- * Similar to v.object().partial() but for records
192
- */
193
- partial() {
194
- const optionalValidator = this.valueValidator.optional();
195
- return new VldRecord(optionalValidator);
196
- }
197
- /**
198
- * Create a loose record variant (allows extra keys)
199
- * Similar to v.object().passthrough() but for records
200
- * Note: Records already allow any keys, so this mainly affects error handling
201
- */
202
- loose() {
203
- // For records, "loose" means we don't throw errors for validation failures
204
- // We return a modified version that catches validation errors
205
- return new VldLooseRecord(this.valueValidator);
206
- }
207
- }
208
- /**
209
- * Loose record variant that allows validation failures
210
- * Used internally by .loose() method
211
- */
212
- class VldLooseRecord extends validators_base.VldBase {
213
- constructor(valueValidator) {
214
- super(validators_base.VLD_VALIDATOR_TYPES.RECORD);
215
- this.valueValidator = valueValidator;
216
- }
217
- get valueSchema() {
218
- return this.valueValidator;
219
- }
220
- parse(value) {
221
- if (typeof value !== 'object' || value === null || Array.isArray(value)) {
222
- throw new Error(locales_runtime.getMessages().invalidRecord);
223
- }
224
- const result = {};
225
- const obj = value;
226
- for (const [key, val] of Object.entries(obj)) {
227
- // Skip dangerous keys
228
- if (security.isDangerousKey(key)) {
229
- continue;
230
- }
231
- // For loose records, skip invalid values instead of throwing
232
- const parseResult = this.valueValidator.safeParse(val);
233
- if (parseResult.success) {
234
- result[key] = parseResult.data;
235
- }
236
- }
237
- return result;
238
- }
239
- safeParse(value) {
240
- try {
241
- return { success: true, data: this.parse(value) };
242
- }
243
- catch (error) {
244
- return { success: false, error: createRecordError(error.message) };
245
- }
246
- }
247
- /**
248
- * Create a partial variant (all keys optional)
249
- */
250
- partial() {
251
- const optionalValidator = this.valueValidator.optional();
252
- return new VldLooseRecord(optionalValidator);
253
- }
254
- /**
255
- * Return self (already loose)
256
- */
257
- loose() {
258
- return this;
259
- }
260
- }
261
-
262
- exports.VldRecord = VldRecord;
1
+ "use strict";var e=require("./base.cjs"),r=require("../locales/runtime.cjs"),s=require("../chunks/security-aXZVI7qW.cjs"),t=require("../chunks/errors-core-DKPohm9I.cjs");function a(e){return new t.VldError([{code:"invalid_object",path:[],message:e}])}require("../registry.cjs"),require("../locales/en.cjs");class VldRecord extends e.VldBase{constructor(r,s,t){super(e.VLD_VALIDATOR_TYPES.RECORD),this.valueValidator=r,this.keyValidator=s,this.errorMessage=t,this._simpleValueMode=this.getSimpleValueMode(r),this._simpleValue="literal"===this._simpleValueMode?r.literal:void 0}get valueSchema(){return this.valueValidator}get keySchema(){return this.keyValidator}static create(e,r){return new VldRecord(e,r)}getSimpleValueMode(r){if(!0===r.isSimple)switch(r.validatorType){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}}getSimpleValueError(e){switch(this._simpleValueMode){case"string":return r.getMessages().invalidString;case"number":return r.getMessages().invalidNumber;case"boolean":return r.getMessages().invalidBoolean;case"bigint":return r.getMessages().invalidBigint;case"symbol":return r.getMessages().invalidSymbol;case"null":return"Expected null, received "+typeof e;case"undefinedValue":return r.getMessages().expectedUndefined;case"literal":return r.getMessages().literalExpected(JSON.stringify(this._simpleValue),JSON.stringify(e));default:return r.getMessages().invalidRecord}}parse(e){if("object"!=typeof e||null===e||Array.isArray(e))throw new Error(this.errorMessage||r.getMessages().invalidRecord);return this.parseKnownRecord(e)}parseKnownRecord(e){if(void 0!==this.keyValidator)return this.parseKeyedRecord(e);const t={},a=Object.keys(e),i=this._simpleValueMode;for(let o=0;o<a.length;o++){const n=a[o];if(s.isDangerousKey(n))continue;const l=e[n];if(void 0!==i)switch(i){case"string":if("string"!=typeof l)throw new Error(r.getMessages().objectField(n,r.getMessages().invalidString));t[n]=l;continue;case"number":if("number"!=typeof l||isNaN(l))throw new Error(r.getMessages().objectField(n,r.getMessages().invalidNumber));t[n]=l;continue;case"boolean":if("boolean"!=typeof l)throw new Error(r.getMessages().objectField(n,r.getMessages().invalidBoolean));t[n]=l;continue;case"bigint":if("bigint"!=typeof l)throw new Error(r.getMessages().objectField(n,r.getMessages().invalidBigint));t[n]=l;continue;case"symbol":if("symbol"!=typeof l)throw new Error(r.getMessages().objectField(n,r.getMessages().invalidSymbol));t[n]=l;continue;case"null":if(null!==l)throw new Error(r.getMessages().objectField(n,this.getSimpleValueError(l)));t[n]=null;continue;case"undefinedValue":if(void 0!==l)throw new Error(r.getMessages().objectField(n,r.getMessages().expectedUndefined));t[n]=void 0;continue;case"literal":if(l!==this._simpleValue)throw new Error(r.getMessages().objectField(n,this.getSimpleValueError(l)));t[n]=this._simpleValue;continue;case"passthrough":t[n]=l;continue}try{t[n]=this.valueValidator.parse(l)}catch(e){throw new Error(r.getMessages().objectField(n,e.message))}}return t}parseKeyedRecord(e){const a={},i=Reflect.ownKeys(e);for(const o of i){if(!Object.prototype.propertyIsEnumerable.call(e,o))continue;if("string"==typeof o&&s.isDangerousKey(o))continue;const i=this.keyValidator.safeParse(o);if(!i.success)throw new t.VldError([{code:"invalid_key",path:[String(o)],message:i.error.message}]);if("string"!=typeof i.data&&"number"!=typeof i.data&&"symbol"!=typeof i.data)throw new t.VldError([{code:"invalid_key",path:[String(o)],message:"Record key transform must return a property key"}]);if("string"!=typeof i.data||!s.isDangerousKey(i.data))try{a[i.data]=this.valueValidator.parse(e[o])}catch(e){throw new Error(r.getMessages().objectField(String(o),e.message))}}return a}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return e instanceof t.VldError?{success:!1,error:e}:{success:!1,error:a(e.message)}}}partial(){const e=this.valueValidator.optional();return new VldRecord(e,this.keyValidator)}loose(){return new VldLooseRecord(this.valueValidator,this.keyValidator)}}class VldLooseRecord extends e.VldBase{constructor(r,s){super(e.VLD_VALIDATOR_TYPES.RECORD),this.valueValidator=r,this.keyValidator=s}get valueSchema(){return this.valueValidator}parse(e){if("object"!=typeof e||null===e||Array.isArray(e))throw new Error(r.getMessages().invalidRecord);const t={},a=e;for(const e of Reflect.ownKeys(a)){if(!Object.prototype.propertyIsEnumerable.call(a,e))continue;if("string"==typeof e&&s.isDangerousKey(e))continue;const r=this.keyValidator?.safeParse(e);if(r&&!r.success)continue;const i=r?.data??e;if("string"!=typeof i&&"number"!=typeof i&&"symbol"!=typeof i)continue;if("string"==typeof i&&s.isDangerousKey(i))continue;const o=this.valueValidator.safeParse(a[e]);o.success&&(t[i]=o.data)}return t}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:a(e.message)}}}partial(){const e=this.valueValidator.optional();return new VldLooseRecord(e,this.keyValidator)}loose(){return this}}exports.VldRecord=VldRecord;
@@ -1,203 +1 @@
1
- 'use strict';
2
-
3
- var validators_base = require('./base.cjs');
4
- var locales_runtime = require('../locales/runtime.cjs');
5
- var errorsCore = require('../chunks/errors-core-DWxLFs2f.cjs');
6
- require('../registry.cjs');
7
- require('../locales/en.cjs');
8
-
9
- function createSetError(message) {
10
- return new errorsCore.VldError([{ code: 'invalid_type', path: [], message }]);
11
- }
12
- /**
13
- * Immutable Set validator
14
- */
15
- class VldSet extends validators_base.VldBase {
16
- /**
17
- * Private constructor to enforce immutability
18
- */
19
- constructor(itemValidator, errorMessage) {
20
- super(validators_base.VLD_VALIDATOR_TYPES.SET);
21
- this.itemValidator = itemValidator;
22
- this.errorMessage = errorMessage;
23
- this._simpleItemMode = this.getSimpleItemMode(itemValidator);
24
- this._simpleItemValue = this._simpleItemMode === 'literal' ? itemValidator.literal : undefined;
25
- }
26
- get itemSchema() {
27
- return this.itemValidator;
28
- }
29
- /**
30
- * Create a new Set validator
31
- */
32
- static create(itemValidator) {
33
- return new VldSet(itemValidator);
34
- }
35
- getSimpleItemMode(itemValidator) {
36
- if (itemValidator.isSimple !== true) {
37
- return undefined;
38
- }
39
- switch (itemValidator.validatorType) {
40
- case validators_base.VLD_VALIDATOR_TYPES.STRING:
41
- return 'string';
42
- case validators_base.VLD_VALIDATOR_TYPES.NUMBER:
43
- return 'number';
44
- case validators_base.VLD_VALIDATOR_TYPES.BOOLEAN:
45
- return 'boolean';
46
- case validators_base.VLD_VALIDATOR_TYPES.BIGINT:
47
- return 'bigint';
48
- case validators_base.VLD_VALIDATOR_TYPES.SYMBOL:
49
- return 'symbol';
50
- case validators_base.VLD_VALIDATOR_TYPES.NULL:
51
- return 'null';
52
- case validators_base.VLD_VALIDATOR_TYPES.UNDEFINED:
53
- case validators_base.VLD_VALIDATOR_TYPES.VOID:
54
- return 'undefinedValue';
55
- case validators_base.VLD_VALIDATOR_TYPES.LITERAL:
56
- return 'literal';
57
- case validators_base.VLD_VALIDATOR_TYPES.ANY:
58
- case validators_base.VLD_VALIDATOR_TYPES.UNKNOWN:
59
- return 'passthrough';
60
- default:
61
- return undefined;
62
- }
63
- }
64
- getSimpleItemError(received) {
65
- switch (this._simpleItemMode) {
66
- case 'string':
67
- return locales_runtime.getMessages().invalidString;
68
- case 'number':
69
- return locales_runtime.getMessages().invalidNumber;
70
- case 'boolean':
71
- return locales_runtime.getMessages().invalidBoolean;
72
- case 'bigint':
73
- return locales_runtime.getMessages().invalidBigint;
74
- case 'symbol':
75
- return locales_runtime.getMessages().invalidSymbol;
76
- case 'null':
77
- return `Expected null, received ${typeof received}`;
78
- case 'undefinedValue':
79
- return locales_runtime.getMessages().expectedUndefined;
80
- case 'literal':
81
- return locales_runtime.getMessages().literalExpected(JSON.stringify(this._simpleItemValue), JSON.stringify(received));
82
- default:
83
- return locales_runtime.getMessages().invalidSet;
84
- }
85
- }
86
- /**
87
- * Parse and validate a Set value
88
- */
89
- parse(value) {
90
- if (!(value instanceof Set)) {
91
- throw new Error(this.errorMessage || locales_runtime.getMessages().invalidSet);
92
- }
93
- return this.parseKnownSet(value);
94
- }
95
- /**
96
- * Parse a value that has already passed the Set instance guard.
97
- * @internal Used by object validators to avoid duplicate hot-path checks.
98
- */
99
- parseKnownSet(value) {
100
- const result = new Set();
101
- const simpleMode = this._simpleItemMode;
102
- if (simpleMode === 'string') {
103
- for (const item of value) {
104
- if (typeof item !== 'string') {
105
- throw new Error(this.getSimpleItemError(item));
106
- }
107
- result.add(item);
108
- }
109
- return result;
110
- }
111
- if (simpleMode === 'number') {
112
- for (const item of value) {
113
- if (typeof item !== 'number' || isNaN(item)) {
114
- throw new Error(this.getSimpleItemError(item));
115
- }
116
- result.add(item);
117
- }
118
- return result;
119
- }
120
- if (simpleMode === 'boolean') {
121
- for (const item of value) {
122
- if (typeof item !== 'boolean') {
123
- throw new Error(this.getSimpleItemError(item));
124
- }
125
- result.add(item);
126
- }
127
- return result;
128
- }
129
- if (simpleMode === 'bigint') {
130
- for (const item of value) {
131
- if (typeof item !== 'bigint') {
132
- throw new Error(this.getSimpleItemError(item));
133
- }
134
- result.add(item);
135
- }
136
- return result;
137
- }
138
- if (simpleMode === 'symbol') {
139
- for (const item of value) {
140
- if (typeof item !== 'symbol') {
141
- throw new Error(this.getSimpleItemError(item));
142
- }
143
- result.add(item);
144
- }
145
- return result;
146
- }
147
- if (simpleMode === 'null') {
148
- for (const item of value) {
149
- if (item !== null) {
150
- throw new Error(this.getSimpleItemError(item));
151
- }
152
- result.add(null);
153
- }
154
- return result;
155
- }
156
- if (simpleMode === 'undefinedValue') {
157
- for (const item of value) {
158
- if (item !== undefined) {
159
- throw new Error(this.getSimpleItemError(item));
160
- }
161
- result.add(undefined);
162
- }
163
- return result;
164
- }
165
- if (simpleMode === 'literal') {
166
- for (const item of value) {
167
- if (item !== this._simpleItemValue) {
168
- throw new Error(this.getSimpleItemError(item));
169
- }
170
- result.add(this._simpleItemValue);
171
- }
172
- return result;
173
- }
174
- if (simpleMode === 'passthrough') {
175
- for (const item of value) {
176
- result.add(item);
177
- }
178
- return result;
179
- }
180
- for (const item of value) {
181
- try {
182
- result.add(this.itemValidator.parse(item));
183
- }
184
- catch (error) {
185
- throw new Error(error.message);
186
- }
187
- }
188
- return result;
189
- }
190
- /**
191
- * Safely parse and validate a Set value
192
- */
193
- safeParse(value) {
194
- try {
195
- return { success: true, data: this.parse(value) };
196
- }
197
- catch (error) {
198
- return { success: false, error: createSetError(error.message) };
199
- }
200
- }
201
- }
202
-
203
- exports.VldSet = VldSet;
1
+ "use strict";var e=require("./base.cjs"),r=require("../locales/runtime.cjs"),t=require("../chunks/errors-core-DKPohm9I.cjs");require("../registry.cjs"),require("../locales/en.cjs");class VldSet extends e.VldBase{constructor(r,t){super(e.VLD_VALIDATOR_TYPES.SET),this.itemValidator=r,this.errorMessage=t,this._simpleItemMode=this.getSimpleItemMode(r),this._simpleItemValue="literal"===this._simpleItemMode?r.literal:void 0}get itemSchema(){return this.itemValidator}static create(e){return new VldSet(e)}getSimpleItemMode(r){if(!0===r.isSimple)switch(r.validatorType){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}}getSimpleItemError(e){switch(this._simpleItemMode){case"string":return r.getMessages().invalidString;case"number":return r.getMessages().invalidNumber;case"boolean":return r.getMessages().invalidBoolean;case"bigint":return r.getMessages().invalidBigint;case"symbol":return r.getMessages().invalidSymbol;case"null":return"Expected null, received "+typeof e;case"undefinedValue":return r.getMessages().expectedUndefined;case"literal":return r.getMessages().literalExpected(JSON.stringify(this._simpleItemValue),JSON.stringify(e));default:return r.getMessages().invalidSet}}parse(e){if(!(e instanceof Set))throw new Error(this.errorMessage||r.getMessages().invalidSet);return this.parseKnownSet(e)}parseKnownSet(e){const r=new Set,t=this._simpleItemMode;if("string"===t){for(const t of e){if("string"!=typeof t)throw new Error(this.getSimpleItemError(t));r.add(t)}return r}if("number"===t){for(const t of e){if("number"!=typeof t||isNaN(t))throw new Error(this.getSimpleItemError(t));r.add(t)}return r}if("boolean"===t){for(const t of e){if("boolean"!=typeof t)throw new Error(this.getSimpleItemError(t));r.add(t)}return r}if("bigint"===t){for(const t of e){if("bigint"!=typeof t)throw new Error(this.getSimpleItemError(t));r.add(t)}return r}if("symbol"===t){for(const t of e){if("symbol"!=typeof t)throw new Error(this.getSimpleItemError(t));r.add(t)}return r}if("null"===t){for(const t of e){if(null!==t)throw new Error(this.getSimpleItemError(t));r.add(null)}return r}if("undefinedValue"===t){for(const t of e){if(void 0!==t)throw new Error(this.getSimpleItemError(t));r.add(void 0)}return r}if("literal"===t){for(const t of e){if(t!==this._simpleItemValue)throw new Error(this.getSimpleItemError(t));r.add(this._simpleItemValue)}return r}if("passthrough"===t){for(const t of e)r.add(t);return r}for(const t of e)try{r.add(this.itemValidator.parse(t))}catch(e){throw new Error(e.message)}return r}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:(r=e.message,new t.VldError([{code:"invalid_type",path:[],message:r}]))}}var r}}exports.VldSet=VldSet;