anyvali 0.3.1 → 0.3.4

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 (40) hide show
  1. package/CHANGELOG.md +67 -44
  2. package/README.md +370 -370
  3. package/dist/parse/coerce.d.ts.map +1 -1
  4. package/dist/parse/coerce.js +14 -0
  5. package/dist/parse/coerce.js.map +1 -1
  6. package/dist/schemas/number.d.ts.map +1 -1
  7. package/dist/schemas/number.js +15 -0
  8. package/dist/schemas/number.js.map +1 -1
  9. package/dist/schemas/optional.d.ts.map +1 -1
  10. package/dist/schemas/optional.js +4 -3
  11. package/dist/schemas/optional.js.map +1 -1
  12. package/package.json +40 -40
  13. package/sdk/js/CHANGELOG.md +13 -13
  14. package/src/format/validators.ts +71 -71
  15. package/src/index.ts +285 -285
  16. package/src/infer.ts +12 -12
  17. package/src/interchange/importer.ts +285 -285
  18. package/src/issue-codes.ts +19 -19
  19. package/src/parse/coerce.ts +15 -0
  20. package/src/schemas/base.ts +322 -322
  21. package/src/schemas/intersection.ts +81 -81
  22. package/src/schemas/number.ts +17 -0
  23. package/src/schemas/object.ts +203 -203
  24. package/src/schemas/optional.ts +4 -3
  25. package/src/schemas/record.ts +55 -55
  26. package/src/schemas/string.ts +192 -192
  27. package/src/schemas/union.ts +53 -53
  28. package/src/types.ts +239 -239
  29. package/tests/unit/collections.test.ts +99 -99
  30. package/tests/unit/date-format.test.ts +18 -18
  31. package/tests/unit/default-mutation.test.ts +32 -32
  32. package/tests/unit/defaults.test.ts +70 -1
  33. package/tests/unit/inference.test.ts +306 -306
  34. package/tests/unit/interchange.test.ts +191 -191
  35. package/tests/unit/object.test.ts +208 -208
  36. package/tests/unit/security-recursion.test.ts +105 -105
  37. package/tests/unit/security.test.ts +1067 -945
  38. package/tests/unit/shared-ref-falsepos.test.ts +33 -33
  39. package/tests/unit/string-pattern-redos.test.ts +46 -46
  40. package/tests/unit/string.test.ts +147 -147
