typebox 1.0.22 → 1.0.24
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.
|
@@ -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
|
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
+
import type { TLocalizedValidationError } from '../../error/index.mjs';
|
|
1
2
|
import type { Static, TProperties, TSchema } from '../../type/index.mjs';
|
|
2
3
|
export declare class AssertError extends Error {
|
|
3
4
|
readonly cause: {
|
|
4
5
|
source: string;
|
|
5
|
-
errors:
|
|
6
|
+
errors: TLocalizedValidationError[];
|
|
6
7
|
value: unknown;
|
|
7
8
|
};
|
|
8
|
-
constructor(source: string, value: unknown, errors:
|
|
9
|
+
constructor(source: string, value: unknown, errors: TLocalizedValidationError[]);
|
|
9
10
|
}
|
|
10
11
|
/** Asserts the a value matches the given type. This function returns a TypeScript type asserts predicate and will throw AssertError if value does not match. */
|
|
11
12
|
export declare function Assert<Context extends TProperties, const Type extends TSchema, Result extends unknown = Static<Type, Context>>(context: Context, type: Type, value: unknown): asserts value is Result;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { type TLocalizedValidationError } from '../../error/errors.mjs';
|
|
1
2
|
import { type TProperties, type TSchema, type StaticDecode } from '../../type/index.mjs';
|
|
2
3
|
import { AssertError } from '../assert/index.mjs';
|
|
3
4
|
export declare class DecodeError extends AssertError {
|
|
4
|
-
constructor(value: unknown, errors:
|
|
5
|
+
constructor(value: unknown, errors: TLocalizedValidationError[]);
|
|
5
6
|
}
|
|
6
7
|
/** Executes Decode callbacks only */
|
|
7
8
|
export declare function DecodeUnsafe(context: TProperties, type: TSchema, value: unknown): unknown;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { type TLocalizedValidationError } from '../../error/errors.mjs';
|
|
1
2
|
import { type TProperties, type TSchema, type StaticEncode } from '../../type/index.mjs';
|
|
2
3
|
import { AssertError } from '../assert/index.mjs';
|
|
3
4
|
export declare class EncodeError extends AssertError {
|
|
4
|
-
constructor(value: unknown, errors:
|
|
5
|
+
constructor(value: unknown, errors: TLocalizedValidationError[]);
|
|
5
6
|
}
|
|
6
7
|
/** Executes Encode callbacks only */
|
|
7
8
|
export declare function EncodeUnsafe(context: TProperties, type: TSchema, value: unknown): unknown;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { type TLocalizedValidationError } from '../../error/errors.mjs';
|
|
1
2
|
import { type TProperties, type TSchema, type StaticParse } from '../../type/index.mjs';
|
|
2
3
|
import { AssertError } from '../assert/index.mjs';
|
|
3
4
|
export declare class ParseError extends AssertError {
|
|
4
|
-
constructor(value: unknown, errors:
|
|
5
|
+
constructor(value: unknown, errors: TLocalizedValidationError[]);
|
|
5
6
|
}
|
|
6
7
|
export declare const Parser: import("../pipeline/pipeline.mjs").PipelineInterface;
|
|
7
8
|
/**
|
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,
|