@scalar/validation 0.3.1 → 0.5.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/CHANGELOG.md +18 -0
- package/dist/coerce.d.ts.map +1 -1
- package/dist/coerce.js +10 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/schema.d.ts +30 -5
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +14 -1
- package/dist/typegen.d.ts +6 -0
- package/dist/typegen.d.ts.map +1 -1
- package/dist/typegen.js +40 -4
- package/dist/types.d.ts +2 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/validate.d.ts +2 -0
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +6 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @scalar/validation
|
|
2
2
|
|
|
3
|
+
## 0.5.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#9211](https://github.com/scalar/scalar/pull/9211): feat: support default values for coersion
|
|
8
|
+
|
|
9
|
+
## 0.4.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- [#8844](https://github.com/scalar/scalar/pull/8844): feat: support default values for coersion
|
|
14
|
+
|
|
15
|
+
## 0.3.2
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- [#9045](https://github.com/scalar/scalar/pull/9045): chore(validation): include package publish files
|
|
20
|
+
|
|
3
21
|
## 0.3.1
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
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;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,
|
|
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,CAmHV,CAAA"}
|
package/dist/coerce.js
CHANGED
|
@@ -114,26 +114,32 @@ export const coerce = (schema, value, cache = new WeakMap()) => {
|
|
|
114
114
|
if (!schema) {
|
|
115
115
|
return value;
|
|
116
116
|
}
|
|
117
|
-
if (schema.type === 'any') {
|
|
117
|
+
if (schema.type === 'any' || schema.type === 'unknown') {
|
|
118
118
|
return value;
|
|
119
119
|
}
|
|
120
|
+
if (schema.type === 'function') {
|
|
121
|
+
if (typeof value === 'function') {
|
|
122
|
+
return value;
|
|
123
|
+
}
|
|
124
|
+
return (() => undefined);
|
|
125
|
+
}
|
|
120
126
|
if (schema.type === 'number') {
|
|
121
127
|
if (validate(schema, value)) {
|
|
122
128
|
return value;
|
|
123
129
|
}
|
|
124
|
-
return 0;
|
|
130
|
+
return (schema.default ?? 0);
|
|
125
131
|
}
|
|
126
132
|
if (schema.type === 'string') {
|
|
127
133
|
if (validate(schema, value)) {
|
|
128
134
|
return value;
|
|
129
135
|
}
|
|
130
|
-
return '';
|
|
136
|
+
return (schema.default ?? '');
|
|
131
137
|
}
|
|
132
138
|
if (schema.type === 'boolean') {
|
|
133
139
|
if (validate(schema, value)) {
|
|
134
140
|
return value;
|
|
135
141
|
}
|
|
136
|
-
return false;
|
|
142
|
+
return (schema.default ?? false);
|
|
137
143
|
}
|
|
138
144
|
if (schema.type === 'nullable') {
|
|
139
145
|
return null;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { coerce } from './coerce.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';
|
|
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, type UnknownSchema, type FunctionSchema, any, array, boolean, evaluate, fn, intersection, lazy, literal, notDefined, nullable, number, object, optional, record, string, union, unknown, } from './schema.js';
|
|
3
3
|
export { type GenerateTypesOptions, generateTypes } from './typegen.js';
|
|
4
4
|
export type { Static } from './types.js';
|
|
5
5
|
export { validate } from './validate.js';
|
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,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,
|
|
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,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,GAAG,EACH,KAAK,EACL,OAAO,EACP,QAAQ,EACR,EAAE,EACF,YAAY,EACZ,IAAI,EACJ,OAAO,EACP,UAAU,EACV,QAAQ,EACR,MAAM,EACN,MAAM,EACN,QAAQ,EACR,MAAM,EACN,MAAM,EACN,KAAK,EACL,OAAO,GACR,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,4 +1,4 @@
|
|
|
1
1
|
export { coerce } from './coerce.js';
|
|
2
|
-
export { any, array, boolean, evaluate, intersection, lazy, literal, notDefined, nullable, number, object, optional, record, string, union, } from './schema.js';
|
|
2
|
+
export { any, array, boolean, evaluate, fn, intersection, lazy, literal, notDefined, nullable, number, object, optional, record, string, union, unknown, } from './schema.js';
|
|
3
3
|
export { generateTypes } from './typegen.js';
|
|
4
4
|
export { validate } from './validate.js';
|
package/dist/schema.d.ts
CHANGED
|
@@ -12,14 +12,17 @@ type Documentation = Partial<{
|
|
|
12
12
|
/** Schema for finite numeric values. {@link Static} resolves to `number`. */
|
|
13
13
|
export type NumberSchema = {
|
|
14
14
|
type: 'number';
|
|
15
|
+
default?: number;
|
|
15
16
|
} & Documentation;
|
|
16
17
|
/** Schema for string values. {@link Static} resolves to `string`. */
|
|
17
18
|
export type StringSchema = {
|
|
18
19
|
type: 'string';
|
|
20
|
+
default?: string;
|
|
19
21
|
} & Documentation;
|
|
20
22
|
/** Schema for boolean values. {@link Static} resolves to `boolean`. */
|
|
21
23
|
export type BooleanSchema = {
|
|
22
24
|
type: 'boolean';
|
|
25
|
+
default?: boolean;
|
|
23
26
|
} & Documentation;
|
|
24
27
|
/** Schema for `null`. {@link Static} resolves to `null`. */
|
|
25
28
|
export type NullableSchema = {
|
|
@@ -33,6 +36,20 @@ export type NotDefinedSchema = {
|
|
|
33
36
|
export type AnySchema = {
|
|
34
37
|
type: 'any';
|
|
35
38
|
} & Documentation;
|
|
39
|
+
/** Schema that accepts any value. {@link Static} resolves to `unknown` instead of `any`. */
|
|
40
|
+
export type UnknownSchema = {
|
|
41
|
+
type: 'unknown';
|
|
42
|
+
} & Documentation;
|
|
43
|
+
/**
|
|
44
|
+
* Schema for function values. Validates only that the value is `typeof === 'function'`.
|
|
45
|
+
* The type parameter `T` carries the full function signature for {@link Static} inference
|
|
46
|
+
* but is never checked at runtime.
|
|
47
|
+
*/
|
|
48
|
+
export type FunctionSchema<T extends (...args: any[]) => any = (...args: unknown[]) => unknown> = {
|
|
49
|
+
type: 'function';
|
|
50
|
+
/** Phantom field — never set at runtime. Carries `T` so that `Static` can extract it. */
|
|
51
|
+
_fn?: T;
|
|
52
|
+
} & Documentation;
|
|
36
53
|
/** Schema for homogeneous lists. {@link Static} resolves to an array of the item static type. */
|
|
37
54
|
export type ArraySchema<Item extends Schema> = {
|
|
38
55
|
type: 'array';
|
|
@@ -93,13 +110,21 @@ export type EvaluateSchema<S extends Schema> = {
|
|
|
93
110
|
expression: (value: unknown) => unknown;
|
|
94
111
|
schema: S;
|
|
95
112
|
};
|
|
96
|
-
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> | UnionSchema<ObjectSchema<any>[]>)[]> | LiteralSchema<any> | LazySchema<any> | EvaluateSchema<any>;
|
|
97
|
-
declare const number: (options?: Documentation
|
|
98
|
-
|
|
99
|
-
|
|
113
|
+
export type Schema = NumberSchema | StringSchema | BooleanSchema | NullableSchema | NotDefinedSchema | AnySchema | UnknownSchema | FunctionSchema<any> | ArraySchema<any> | RecordSchema<any, any> | ObjectSchema<Record<string, any>> | UnionSchema<any[]> | OptionalSchema<any> | IntersectionSchema<readonly (ObjectSchema<any> | UnionSchema<ObjectSchema<any>[]>)[]> | LiteralSchema<any> | LazySchema<any> | EvaluateSchema<any>;
|
|
114
|
+
declare const number: (options?: Documentation & {
|
|
115
|
+
default?: number;
|
|
116
|
+
}) => NumberSchema;
|
|
117
|
+
declare const string: (options?: Documentation & {
|
|
118
|
+
default?: string;
|
|
119
|
+
}) => StringSchema;
|
|
120
|
+
declare const boolean: (options?: Documentation & {
|
|
121
|
+
default?: boolean;
|
|
122
|
+
}) => BooleanSchema;
|
|
100
123
|
declare const nullable: (options?: Documentation) => NullableSchema;
|
|
101
124
|
declare const notDefined: (options?: Documentation) => NotDefinedSchema;
|
|
102
125
|
declare const any: (options?: Documentation) => AnySchema;
|
|
126
|
+
declare const unknown: (options?: Documentation) => UnknownSchema;
|
|
127
|
+
declare const fn: <T extends (...args: any[]) => any = (...args: unknown[]) => unknown>(options?: Documentation) => FunctionSchema<T>;
|
|
103
128
|
declare const array: <Item extends Schema>(items: Item, options?: Documentation) => ArraySchema<Item>;
|
|
104
129
|
declare const record: <Key extends StringSchema | AnySchema, Value extends Schema>(key: Key, value: Value, options?: Documentation) => RecordSchema<Key, Value>;
|
|
105
130
|
declare const object: <Properties extends Record<string, Schema>>(properties: Properties, options?: Documentation) => ObjectSchema<Properties>;
|
|
@@ -109,5 +134,5 @@ declare const optional: <S extends Schema>(schema: S, options?: Documentation) =
|
|
|
109
134
|
declare const literal: <Value extends string | number | boolean | bigint>(value: Value) => LiteralSchema<Value>;
|
|
110
135
|
declare const lazy: <S extends () => Schema>(schema: S) => LazySchema<S>;
|
|
111
136
|
declare const evaluate: <S extends Schema>(expression: (value: unknown) => unknown, schema: S) => EvaluateSchema<S>;
|
|
112
|
-
export { number, string, boolean, nullable, notDefined, any, array, record, object, union, intersection, optional, literal, lazy, evaluate, };
|
|
137
|
+
export { number, string, boolean, nullable, notDefined, any, unknown, fn, array, record, object, union, intersection, optional, literal, lazy, evaluate, };
|
|
113
138
|
//# 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;;;;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,QAAQ,CAAA;
|
|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O,SAAS,SAAS,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,EAAE,EAC3G,SAAS,OAAO,EAChB,UAAU,aAAa,KACtB,kBAAkB,CAAC,OAAO,CAK3B,CAAA;AAEF,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,OAAO,EACP,EAAE,EACF,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,15 +1,18 @@
|
|
|
1
1
|
const number = (options) => ({
|
|
2
2
|
type: 'number',
|
|
3
|
+
default: options?.default,
|
|
3
4
|
typeName: options?.typeName,
|
|
4
5
|
typeComment: options?.typeComment,
|
|
5
6
|
});
|
|
6
7
|
const string = (options) => ({
|
|
7
8
|
type: 'string',
|
|
9
|
+
default: options?.default,
|
|
8
10
|
typeName: options?.typeName,
|
|
9
11
|
typeComment: options?.typeComment,
|
|
10
12
|
});
|
|
11
13
|
const boolean = (options) => ({
|
|
12
14
|
type: 'boolean',
|
|
15
|
+
default: options?.default,
|
|
13
16
|
typeName: options?.typeName,
|
|
14
17
|
typeComment: options?.typeComment,
|
|
15
18
|
});
|
|
@@ -28,6 +31,16 @@ const any = (options) => ({
|
|
|
28
31
|
typeName: options?.typeName,
|
|
29
32
|
typeComment: options?.typeComment,
|
|
30
33
|
});
|
|
34
|
+
const unknown = (options) => ({
|
|
35
|
+
type: 'unknown',
|
|
36
|
+
typeName: options?.typeName,
|
|
37
|
+
typeComment: options?.typeComment,
|
|
38
|
+
});
|
|
39
|
+
const fn = (options) => ({
|
|
40
|
+
type: 'function',
|
|
41
|
+
typeName: options?.typeName,
|
|
42
|
+
typeComment: options?.typeComment,
|
|
43
|
+
});
|
|
31
44
|
const array = (items, options) => ({
|
|
32
45
|
type: 'array',
|
|
33
46
|
items,
|
|
@@ -78,4 +91,4 @@ const evaluate = (expression, schema) => ({
|
|
|
78
91
|
expression,
|
|
79
92
|
schema,
|
|
80
93
|
});
|
|
81
|
-
export { number, string, boolean, nullable, notDefined, any, array, record, object, union, intersection, optional, literal, lazy, evaluate, };
|
|
94
|
+
export { number, string, boolean, nullable, notDefined, any, unknown, fn, array, record, object, union, intersection, optional, literal, lazy, evaluate, };
|
package/dist/typegen.d.ts
CHANGED
|
@@ -11,6 +11,12 @@ export type GenerateTypesOptions = {
|
|
|
11
11
|
* trailing root type) in `export namespace Name { ... }` so consumers reference `Name.SomeType`.
|
|
12
12
|
*/
|
|
13
13
|
namespace?: string;
|
|
14
|
+
/**
|
|
15
|
+
* When set to a valid TypeScript identifier, the root schema is emitted as
|
|
16
|
+
* `export type <typeName> = …` instead of an anonymous inline type.
|
|
17
|
+
* Overrides any `typeName` already present on the schema itself.
|
|
18
|
+
*/
|
|
19
|
+
typeName?: string;
|
|
14
20
|
};
|
|
15
21
|
/**
|
|
16
22
|
* Returns TypeScript for the schema: named `typeName` nodes become `export type` aliases (once each),
|
package/dist/typegen.d.ts.map
CHANGED
|
@@ -1 +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;
|
|
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;IAClB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,CAAA;AAcD;;;;;;;;GAQG;AACH,eAAO,MAAM,aAAa,GAAI,QAAQ,MAAM,EAAE,UAAU,oBAAoB,KAAG,MA0C9E,CAAA"}
|
package/dist/typegen.js
CHANGED
|
@@ -10,19 +10,35 @@ const DEFAULT_MAX_DEPTH = 10;
|
|
|
10
10
|
*/
|
|
11
11
|
export const generateTypes = (schema, options) => {
|
|
12
12
|
const maxDepth = options?.maxDepth ?? DEFAULT_MAX_DEPTH;
|
|
13
|
+
const rootTypeName = options?.typeName && isValidTypeScriptIdentifier(options.typeName) ? options.typeName : undefined;
|
|
13
14
|
const ctx = {
|
|
14
15
|
definitions: new Map(),
|
|
15
16
|
declarations: [],
|
|
16
17
|
inProgress: new Set(),
|
|
17
18
|
};
|
|
18
|
-
|
|
19
|
+
// Override the schema's own typeName so the root is emitted under the
|
|
20
|
+
// caller-specified name. This also handles schemas that already carry a
|
|
21
|
+
// typeName — the option wins.
|
|
22
|
+
let effectiveSchema = schema;
|
|
23
|
+
if (rootTypeName) {
|
|
24
|
+
effectiveSchema = { ...schema, typeName: rootTypeName };
|
|
25
|
+
}
|
|
26
|
+
const root = emitSchema(effectiveSchema, maxDepth, ctx, '');
|
|
27
|
+
// When a root typeName is provided and the schema did not already emit it as
|
|
28
|
+
// a named declaration (e.g. intersection / union schemas have no typeName
|
|
29
|
+
// slot), register it now so the output contains an explicit
|
|
30
|
+
// `export type <typeName> = …` instead of a bare anonymous type.
|
|
31
|
+
if (rootTypeName && root !== rootTypeName) {
|
|
32
|
+
ctx.definitions.set(rootTypeName, root);
|
|
33
|
+
ctx.declarations.push({ name: rootTypeName, body: root });
|
|
34
|
+
}
|
|
19
35
|
if (ctx.declarations.length === 0) {
|
|
20
36
|
return root;
|
|
21
37
|
}
|
|
22
38
|
const declStrings = ctx.declarations.map(formatNamedDeclaration);
|
|
23
39
|
const body = declStrings.join('\n\n');
|
|
24
40
|
const lastDeclared = ctx.declarations.at(-1)?.name;
|
|
25
|
-
let content = lastDeclared === root ? body : `${body}\n\n${root}`;
|
|
41
|
+
let content = lastDeclared === root || lastDeclared === rootTypeName ? body : `${body}\n\n${root}`;
|
|
26
42
|
const ns = options?.namespace;
|
|
27
43
|
if (ns && isValidTypeScriptIdentifier(ns)) {
|
|
28
44
|
content = wrapDeclarationsInNamespace(ns, content);
|
|
@@ -135,6 +151,10 @@ const structuralEmit = (schema, depth, ctx, braceIndent) => {
|
|
|
135
151
|
return 'undefined';
|
|
136
152
|
case 'any':
|
|
137
153
|
return 'any';
|
|
154
|
+
case 'unknown':
|
|
155
|
+
return 'unknown';
|
|
156
|
+
case 'function':
|
|
157
|
+
return '(...args: unknown[]) => unknown';
|
|
138
158
|
case 'array': {
|
|
139
159
|
const item = emitSchema(schema.items, next, ctx, braceIndent);
|
|
140
160
|
return needsArrayItemParen(item) ? `(${item})[]` : `${item}[]`;
|
|
@@ -195,14 +215,30 @@ const literalToTs = (value) => {
|
|
|
195
215
|
return JSON.stringify(value);
|
|
196
216
|
};
|
|
197
217
|
const needsArrayItemParen = (t) => {
|
|
198
|
-
if (t === 'number' ||
|
|
218
|
+
if (t === 'number' ||
|
|
219
|
+
t === 'string' ||
|
|
220
|
+
t === 'boolean' ||
|
|
221
|
+
t === 'null' ||
|
|
222
|
+
t === 'undefined' ||
|
|
223
|
+
t === 'any' ||
|
|
224
|
+
t === 'unknown') {
|
|
199
225
|
return false;
|
|
200
226
|
}
|
|
227
|
+
// `() => void[]` parses as `() => (void[])`, so function signatures need wrapping.
|
|
228
|
+
if (t.includes(' => ')) {
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
201
231
|
// Union: `A | B[]` is `A | (B[])`; intersection: `A & B[]` is `A & (B[])`. Wrap the whole item type.
|
|
202
232
|
return t.includes(' | ') || t.includes(' & ');
|
|
203
233
|
};
|
|
204
234
|
const wrapUnionMember = (t) => {
|
|
205
|
-
if (t === 'number' ||
|
|
235
|
+
if (t === 'number' ||
|
|
236
|
+
t === 'string' ||
|
|
237
|
+
t === 'boolean' ||
|
|
238
|
+
t === 'null' ||
|
|
239
|
+
t === 'undefined' ||
|
|
240
|
+
t === 'any' ||
|
|
241
|
+
t === 'unknown') {
|
|
206
242
|
return t;
|
|
207
243
|
}
|
|
208
244
|
if (/^(?:-?(?:\d+(?:\.\d+)?|\.\d+)(?:[eE][+-]?\d+)?|-?\d+n|"(?:[^"\\]|\\.)*"|true|false)$/.test(t)) {
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AnySchema, ArraySchema, BooleanSchema, EvaluateSchema, IntersectionSchema, LazySchema, LiteralSchema, NotDefinedSchema, NullableSchema, NumberSchema, ObjectSchema, OptionalSchema, RecordSchema, Schema, StringSchema, UnionSchema } from './schema.js';
|
|
1
|
+
import type { AnySchema, ArraySchema, BooleanSchema, EvaluateSchema, FunctionSchema, IntersectionSchema, LazySchema, LiteralSchema, NotDefinedSchema, NullableSchema, NumberSchema, ObjectSchema, OptionalSchema, RecordSchema, Schema, StringSchema, UnionSchema, UnknownSchema } from './schema.js';
|
|
2
2
|
export type Static<T> = _Static<T, 10>;
|
|
3
3
|
/**
|
|
4
4
|
* Folds intersection member schemas into an intersection of their static types.
|
|
@@ -22,7 +22,7 @@ type ObjectStatics<Properties, Depth extends number> = [keyof Properties] extend
|
|
|
22
22
|
} & {
|
|
23
23
|
[K in OptionalPropertyKeys<Properties>]?: _Static<OptionalSchemaInner<Properties[K]>, Prev<Depth>>;
|
|
24
24
|
};
|
|
25
|
-
type _Static<T, Depth extends number = 10> = Depth extends 0 ? any : T extends LiteralSchema<infer Value> ? Value : T extends NumberSchema ? number : T extends StringSchema ? string : T extends BooleanSchema ? boolean : T extends NullableSchema ? null : T extends NotDefinedSchema ? undefined : T extends AnySchema ? any : T extends ArraySchema<infer Item> ? Array<_Static<Item, Prev<Depth>>> : T extends RecordSchema<infer Key, infer Value> ? Record<_Static<Key, Prev<Depth>> & PropertyKey, _Static<Value, Prev<Depth>>> : T extends ObjectSchema<infer Properties> ? ObjectStatics<Properties, Depth> : T extends OptionalSchema<infer S> ? _Static<S, Prev<Depth>> | undefined : T extends IntersectionSchema<infer Schemas> ? IntersectObjectStatics<Schemas, Prev<Depth>> : T extends UnionSchema<infer Schemas> ? _Static<Schemas[number], Prev<Depth>> : T extends EvaluateSchema<infer S> ? _Static<S, Prev<Depth>> : T extends LazySchema<infer S> ? _Static<ReturnType<S>, Prev<Depth>> : never;
|
|
25
|
+
type _Static<T, Depth extends number = 10> = Depth extends 0 ? any : T extends LiteralSchema<infer Value> ? Value : T extends NumberSchema ? number : T extends StringSchema ? string : T extends BooleanSchema ? boolean : T extends NullableSchema ? null : T extends NotDefinedSchema ? undefined : T extends AnySchema ? any : T extends UnknownSchema ? unknown : T extends FunctionSchema<infer F> ? F : T extends ArraySchema<infer Item> ? Array<_Static<Item, Prev<Depth>>> : T extends RecordSchema<infer Key, infer Value> ? Record<_Static<Key, Prev<Depth>> & PropertyKey, _Static<Value, Prev<Depth>>> : T extends ObjectSchema<infer Properties> ? ObjectStatics<Properties, Depth> : T extends OptionalSchema<infer S> ? _Static<S, Prev<Depth>> | undefined : T extends IntersectionSchema<infer Schemas> ? IntersectObjectStatics<Schemas, Prev<Depth>> : T extends UnionSchema<infer Schemas> ? _Static<Schemas[number], Prev<Depth>> : T extends EvaluateSchema<infer S> ? _Static<S, Prev<Depth>> : T extends LazySchema<infer S> ? _Static<ReturnType<S>, Prev<Depth>> : never;
|
|
26
26
|
type Prev<T extends number> = T extends 10 ? 9 : T extends 9 ? 8 : T extends 8 ? 7 : T extends 7 ? 6 : T extends 6 ? 5 : T extends 5 ? 4 : T extends 4 ? 3 : T extends 3 ? 2 : T extends 2 ? 1 : T extends 1 ? 0 : 0;
|
|
27
27
|
export {};
|
|
28
28
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,SAAS,EACT,WAAW,EACX,aAAa,EACb,cAAc,EACd,kBAAkB,EAClB,UAAU,EACV,aAAa,EACb,gBAAgB,EAChB,cAAc,EACd,YAAY,EACZ,YAAY,EACZ,cAAc,EACd,YAAY,EACZ,MAAM,EACN,YAAY,EACZ,WAAW,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,SAAS,EACT,WAAW,EACX,aAAa,EACb,cAAc,EACd,cAAc,EACd,kBAAkB,EAClB,UAAU,EACV,aAAa,EACb,gBAAgB,EAChB,cAAc,EACd,YAAY,EACZ,YAAY,EACZ,cAAc,EACd,YAAY,EACZ,MAAM,EACN,YAAY,EACZ,WAAW,EACX,aAAa,EACd,MAAM,UAAU,CAA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-5,6 +5,8 @@ import type { Schema } from './schema.js';
|
|
|
5
5
|
* The schema describes the expected structure/type of data.
|
|
6
6
|
* Supported schema types include:
|
|
7
7
|
* - 'any': Accepts any value.
|
|
8
|
+
* - 'unknown': Accepts any value (generates `unknown` instead of `any` in types).
|
|
9
|
+
* - 'function': Only functions are valid (signature is not checked at runtime).
|
|
8
10
|
* - 'number': Only numbers are valid.
|
|
9
11
|
* - 'string': Only strings are valid.
|
|
10
12
|
* - 'boolean': Only booleans are valid.
|
package/dist/validate.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../src/validate.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AAEtC
|
|
1
|
+
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../src/validate.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AAEtC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,eAAO,MAAM,QAAQ,GAAI,QAAQ,MAAM,GAAG,SAAS,EAAE,OAAO,OAAO,KAAG,OAwErE,CAAA"}
|
package/dist/validate.js
CHANGED
|
@@ -5,6 +5,8 @@ import { isObject } from './helpers/is-object.js';
|
|
|
5
5
|
* The schema describes the expected structure/type of data.
|
|
6
6
|
* Supported schema types include:
|
|
7
7
|
* - 'any': Accepts any value.
|
|
8
|
+
* - 'unknown': Accepts any value (generates `unknown` instead of `any` in types).
|
|
9
|
+
* - 'function': Only functions are valid (signature is not checked at runtime).
|
|
8
10
|
* - 'number': Only numbers are valid.
|
|
9
11
|
* - 'string': Only strings are valid.
|
|
10
12
|
* - 'boolean': Only booleans are valid.
|
|
@@ -36,9 +38,12 @@ export const validate = (schema, value) => {
|
|
|
36
38
|
if (!schema) {
|
|
37
39
|
return false;
|
|
38
40
|
}
|
|
39
|
-
if (schema.type === 'any') {
|
|
41
|
+
if (schema.type === 'any' || schema.type === 'unknown') {
|
|
40
42
|
return true;
|
|
41
43
|
}
|
|
44
|
+
if (schema.type === 'function') {
|
|
45
|
+
return typeof value === 'function';
|
|
46
|
+
}
|
|
42
47
|
if (schema.type === 'number') {
|
|
43
48
|
return typeof value === 'number' && !Number.isNaN(value) && Number.isFinite(value);
|
|
44
49
|
}
|