package/src/index.ts CHANGED
@@ -1,285 +1,285 @@
1
- // ---- Re-export types ----
2
- export type {
3
- SchemaNode,
4
- SchemaKind,
5
- ParseResult,
6
- ValidationIssue,
7
- AnyValiDocument,
8
- ExportMode,
9
- UnknownKeyMode,
10
- CoercionConfig,
11
- StringFormat,
12
- ParseContext,
13
- MetadataOptions,
14
- DescribeOptions,
15
- } from "./types.js";
16
-
17
- export { ISSUE_CODES } from "./issue-codes.js";
18
- export type { IssueCode } from "./issue-codes.js";
19
-
20
- export type { SchemaAny, Infer, InferInput } from "./infer.js";
21
-
22
- export { ValidationError } from "./errors.js";
23
-
24
- // ---- Re-export schema classes ----
25
- export {
26
- BaseSchema,
27
- ABSENT,
28
- StringSchema,
29
- NumberSchema,
30
- Float32Schema,
31
- Float64Schema,
32
- IntSchema,
33
- Int8Schema,
34
- Int16Schema,
35
- Int32Schema,
36
- Int64Schema,
37
- Uint8Schema,
38
- Uint16Schema,
39
- Uint32Schema,
40
- Uint64Schema,
41
- BoolSchema,
42
- NullSchema,
43
- AnySchema,
44
- UnknownSchema,
45
- NeverSchema,
46
- LiteralSchema,
47
- EnumSchema,
48
- ArraySchema,
49
- TupleSchema,
50
- ObjectSchema,
51
- RecordSchema,
52
- UnionSchema,
53
- IntersectionSchema,
54
- OptionalSchema,
55
- NullableSchema,
56
- RefSchema,
57
- } from "./schemas/index.js";
58
-
59
- // ---- Builder functions ----
60
-
61
- import { StringSchema } from "./schemas/string.js";
62
- import { NumberSchema, Float32Schema, Float64Schema } from "./schemas/number.js";
63
- import {
64
- IntSchema,
65
- Int8Schema,
66
- Int16Schema,
67
- Int32Schema,
68
- Int64Schema,
69
- Uint8Schema,
70
- Uint16Schema,
71
- Uint32Schema,
72
- Uint64Schema,
73
- } from "./schemas/int.js";
74
- import { BoolSchema } from "./schemas/bool.js";
75
- import { NullSchema } from "./schemas/null.js";
76
- import { AnySchema } from "./schemas/any.js";
77
- import { UnknownSchema } from "./schemas/unknown.js";
78
- import { NeverSchema } from "./schemas/never.js";
79
- import { LiteralSchema } from "./schemas/literal.js";
80
- import { EnumSchema } from "./schemas/enum.js";
81
- import { ArraySchema } from "./schemas/array.js";
82
- import { TupleSchema } from "./schemas/tuple.js";
83
- import { ObjectSchema } from "./schemas/object.js";
84
- import { RecordSchema } from "./schemas/record.js";
85
- import { UnionSchema } from "./schemas/union.js";
86
- import { IntersectionSchema } from "./schemas/intersection.js";
87
- import { OptionalSchema } from "./schemas/optional.js";
88
- import { NullableSchema } from "./schemas/nullable.js";
89
- import { BaseSchema } from "./schemas/base.js";
90
-
91
- import type { UnknownKeyMode, ParseResult, AnyValiDocument, ExportMode } from "./types.js";
92
-
93
- /** Create a string schema */
94
- export function string(): StringSchema {
95
- return new StringSchema();
96
- }
97
-
98
- /** Create a number (float64) schema */
99
- export function number(): NumberSchema {
100
- return new NumberSchema();
101
- }
102
-
103
- /** Create a float32 schema */
104
- export function float32(): Float32Schema {
105
- return new Float32Schema();
106
- }
107
-
108
- /** Create a float64 schema */
109
- export function float64(): Float64Schema {
110
- return new Float64Schema();
111
- }
112
-
113
- /** Create an int (int64) schema */
114
- export function int(): IntSchema {
115
- return new IntSchema();
116
- }
117
-
118
- /** Create an int8 schema */
119
- export function int8(): Int8Schema {
120
- return new Int8Schema();
121
- }
122
-
123
- /** Create an int16 schema */
124
- export function int16(): Int16Schema {
125
- return new Int16Schema();
126
- }
127
-
128
- /** Create an int32 schema */
129
- export function int32(): Int32Schema {
130
- return new Int32Schema();
131
- }
132
-
133
- /** Create an int64 schema */
134
- export function int64(): Int64Schema {
135
- return new Int64Schema();
136
- }
137
-
138
- /** Create a uint8 schema */
139
- export function uint8(): Uint8Schema {
140
- return new Uint8Schema();
141
- }
142
-
143
- /** Create a uint16 schema */
144
- export function uint16(): Uint16Schema {
145
- return new Uint16Schema();
146
- }
147
-
148
- /** Create a uint32 schema */
149
- export function uint32(): Uint32Schema {
150
- return new Uint32Schema();
151
- }
152
-
153
- /** Create a uint64 schema */
154
- export function uint64(): Uint64Schema {
155
- return new Uint64Schema();
156
- }
157
-
158
- /** Create a boolean schema */
159
- export function bool(): BoolSchema {
160
- return new BoolSchema();
161
- }
162
-
163
- /** Create a null schema. Named null_ to avoid conflict with the null keyword. */
164
- export function null_(): NullSchema {
165
- return new NullSchema();
166
- }
167
-
168
- /** Create an any schema */
169
- export function any(): AnySchema {
170
- return new AnySchema();
171
- }
172
-
173
- /** Create an unknown schema */
174
- export function unknown(): UnknownSchema {
175
- return new UnknownSchema();
176
- }
177
-
178
- /** Create a never schema */
179
- export function never(): NeverSchema {
180
- return new NeverSchema();
181
- }
182
-
183
- /** Create a literal schema */
184
- export function literal<T extends string | number | boolean | null>(
185
- value: T
186
- ): LiteralSchema<T> {
187
- return new LiteralSchema(value);
188
- }
189
-
190
- /** Create an enum schema. Named enum_ to avoid conflict with the enum keyword. */
191
- export function enum_<T extends readonly (string | number)[]>(
192
- values: T,
193
- ): EnumSchema<T> {
194
- return new EnumSchema(values);
195
- }
196
-
197
- /** Create an array schema */
198
- export function array<T extends BaseSchema<any, any>>(
199
- items: T,
200
- ): ArraySchema<T> {
201
- return new ArraySchema(items);
202
- }
203
-
204
- /** Create a tuple schema */
205
- export function tuple<T extends BaseSchema<any, any>[]>(
206
- items: [...T],
207
- ): TupleSchema<T> {
208
- return new TupleSchema(items);
209
- }
210
-
211
- /** Create an object schema */
212
- export function object<T extends Record<string, BaseSchema<any, any>>>(
213
- shape: T,
214
- options?: { unknownKeys?: UnknownKeyMode },
215
- ): ObjectSchema<T> {
216
- return new ObjectSchema(shape, options);
217
- }
218
-
219
- /** Create a record schema */
220
- export function record<T extends BaseSchema<any, any>>(
221
- valueSchema: T,
222
- ): RecordSchema<T> {
223
- return new RecordSchema(valueSchema);
224
- }
225
-
226
- /** Create a union schema */
227
- export function union<T extends BaseSchema<any, any>[]>(
228
- variants: [...T],
229
- ): UnionSchema<T> {
230
- return new UnionSchema(variants);
231
- }
232
-
233
- /** Create an intersection schema */
234
- export function intersection<T extends BaseSchema<any, any>[]>(
235
- schemas: [...T],
236
- ): IntersectionSchema<T> {
237
- return new IntersectionSchema(schemas);
238
- }
239
-
240
- /** Wrap a schema as optional */
241
- export function optional<T extends BaseSchema<any, any>>(
242
- schema: T,
243
- ): OptionalSchema<T> {
244
- return new OptionalSchema(schema);
245
- }
246
-
247
- /** Wrap a schema as nullable */
248
- export function nullable<T extends BaseSchema<any, any>>(
249
- schema: T,
250
- ): NullableSchema<T> {
251
- return new NullableSchema(schema);
252
- }
253
-
254
- // ---- Top-level parse functions ----
255
-
256
- /** Parse input using the given schema. Throws ValidationError on failure. */
257
- export function parse<T>(schema: BaseSchema<unknown, T>, input: unknown): T {
258
- return schema.parse(input);
259
- }
260
-
261
- /** Parse input using the given schema. Returns a result object. */
262
- export function safeParse<T>(
263
- schema: BaseSchema<unknown, T>,
264
- input: unknown
265
- ): ParseResult<T> {
266
- return schema.safeParse(input);
267
- }
268
-
269
- // ---- Interchange functions ----
270
-
271
- import { exportSchema as _exportSchema } from "./interchange/exporter.js";
272
- import { importSchema as _importSchema } from "./interchange/importer.js";
273
-
274
- /** Export a schema to an AnyValiDocument */
275
- export function exportSchema(
276
- schema: BaseSchema,
277
- mode: ExportMode = "portable"
278
- ): AnyValiDocument {
279
- return _exportSchema(schema, mode);
280
- }
281
-
282
- /** Import an AnyValiDocument to a live schema */
283
- export function importSchema(doc: AnyValiDocument): BaseSchema {
284
- return _importSchema(doc);
285
- }
1
+ // ---- Re-export types ----
2
+ export type {
3
+ SchemaNode,
4
+ SchemaKind,
5
+ ParseResult,
6
+ ValidationIssue,
7
+ AnyValiDocument,
8
+ ExportMode,
9
+ UnknownKeyMode,
10
+ CoercionConfig,
11
+ StringFormat,
12
+ ParseContext,
13
+ MetadataOptions,
14
+ DescribeOptions,
15
+ } from "./types.js";
16
+
17
+ export { ISSUE_CODES } from "./issue-codes.js";
18
+ export type { IssueCode } from "./issue-codes.js";
19
+
20
+ export type { SchemaAny, Infer, InferInput } from "./infer.js";
21
+
22
+ export { ValidationError } from "./errors.js";
23
+
24
+ // ---- Re-export schema classes ----
25
+ export {
26
+ BaseSchema,
27
+ ABSENT,
28
+ StringSchema,
29
+ NumberSchema,
30
+ Float32Schema,
31
+ Float64Schema,
32
+ IntSchema,
33
+ Int8Schema,
34
+ Int16Schema,
35
+ Int32Schema,
36
+ Int64Schema,
37
+ Uint8Schema,
38
+ Uint16Schema,
39
+ Uint32Schema,
40
+ Uint64Schema,
41
+ BoolSchema,
42
+ NullSchema,
43
+ AnySchema,
44
+ UnknownSchema,
45
+ NeverSchema,
46
+ LiteralSchema,
47
+ EnumSchema,
48
+ ArraySchema,
49
+ TupleSchema,
50
+ ObjectSchema,
51
+ RecordSchema,
52
+ UnionSchema,
53
+ IntersectionSchema,
54
+ OptionalSchema,
55
+ NullableSchema,
56
+ RefSchema,
57
+ } from "./schemas/index.js";
58
+
59
+ // ---- Builder functions ----
60
+
61
+ import { StringSchema } from "./schemas/string.js";
62
+ import { NumberSchema, Float32Schema, Float64Schema } from "./schemas/number.js";
63
+ import {
64
+ IntSchema,
65
+ Int8Schema,
66
+ Int16Schema,
67
+ Int32Schema,
68
+ Int64Schema,
69
+ Uint8Schema,
70
+ Uint16Schema,
71
+ Uint32Schema,
72
+ Uint64Schema,
73
+ } from "./schemas/int.js";
74
+ import { BoolSchema } from "./schemas/bool.js";
75
+ import { NullSchema } from "./schemas/null.js";
76
+ import { AnySchema } from "./schemas/any.js";
77
+ import { UnknownSchema } from "./schemas/unknown.js";
78
+ import { NeverSchema } from "./schemas/never.js";
79
+ import { LiteralSchema } from "./schemas/literal.js";
80
+ import { EnumSchema } from "./schemas/enum.js";
81
+ import { ArraySchema } from "./schemas/array.js";
82
+ import { TupleSchema } from "./schemas/tuple.js";
83
+ import { ObjectSchema } from "./schemas/object.js";
84
+ import { RecordSchema } from "./schemas/record.js";
85
+ import { UnionSchema } from "./schemas/union.js";
86
+ import { IntersectionSchema } from "./schemas/intersection.js";
87
+ import { OptionalSchema } from "./schemas/optional.js";
88
+ import { NullableSchema } from "./schemas/nullable.js";
89
+ import { BaseSchema } from "./schemas/base.js";
90
+
91
+ import type { UnknownKeyMode, ParseResult, AnyValiDocument, ExportMode } from "./types.js";
92
+
93
+ /** Create a string schema */
94
+ export function string(): StringSchema {
95
+ return new StringSchema();
96
+ }
97
+
98
+ /** Create a number (float64) schema */
99
+ export function number(): NumberSchema {
100
+ return new NumberSchema();
101
+ }
102
+
103
+ /** Create a float32 schema */
104
+ export function float32(): Float32Schema {
105
+ return new Float32Schema();
106
+ }
107
+
108
+ /** Create a float64 schema */
109
+ export function float64(): Float64Schema {
110
+ return new Float64Schema();
111
+ }
112
+
113
+ /** Create an int (int64) schema */
114
+ export function int(): IntSchema {
115
+ return new IntSchema();
116
+ }
117
+
118
+ /** Create an int8 schema */
119
+ export function int8(): Int8Schema {
120
+ return new Int8Schema();
121
+ }
122
+
123
+ /** Create an int16 schema */
124
+ export function int16(): Int16Schema {
125
+ return new Int16Schema();
126
+ }
127
+
128
+ /** Create an int32 schema */
129
+ export function int32(): Int32Schema {
130
+ return new Int32Schema();
131
+ }
132
+
133
+ /** Create an int64 schema */
134
+ export function int64(): Int64Schema {
135
+ return new Int64Schema();
136
+ }
137
+
138
+ /** Create a uint8 schema */
139
+ export function uint8(): Uint8Schema {
140
+ return new Uint8Schema();
141
+ }
142
+
143
+ /** Create a uint16 schema */
144
+ export function uint16(): Uint16Schema {
145
+ return new Uint16Schema();
146
+ }
147
+
148
+ /** Create a uint32 schema */
149
+ export function uint32(): Uint32Schema {
150
+ return new Uint32Schema();
151
+ }
152
+
153
+ /** Create a uint64 schema */
154
+ export function uint64(): Uint64Schema {
155
+ return new Uint64Schema();
156
+ }
157
+
158
+ /** Create a boolean schema */
159
+ export function bool(): BoolSchema {
160
+ return new BoolSchema();
161
+ }
162
+
163
+ /** Create a null schema. Named null_ to avoid conflict with the null keyword. */
164
+ export function null_(): NullSchema {
165
+ return new NullSchema();
166
+ }
167
+
168
+ /** Create an any schema */
169
+ export function any(): AnySchema {
170
+ return new AnySchema();
171
+ }
172
+
173
+ /** Create an unknown schema */
174
+ export function unknown(): UnknownSchema {
175
+ return new UnknownSchema();
176
+ }
177
+
178
+ /** Create a never schema */
179
+ export function never(): NeverSchema {
180
+ return new NeverSchema();
181
+ }
182
+
183
+ /** Create a literal schema */
184
+ export function literal<T extends string | number | boolean | null>(
185
+ value: T
186
+ ): LiteralSchema<T> {
187
+ return new LiteralSchema(value);
188
+ }
189
+
190
+ /** Create an enum schema. Named enum_ to avoid conflict with the enum keyword. */
191
+ export function enum_<T extends readonly (string | number)[]>(
192
+ values: T,
193
+ ): EnumSchema<T> {
194
+ return new EnumSchema(values);
195
+ }
196
+
197
+ /** Create an array schema */
198
+ export function array<T extends BaseSchema<any, any>>(
199
+ items: T,
200
+ ): ArraySchema<T> {
201
+ return new ArraySchema(items);
202
+ }
203
+
204
+ /** Create a tuple schema */
205
+ export function tuple<T extends BaseSchema<any, any>[]>(
206
+ items: [...T],
207
+ ): TupleSchema<T> {
208
+ return new TupleSchema(items);
209
+ }
210
+
211
+ /** Create an object schema */
212
+ export function object<T extends Record<string, BaseSchema<any, any>>>(
213
+ shape: T,
214
+ options?: { unknownKeys?: UnknownKeyMode },
215
+ ): ObjectSchema<T> {
216
+ return new ObjectSchema(shape, options);
217
+ }
218
+
219
+ /** Create a record schema */
220
+ export function record<T extends BaseSchema<any, any>>(
221
+ valueSchema: T,
222
+ ): RecordSchema<T> {
223
+ return new RecordSchema(valueSchema);
224
+ }
225
+
226
+ /** Create a union schema */
227
+ export function union<T extends BaseSchema<any, any>[]>(
228
+ variants: [...T],
229
+ ): UnionSchema<T> {
230
+ return new UnionSchema(variants);
231
+ }
232
+
233
+ /** Create an intersection schema */
234
+ export function intersection<T extends BaseSchema<any, any>[]>(
235
+ schemas: [...T],
236
+ ): IntersectionSchema<T> {
237
+ return new IntersectionSchema(schemas);
238
+ }
239
+
240
+ /** Wrap a schema as optional */
241
+ export function optional<T extends BaseSchema<any, any>>(
242
+ schema: T,
243
+ ): OptionalSchema<T> {
244
+ return new OptionalSchema(schema);
245
+ }
246
+
247
+ /** Wrap a schema as nullable */
248
+ export function nullable<T extends BaseSchema<any, any>>(
249
+ schema: T,
250
+ ): NullableSchema<T> {
251
+ return new NullableSchema(schema);
252
+ }
253
+
254
+ // ---- Top-level parse functions ----
255
+
256
+ /** Parse input using the given schema. Throws ValidationError on failure. */
257
+ export function parse<T>(schema: BaseSchema<unknown, T>, input: unknown): T {
258
+ return schema.parse(input);
259
+ }
260
+
261
+ /** Parse input using the given schema. Returns a result object. */
262
+ export function safeParse<T>(
263
+ schema: BaseSchema<unknown, T>,
264
+ input: unknown
265
+ ): ParseResult<T> {
266
+ return schema.safeParse(input);
267
+ }
268
+
269
+ // ---- Interchange functions ----
270
+
271
+ import { exportSchema as _exportSchema } from "./interchange/exporter.js";
272
+ import { importSchema as _importSchema } from "./interchange/importer.js";
273
+
274
+ /** Export a schema to an AnyValiDocument */
275
+ export function exportSchema(
276
+ schema: BaseSchema,
277
+ mode: ExportMode = "portable"
278
+ ): AnyValiDocument {
279
+ return _exportSchema(schema, mode);
280
+ }
281
+
282
+ /** Import an AnyValiDocument to a live schema */
283
+ export function importSchema(doc: AnyValiDocument): BaseSchema {
284
+ return _importSchema(doc);
285
+ }
package/src/infer.ts CHANGED
@@ -1,12 +1,12 @@
1
- import type { BaseSchema } from "./schemas/base.js";
2
-
3
- /** Any AnyVali schema. Use as a generic constraint. Equivalent to Zod's ZodTypeAny. */
4
- export type SchemaAny = BaseSchema<any, any>;
5
-
6
- /** Extract the output type from a schema. Equivalent to Zod's z.infer. */
7
- export type Infer<T extends SchemaAny> = T["_output"];
8
-
9
- /** Extract the input type from a schema. */
10
- export type InferInput<T extends SchemaAny> = T extends BaseSchema<infer I, any>
11
- ? I
12
- : never;
1
+ import type { BaseSchema } from "./schemas/base.js";
2
+
3
+ /** Any AnyVali schema. Use as a generic constraint. Equivalent to Zod's ZodTypeAny. */
4
+ export type SchemaAny = BaseSchema<any, any>;
5
+
6
+ /** Extract the output type from a schema. Equivalent to Zod's z.infer. */
7
+ export type Infer<T extends SchemaAny> = T["_output"];
8
+
9
+ /** Extract the input type from a schema. */
10
+ export type InferInput<T extends SchemaAny> = T extends BaseSchema<infer I, any>
11
+ ? I
12
+ : never;