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