xanv 1.1.11 → 1.1.13

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 (127) hide show
  1. package/XanvType.cjs +62 -0
  2. package/XanvType.cjs.map +1 -0
  3. package/XanvType.d.ts +20 -14
  4. package/XanvType.js +60 -54
  5. package/XanvType.js.map +1 -1
  6. package/index.cjs +61 -0
  7. package/index.cjs.map +1 -0
  8. package/index.d.ts +25 -29
  9. package/index.js +41 -17
  10. package/index.js.map +1 -1
  11. package/package.json +4 -19
  12. package/readme.md +199 -199
  13. package/types/Any.cjs +16 -0
  14. package/types/Any.cjs.map +1 -0
  15. package/types/Any.d.ts +3 -3
  16. package/types/Any.js +14 -6
  17. package/types/Any.js.map +1 -1
  18. package/types/Array.cjs +66 -0
  19. package/types/Array.cjs.map +1 -0
  20. package/types/Array.d.ts +9 -9
  21. package/types/Array.js +64 -80
  22. package/types/Array.js.map +1 -1
  23. package/types/Boolean.cjs +15 -0
  24. package/types/Boolean.cjs.map +1 -0
  25. package/types/Boolean.d.ts +2 -2
  26. package/types/Boolean.js +13 -7
  27. package/types/Boolean.js.map +1 -1
  28. package/types/{Date.mjs → Date.cjs} +15 -7
  29. package/types/Date.cjs.map +1 -0
  30. package/types/Date.d.ts +2 -2
  31. package/types/Date.js +13 -7
  32. package/types/Date.js.map +1 -1
  33. package/types/Enum.cjs +28 -0
  34. package/types/Enum.cjs.map +1 -0
  35. package/types/Enum.d.ts +4 -5
  36. package/types/Enum.js +26 -25
  37. package/types/Enum.js.map +1 -1
  38. package/types/File.cjs +62 -0
  39. package/types/File.cjs.map +1 -0
  40. package/types/File.d.ts +8 -8
  41. package/types/File.js +60 -52
  42. package/types/File.js.map +1 -1
  43. package/types/Function.cjs +33 -0
  44. package/types/Function.cjs.map +1 -0
  45. package/types/Function.d.ts +15 -0
  46. package/types/Function.js +31 -0
  47. package/types/Function.js.map +1 -0
  48. package/types/Json.cjs +19 -0
  49. package/types/Json.cjs.map +1 -0
  50. package/types/Json.d.ts +3 -3
  51. package/types/Json.js +17 -11
  52. package/types/Json.js.map +1 -1
  53. package/types/Map.cjs +31 -0
  54. package/types/Map.cjs.map +1 -0
  55. package/types/Map.d.ts +6 -6
  56. package/types/Map.js +29 -23
  57. package/types/Map.js.map +1 -1
  58. package/types/{Number.mjs → Number.cjs} +77 -67
  59. package/types/Number.cjs.map +1 -0
  60. package/types/Number.d.ts +10 -10
  61. package/types/Number.js +75 -67
  62. package/types/Number.js.map +1 -1
  63. package/types/Object.cjs +22 -0
  64. package/types/Object.cjs.map +1 -0
  65. package/types/Object.d.ts +11 -5
  66. package/types/Object.js +20 -26
  67. package/types/Object.js.map +1 -1
  68. package/types/Promise.cjs +29 -0
  69. package/types/Promise.cjs.map +1 -0
  70. package/types/Promise.d.ts +11 -0
  71. package/types/Promise.js +27 -0
  72. package/types/Promise.js.map +1 -0
  73. package/types/{Record.mjs → Record.cjs} +31 -24
  74. package/types/Record.cjs.map +1 -0
  75. package/types/Record.d.ts +7 -7
  76. package/types/Record.js +29 -24
  77. package/types/Record.js.map +1 -1
  78. package/types/{Set.mjs → Set.cjs} +41 -33
  79. package/types/Set.cjs.map +1 -0
  80. package/types/Set.d.ts +6 -6
  81. package/types/Set.js +39 -33
  82. package/types/Set.js.map +1 -1
  83. package/types/{String.mjs → String.cjs} +95 -87
  84. package/types/String.cjs.map +1 -0
  85. package/types/String.d.ts +12 -12
  86. package/types/String.js +93 -87
  87. package/types/String.js.map +1 -1
  88. package/types/Tuple.cjs +31 -0
  89. package/types/Tuple.cjs.map +1 -0
  90. package/types/Tuple.d.ts +9 -5
  91. package/types/Tuple.js +29 -23
  92. package/types/Tuple.js.map +1 -1
  93. package/types/Union.cjs +34 -0
  94. package/types/Union.cjs.map +1 -0
  95. package/types/Union.d.ts +5 -4
  96. package/types/Union.js +32 -28
  97. package/types/Union.js.map +1 -1
  98. package/types.d.ts +29 -10
  99. package/XanvType.mjs +0 -54
  100. package/XanvType.mjs.map +0 -1
  101. package/index.mjs +0 -17
  102. package/index.mjs.map +0 -1
  103. package/types/Any.mjs +0 -6
  104. package/types/Any.mjs.map +0 -1
  105. package/types/Array.mjs +0 -80
  106. package/types/Array.mjs.map +0 -1
  107. package/types/Boolean.mjs +0 -7
  108. package/types/Boolean.mjs.map +0 -1
  109. package/types/Date.mjs.map +0 -1
  110. package/types/Enum.mjs +0 -25
  111. package/types/Enum.mjs.map +0 -1
  112. package/types/File.mjs +0 -52
  113. package/types/File.mjs.map +0 -1
  114. package/types/Json.mjs +0 -11
  115. package/types/Json.mjs.map +0 -1
  116. package/types/Map.mjs +0 -23
  117. package/types/Map.mjs.map +0 -1
  118. package/types/Number.mjs.map +0 -1
  119. package/types/Object.mjs +0 -26
  120. package/types/Object.mjs.map +0 -1
  121. package/types/Record.mjs.map +0 -1
  122. package/types/Set.mjs.map +0 -1
  123. package/types/String.mjs.map +0 -1
  124. package/types/Tuple.mjs +0 -23
  125. package/types/Tuple.mjs.map +0 -1
  126. package/types/Union.mjs +0 -28
  127. package/types/Union.mjs.map +0 -1
