typebox 1.0.21 → 1.0.23
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.
|
@@ -4,7 +4,7 @@ import { Any } from '../../types/any.mjs';
|
|
|
4
4
|
import { Array, IsArray, ArrayOptions } from '../../types/array.mjs';
|
|
5
5
|
import { AsyncIterator, IsAsyncIterator } from '../../types/async-iterator.mjs';
|
|
6
6
|
import { Constructor, IsConstructor } from '../../types/constructor.mjs';
|
|
7
|
-
import { Function, IsFunction } from '../../types/function.mjs';
|
|
7
|
+
import { Function as _Function, IsFunction } from '../../types/function.mjs';
|
|
8
8
|
import { Intersect, IsIntersect } from '../../types/intersect.mjs';
|
|
9
9
|
import { Iterator, IsIterator } from '../../types/iterator.mjs';
|
|
10
10
|
import { Object, IsObject } from '../../types/object.mjs';
|
|
@@ -32,7 +32,7 @@ function FromType(type) {
|
|
|
32
32
|
IsArray(type) ? Array(FromType(type.items), ArrayOptions(type)) :
|
|
33
33
|
IsAsyncIterator(type) ? AsyncIterator(FromType(type.iteratorItems)) :
|
|
34
34
|
IsConstructor(type) ? Constructor(FromTypes(type.parameters), FromType(type.instanceType)) :
|
|
35
|
-
IsFunction(type) ?
|
|
35
|
+
IsFunction(type) ? _Function(FromTypes(type.parameters), FromType(type.returnType)) :
|
|
36
36
|
IsIntersect(type) ? Intersect(FromTypes(type.allOf)) :
|
|
37
37
|
IsIterator(type) ? Iterator(FromType(type.iteratorItems)) :
|
|
38
38
|
IsObject(type) ? Object(FromProperties(type.properties)) :
|
|
@@ -14,7 +14,7 @@ import { Array, IsArray, ArrayOptions } from '../types/array.mjs';
|
|
|
14
14
|
import { AsyncIterator, IsAsyncIterator, AsyncIteratorOptions } from '../types/async-iterator.mjs';
|
|
15
15
|
import { Constructor, IsConstructor, ConstructorOptions } from '../types/constructor.mjs';
|
|
16
16
|
import { Deferred, IsDeferred } from '../types/deferred.mjs';
|
|
17
|
-
import { Function, IsFunction, FunctionOptions } from '../types/function.mjs';
|
|
17
|
+
import { Function as _Function, IsFunction, FunctionOptions } from '../types/function.mjs';
|
|
18
18
|
import { IsCall } from '../types/call.mjs';
|
|
19
19
|
import { Intersect, IsIntersect, IntersectOptions } from '../types/intersect.mjs';
|
|
20
20
|
import { Iterator, IsIterator, IteratorOptions } from '../types/iterator.mjs';
|
|
@@ -147,7 +147,7 @@ export function InstantiateType(context, state, input) {
|
|
|
147
147
|
IsCall(type) ? CallInstantiate(context, state, type.target, type.arguments) :
|
|
148
148
|
IsConstructor(type) ? Constructor(InstantiateTypes(context, state, type.parameters), InstantiateType(context, state, type.instanceType), ConstructorOptions(type)) :
|
|
149
149
|
IsDeferred(type) ? InstantiateDeferred(context, state, type.action, type.parameters, type.options) :
|
|
150
|
-
IsFunction(type) ?
|
|
150
|
+
IsFunction(type) ? _Function(InstantiateTypes(context, state, type.parameters), InstantiateType(context, state, type.returnType), FunctionOptions(type)) :
|
|
151
151
|
IsIntersect(type) ? Intersect(InstantiateTypes(context, state, type.allOf), IntersectOptions(type)) :
|
|
152
152
|
IsIterator(type) ? Iterator(InstantiateType(context, state, type.iteratorItems), IteratorOptions(type)) :
|
|
153
153
|
IsObject(type) ? Object(InstantiateProperties(context, state, type.properties), ObjectOptions(type)) :
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
-
import { IsLocalizedValidationError } from '../../error/index.mjs';
|
|
3
|
-
import { Guard } from '../../guard/index.mjs';
|
|
4
2
|
import { IsKind } from './schema.mjs';
|
|
5
3
|
// ------------------------------------------------------------------
|
|
6
4
|
// BaseNotImplemented
|
|
@@ -16,31 +14,8 @@ export class BaseNotImplemented extends Error {
|
|
|
16
14
|
});
|
|
17
15
|
}
|
|
18
16
|
}
|
|
19
|
-
// ------------------------------------------------------------------------------------
|
|
20
|
-
// BaseValidator
|
|
21
|
-
// ------------------------------------------------------------------------------------
|
|
22
|
-
class BaseValidator {
|
|
23
|
-
constructor(check, errors) {
|
|
24
|
-
this.check = check;
|
|
25
|
-
this.errors = errors;
|
|
26
|
-
this.vendor = 'typebox';
|
|
27
|
-
this.version = 1;
|
|
28
|
-
this.validate = (value) => {
|
|
29
|
-
return this.check(value)
|
|
30
|
-
? this.Success(value)
|
|
31
|
-
: this.Failure(this.errors(value));
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
Success(value) {
|
|
35
|
-
return { value };
|
|
36
|
-
}
|
|
37
|
-
Failure(errors) {
|
|
38
|
-
const issues = errors.reduce((result, error) => [...result, ...CreateIssues(error)], []);
|
|
39
|
-
return { issues };
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
17
|
// ------------------------------------------------------------------
|
|
43
|
-
// Type
|
|
18
|
+
// Type.Base<...>
|
|
44
19
|
// ------------------------------------------------------------------
|
|
45
20
|
/** Base class for creating extension types. */
|
|
46
21
|
export class Base {
|
|
@@ -86,54 +61,25 @@ export class Base {
|
|
|
86
61
|
export function IsBase(value) {
|
|
87
62
|
return IsKind(value, 'Base');
|
|
88
63
|
}
|
|
89
|
-
//
|
|
90
|
-
//
|
|
91
|
-
//
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const message = Guard.IsString(issue.message) ? issue.message : 'unknown';
|
|
111
|
-
const path = Guard.IsArray(issue.path) ? [...leading, ...issue.path] : leading;
|
|
112
|
-
return { message, path };
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
function IssuesFromRegularError(error) {
|
|
116
|
-
const path = PathSegments(error.instancePath);
|
|
117
|
-
return [{ path, message: error.message }];
|
|
118
|
-
}
|
|
119
|
-
function IssuesFromLocalizedError(error) {
|
|
120
|
-
return IsStandardSchemaV1Error(error)
|
|
121
|
-
? IssuesFromStandardSchemaV1Error(error)
|
|
122
|
-
: IssuesFromRegularError(error);
|
|
123
|
-
}
|
|
124
|
-
// --------------------------------------------------------
|
|
125
|
-
// IssuesFromUnknown
|
|
126
|
-
// --------------------------------------------------------
|
|
127
|
-
function IssuesFromUnknown(error) {
|
|
128
|
-
const path = Guard.HasPropertyKey(error, 'path') && Guard.IsArray(error.path) && error.path.every(segment => Guard.IsString(segment)) ? error.path : [];
|
|
129
|
-
const message = Guard.HasPropertyKey(error, 'message') && Guard.IsString(error.message) ? error.message : 'unknown';
|
|
130
|
-
return [{ path, message }];
|
|
131
|
-
}
|
|
132
|
-
// --------------------------------------------------------
|
|
133
|
-
// CreateIssues
|
|
134
|
-
// --------------------------------------------------------
|
|
135
|
-
function CreateIssues(error) {
|
|
136
|
-
return IsLocalizedValidationError(error)
|
|
137
|
-
? IssuesFromLocalizedError(error)
|
|
138
|
-
: IssuesFromUnknown(error);
|
|
64
|
+
// ------------------------------------------------------------------
|
|
65
|
+
// BaseValidator
|
|
66
|
+
// ------------------------------------------------------------------
|
|
67
|
+
class BaseValidator {
|
|
68
|
+
constructor(check, errors) {
|
|
69
|
+
this.check = check;
|
|
70
|
+
this.errors = errors;
|
|
71
|
+
this.vendor = 'typebox';
|
|
72
|
+
this.version = 1;
|
|
73
|
+
this.validate = (value) => {
|
|
74
|
+
return this.check(value)
|
|
75
|
+
? this.Success(value)
|
|
76
|
+
: this.Failure(this.errors(value));
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
Success(value) {
|
|
80
|
+
return { value };
|
|
81
|
+
}
|
|
82
|
+
Failure(issues) {
|
|
83
|
+
return { issues };
|
|
84
|
+
}
|
|
139
85
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -110,13 +110,13 @@ type T = Static<typeof T> // type T = {
|
|
|
110
110
|
|
|
111
111
|
## Script
|
|
112
112
|
|
|
113
|
-
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/script/overview) | [Example](https://www.typescriptlang.org/play
|
|
113
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/script/overview) | [Example](https://www.typescriptlang.org/play/?moduleResolution=99&target=99&jsx=0&module=199#code/JYWwDg9gTgLgBAFQJ5gKYBo4G84xauAZRgEMZgBjOAXzgDMoIQ4ByPNAIwgA8WAoPhQgA7AM7wEcALyJ8AOkIUowMDAAUAAxx84cbgC44wgK4gOqKJh1wkhk2YtXdALzunzUOH2oaAlLoDAoOCQ0LCggHoIuCExCWlsa3DklNTAqID2VEMWCA4AK1QKGBZ0JLSKyvTo3ShUAEdjYDqAE0MAbRZeTBYkUtZnFgBdMqqxqozdMEY0WGBUUUMscvHVsMmAg2xcfBz7DxYaUbWT0I3dW22svfcLQ+pj06eA87hXK93WfbuaFeeTjbUP7-VYZIGCETiIgJZBoBRKFTqHCSB5wLTWdoAaTgwGEcAA1qgkBA6IghoYEFihnAAD5GYwAGwZ3j8IIB0ViUMICWWbKeG2urDyhWKpWBfMqGzqjWaqDacE63VYfR6gxG4olaQ20wgs3ICyWGs1KVeW20xtBNSCJGESAA8nQOkaLesrcEcIKWN8oPcnC6Jm6gh7Pl7GQz7s7-cFXrpqZGo9Ugg94wndK9LubU6kY3AbfbHQqU6mc7pg2gbg4fUcvFns4HAmXsl8wxHayb63A423kq9k93wq93pn+yEc3mHU6R2cO6WduXm5XfTWp9GZx956Gma2V5EO12d4nAn2Dy8rUCT6ffnwstCZMQyJQADxZElEAB8IIyN+5Ml5F7gGxbN6tL0kyjwnhslzAXSJhgUWCYbO80Ggcy-4AdE1BAA)
|
|
114
114
|
|
|
115
|
-
TypeBox is a
|
|
115
|
+
TypeBox is a type system designed to use Json Schema as an AST for runtime type representation. The Script function provides a full syntactic frontend to the type system and enables Json Schema to be constructed using native TypeScript syntax. TypeBox provides full static and runtime type safety for string-encoded types.
|
|
116
116
|
|
|
117
117
|
### Example
|
|
118
118
|
|
|
119
|
-
The following uses Script to
|
|
119
|
+
The following uses Script to construct and map Json Schema.
|
|
120
120
|
|
|
121
121
|
```typescript
|
|
122
122
|
import Type, { type Static } from 'typebox'
|
|
@@ -125,19 +125,42 @@ const T = Type.Script(`{
|
|
|
125
125
|
x: number,
|
|
126
126
|
y: number,
|
|
127
127
|
z: number
|
|
128
|
-
}`) // const T =
|
|
129
|
-
//
|
|
130
|
-
//
|
|
131
|
-
//
|
|
132
|
-
// }
|
|
128
|
+
}`) // const T = {
|
|
129
|
+
// type: 'object',
|
|
130
|
+
// required: ['x', 'y', 'z'],
|
|
131
|
+
// properties: {
|
|
132
|
+
// x: { type: 'number' },
|
|
133
|
+
// y: { type: 'number' },
|
|
134
|
+
// z: { type: 'number' }
|
|
135
|
+
// }
|
|
136
|
+
// }
|
|
133
137
|
|
|
134
138
|
const S = Type.Script({ T }, `{
|
|
135
139
|
[K in keyof T]: T[K] | null
|
|
136
|
-
}`) // const S
|
|
137
|
-
//
|
|
138
|
-
//
|
|
139
|
-
//
|
|
140
|
-
//
|
|
140
|
+
}`) // const S = {
|
|
141
|
+
// type: 'object',
|
|
142
|
+
// required: ['x', 'y', 'z'],
|
|
143
|
+
// properties: {
|
|
144
|
+
// x: {
|
|
145
|
+
// anyOf: [
|
|
146
|
+
// { type: 'number' },
|
|
147
|
+
// { type: 'null' }
|
|
148
|
+
// ]
|
|
149
|
+
// },
|
|
150
|
+
// y: {
|
|
151
|
+
// anyOf: [
|
|
152
|
+
// { type: 'number' },
|
|
153
|
+
// { type: 'null' }
|
|
154
|
+
// ]
|
|
155
|
+
// },
|
|
156
|
+
// z: {
|
|
157
|
+
// anyOf: [
|
|
158
|
+
// { type: 'number' },
|
|
159
|
+
// { type: 'null' }
|
|
160
|
+
// ]
|
|
161
|
+
// },
|
|
162
|
+
// }
|
|
163
|
+
// }
|
|
141
164
|
|
|
142
165
|
type S = Static<typeof S> // type S = {
|
|
143
166
|
// x: number | null,
|