@scalar/validation 0.1.0 → 0.2.0
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.
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +6 -0
- package/README.md +1 -1
- package/dist/coerce.d.ts.map +1 -1
- package/dist/coerce.js +60 -9
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/schema.d.ts +48 -24
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +43 -12
- package/dist/typegen.d.ts +25 -0
- package/dist/typegen.d.ts.map +1 -0
- package/dist/typegen.js +221 -0
- package/dist/types.d.ts +19 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/validate.d.ts +4 -2
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +17 -2
- package/package.json +1 -1
- package/src/coerce.test.ts +67 -0
- package/src/coerce.ts +64 -11
- package/src/index.ts +17 -0
- package/src/schema.ts +89 -21
- package/src/typegen.test.ts +214 -0
- package/src/typegen.ts +268 -0
- package/src/types.ts +42 -8
- package/src/validate.test.ts +53 -0
- package/src/validate.ts +17 -2
package/.turbo/turbo-build.log
CHANGED
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -70,7 +70,7 @@ Use **`validate`** when you need a yes/no. Use **`coerce`** when you want a stab
|
|
|
70
70
|
| `record(key, value)` | Plain object; keys and values match | `Record<…, …>` |
|
|
71
71
|
| `object(props)` | Plain object; each key in `props` | Object of static fields |
|
|
72
72
|
| `union([a, b, …])` | Matches any member | Union of branches |
|
|
73
|
-
| `optional(s)` |
|
|
73
|
+
| `optional(s)` | `undefined` or matches `s` | `Static<s> \| undefined`; in `object({ … })`, property becomes `key?: Static<s>` |
|
|
74
74
|
| `lazy(() => schema)` | Defers schema (recursion) | Inferred from inner schema |
|
|
75
75
|
| `evaluate(fn, schema)` | Runs `fn(value)` then validates `schema` | `Static<schema>` |
|
|
76
76
|
|
package/dist/coerce.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"coerce.d.ts","sourceRoot":"","sources":["../src/coerce.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AACtC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"coerce.d.ts","sourceRoot":"","sources":["../src/coerce.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AACtC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AA0FrC;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,MAAM,GAAI,CAAC,SAAS,MAAM,EACrC,QAAQ,CAAC,EACT,OAAO,OAAO,EACd,QAAO,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAiB,KAClD,MAAM,CAAC,CAAC,CA6GV,CAAA"}
|
package/dist/coerce.js
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
import { isObject } from './helpers/is-object.js';
|
|
2
2
|
import { validate } from './validate.js';
|
|
3
|
+
/**
|
|
4
|
+
* True when this property schema is only used to discriminate union branches
|
|
5
|
+
* (single literal, or a union of literals). No presence bonus when the value
|
|
6
|
+
* does not match — avoids ties like `type: literal('a')` vs `type: union([lit('b'), lit('c')])`.
|
|
7
|
+
*/
|
|
8
|
+
const isDiscriminatorProperty = (schema) => {
|
|
9
|
+
if (schema.type === 'optional') {
|
|
10
|
+
return isDiscriminatorProperty(schema.schema);
|
|
11
|
+
}
|
|
12
|
+
if (schema.type === 'literal') {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
if (schema.type === 'union') {
|
|
16
|
+
return schema.schemas.length > 0 && schema.schemas.every(isDiscriminatorProperty);
|
|
17
|
+
}
|
|
18
|
+
return false;
|
|
19
|
+
};
|
|
3
20
|
/**
|
|
4
21
|
* Computes a "score" indicating how well a value matches a schema,
|
|
5
22
|
* used for picking the best branch in union coercion.
|
|
@@ -13,16 +30,23 @@ const scoreUnion = (schema, value) => {
|
|
|
13
30
|
if (!isObject(value)) {
|
|
14
31
|
return 0;
|
|
15
32
|
}
|
|
16
|
-
//
|
|
17
|
-
//
|
|
18
|
-
//
|
|
33
|
+
// Missing keys contribute 0 (including optional keys — matches prior union heuristics).
|
|
34
|
+
// Discriminator properties (`literal` or `union` of literals): recurse with scoreUnion;
|
|
35
|
+
// matching values get a high weight (×10) so `type: literal('A')` beats unrelated fields
|
|
36
|
+
// on another branch; mismatches score 0 (no "key present" tie-break).
|
|
37
|
+
// Other properties: scoreUnion plus +1 when the value fails validation so `{ a: null }`
|
|
38
|
+
// can still prefer the branch that declares `a`.
|
|
19
39
|
return Object.keys(schema.properties).reduce((acc, key) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
40
|
+
if (!(key in value)) {
|
|
41
|
+
return acc;
|
|
42
|
+
}
|
|
43
|
+
const propSchema = schema.properties[key];
|
|
44
|
+
const raw = value[key];
|
|
45
|
+
const base = scoreUnion(propSchema, raw);
|
|
46
|
+
if (isDiscriminatorProperty(propSchema)) {
|
|
47
|
+
return acc + (base > 0 ? base * 10 : 0);
|
|
24
48
|
}
|
|
25
|
-
return acc + (
|
|
49
|
+
return acc + (base > 0 ? base : 1);
|
|
26
50
|
}, 0);
|
|
27
51
|
}
|
|
28
52
|
if (schema.type === 'array') {
|
|
@@ -33,10 +57,19 @@ const scoreUnion = (schema, value) => {
|
|
|
33
57
|
// TODO: implement smarter scoring for records (just a placeholder for now)
|
|
34
58
|
return isObject(value) ? 1 : 0;
|
|
35
59
|
}
|
|
60
|
+
if (schema.type === 'optional') {
|
|
61
|
+
return value === undefined ? 1 : scoreUnion(schema.schema, value);
|
|
62
|
+
}
|
|
36
63
|
if (schema.type === 'union') {
|
|
37
64
|
// For a union, use the highest score among all sub-schemas
|
|
38
65
|
return Math.max(...schema.schemas.map((schema) => scoreUnion(schema, value)));
|
|
39
66
|
}
|
|
67
|
+
if (schema.type === 'intersection') {
|
|
68
|
+
if (schema.schemas.length === 0) {
|
|
69
|
+
return 1;
|
|
70
|
+
}
|
|
71
|
+
return schema.schemas.reduce((acc, sub) => acc + scoreUnion(sub, value), 0);
|
|
72
|
+
}
|
|
40
73
|
if (schema.type === 'lazy') {
|
|
41
74
|
// For a lazy schema, evaluate the inner schema and recurse
|
|
42
75
|
return scoreUnion(schema.schema(), value);
|
|
@@ -108,6 +141,12 @@ export const coerce = (schema, value, cache = new WeakMap()) => {
|
|
|
108
141
|
if (schema.type === 'notDefined') {
|
|
109
142
|
return undefined;
|
|
110
143
|
}
|
|
144
|
+
if (schema.type === 'optional') {
|
|
145
|
+
if (value === undefined) {
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
return coerce(schema.schema, value, cache);
|
|
149
|
+
}
|
|
111
150
|
if (schema.type === 'array') {
|
|
112
151
|
if (!Array.isArray(value)) {
|
|
113
152
|
return [];
|
|
@@ -123,7 +162,16 @@ export const coerce = (schema, value, cache = new WeakMap()) => {
|
|
|
123
162
|
if (schema.type === 'object') {
|
|
124
163
|
const keys = Object.keys(schema.properties);
|
|
125
164
|
const target = isObject(value) ? value : null;
|
|
126
|
-
|
|
165
|
+
const entries = [];
|
|
166
|
+
for (const key of keys) {
|
|
167
|
+
const propSchema = schema.properties[key];
|
|
168
|
+
const raw = target?.[key];
|
|
169
|
+
if (propSchema.type === 'optional' && raw === undefined) {
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
entries.push([key, coerce(propSchema, raw, cache)]);
|
|
173
|
+
}
|
|
174
|
+
return Object.fromEntries(entries);
|
|
127
175
|
}
|
|
128
176
|
if (schema.type === 'union') {
|
|
129
177
|
const branch = schema.schemas.reduce((acc, schema) => {
|
|
@@ -133,6 +181,9 @@ export const coerce = (schema, value, cache = new WeakMap()) => {
|
|
|
133
181
|
// We need some way to pick one of the union values
|
|
134
182
|
return coerce(branch.schema, value, cache);
|
|
135
183
|
}
|
|
184
|
+
if (schema.type === 'intersection') {
|
|
185
|
+
return schema.schemas.reduce((acc, subSchema) => Object.assign(acc, coerce(subSchema, value, cache)), {});
|
|
186
|
+
}
|
|
136
187
|
if (schema.type === 'literal') {
|
|
137
188
|
return schema.value;
|
|
138
189
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { coerce } from './coerce.js';
|
|
2
|
-
export { type Schema, any, array, boolean, evaluate, lazy, literal, notDefined, nullable, number, object, optional, record, string, union, } from './schema.js';
|
|
2
|
+
export { type AnySchema, type ArraySchema, type BooleanSchema, type EvaluateSchema, type IntersectionSchema, type LazySchema, type LiteralSchema, type NotDefinedSchema, type NullableSchema, type NumberSchema, type ObjectSchema, type OptionalSchema, type RecordSchema, type Schema, type StringSchema, type UnionSchema, any, array, boolean, evaluate, intersection, lazy, literal, notDefined, nullable, number, object, optional, record, string, union, } from './schema.js';
|
|
3
|
+
export { type GenerateTypesOptions, generateTypes } from './typegen.js';
|
|
3
4
|
export type { Static } from './types.js';
|
|
4
5
|
export { validate } from './validate.js';
|
|
5
6
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AACjC,OAAO,EACL,KAAK,MAAM,EACX,GAAG,EACH,KAAK,EACL,OAAO,EACP,QAAQ,EACR,IAAI,EACJ,OAAO,EACP,UAAU,EACV,QAAQ,EACR,MAAM,EACN,MAAM,EACN,QAAQ,EACR,MAAM,EACN,MAAM,EACN,KAAK,GACN,MAAM,UAAU,CAAA;AACjB,YAAY,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AACrC,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AACjC,OAAO,EACL,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,kBAAkB,EACvB,KAAK,UAAU,EACf,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,KAAK,MAAM,EACX,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,GAAG,EACH,KAAK,EACL,OAAO,EACP,QAAQ,EACR,YAAY,EACZ,IAAI,EACJ,OAAO,EACP,UAAU,EACV,QAAQ,EACR,MAAM,EACN,MAAM,EACN,QAAQ,EACR,MAAM,EACN,MAAM,EACN,KAAK,GACN,MAAM,UAAU,CAAA;AACjB,OAAO,EAAE,KAAK,oBAAoB,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AACpE,YAAY,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AACrC,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { coerce } from './coerce.js';
|
|
2
|
-
export { any, array, boolean, evaluate, lazy, literal, notDefined, nullable, number, object, optional, record, string, union, } from './schema.js';
|
|
2
|
+
export { any, array, boolean, evaluate, intersection, lazy, literal, notDefined, nullable, number, object, optional, record, string, union, } from './schema.js';
|
|
3
|
+
export { generateTypes } from './typegen.js';
|
|
3
4
|
export { validate } from './validate.js';
|
package/dist/schema.d.ts
CHANGED
|
@@ -1,53 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optional metadata for type generation and documentation.
|
|
3
|
+
* - typeName: Used as the exported TypeScript type name if valid.
|
|
4
|
+
* - typeComment: Adds a JSDoc comment to the generated type declaration.
|
|
5
|
+
*/
|
|
6
|
+
type Documentation = Partial<{
|
|
7
|
+
/** Adds a JSDoc comment to the generated type declaration. */
|
|
8
|
+
typeComment: string;
|
|
9
|
+
/** Used as the exported TypeScript type name if valid. */
|
|
10
|
+
typeName: string;
|
|
11
|
+
}>;
|
|
1
12
|
/** Schema for finite numeric values. {@link Static} resolves to `number`. */
|
|
2
13
|
export type NumberSchema = {
|
|
3
14
|
type: 'number';
|
|
4
|
-
};
|
|
15
|
+
} & Documentation;
|
|
5
16
|
/** Schema for string values. {@link Static} resolves to `string`. */
|
|
6
17
|
export type StringSchema = {
|
|
7
18
|
type: 'string';
|
|
8
|
-
};
|
|
19
|
+
} & Documentation;
|
|
9
20
|
/** Schema for boolean values. {@link Static} resolves to `boolean`. */
|
|
10
21
|
export type BooleanSchema = {
|
|
11
22
|
type: 'boolean';
|
|
12
|
-
};
|
|
23
|
+
} & Documentation;
|
|
13
24
|
/** Schema for `null`. {@link Static} resolves to `null`. */
|
|
14
25
|
export type NullableSchema = {
|
|
15
26
|
type: 'nullable';
|
|
16
|
-
};
|
|
27
|
+
} & Documentation;
|
|
17
28
|
/** Schema for a missing or omitted value. {@link Static} resolves to `undefined`. */
|
|
18
29
|
export type NotDefinedSchema = {
|
|
19
30
|
type: 'notDefined';
|
|
20
|
-
};
|
|
31
|
+
} & Documentation;
|
|
21
32
|
/** Schema that accepts any value without narrowing. {@link Static} resolves to `any`. */
|
|
22
33
|
export type AnySchema = {
|
|
23
34
|
type: 'any';
|
|
24
|
-
};
|
|
35
|
+
} & Documentation;
|
|
25
36
|
/** Schema for homogeneous lists. {@link Static} resolves to an array of the item static type. */
|
|
26
37
|
export type ArraySchema<Item extends Schema> = {
|
|
27
38
|
type: 'array';
|
|
28
39
|
items: Item;
|
|
29
|
-
};
|
|
40
|
+
} & Documentation;
|
|
30
41
|
/** Schema for key-value maps with uniform value shape. Keys are constrained to string or number schemas. */
|
|
31
42
|
export type RecordSchema<Key extends StringSchema | NumberSchema | AnySchema, Value extends Schema> = {
|
|
32
43
|
type: 'record';
|
|
33
44
|
key: Key;
|
|
34
45
|
value: Value;
|
|
35
|
-
};
|
|
46
|
+
} & Documentation;
|
|
36
47
|
/** Schema for objects with a fixed set of named properties, each with its own schema. */
|
|
37
48
|
export type ObjectSchema<Properties extends Record<string, Schema>> = {
|
|
38
49
|
type: 'object';
|
|
39
50
|
properties: Properties;
|
|
40
|
-
};
|
|
51
|
+
} & Documentation;
|
|
41
52
|
/** Schema that matches if any member schema matches (discriminated union when literals or object tags differ). */
|
|
42
53
|
export type UnionSchema<Schemas extends Schema[]> = {
|
|
43
54
|
type: 'union';
|
|
44
55
|
schemas: Schemas;
|
|
45
|
-
};
|
|
56
|
+
} & Documentation;
|
|
57
|
+
/**
|
|
58
|
+
* Schema that accepts `undefined` or a value matching the inner schema.
|
|
59
|
+
* In {@link Static} and type generation, object properties use `key?:` instead of `T | undefined`.
|
|
60
|
+
*/
|
|
61
|
+
export type OptionalSchema<S extends Schema> = {
|
|
62
|
+
type: 'optional';
|
|
63
|
+
schema: S;
|
|
64
|
+
} & Documentation;
|
|
65
|
+
export type IntersectionSchema<Schemas extends readonly ObjectSchema<any>[]> = {
|
|
66
|
+
type: 'intersection';
|
|
67
|
+
schemas: Schemas;
|
|
68
|
+
} & Documentation;
|
|
46
69
|
/** Schema for a single exact constant (string, number, boolean, or bigint). {@link Static} is that literal type. */
|
|
47
70
|
export type LiteralSchema<T extends string | number | boolean | bigint> = {
|
|
48
71
|
type: 'literal';
|
|
49
72
|
value: T;
|
|
50
|
-
};
|
|
73
|
+
} & Documentation;
|
|
51
74
|
/**
|
|
52
75
|
* Schema for self-referential or recursive types (such as trees or linked lists).
|
|
53
76
|
* The `schema` property is a factory function returning a schema instance, allowing
|
|
@@ -66,20 +89,21 @@ export type EvaluateSchema<S extends Schema> = {
|
|
|
66
89
|
expression: (value: unknown) => unknown;
|
|
67
90
|
schema: S;
|
|
68
91
|
};
|
|
69
|
-
export type Schema = NumberSchema | StringSchema | BooleanSchema | NullableSchema | NotDefinedSchema | AnySchema | ArraySchema<any> | RecordSchema<any, any> | ObjectSchema<Record<string, any>> | UnionSchema<any[]> | LiteralSchema<any> | LazySchema<any> | EvaluateSchema<any>;
|
|
70
|
-
declare const number: () => NumberSchema;
|
|
71
|
-
declare const string: () => StringSchema;
|
|
72
|
-
declare const boolean: () => BooleanSchema;
|
|
73
|
-
declare const nullable: () => NullableSchema;
|
|
74
|
-
declare const notDefined: () => NotDefinedSchema;
|
|
75
|
-
declare const any: () => AnySchema;
|
|
76
|
-
declare const array: <Item extends Schema>(items: Item) => ArraySchema<Item>;
|
|
77
|
-
declare const record: <Key extends StringSchema | AnySchema, Value extends Schema>(key: Key, value: Value) => RecordSchema<Key, Value>;
|
|
78
|
-
declare const object: <Properties extends Record<string, Schema>>(properties: Properties) => ObjectSchema<Properties>;
|
|
79
|
-
declare const union: <Schemas extends Schema[]>(schemas: Schemas) => UnionSchema<Schemas>;
|
|
80
|
-
declare const
|
|
92
|
+
export type Schema = NumberSchema | StringSchema | BooleanSchema | NullableSchema | NotDefinedSchema | AnySchema | ArraySchema<any> | RecordSchema<any, any> | ObjectSchema<Record<string, any>> | UnionSchema<any[]> | OptionalSchema<any> | IntersectionSchema<readonly ObjectSchema<any>[]> | LiteralSchema<any> | LazySchema<any> | EvaluateSchema<any>;
|
|
93
|
+
declare const number: (options?: Documentation) => NumberSchema;
|
|
94
|
+
declare const string: (options?: Documentation) => StringSchema;
|
|
95
|
+
declare const boolean: (options?: Documentation) => BooleanSchema;
|
|
96
|
+
declare const nullable: (options?: Documentation) => NullableSchema;
|
|
97
|
+
declare const notDefined: (options?: Documentation) => NotDefinedSchema;
|
|
98
|
+
declare const any: (options?: Documentation) => AnySchema;
|
|
99
|
+
declare const array: <Item extends Schema>(items: Item, options?: Documentation) => ArraySchema<Item>;
|
|
100
|
+
declare const record: <Key extends StringSchema | AnySchema, Value extends Schema>(key: Key, value: Value, options?: Documentation) => RecordSchema<Key, Value>;
|
|
101
|
+
declare const object: <Properties extends Record<string, Schema>>(properties: Properties, options?: Documentation) => ObjectSchema<Properties>;
|
|
102
|
+
declare const union: <Schemas extends Schema[]>(schemas: Schemas, options?: Documentation) => UnionSchema<Schemas>;
|
|
103
|
+
declare const intersection: <Schemas extends readonly ObjectSchema<any>[]>(schemas: Schemas, options?: Documentation) => IntersectionSchema<Schemas>;
|
|
104
|
+
declare const optional: <S extends Schema>(schema: S, options?: Documentation) => OptionalSchema<S>;
|
|
81
105
|
declare const literal: <Value extends string | number | boolean | bigint>(value: Value) => LiteralSchema<Value>;
|
|
82
106
|
declare const lazy: <S extends () => Schema>(schema: S) => LazySchema<S>;
|
|
83
107
|
declare const evaluate: <S extends Schema>(expression: (value: unknown) => unknown, schema: S) => EvaluateSchema<S>;
|
|
84
|
-
export { number, string, boolean, nullable, notDefined, any, array, record, object, union, optional, literal, lazy, evaluate, };
|
|
108
|
+
export { number, string, boolean, nullable, notDefined, any, array, record, object, union, intersection, optional, literal, lazy, evaluate, };
|
|
85
109
|
//# sourceMappingURL=schema.d.ts.map
|
package/dist/schema.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,MAAM,MAAM,YAAY,GAAG;IACzB,IAAI,EAAE,QAAQ,CAAA;CACf,CAAA;
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,KAAK,aAAa,GAAG,OAAO,CAAC;IAC3B,8DAA8D;IAC9D,WAAW,EAAE,MAAM,CAAA;IACnB,0DAA0D;IAC1D,QAAQ,EAAE,MAAM,CAAA;CACjB,CAAC,CAAA;AAEF,6EAA6E;AAC7E,MAAM,MAAM,YAAY,GAAG;IACzB,IAAI,EAAE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EF,QAAA,MAAM,QAAQ,GAAI,CAAC,SAAS,MAAM,EAAE,QAAQ,CAAC,EAAE,UAAU,aAAa,KAAG,cAAc,CAAC,CAAC,CAKvF,CAAA;AAEF,QAAA,MAAM,OAAO,GAAI,KAAK,SAAS,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,EAAE,OAAO,KAAK,KAAG,aAAa,CAAC,KAAK,CAGnG,CAAA;AAEF,QAAA,MAAM,IAAI,GAAI,CAAC,SAAS,MAAM,MAAM,EAAE,QAAQ,CAAC,KAAG,UAAU,CAAC,CAAC,CAG5D,CAAA;AAEF,QAAA,MAAM,QAAQ,GAAI,CAAC,SAAS,MAAM,EAAE,YAAY,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,EAAE,QAAQ,CAAC,KAAG,cAAc,CAAC,CAAC,CAIvG,CAAA;AAEF,OAAO,EACL,MAAM,EACN,MAAM,EACN,OAAO,EACP,QAAQ,EACR,UAAU,EACV,GAAG,EACH,KAAK,EACL,MAAM,EACN,MAAM,EACN,KAAK,EACL,YAAY,EACZ,QAAQ,EACR,OAAO,EACP,IAAI,EACJ,QAAQ,GACT,CAAA"}
|
package/dist/schema.js
CHANGED
|
@@ -1,39 +1,70 @@
|
|
|
1
|
-
const number = () => ({
|
|
1
|
+
const number = (options) => ({
|
|
2
2
|
type: 'number',
|
|
3
|
+
typeName: options?.typeName,
|
|
4
|
+
typeComment: options?.typeComment,
|
|
3
5
|
});
|
|
4
|
-
const string = () => ({
|
|
6
|
+
const string = (options) => ({
|
|
5
7
|
type: 'string',
|
|
8
|
+
typeName: options?.typeName,
|
|
9
|
+
typeComment: options?.typeComment,
|
|
6
10
|
});
|
|
7
|
-
const boolean = () => ({
|
|
11
|
+
const boolean = (options) => ({
|
|
8
12
|
type: 'boolean',
|
|
13
|
+
typeName: options?.typeName,
|
|
14
|
+
typeComment: options?.typeComment,
|
|
9
15
|
});
|
|
10
|
-
const nullable = () => ({
|
|
16
|
+
const nullable = (options) => ({
|
|
11
17
|
type: 'nullable',
|
|
18
|
+
typeName: options?.typeName,
|
|
19
|
+
typeComment: options?.typeComment,
|
|
12
20
|
});
|
|
13
|
-
const notDefined = () => ({
|
|
21
|
+
const notDefined = (options) => ({
|
|
14
22
|
type: 'notDefined',
|
|
23
|
+
typeName: options?.typeName,
|
|
24
|
+
typeComment: options?.typeComment,
|
|
15
25
|
});
|
|
16
|
-
const any = () => ({
|
|
26
|
+
const any = (options) => ({
|
|
17
27
|
type: 'any',
|
|
28
|
+
typeName: options?.typeName,
|
|
29
|
+
typeComment: options?.typeComment,
|
|
18
30
|
});
|
|
19
|
-
const array = (items) => ({
|
|
31
|
+
const array = (items, options) => ({
|
|
20
32
|
type: 'array',
|
|
21
33
|
items,
|
|
34
|
+
typeName: options?.typeName,
|
|
35
|
+
typeComment: options?.typeComment,
|
|
22
36
|
});
|
|
23
|
-
const record = (key, value) => ({
|
|
37
|
+
const record = (key, value, options) => ({
|
|
24
38
|
type: 'record',
|
|
25
39
|
key,
|
|
26
40
|
value,
|
|
41
|
+
typeName: options?.typeName,
|
|
42
|
+
typeComment: options?.typeComment,
|
|
27
43
|
});
|
|
28
|
-
const object = (properties) => ({
|
|
44
|
+
const object = (properties, options) => ({
|
|
29
45
|
type: 'object',
|
|
30
46
|
properties,
|
|
47
|
+
typeName: options?.typeName,
|
|
48
|
+
typeComment: options?.typeComment,
|
|
31
49
|
});
|
|
32
|
-
const union = (schemas) => ({
|
|
50
|
+
const union = (schemas, options) => ({
|
|
33
51
|
type: 'union',
|
|
34
52
|
schemas,
|
|
53
|
+
typeName: options?.typeName,
|
|
54
|
+
typeComment: options?.typeComment,
|
|
55
|
+
});
|
|
56
|
+
const intersection = (schemas, options) => ({
|
|
57
|
+
type: 'intersection',
|
|
58
|
+
schemas,
|
|
59
|
+
typeName: options?.typeName,
|
|
60
|
+
typeComment: options?.typeComment,
|
|
61
|
+
});
|
|
62
|
+
const optional = (schema, options) => ({
|
|
63
|
+
type: 'optional',
|
|
64
|
+
schema,
|
|
65
|
+
typeName: options?.typeName,
|
|
66
|
+
typeComment: options?.typeComment,
|
|
35
67
|
});
|
|
36
|
-
const optional = (schema) => union([schema, notDefined()]);
|
|
37
68
|
const literal = (value) => ({
|
|
38
69
|
type: 'literal',
|
|
39
70
|
value,
|
|
@@ -47,4 +78,4 @@ const evaluate = (expression, schema) => ({
|
|
|
47
78
|
expression,
|
|
48
79
|
schema,
|
|
49
80
|
});
|
|
50
|
-
export { number, string, boolean, nullable, notDefined, any, array, record, object, union, optional, literal, lazy, evaluate, };
|
|
81
|
+
export { number, string, boolean, nullable, notDefined, any, array, record, object, union, intersection, optional, literal, lazy, evaluate, };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Schema } from './schema.js';
|
|
2
|
+
export type GenerateTypesOptions = {
|
|
3
|
+
maxDepth?: number;
|
|
4
|
+
/**
|
|
5
|
+
* ISO 8601 timestamp printed in the autogenerated file banner.
|
|
6
|
+
* Defaults to the time of the `generateTypes` call when the banner is emitted.
|
|
7
|
+
*/
|
|
8
|
+
generatedAt?: string;
|
|
9
|
+
/**
|
|
10
|
+
* When set to a valid TypeScript identifier, wraps all emitted `export type` declarations (and any
|
|
11
|
+
* trailing root type) in `export namespace Name { ... }` so consumers reference `Name.SomeType`.
|
|
12
|
+
*/
|
|
13
|
+
namespace?: string;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Returns TypeScript for the schema: named `typeName` nodes become `export type` aliases (once each),
|
|
17
|
+
* referenced by name elsewhere. With no named nodes, returns a single inline type expression (same as before).
|
|
18
|
+
*
|
|
19
|
+
* When at least one named type is emitted, the result is prefixed with a banner stating the output is
|
|
20
|
+
* autogenerated and must not be edited manually, plus a generation timestamp.
|
|
21
|
+
*
|
|
22
|
+
* Pass `namespace` to wrap declarations in `export namespace … { … }`.
|
|
23
|
+
*/
|
|
24
|
+
export declare const generateTypes: (schema: Schema, options?: GenerateTypesOptions) => string;
|
|
25
|
+
//# sourceMappingURL=typegen.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typegen.d.ts","sourceRoot":"","sources":["../src/typegen.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AAItC,MAAM,MAAM,oBAAoB,GAAG;IACjC,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAA;AAcD;;;;;;;;GAQG;AACH,eAAO,MAAM,aAAa,GAAI,QAAQ,MAAM,EAAE,UAAU,oBAAoB,KAAG,MAuB9E,CAAA"}
|
package/dist/typegen.js
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
const DEFAULT_MAX_DEPTH = 10;
|
|
2
|
+
/**
|
|
3
|
+
* Returns TypeScript for the schema: named `typeName` nodes become `export type` aliases (once each),
|
|
4
|
+
* referenced by name elsewhere. With no named nodes, returns a single inline type expression (same as before).
|
|
5
|
+
*
|
|
6
|
+
* When at least one named type is emitted, the result is prefixed with a banner stating the output is
|
|
7
|
+
* autogenerated and must not be edited manually, plus a generation timestamp.
|
|
8
|
+
*
|
|
9
|
+
* Pass `namespace` to wrap declarations in `export namespace … { … }`.
|
|
10
|
+
*/
|
|
11
|
+
export const generateTypes = (schema, options) => {
|
|
12
|
+
const maxDepth = options?.maxDepth ?? DEFAULT_MAX_DEPTH;
|
|
13
|
+
const ctx = {
|
|
14
|
+
definitions: new Map(),
|
|
15
|
+
declarations: [],
|
|
16
|
+
inProgress: new Set(),
|
|
17
|
+
};
|
|
18
|
+
const root = emitSchema(schema, maxDepth, ctx, '');
|
|
19
|
+
if (ctx.declarations.length === 0) {
|
|
20
|
+
return root;
|
|
21
|
+
}
|
|
22
|
+
const declStrings = ctx.declarations.map(formatNamedDeclaration);
|
|
23
|
+
const body = declStrings.join('\n\n');
|
|
24
|
+
const lastDeclared = ctx.declarations.at(-1)?.name;
|
|
25
|
+
let content = lastDeclared === root ? body : `${body}\n\n${root}`;
|
|
26
|
+
const ns = options?.namespace;
|
|
27
|
+
if (ns && isValidTypeScriptIdentifier(ns)) {
|
|
28
|
+
content = wrapDeclarationsInNamespace(ns, content);
|
|
29
|
+
}
|
|
30
|
+
const generatedAt = options?.generatedAt ?? new Date().toISOString();
|
|
31
|
+
return `${formatAutogeneratedFileBanner(generatedAt)}${content}`;
|
|
32
|
+
};
|
|
33
|
+
const formatAutogeneratedFileBanner = (generatedAt) => `/**\n * This file is autogenerated. Do not edit it.\n *\n * Generated at: ${generatedAt}\n */\n\n`;
|
|
34
|
+
const wrapDeclarationsInNamespace = (namespace, content) => {
|
|
35
|
+
const body = content
|
|
36
|
+
.split('\n')
|
|
37
|
+
.map((line) => (line === '' ? '' : ` ${line}`))
|
|
38
|
+
.join('\n');
|
|
39
|
+
return `export namespace ${namespace} {\n${body}\n}\n`;
|
|
40
|
+
};
|
|
41
|
+
const formatNamedDeclaration = (d) => {
|
|
42
|
+
const commentBlock = d.comment ? formatTypeCommentAsJsDoc(d.comment) : '';
|
|
43
|
+
const comment = commentBlock ? `${commentBlock}\n` : '';
|
|
44
|
+
return `${comment}export type ${d.name} = ${d.body}`;
|
|
45
|
+
};
|
|
46
|
+
/** Inline JSDoc for one line; multi-line comments use a newline after the opening delimiter and ` * ` on each line. */
|
|
47
|
+
const formatTypeCommentAsJsDoc = (comment) => {
|
|
48
|
+
const lines = comment.split(/\r?\n/);
|
|
49
|
+
const trimmed = lines.map((line) => line.trim());
|
|
50
|
+
while (trimmed.length > 0 && trimmed[0] === '') {
|
|
51
|
+
trimmed.shift();
|
|
52
|
+
}
|
|
53
|
+
while (trimmed.length > 0 && trimmed.at(-1) === '') {
|
|
54
|
+
trimmed.pop();
|
|
55
|
+
}
|
|
56
|
+
if (trimmed.length === 0) {
|
|
57
|
+
return '';
|
|
58
|
+
}
|
|
59
|
+
if (trimmed.length === 1) {
|
|
60
|
+
return `/** ${trimmed[0]} */`;
|
|
61
|
+
}
|
|
62
|
+
const body = trimmed.map((line) => ` * ${line}`).join('\n');
|
|
63
|
+
return `/** \n${body}\n */`;
|
|
64
|
+
};
|
|
65
|
+
/** Prefixes each line of a JSDoc block with `indent` for object property comments. */
|
|
66
|
+
const formatTypeCommentAsIndentedJsDoc = (indent, comment) => {
|
|
67
|
+
const doc = formatTypeCommentAsJsDoc(comment);
|
|
68
|
+
if (!doc) {
|
|
69
|
+
return '';
|
|
70
|
+
}
|
|
71
|
+
return doc
|
|
72
|
+
.split('\n')
|
|
73
|
+
.map((line) => `${indent}${line}`)
|
|
74
|
+
.join('\n');
|
|
75
|
+
};
|
|
76
|
+
const getTypeName = (schema) => {
|
|
77
|
+
if (schema.type === 'lazy' || schema.type === 'evaluate') {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
const name = schema.typeName;
|
|
81
|
+
if (!name || !isValidTypeScriptIdentifier(name)) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
return name;
|
|
85
|
+
};
|
|
86
|
+
const getTypeComment = (schema) => {
|
|
87
|
+
if (schema.type === 'lazy' || schema.type === 'evaluate') {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
if (schema.type === 'optional') {
|
|
91
|
+
return schema.typeComment ?? getTypeComment(schema.schema);
|
|
92
|
+
}
|
|
93
|
+
return schema.typeComment;
|
|
94
|
+
};
|
|
95
|
+
const isValidTypeScriptIdentifier = (name) => /^[$_A-Za-z][$_\w]*$/.test(name);
|
|
96
|
+
const emitSchema = (schema, depth, ctx, braceIndent) => {
|
|
97
|
+
const name = getTypeName(schema);
|
|
98
|
+
if (name) {
|
|
99
|
+
if (ctx.definitions.has(name)) {
|
|
100
|
+
return name;
|
|
101
|
+
}
|
|
102
|
+
if (ctx.inProgress.has(name)) {
|
|
103
|
+
return name;
|
|
104
|
+
}
|
|
105
|
+
ctx.inProgress.add(name);
|
|
106
|
+
const body = structuralEmit(schema, depth, ctx, '');
|
|
107
|
+
ctx.inProgress.delete(name);
|
|
108
|
+
if (!ctx.definitions.has(name)) {
|
|
109
|
+
ctx.definitions.set(name, body);
|
|
110
|
+
ctx.declarations.push({
|
|
111
|
+
name,
|
|
112
|
+
body,
|
|
113
|
+
comment: getTypeComment(schema),
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
return name;
|
|
117
|
+
}
|
|
118
|
+
return structuralEmit(schema, depth, ctx, braceIndent);
|
|
119
|
+
};
|
|
120
|
+
const structuralEmit = (schema, depth, ctx, braceIndent) => {
|
|
121
|
+
if (depth <= 0) {
|
|
122
|
+
return 'any';
|
|
123
|
+
}
|
|
124
|
+
const next = depth - 1;
|
|
125
|
+
switch (schema.type) {
|
|
126
|
+
case 'number':
|
|
127
|
+
return 'number';
|
|
128
|
+
case 'string':
|
|
129
|
+
return 'string';
|
|
130
|
+
case 'boolean':
|
|
131
|
+
return 'boolean';
|
|
132
|
+
case 'nullable':
|
|
133
|
+
return 'null';
|
|
134
|
+
case 'notDefined':
|
|
135
|
+
return 'undefined';
|
|
136
|
+
case 'any':
|
|
137
|
+
return 'any';
|
|
138
|
+
case 'array': {
|
|
139
|
+
const item = emitSchema(schema.items, next, ctx, braceIndent);
|
|
140
|
+
return needsArrayItemParen(item) ? `(${item})[]` : `${item}[]`;
|
|
141
|
+
}
|
|
142
|
+
case 'record': {
|
|
143
|
+
const key = emitSchema(schema.key, next, ctx, braceIndent);
|
|
144
|
+
const value = emitSchema(schema.value, next, ctx, braceIndent);
|
|
145
|
+
return `Record<${key}, ${value}>`;
|
|
146
|
+
}
|
|
147
|
+
case 'object': {
|
|
148
|
+
const entries = Object.entries(schema.properties);
|
|
149
|
+
if (entries.length === 0) {
|
|
150
|
+
return '{}';
|
|
151
|
+
}
|
|
152
|
+
const keyIndent = `${braceIndent} `;
|
|
153
|
+
const props = entries.map(([key, child]) => {
|
|
154
|
+
const tsKey = /^[$_a-zA-Z][$_\w]*$/.test(key) ? key : JSON.stringify(key);
|
|
155
|
+
const optionalProp = child.type === 'optional';
|
|
156
|
+
const valueSchema = optionalProp ? child.schema : child;
|
|
157
|
+
const value = emitSchema(valueSchema, next, ctx, keyIndent);
|
|
158
|
+
const propComment = getTypeComment(child);
|
|
159
|
+
const docBlock = propComment ? formatTypeCommentAsIndentedJsDoc(keyIndent, propComment) : '';
|
|
160
|
+
const propLine = optionalProp ? `${keyIndent}${tsKey}?: ${value};` : `${keyIndent}${tsKey}: ${value};`;
|
|
161
|
+
return docBlock ? `${docBlock}\n${propLine}` : propLine;
|
|
162
|
+
});
|
|
163
|
+
return `{\n${props.join('\n')}\n${braceIndent}}`;
|
|
164
|
+
}
|
|
165
|
+
case 'optional': {
|
|
166
|
+
const inner = emitSchema(schema.schema, next, ctx, braceIndent);
|
|
167
|
+
return `${wrapUnionMember(inner)} | undefined`;
|
|
168
|
+
}
|
|
169
|
+
case 'union':
|
|
170
|
+
if (schema.schemas.length === 0) {
|
|
171
|
+
return 'never';
|
|
172
|
+
}
|
|
173
|
+
return schema.schemas.map((s) => wrapUnionMember(emitSchema(s, next, ctx, braceIndent))).join(' | ');
|
|
174
|
+
case 'intersection':
|
|
175
|
+
if (schema.schemas.length === 0) {
|
|
176
|
+
return 'unknown';
|
|
177
|
+
}
|
|
178
|
+
return schema.schemas.map((s) => wrapIntersectionMember(emitSchema(s, next, ctx, braceIndent))).join(' & ');
|
|
179
|
+
case 'literal':
|
|
180
|
+
return literalToTs(schema.value);
|
|
181
|
+
case 'lazy':
|
|
182
|
+
return emitSchema(schema.schema(), next, ctx, braceIndent);
|
|
183
|
+
case 'evaluate':
|
|
184
|
+
return emitSchema(schema.schema, next, ctx, braceIndent);
|
|
185
|
+
default: {
|
|
186
|
+
const _exhaustive = schema;
|
|
187
|
+
return _exhaustive;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
const literalToTs = (value) => {
|
|
192
|
+
if (typeof value === 'bigint') {
|
|
193
|
+
return `${value}n`;
|
|
194
|
+
}
|
|
195
|
+
return JSON.stringify(value);
|
|
196
|
+
};
|
|
197
|
+
const needsArrayItemParen = (t) => {
|
|
198
|
+
if (t === 'number' || t === 'string' || t === 'boolean' || t === 'null' || t === 'undefined' || t === 'any') {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
// Union: `A | B[]` is `A | (B[])`; intersection: `A & B[]` is `A & (B[])`. Wrap the whole item type.
|
|
202
|
+
return t.includes(' | ') || t.includes(' & ');
|
|
203
|
+
};
|
|
204
|
+
const wrapUnionMember = (t) => {
|
|
205
|
+
if (t === 'number' || t === 'string' || t === 'boolean' || t === 'null' || t === 'undefined' || t === 'any') {
|
|
206
|
+
return t;
|
|
207
|
+
}
|
|
208
|
+
if (/^(?:-?(?:\d+(?:\.\d+)?|\.\d+)(?:[eE][+-]?\d+)?|-?\d+n|"(?:[^"\\]|\\.)*"|true|false)$/.test(t)) {
|
|
209
|
+
return t;
|
|
210
|
+
}
|
|
211
|
+
if (/^[$_A-Za-z][$_\w]*$/.test(t)) {
|
|
212
|
+
return t;
|
|
213
|
+
}
|
|
214
|
+
return `(${t})`;
|
|
215
|
+
};
|
|
216
|
+
const wrapIntersectionMember = (t) => {
|
|
217
|
+
if (t.includes(' | ') || t.includes(' & ')) {
|
|
218
|
+
return `(${t})`;
|
|
219
|
+
}
|
|
220
|
+
return t;
|
|
221
|
+
};
|