package/readme.md CHANGED
@@ -1,199 +1,199 @@
1
- # Xanv — Lightweight Runtime Validation with TypeScript Inference
2
-
3
- [![npm version](https://img.shields.io/badge/npm-v0.0.0-blue.svg)](#)
4
- [![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE)
5
-
6
- **Xanv** is a minimal runtime validation library that pairs elegantly with TypeScript. It provides a fluent API for building runtime-safe schemas that retain full static typing at compile time. With generic class-based validators, you get the best of both worlds — reliable runtime checks and precise type inference.
7
-
8
- ---
9
-
10
- ## Table of Contents
11
-
12
- * Features
13
- * Installation
14
- * Quick Start
15
- * Type Inference & TypeScript Integration
16
- * API Reference
17
- * Advanced Examples
18
- * Migration Notes
19
- * Development & Testing
20
- * Contributing
21
- * Changelog
22
- * License
23
-
24
- ---
25
-
26
- ## Features
27
-
28
- * 🚀 **Lightweight & dependency-free** — zero external dependencies.
29
- * 🔗 **Fluent API** — chainable constraints and transformations.
30
- * 🧩 **Generic XV classes** — `XVArray<T>`, `XVObject<S>`, `XVMap<K, V>`, and more, for perfect type inference.
31
- * 🧠 **Type inference helper** — `xv.infer<T>` extracts TypeScript types directly from schema definitions.
32
-
33
- ---
34
-
35
- ## Installation
36
-
37
- ```bash
38
- npm install xanv
39
- # or
40
- yarn add xanv
41
- ```
42
-
43
- ---
44
-
45
- ## Quick Start
46
-
47
- Validate and parse data with type safety:
48
-
49
- ```ts
50
- import { xv } from 'xanv';
51
-
52
- const schema = xv.object({
53
- id: xv.string().min(3),
54
- age: xv.number().min(0),
55
- tags: xv.array(xv.string()),
56
- });
57
-
58
- const parsed = schema.parse({ id: 'abc', age: 30, tags: ['x'] });
59
- console.log(parsed.id);
60
- ```
61
-
62
- The `parse()` method returns a validated value or throws on failure.
63
- Control missing or nullable values with `.optional()`, `.nullable()`, and `.default()`.
64
-
65
- ---
66
-
67
- ## Type Inference & TypeScript Integration
68
-
69
- Because each factory returns a **generic class instance**, Xanv retains full type information across schema definitions.
70
-
71
- ```ts
72
- import { xv } from 'xanv';
73
-
74
- const schema = {
75
- id: xv.string(),
76
- age: xv.number(),
77
- tags: xv.array(xv.string()),
78
- } as const;
79
-
80
- type SchemaT = xv.infer<typeof schema>;
81
- // { id: string; age: number; tags: string[] }
82
-
83
- const obj = xv.object(schema);
84
- type ObjT = xv.infer<typeof schema>; // same as SchemaT
85
-
86
- const maybe: ObjT | undefined | null = obj.parse({ id: 'a', age: 1, tags: ['x'] });
87
- ```
88
-
89
- ### How Inference Works
90
-
91
- * Each `XV` class is generic, e.g. `XVObject<S>`, `XVArray<T>`, `XVRecord<K, V>`, `XVTuple<Ts>`.
92
- * The `xv.infer<typeof schema>` utility traverses schema literals and extracts TypeScript equivalents.
93
-
94
- ---
95
-
96
- ## API Reference
97
-
98
- Each top-level factory method on the `xv` export returns a typed validator class.
99
-
100
- | Method | Returns | Description |
101
- | ------------------------- | -------------------- | ---------------------------- |
102
- | `xv.string(length?)` | `XVString<string>` | String validator |
103
- | `xv.number()` | `XVNumber<number>` | Number validator |
104
- | `xv.boolean()` | `XVBoolean<boolean>` | Boolean validator |
105
- | `xv.date()` | `XVDate<Date>` | Date validator |
106
- | `xv.array(type, length?)` | `XVArray<T>` | Array of a specific type |
107
- | `xv.tuple(types)` | `XVTuple<T>` | Tuple with precise types |
108
- | `xv.union(types)` | `XVUnion<T>` | Union of multiple validators |
109
- | `xv.object(schema?)` | `XVObject<S>` | Object validator |
110
- | `xv.map(key, value)` | `XVMap<K, V>` | Map validator |
111
- | `xv.set(type)` | `XVSet<T>` | Set validator |
112
- | `xv.record(key, value)` | `XVRecord<K, V>` | Record validator |
113
- | `xv.enum(values)` | `XVEnum<T>` | Enum validator |
114
-
115
- ### Common Instance Methods
116
-
117
- * `parse(value: any): T | undefined | null`
118
- * `optional(): this`
119
- * `nullable(): this`
120
- * `default(value: T | (() => T)): this`
121
- * `transform(cb: (value: T) => T): this`
122
-
123
- Additional constraint methods are available in `src/types` (e.g., `.min`, `.max`, `.email`, `.unique`, `.float`, `.integer`, etc.).
124
-
125
- ---
126
-
127
- ## Advanced Examples
128
-
129
- ### Tuple with Exact Types
130
-
131
- ```ts
132
- const tpl = xv.tuple([xv.string(), xv.number()]);
133
- type Tpl = xv.infer<typeof ({ a: tpl })['a']>;
134
- // [string, number]
135
- ```
136
-
137
- ### Nested Objects and Arrays
138
-
139
- ```ts
140
- const user = xv.object({ name: xv.string(), id: xv.number() });
141
- const schema = xv.object({ users: xv.array(user) });
142
-
143
- type SchemaT = xv.infer<typeof schema['arg']>;
144
- // { users: { name: string; id: number }[] }
145
- ```
146
-
147
- ### Validation with Transform and Default
148
-
149
- ```ts
150
- const s = xv.string().transform(v => v.trim()).default('n/a');
151
- const parsed = s.parse(undefined); // "n/a"
152
- ```
153
-
154
- ---
155
-
156
- ## Migration Notes
157
-
158
- If you’re upgrading from a pre-generic version of Xanv:
159
-
160
- * Factories now return **generic class types** — remove legacy wrappers.
161
- * Use `as const` on schema literals for optimal `xv.infer` inference.
162
-
163
- ---
164
-
165
- ## Development & Testing
166
-
167
- Run TypeScript checks and tests:
168
-
169
- ```bash
170
- npx tsc --noEmit
171
- npm test
172
- ```
173
-
174
- ### Adding New Types
175
-
176
- 1. Create a new generic class under `src/types`.
177
- 2. Add its factory signature in `src/index.ts`.
178
- 3. Write unit tests and update documentation examples.
179
-
180
- ---
181
-
182
- ## Contributing
183
-
184
- * Open issues before large changes.
185
- * Keep PRs focused and include relevant tests.
186
- * Update public type definitions and README when altering APIs.
187
-
188
- ---
189
-
190
- ## Changelog
191
-
192
- Refer to `CHANGELOG.md` for updates.
193
- Include short migration notes for any breaking changes.
194
-
195
- ---
196
-
197
- ## License
198
-
199
- MIT © 2025
1
+ # Xanv — Lightweight Runtime Validation with TypeScript Inference
2
+
3
+ [![npm version](https://img.shields.io/badge/npm-v0.0.0-blue.svg)](#)
4
+ [![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE)
5
+
6
+ **Xanv** is a minimal runtime validation library that pairs elegantly with TypeScript. It provides a fluent API for building runtime-safe schemas that retain full static typing at compile time. With generic class-based validators, you get the best of both worlds — reliable runtime checks and precise type inference.
7
+
8
+ ---
9
+
10
+ ## Table of Contents
11
+
12
+ * Features
13
+ * Installation
14
+ * Quick Start
15
+ * Type Inference & TypeScript Integration
16
+ * API Reference
17
+ * Advanced Examples
18
+ * Migration Notes
19
+ * Development & Testing
20
+ * Contributing
21
+ * Changelog
22
+ * License
23
+
24
+ ---
25
+
26
+ ## Features
27
+
28
+ * 🚀 **Lightweight & dependency-free** — zero external dependencies.
29
+ * 🔗 **Fluent API** — chainable constraints and transformations.
30
+ * 🧩 **Generic XV classes** — `XVArray<T>`, `XVObject<S>`, `XVMap<K, V>`, and more, for perfect type inference.
31
+ * 🧠 **Type inference helper** — `xv.infer<T>` extracts TypeScript types directly from schema definitions.
32
+
33
+ ---
34
+
35
+ ## Installation
36
+
37
+ ```bash
38
+ npm install xanv
39
+ # or
40
+ yarn add xanv
41
+ ```
42
+
43
+ ---
44
+
45
+ ## Quick Start
46
+
47
+ Validate and parse data with type safety:
48
+
49
+ ```ts
50
+ import { xv } from 'xanv';
51
+
52
+ const schema = xv.object({
53
+ id: xv.string().min(3),
54
+ age: xv.number().min(0),
55
+ tags: xv.array(xv.string()),
56
+ });
57
+
58
+ const parsed = schema.parse({ id: 'abc', age: 30, tags: ['x'] });
59
+ console.log(parsed.id);
60
+ ```
61
+
62
+ The `parse()` method returns a validated value or throws on failure.
63
+ Control missing or nullable values with `.optional()`, `.nullable()`, and `.default()`.
64
+
65
+ ---
66
+
67
+ ## Type Inference & TypeScript Integration
68
+
69
+ Because each factory returns a **generic class instance**, Xanv retains full type information across schema definitions.
70
+
71
+ ```ts
72
+ import { xv } from 'xanv';
73
+
74
+ const schema = {
75
+ id: xv.string(),
76
+ age: xv.number(),
77
+ tags: xv.array(xv.string()),
78
+ } as const;
79
+
80
+ type SchemaT = xv.infer<typeof schema>;
81
+ // { id: string; age: number; tags: string[] }
82
+
83
+ const obj = xv.object(schema);
84
+ type ObjT = xv.infer<typeof schema>; // same as SchemaT
85
+
86
+ const maybe: ObjT | undefined | null = obj.parse({ id: 'a', age: 1, tags: ['x'] });
87
+ ```
88
+
89
+ ### How Inference Works
90
+
91
+ * Each `XV` class is generic, e.g. `XVObject<S>`, `XVArray<T>`, `XVRecord<K, V>`, `XVTuple<Ts>`.
92
+ * The `xv.infer<typeof schema>` utility traverses schema literals and extracts TypeScript equivalents.
93
+
94
+ ---
95
+
96
+ ## API Reference
97
+
98
+ Each top-level factory method on the `xv` export returns a typed validator class.
99
+
100
+ | Method | Returns | Description |
101
+ | ------------------------- | -------------------- | ---------------------------- |
102
+ | `xv.string(length?)` | `XVString<string>` | String validator |
103
+ | `xv.number()` | `XVNumber<number>` | Number validator |
104
+ | `xv.boolean()` | `XVBoolean<boolean>` | Boolean validator |
105
+ | `xv.date()` | `XVDate<Date>` | Date validator |
106
+ | `xv.array(type, length?)` | `XVArray<T>` | Array of a specific type |
107
+ | `xv.tuple(types)` | `XVTuple<T>` | Tuple with precise types |
108
+ | `xv.union(types)` | `XVUnion<T>` | Union of multiple validators |
109
+ | `xv.object(schema?)` | `XVObject<S>` | Object validator |
110
+ | `xv.map(key, value)` | `XVMap<K, V>` | Map validator |
111
+ | `xv.set(type)` | `XVSet<T>` | Set validator |
112
+ | `xv.record(key, value)` | `XVRecord<K, V>` | Record validator |
113
+ | `xv.enum(values)` | `XVEnum<T>` | Enum validator |
114
+
115
+ ### Common Instance Methods
116
+
117
+ * `parse(value: any): T | undefined | null`
118
+ * `optional(): this`
119
+ * `nullable(): this`
120
+ * `default(value: T | (() => T)): this`
121
+ * `transform(cb: (value: T) => T): this`
122
+
123
+ Additional constraint methods are available in `src/types` (e.g., `.min`, `.max`, `.email`, `.unique`, `.float`, `.integer`, etc.).
124
+
125
+ ---
126
+
127
+ ## Advanced Examples
128
+
129
+ ### Tuple with Exact Types
130
+
131
+ ```ts
132
+ const tpl = xv.tuple([xv.string(), xv.number()]);
133
+ type Tpl = xv.infer<typeof ({ a: tpl })['a']>;
134
+ // [string, number]
135
+ ```
136
+
137
+ ### Nested Objects and Arrays
138
+
139
+ ```ts
140
+ const user = xv.object({ name: xv.string(), id: xv.number() });
141
+ const schema = xv.object({ users: xv.array(user) });
142
+
143
+ type SchemaT = xv.infer<typeof schema['arg']>;
144
+ // { users: { name: string; id: number }[] }
145
+ ```
146
+
147
+ ### Validation with Transform and Default
148
+
149
+ ```ts
150
+ const s = xv.string().transform(v => v.trim()).default('n/a');
151
+ const parsed = s.parse(undefined); // "n/a"
152
+ ```
153
+
154
+ ---
155
+
156
+ ## Migration Notes
157
+
158
+ If you’re upgrading from a pre-generic version of Xanv:
159
+
160
+ * Factories now return **generic class types** — remove legacy wrappers.
161
+ * Use `as const` on schema literals for optimal `xv.infer` inference.
162
+
163
+ ---
164
+
165
+ ## Development & Testing
166
+
167
+ Run TypeScript checks and tests:
168
+
169
+ ```bash
170
+ npx tsc --noEmit
171
+ npm test
172
+ ```
173
+
174
+ ### Adding New Types
175
+
176
+ 1. Create a new generic class under `src/types`.
177
+ 2. Add its factory signature in `src/index.ts`.
178
+ 3. Write unit tests and update documentation examples.
179
+
180
+ ---
181
+
182
+ ## Contributing
183
+
184
+ * Open issues before large changes.
185
+ * Keep PRs focused and include relevant tests.
186
+ * Update public type definitions and README when altering APIs.
187
+
188
+ ---
189
+
190
+ ## Changelog
191
+
192
+ Refer to `CHANGELOG.md` for updates.
193
+ Include short migration notes for any breaking changes.
194
+
195
+ ---
196
+
197
+ ## License
198
+
199
+ MIT © 2025
package/types/Any.cjs ADDED
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ var XanvType = require('../XanvType.cjs');
4
+
5
+ class XVAny extends XanvType {
6
+ check(value) {
7
+ // Accept any value as-is
8
+ return value;
9
+ }
10
+ parse(value) {
11
+ return super.parse(value);
12
+ }
13
+ }
14
+
15
+ module.exports = XVAny;
16
+ //# sourceMappingURL=Any.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Any.cjs","sources":["../../src/types/Any.ts"],"sourcesContent":["import XanvType from \"../XanvType\";\r\n\r\nclass XVAny extends XanvType<any, any> {\r\n protected check(value: any): any {\r\n // Accept any value as-is\r\n return value;\r\n }\r\n\r\n parse(value: any): any | undefined | null {\r\n return super.parse(value);\r\n }\r\n}\r\n\r\nexport default XVAny;\r\n"],"names":[],"mappings":";;;;AAEA,MAAM,KAAM,SAAQ,QAAkB,CAAA;AACzB,IAAA,KAAK,CAAC,KAAU,EAAA;;AAEvB,QAAA,OAAO,KAAK,CAAC;IAChB,CAAC;AAED,IAAA,KAAK,CAAC,KAAU,EAAA;AACb,QAAA,OAAO,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC7B,CAAC;AACH;;;;"}
package/types/Any.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import XanvType from '../XanvType.js';
2
2
 
3
- declare class XVAny<T = any> extends XanvType<T> {
4
- protected check(): void;
5
- parse(value: any): T | undefined | null;
3
+ declare class XVAny extends XanvType<any, any> {
4
+ protected check(value: any): any;
5
+ parse(value: any): any | undefined | null;
6
6
  }
7
7
 
8
8
  export { XVAny as default };
package/types/Any.js CHANGED
@@ -1,6 +1,14 @@
1
- 'use strict';Object.defineProperty(exports,'__esModule',{value:true});var XanvType=require('../XanvType.js');class XVAny extends XanvType.default {
2
- check() { }
3
- parse(value) {
4
- return super.parse(value);
5
- }
6
- }exports.default=XVAny;//# sourceMappingURL=Any.js.map
1
+ import XanvType from '../XanvType.js';
2
+
3
+ class XVAny extends XanvType {
4
+ check(value) {
5
+ // Accept any value as-is
6
+ return value;
7
+ }
8
+ parse(value) {
9
+ return super.parse(value);
10
+ }
11
+ }
12
+
13
+ export { XVAny as default };
14
+ //# sourceMappingURL=Any.js.map
package/types/Any.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"Any.js","sources":["../../src/types/Any.ts"],"sourcesContent":["import XanvType from \"../XanvType\";\n\nclass XVAny<T = any> extends XanvType<T> {\n protected check() { }\n\n parse(value: any): T | undefined | null {\n return super.parse(value) as any;\n }\n}\n\nexport default XVAny;"],"names":["XanvType"],"mappings":"6GAEA,MAAM,KAAe,SAAQA,gBAAW,CAAA;AAC3B,IAAA,KAAK,KAAK;AAEpB,IAAA,KAAK,CAAC,KAAU,EAAA;AACb,QAAA,OAAO,KAAK,CAAC,KAAK,CAAC,KAAK,CAAQ;IACnC;AACF"}
1
+ {"version":3,"file":"Any.js","sources":["../../src/types/Any.ts"],"sourcesContent":["import XanvType from \"../XanvType\";\r\n\r\nclass XVAny extends XanvType<any, any> {\r\n protected check(value: any): any {\r\n // Accept any value as-is\r\n return value;\r\n }\r\n\r\n parse(value: any): any | undefined | null {\r\n return super.parse(value);\r\n }\r\n}\r\n\r\nexport default XVAny;\r\n"],"names":[],"mappings":";;AAEA,MAAM,KAAM,SAAQ,QAAkB,CAAA;AACzB,IAAA,KAAK,CAAC,KAAU,EAAA;;AAEvB,QAAA,OAAO,KAAK,CAAC;IAChB,CAAC;AAED,IAAA,KAAK,CAAC,KAAU,EAAA;AACb,QAAA,OAAO,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC7B,CAAC;AACH;;;;"}
@@ -0,0 +1,66 @@
1
+ 'use strict';
2
+
3
+ var XanvType = require('../XanvType.cjs');
4
+
5
+ class XVArray extends XanvType {
6
+ constructor(type, length) {
7
+ super();
8
+ this.type = type;
9
+ this.length = length;
10
+ }
11
+ check(value) {
12
+ if (!Array.isArray(value)) {
13
+ throw new Error(`Value should be an array, received ${typeof value}`);
14
+ }
15
+ if (this.length !== undefined && value.length !== this.length) {
16
+ throw new Error(`Array length should be ${this.length}, received ${value.length}`);
17
+ }
18
+ const result = [];
19
+ if (this.type) {
20
+ for (let i = 0; i < value.length; i++) {
21
+ try {
22
+ result[i] = this.type.parse(value[i]);
23
+ }
24
+ catch (err) {
25
+ throw new Error(`Array item at index ${i} should be of type ${this.type.constructor.name}, received ${typeof value[i]}: ${err.message}`);
26
+ }
27
+ }
28
+ }
29
+ else {
30
+ result.push(...value);
31
+ }
32
+ return result;
33
+ }
34
+ min(length) {
35
+ return this.set("min", (v) => {
36
+ const arr = v;
37
+ if (arr.length < length) {
38
+ throw new Error(`Array length should be at least ${length} items, received ${arr.length}`);
39
+ }
40
+ }, length);
41
+ }
42
+ max(length) {
43
+ return this.set("max", (v) => {
44
+ const arr = v;
45
+ if (arr.length > length) {
46
+ throw new Error(`Array length should not exceed ${length} items, received ${arr.length}`);
47
+ }
48
+ }, length);
49
+ }
50
+ unique() {
51
+ return this.set("unique", (v) => {
52
+ const arr = v;
53
+ const seen = new Set();
54
+ for (const item of arr) {
55
+ const key = JSON.stringify(item);
56
+ if (seen.has(key)) {
57
+ throw new Error(`Array items should be unique, found duplicate: ${key}`);
58
+ }
59
+ seen.add(key);
60
+ }
61
+ });
62
+ }
63
+ }
64
+
65
+ module.exports = XVArray;
66
+ //# sourceMappingURL=Array.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Array.cjs","sources":["../../src/types/Array.ts"],"sourcesContent":["import XVMap from \"./Map\";\r\nimport XVObject from \"./Object\";\r\nimport XVRecord from \"./Record\";\r\nimport XanvType from \"../XanvType\";\r\nimport { XVInstanceType, Infer } from \"../types\";\r\n\r\nclass XVArray<T extends XVInstanceType = XVInstanceType> extends XanvType<Infer<T>[]> {\r\n private type?: T;\r\n private length?: number;\r\n\r\n constructor(type?: T, length?: number) {\r\n super();\r\n this.type = type;\r\n this.length = length;\r\n }\r\n\r\n protected check(value: unknown): Infer<T>[] {\r\n if (!Array.isArray(value)) {\r\n throw new Error(`Value should be an array, received ${typeof value}`);\r\n }\r\n\r\n if (this.length !== undefined && value.length !== this.length) {\r\n throw new Error(`Array length should be ${this.length}, received ${value.length}`);\r\n }\r\n\r\n const result: Infer<T>[] = [];\r\n\r\n if (this.type) {\r\n for (let i = 0; i < value.length; i++) {\r\n try {\r\n result[i] = this.type.parse(value[i]) as Infer<T>;\r\n } catch (err: any) {\r\n throw new Error(\r\n `Array item at index ${i} should be of type ${this.type.constructor.name}, received ${typeof value[i]}: ${err.message}`\r\n );\r\n }\r\n }\r\n } else {\r\n result.push(...(value as any[]));\r\n }\r\n\r\n return result;\r\n }\r\n\r\n min(length: number) {\r\n return this.set(\"min\", (v: unknown) => {\r\n const arr = v as Infer<T>[];\r\n if (arr.length < length) {\r\n throw new Error(`Array length should be at least ${length} items, received ${arr.length}`);\r\n }\r\n }, length);\r\n }\r\n\r\n\r\n max(length: number) {\r\n return this.set(\"max\", (v: unknown) => {\r\n const arr = v as Infer<T>[];\r\n if (arr.length > length) {\r\n throw new Error(`Array length should not exceed ${length} items, received ${arr.length}`);\r\n }\r\n }, length);\r\n }\r\n\r\n unique() {\r\n return this.set(\"unique\", (v: unknown) => {\r\n const arr = v as Infer<T>[];\r\n const seen = new Set<string>();\r\n\r\n for (const item of arr) {\r\n const key = JSON.stringify(item);\r\n if (seen.has(key)) {\r\n throw new Error(`Array items should be unique, found duplicate: ${key}`);\r\n }\r\n seen.add(key);\r\n }\r\n });\r\n }\r\n}\r\n\r\nexport default XVArray;\r\n"],"names":[],"mappings":"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"}
package/types/Array.d.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  import XanvType from '../XanvType.js';
2
- import { XVInstanceType } from '../types.js';
2
+ import { XVInstanceType, Infer } from '../types.js';
3
3
 
4
- declare class XVArray<T = any[]> extends XanvType<T[]> {
5
- private type?;
6
- private length?;
7
- constructor(type?: XVInstanceType, length?: number);
8
- protected check(value: any): void;
9
- min(length: number): this;
10
- max(length: number): this;
11
- unique(): this;
4
+ declare class XVArray<T extends XVInstanceType = XVInstanceType> extends XanvType<Infer<T>[]> {
5
+ private type?;
6
+ private length?;
7
+ constructor(type?: T, length?: number);
8
+ protected check(value: unknown): Infer<T>[];
9
+ min(length: number): this;
10
+ max(length: number): this;
11
+ unique(): this;
12
12
  }
13
13
 
14
14
  export { XVArray as default };