@sinclair/typebox 0.34.8 → 0.34.9
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/build/cjs/compiler/compiler.d.ts +4 -0
- package/build/cjs/compiler/compiler.js +8 -0
- package/build/cjs/value/parse/parse.d.ts +20 -4
- package/build/cjs/value/parse/parse.js +73 -17
- package/build/esm/compiler/compiler.d.mts +4 -0
- package/build/esm/compiler/compiler.mjs +8 -0
- package/build/esm/value/parse/parse.d.mts +20 -4
- package/build/esm/value/parse/parse.mjs +70 -16
- package/package.json +1 -1
- package/readme.md +39 -73
|
@@ -12,6 +12,10 @@ export declare class TypeCheck<T extends TSchema> {
|
|
|
12
12
|
constructor(schema: T, references: TSchema[], checkFunc: CheckFunction, code: string);
|
|
13
13
|
/** Returns the generated assertion code used to validate this type. */
|
|
14
14
|
Code(): string;
|
|
15
|
+
/** Returns the schema type used to validate */
|
|
16
|
+
Schema(): T;
|
|
17
|
+
/** Returns reference types used to validate */
|
|
18
|
+
References(): TSchema[];
|
|
15
19
|
/** Returns an iterator for each error in this value. */
|
|
16
20
|
Errors(value: unknown): ValueErrorIterator;
|
|
17
21
|
/** Returns true if the value matches the compiled type. */
|
|
@@ -36,6 +36,14 @@ class TypeCheck {
|
|
|
36
36
|
Code() {
|
|
37
37
|
return this.code;
|
|
38
38
|
}
|
|
39
|
+
/** Returns the schema type used to validate */
|
|
40
|
+
Schema() {
|
|
41
|
+
return this.schema;
|
|
42
|
+
}
|
|
43
|
+
/** Returns reference types used to validate */
|
|
44
|
+
References() {
|
|
45
|
+
return this.references;
|
|
46
|
+
}
|
|
39
47
|
/** Returns an iterator for each error in this value. */
|
|
40
48
|
Errors(value) {
|
|
41
49
|
return (0, index_2.Errors)(this.schema, this.references, value);
|
|
@@ -1,6 +1,22 @@
|
|
|
1
|
+
import { TypeBoxError } from '../../type/error/index';
|
|
1
2
|
import { TSchema } from '../../type/schema/index';
|
|
2
3
|
import { StaticDecode } from '../../type/static/index';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export
|
|
4
|
+
export declare class ParseError extends TypeBoxError {
|
|
5
|
+
constructor(message: string);
|
|
6
|
+
}
|
|
7
|
+
export type TParseOperation = 'Clone' | 'Clean' | 'Default' | 'Convert' | 'Assert' | 'Decode' | ({} & string);
|
|
8
|
+
export type TParseFunction = (type: TSchema, references: TSchema[], value: unknown) => unknown;
|
|
9
|
+
export declare namespace ParseRegistry {
|
|
10
|
+
function Delete(key: string): void;
|
|
11
|
+
function Set(key: string, callback: TParseFunction): void;
|
|
12
|
+
function Get(key: string): TParseFunction | undefined;
|
|
13
|
+
}
|
|
14
|
+
export declare const ParseDefault: readonly ["Clone", "Clean", "Default", "Convert", "Assert", "Decode"];
|
|
15
|
+
/** Parses a value using the default parse pipeline. Will throws an `AssertError` if invalid. */
|
|
16
|
+
export declare function Parse<Type extends TSchema, Output = StaticDecode<Type>, Result extends Output = Output>(schema: Type, references: TSchema[], value: unknown): Result;
|
|
17
|
+
/** Parses a value using the default parse pipeline. Will throws an `AssertError` if invalid. */
|
|
18
|
+
export declare function Parse<Type extends TSchema, Output = StaticDecode<Type>, Result extends Output = Output>(schema: Type, value: unknown): Result;
|
|
19
|
+
/** Parses a value using the specified operations. */
|
|
20
|
+
export declare function Parse<Type extends TSchema>(operations: TParseOperation[], schema: Type, references: TSchema[], value: unknown): unknown;
|
|
21
|
+
/** Parses a value using the specified operations. */
|
|
22
|
+
export declare function Parse<Type extends TSchema>(operations: TParseOperation[], schema: Type, value: unknown): unknown;
|
|
@@ -1,29 +1,85 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.ParseDefault = exports.ParseRegistry = exports.ParseError = void 0;
|
|
4
5
|
exports.Parse = Parse;
|
|
5
|
-
const index_1 = require("
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
6
|
+
const index_1 = require("../../type/error/index");
|
|
7
|
+
const index_2 = require("../transform/index");
|
|
8
|
+
const index_3 = require("../assert/index");
|
|
9
|
+
const index_4 = require("../default/index");
|
|
10
|
+
const index_5 = require("../convert/index");
|
|
11
|
+
const index_6 = require("../clean/index");
|
|
12
|
+
const index_7 = require("../clone/index");
|
|
13
|
+
// ------------------------------------------------------------------
|
|
14
|
+
// Guards
|
|
15
|
+
// ------------------------------------------------------------------
|
|
16
|
+
const index_8 = require("../guard/index");
|
|
17
|
+
// ------------------------------------------------------------------
|
|
18
|
+
// Error
|
|
19
|
+
// ------------------------------------------------------------------
|
|
20
|
+
class ParseError extends index_1.TypeBoxError {
|
|
21
|
+
constructor(message) {
|
|
22
|
+
super(message);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.ParseError = ParseError;
|
|
26
|
+
// prettier-ignore
|
|
27
|
+
var ParseRegistry;
|
|
28
|
+
(function (ParseRegistry) {
|
|
29
|
+
const registry = new Map([
|
|
30
|
+
['Clone', (_type, _references, value) => (0, index_7.Clone)(value)],
|
|
31
|
+
['Clean', (type, references, value) => (0, index_6.Clean)(type, references, value)],
|
|
32
|
+
['Default', (type, references, value) => (0, index_4.Default)(type, references, value)],
|
|
33
|
+
['Convert', (type, references, value) => (0, index_5.Convert)(type, references, value)],
|
|
34
|
+
['Assert', (type, references, value) => { (0, index_3.Assert)(type, references, value); return value; }],
|
|
35
|
+
['Decode', (type, references, value) => ((0, index_2.HasTransform)(type, references) ? (0, index_2.TransformDecode)(type, references, value) : value)],
|
|
36
|
+
['Encode', (type, references, value) => ((0, index_2.HasTransform)(type, references) ? (0, index_2.TransformEncode)(type, references, value) : value)],
|
|
37
|
+
]);
|
|
38
|
+
// Deletes an entry from the registry
|
|
39
|
+
function Delete(key) {
|
|
40
|
+
registry.delete(key);
|
|
41
|
+
}
|
|
42
|
+
ParseRegistry.Delete = Delete;
|
|
43
|
+
// Sets an entry in the registry
|
|
44
|
+
function Set(key, callback) {
|
|
45
|
+
registry.set(key, callback);
|
|
46
|
+
}
|
|
47
|
+
ParseRegistry.Set = Set;
|
|
48
|
+
// Gets an entry in the registry
|
|
49
|
+
function Get(key) {
|
|
50
|
+
return registry.get(key);
|
|
51
|
+
}
|
|
52
|
+
ParseRegistry.Get = Get;
|
|
53
|
+
})(ParseRegistry || (exports.ParseRegistry = ParseRegistry = {}));
|
|
54
|
+
// ------------------------------------------------------------------
|
|
55
|
+
// Default Parse Sequence
|
|
56
|
+
// ------------------------------------------------------------------
|
|
11
57
|
// prettier-ignore
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
58
|
+
exports.ParseDefault = [
|
|
59
|
+
'Clone',
|
|
60
|
+
'Clean',
|
|
61
|
+
'Default',
|
|
62
|
+
'Convert',
|
|
63
|
+
'Assert',
|
|
64
|
+
'Decode'
|
|
19
65
|
];
|
|
20
66
|
// ------------------------------------------------------------------
|
|
21
67
|
// ParseValue
|
|
22
68
|
// ------------------------------------------------------------------
|
|
23
|
-
function ParseValue(
|
|
24
|
-
return
|
|
69
|
+
function ParseValue(operations, type, references, value) {
|
|
70
|
+
return operations.reduce((value, operationKey) => {
|
|
71
|
+
const operation = ParseRegistry.Get(operationKey);
|
|
72
|
+
if ((0, index_8.IsUndefined)(operation))
|
|
73
|
+
throw new ParseError(`Unable to find Parse operation '${operationKey}'`);
|
|
74
|
+
return operation(type, references, value);
|
|
75
|
+
}, value);
|
|
25
76
|
}
|
|
26
|
-
/** Parses a value
|
|
77
|
+
/** Parses a value */
|
|
27
78
|
function Parse(...args) {
|
|
28
|
-
|
|
79
|
+
// prettier-ignore
|
|
80
|
+
const [operations, schema, references, value] = (args.length === 4 ? [args[0], args[1], args[2], args[3]] :
|
|
81
|
+
args.length === 3 ? (0, index_8.IsArray)(args[0]) ? [args[0], args[1], [], args[2]] : [exports.ParseDefault, args[0], args[1], args[2]] :
|
|
82
|
+
args.length === 2 ? [exports.ParseDefault, args[0], [], args[1]] :
|
|
83
|
+
(() => { throw new ParseError('Invalid Arguments'); })());
|
|
84
|
+
return ParseValue(operations, schema, references, value);
|
|
29
85
|
}
|
|
@@ -12,6 +12,10 @@ export declare class TypeCheck<T extends TSchema> {
|
|
|
12
12
|
constructor(schema: T, references: TSchema[], checkFunc: CheckFunction, code: string);
|
|
13
13
|
/** Returns the generated assertion code used to validate this type. */
|
|
14
14
|
Code(): string;
|
|
15
|
+
/** Returns the schema type used to validate */
|
|
16
|
+
Schema(): T;
|
|
17
|
+
/** Returns reference types used to validate */
|
|
18
|
+
References(): TSchema[];
|
|
15
19
|
/** Returns an iterator for each error in this value. */
|
|
16
20
|
Errors(value: unknown): ValueErrorIterator;
|
|
17
21
|
/** Returns true if the value matches the compiled type. */
|
|
@@ -32,6 +32,14 @@ export class TypeCheck {
|
|
|
32
32
|
Code() {
|
|
33
33
|
return this.code;
|
|
34
34
|
}
|
|
35
|
+
/** Returns the schema type used to validate */
|
|
36
|
+
Schema() {
|
|
37
|
+
return this.schema;
|
|
38
|
+
}
|
|
39
|
+
/** Returns reference types used to validate */
|
|
40
|
+
References() {
|
|
41
|
+
return this.references;
|
|
42
|
+
}
|
|
35
43
|
/** Returns an iterator for each error in this value. */
|
|
36
44
|
Errors(value) {
|
|
37
45
|
return Errors(this.schema, this.references, value);
|
|
@@ -1,6 +1,22 @@
|
|
|
1
|
+
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
1
2
|
import { TSchema } from '../../type/schema/index.mjs';
|
|
2
3
|
import { StaticDecode } from '../../type/static/index.mjs';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export
|
|
4
|
+
export declare class ParseError extends TypeBoxError {
|
|
5
|
+
constructor(message: string);
|
|
6
|
+
}
|
|
7
|
+
export type TParseOperation = 'Clone' | 'Clean' | 'Default' | 'Convert' | 'Assert' | 'Decode' | ({} & string);
|
|
8
|
+
export type TParseFunction = (type: TSchema, references: TSchema[], value: unknown) => unknown;
|
|
9
|
+
export declare namespace ParseRegistry {
|
|
10
|
+
function Delete(key: string): void;
|
|
11
|
+
function Set(key: string, callback: TParseFunction): void;
|
|
12
|
+
function Get(key: string): TParseFunction | undefined;
|
|
13
|
+
}
|
|
14
|
+
export declare const ParseDefault: readonly ["Clone", "Clean", "Default", "Convert", "Assert", "Decode"];
|
|
15
|
+
/** Parses a value using the default parse pipeline. Will throws an `AssertError` if invalid. */
|
|
16
|
+
export declare function Parse<Type extends TSchema, Output = StaticDecode<Type>, Result extends Output = Output>(schema: Type, references: TSchema[], value: unknown): Result;
|
|
17
|
+
/** Parses a value using the default parse pipeline. Will throws an `AssertError` if invalid. */
|
|
18
|
+
export declare function Parse<Type extends TSchema, Output = StaticDecode<Type>, Result extends Output = Output>(schema: Type, value: unknown): Result;
|
|
19
|
+
/** Parses a value using the specified operations. */
|
|
20
|
+
export declare function Parse<Type extends TSchema>(operations: TParseOperation[], schema: Type, references: TSchema[], value: unknown): unknown;
|
|
21
|
+
/** Parses a value using the specified operations. */
|
|
22
|
+
export declare function Parse<Type extends TSchema>(operations: TParseOperation[], schema: Type, value: unknown): unknown;
|
|
@@ -1,25 +1,79 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
2
|
+
import { TransformDecode, TransformEncode, HasTransform } from '../transform/index.mjs';
|
|
3
|
+
import { Assert } from '../assert/index.mjs';
|
|
4
|
+
import { Default } from '../default/index.mjs';
|
|
5
|
+
import { Convert } from '../convert/index.mjs';
|
|
6
|
+
import { Clean } from '../clean/index.mjs';
|
|
6
7
|
import { Clone } from '../clone/index.mjs';
|
|
8
|
+
// ------------------------------------------------------------------
|
|
9
|
+
// Guards
|
|
10
|
+
// ------------------------------------------------------------------
|
|
11
|
+
import { IsArray, IsUndefined } from '../guard/index.mjs';
|
|
12
|
+
// ------------------------------------------------------------------
|
|
13
|
+
// Error
|
|
14
|
+
// ------------------------------------------------------------------
|
|
15
|
+
export class ParseError extends TypeBoxError {
|
|
16
|
+
constructor(message) {
|
|
17
|
+
super(message);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
// prettier-ignore
|
|
21
|
+
export var ParseRegistry;
|
|
22
|
+
(function (ParseRegistry) {
|
|
23
|
+
const registry = new Map([
|
|
24
|
+
['Clone', (_type, _references, value) => Clone(value)],
|
|
25
|
+
['Clean', (type, references, value) => Clean(type, references, value)],
|
|
26
|
+
['Default', (type, references, value) => Default(type, references, value)],
|
|
27
|
+
['Convert', (type, references, value) => Convert(type, references, value)],
|
|
28
|
+
['Assert', (type, references, value) => { Assert(type, references, value); return value; }],
|
|
29
|
+
['Decode', (type, references, value) => (HasTransform(type, references) ? TransformDecode(type, references, value) : value)],
|
|
30
|
+
['Encode', (type, references, value) => (HasTransform(type, references) ? TransformEncode(type, references, value) : value)],
|
|
31
|
+
]);
|
|
32
|
+
// Deletes an entry from the registry
|
|
33
|
+
function Delete(key) {
|
|
34
|
+
registry.delete(key);
|
|
35
|
+
}
|
|
36
|
+
ParseRegistry.Delete = Delete;
|
|
37
|
+
// Sets an entry in the registry
|
|
38
|
+
function Set(key, callback) {
|
|
39
|
+
registry.set(key, callback);
|
|
40
|
+
}
|
|
41
|
+
ParseRegistry.Set = Set;
|
|
42
|
+
// Gets an entry in the registry
|
|
43
|
+
function Get(key) {
|
|
44
|
+
return registry.get(key);
|
|
45
|
+
}
|
|
46
|
+
ParseRegistry.Get = Get;
|
|
47
|
+
})(ParseRegistry || (ParseRegistry = {}));
|
|
48
|
+
// ------------------------------------------------------------------
|
|
49
|
+
// Default Parse Sequence
|
|
50
|
+
// ------------------------------------------------------------------
|
|
7
51
|
// prettier-ignore
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
52
|
+
export const ParseDefault = [
|
|
53
|
+
'Clone',
|
|
54
|
+
'Clean',
|
|
55
|
+
'Default',
|
|
56
|
+
'Convert',
|
|
57
|
+
'Assert',
|
|
58
|
+
'Decode'
|
|
15
59
|
];
|
|
16
60
|
// ------------------------------------------------------------------
|
|
17
61
|
// ParseValue
|
|
18
62
|
// ------------------------------------------------------------------
|
|
19
|
-
function ParseValue(
|
|
20
|
-
return
|
|
63
|
+
function ParseValue(operations, type, references, value) {
|
|
64
|
+
return operations.reduce((value, operationKey) => {
|
|
65
|
+
const operation = ParseRegistry.Get(operationKey);
|
|
66
|
+
if (IsUndefined(operation))
|
|
67
|
+
throw new ParseError(`Unable to find Parse operation '${operationKey}'`);
|
|
68
|
+
return operation(type, references, value);
|
|
69
|
+
}, value);
|
|
21
70
|
}
|
|
22
|
-
/** Parses a value
|
|
71
|
+
/** Parses a value */
|
|
23
72
|
export function Parse(...args) {
|
|
24
|
-
|
|
73
|
+
// prettier-ignore
|
|
74
|
+
const [operations, schema, references, value] = (args.length === 4 ? [args[0], args[1], args[2], args[3]] :
|
|
75
|
+
args.length === 3 ? IsArray(args[0]) ? [args[0], args[1], [], args[2]] : [ParseDefault, args[0], args[1], args[2]] :
|
|
76
|
+
args.length === 2 ? [ParseDefault, args[0], [], args[1]] :
|
|
77
|
+
(() => { throw new ParseError('Invalid Arguments'); })());
|
|
78
|
+
return ParseValue(operations, schema, references, value);
|
|
25
79
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -78,9 +78,6 @@ License MIT
|
|
|
78
78
|
- [Unsafe](#types-unsafe)
|
|
79
79
|
- [Syntax](#syntax)
|
|
80
80
|
- [Parse](#syntax-parse)
|
|
81
|
-
- [Compose](#syntax-compose)
|
|
82
|
-
- [Context](#syntax-context)
|
|
83
|
-
- [Module](#syntax-module)
|
|
84
81
|
- [Static](#syntax-static)
|
|
85
82
|
- [Limits](#syntax-limits)
|
|
86
83
|
- [Values](#values)
|
|
@@ -1025,9 +1022,9 @@ if(TypeGuard.IsString(T)) {
|
|
|
1025
1022
|
|
|
1026
1023
|
## Syntax Types
|
|
1027
1024
|
|
|
1028
|
-
|
|
1025
|
+
Syntax Types is a feature that enables TypeBox to parse TypeScript type annotations directly into Json Schema. This feature works both at runtime and statically within the type system. Syntax Types use Json Schema as the Abstract Syntax Tree (AST) parse target for TypeScript types. Syntax Types are designed to offer a syntactical frontend to the standard Type Builder API.
|
|
1029
1026
|
|
|
1030
|
-
|
|
1027
|
+
This feature is available via optional import.
|
|
1031
1028
|
|
|
1032
1029
|
```typescript
|
|
1033
1030
|
import { Parse } from '@sinclair/typebox/syntax'
|
|
@@ -1037,7 +1034,7 @@ import { Parse } from '@sinclair/typebox/syntax'
|
|
|
1037
1034
|
|
|
1038
1035
|
### Parse
|
|
1039
1036
|
|
|
1040
|
-
Use the Parse function to
|
|
1037
|
+
Use the Parse function to transform a TypeScript type annotation into a TypeBox type. This function will return the parsed TypeBox type or undefined on error.
|
|
1041
1038
|
|
|
1042
1039
|
```typescript
|
|
1043
1040
|
const A = Parse('string') // const A: TString
|
|
@@ -1054,45 +1051,46 @@ const C = Parse(`{ x: number, y: number }`) // const C: TObject<{
|
|
|
1054
1051
|
// }>
|
|
1055
1052
|
```
|
|
1056
1053
|
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
<a name='syntax-compose'></a>
|
|
1060
|
-
|
|
1061
|
-
### Compose
|
|
1062
|
-
|
|
1063
|
-
Syntax Types are designed to be interchangeable with standard Types.
|
|
1054
|
+
Syntax Types can be composed with Standard Types.
|
|
1064
1055
|
|
|
1065
1056
|
```typescript
|
|
1066
1057
|
const T = Type.Object({ // const T: TObject<{
|
|
1067
1058
|
x: Parse('number'), // x: TNumber,
|
|
1068
|
-
y: Parse('
|
|
1069
|
-
z: Parse('
|
|
1059
|
+
y: Parse('string'), // y: TString,
|
|
1060
|
+
z: Parse('boolean') // z: TBoolean
|
|
1070
1061
|
}) // }>
|
|
1071
1062
|
```
|
|
1072
1063
|
|
|
1073
|
-
|
|
1064
|
+
It is also possible to pass Standard Types to Syntax Types in the following way.
|
|
1074
1065
|
|
|
1075
|
-
|
|
1066
|
+
```typescript
|
|
1067
|
+
const X = Type.Number()
|
|
1068
|
+
const Y = Type.String()
|
|
1069
|
+
const Z = Type.Boolean()
|
|
1070
|
+
|
|
1071
|
+
const T = Parse({ X, Y, Z }, `{
|
|
1072
|
+
x: X,
|
|
1073
|
+
y: Y,
|
|
1074
|
+
z: Z
|
|
1075
|
+
}`)
|
|
1076
|
+
```
|
|
1076
1077
|
|
|
1077
|
-
Syntax Types
|
|
1078
|
+
Syntax Types can also be used to parse Module types.
|
|
1078
1079
|
|
|
1079
1080
|
```typescript
|
|
1080
|
-
const
|
|
1081
|
-
|
|
1081
|
+
const Foo = Parse(`module Foo {
|
|
1082
|
+
|
|
1083
|
+
export type PartialUser = Pick<User, 'id'> & Partial<Omit<User, 'id'>>
|
|
1084
|
+
|
|
1082
1085
|
export interface User {
|
|
1083
1086
|
id: string
|
|
1084
1087
|
name: string
|
|
1085
1088
|
email: string
|
|
1086
1089
|
}
|
|
1087
|
-
|
|
1088
|
-
export type PartialUser = (
|
|
1089
|
-
Pick<User, 'id'> &
|
|
1090
|
-
Partial<Omit<User, 'id'>>
|
|
1091
|
-
)
|
|
1092
1090
|
|
|
1093
1091
|
}`)
|
|
1094
1092
|
|
|
1095
|
-
const PartialUser =
|
|
1093
|
+
const PartialUser = Foo.Import('PartialUser') // TImport<{...}, 'PartialUser'>
|
|
1096
1094
|
|
|
1097
1095
|
type PartialUser = Static<typeof PartialUser> // type PartialUser = {
|
|
1098
1096
|
// id: string,
|
|
@@ -1102,45 +1100,11 @@ type PartialUser = Static<typeof PartialUser> // type PartialUser = {
|
|
|
1102
1100
|
// }
|
|
1103
1101
|
```
|
|
1104
1102
|
|
|
1105
|
-
<a name='syntax-context'></a>
|
|
1106
|
-
|
|
1107
|
-
### Context
|
|
1108
|
-
|
|
1109
|
-
The Parse function takes an optional leading Context object that contains external types. This Context allows the syntax to reference these external types using the property identifiers provided by the Context. The following passes the external type `T` to Parse.
|
|
1110
|
-
|
|
1111
|
-
```typescript
|
|
1112
|
-
const T = Type.Object({ // const T: TObject<{
|
|
1113
|
-
x: Type.Number(), // x: TNumber,
|
|
1114
|
-
y: Type.Number(), // y: TNumber,
|
|
1115
|
-
z: Type.Number() // z: TNumber
|
|
1116
|
-
}) // }>
|
|
1117
|
-
|
|
1118
|
-
const A = Parse({ T }, 'Partial<T>') // const A: TObject<{
|
|
1119
|
-
// x: TOptional<TNumber>,
|
|
1120
|
-
// y: TOptional<TNumber>,
|
|
1121
|
-
// z: TOptional<TNumber>
|
|
1122
|
-
// }>
|
|
1123
|
-
|
|
1124
|
-
const B = Parse({ T }, 'keyof T') // const B: TUnion<[
|
|
1125
|
-
// TLiteral<'x'>,
|
|
1126
|
-
// TLiteral<'y'>,
|
|
1127
|
-
// TLiteral<'z'>
|
|
1128
|
-
// ]>
|
|
1129
|
-
|
|
1130
|
-
const C = Parse({ T }, 'T & { w: number }') // const C: TIntersect<[TObject<{
|
|
1131
|
-
// x: TNumber;
|
|
1132
|
-
// y: TNumber;
|
|
1133
|
-
// z: TNumber;
|
|
1134
|
-
// }>, TObject<{
|
|
1135
|
-
// w: TNumber;
|
|
1136
|
-
// }>]>
|
|
1137
|
-
```
|
|
1138
|
-
|
|
1139
1103
|
<a name='syntax-static'></a>
|
|
1140
1104
|
|
|
1141
1105
|
### Static
|
|
1142
1106
|
|
|
1143
|
-
Syntax Types provide two Static types
|
|
1107
|
+
Syntax Types provide two Static types specific to inferring TypeBox and TypeScript types from strings.
|
|
1144
1108
|
|
|
1145
1109
|
```typescript
|
|
1146
1110
|
import { StaticParseAsSchema, StaticParseAsType } from '@sinclair/typebox/syntax'
|
|
@@ -1154,8 +1118,8 @@ type S = StaticParseAsSchema<{}, '{ x: number }'> // type S: TObject<{
|
|
|
1154
1118
|
// Will infer as a type
|
|
1155
1119
|
|
|
1156
1120
|
type T = StaticParseAsType<{}, '{ x: number }'> // type T = {
|
|
1157
|
-
//
|
|
1158
|
-
//
|
|
1121
|
+
// x: number
|
|
1122
|
+
// }
|
|
1159
1123
|
```
|
|
1160
1124
|
|
|
1161
1125
|
|
|
@@ -1361,26 +1325,28 @@ const B = Value.Encode(Type.String(), 42) // throw
|
|
|
1361
1325
|
|
|
1362
1326
|
### Parse
|
|
1363
1327
|
|
|
1364
|
-
Use the Parse function to parse a value
|
|
1328
|
+
Use the Parse function to parse a value. This function calls the `Clone` `Clean`, `Default`, `Convert`, `Assert` and `Decode` Value functions in this exact order to process a value.
|
|
1365
1329
|
|
|
1366
1330
|
```typescript
|
|
1367
|
-
const
|
|
1331
|
+
const R = Value.Parse(Type.String(), 'hello') // const R: string = "hello"
|
|
1368
1332
|
|
|
1369
|
-
//
|
|
1333
|
+
const E = Value.Parse(Type.String(), undefined) // throws AssertError
|
|
1334
|
+
```
|
|
1370
1335
|
|
|
1371
|
-
|
|
1336
|
+
You can override the order in which functions are are run, or omit functions entirely in the following way.
|
|
1372
1337
|
|
|
1373
|
-
|
|
1338
|
+
```typescript
|
|
1339
|
+
// Runs no functions.
|
|
1374
1340
|
|
|
1375
|
-
const
|
|
1341
|
+
const R = Value.Parse([], Type.String(), 12345)
|
|
1376
1342
|
|
|
1377
|
-
//
|
|
1343
|
+
// Runs the Assert() function.
|
|
1378
1344
|
|
|
1379
|
-
const
|
|
1345
|
+
const E = Value.Parse(['Assert'], Type.String(), 12345)
|
|
1380
1346
|
|
|
1381
|
-
// Assert
|
|
1347
|
+
// Runs the Convert() function followed by the Assert() function.
|
|
1382
1348
|
|
|
1383
|
-
const
|
|
1349
|
+
const S = Value.Parse(['Convert', 'Assert'], Type.String(), 12345)
|
|
1384
1350
|
```
|
|
1385
1351
|
|
|
1386
1352
|
<a name='values-equal'></a>
|