@sinclair/typebox 0.34.27 → 0.34.28
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 { StaticDecode } from '../../type/static/index';
|
|
|
4
4
|
export declare class ParseError extends TypeBoxError {
|
|
5
5
|
constructor(message: string);
|
|
6
6
|
}
|
|
7
|
-
export type TParseOperation = '
|
|
7
|
+
export type TParseOperation = 'Assert' | 'Cast' | 'Clean' | 'Clone' | 'Convert' | 'Decode' | 'Default' | 'Encode' | ({} & string);
|
|
8
8
|
export type TParseFunction = (type: TSchema, references: TSchema[], value: unknown) => unknown;
|
|
9
9
|
export declare namespace ParseRegistry {
|
|
10
10
|
function Delete(key: string): void;
|
|
@@ -6,14 +6,15 @@ exports.Parse = Parse;
|
|
|
6
6
|
const index_1 = require("../../type/error/index");
|
|
7
7
|
const index_2 = require("../transform/index");
|
|
8
8
|
const index_3 = require("../assert/index");
|
|
9
|
-
const index_4 = require("../
|
|
10
|
-
const index_5 = require("../
|
|
11
|
-
const index_6 = require("../
|
|
12
|
-
const index_7 = require("../
|
|
9
|
+
const index_4 = require("../cast/index");
|
|
10
|
+
const index_5 = require("../clean/index");
|
|
11
|
+
const index_6 = require("../clone/index");
|
|
12
|
+
const index_7 = require("../convert/index");
|
|
13
|
+
const index_8 = require("../default/index");
|
|
13
14
|
// ------------------------------------------------------------------
|
|
14
15
|
// Guards
|
|
15
16
|
// ------------------------------------------------------------------
|
|
16
|
-
const
|
|
17
|
+
const index_9 = require("../guard/index");
|
|
17
18
|
// ------------------------------------------------------------------
|
|
18
19
|
// Error
|
|
19
20
|
// ------------------------------------------------------------------
|
|
@@ -27,12 +28,13 @@ exports.ParseError = ParseError;
|
|
|
27
28
|
var ParseRegistry;
|
|
28
29
|
(function (ParseRegistry) {
|
|
29
30
|
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
31
|
['Assert', (type, references, value) => { (0, index_3.Assert)(type, references, value); return value; }],
|
|
32
|
+
['Cast', (type, references, value) => (0, index_4.Cast)(type, references, value)],
|
|
33
|
+
['Clean', (type, references, value) => (0, index_5.Clean)(type, references, value)],
|
|
34
|
+
['Clone', (_type, _references, value) => (0, index_6.Clone)(value)],
|
|
35
|
+
['Convert', (type, references, value) => (0, index_7.Convert)(type, references, value)],
|
|
35
36
|
['Decode', (type, references, value) => ((0, index_2.HasTransform)(type, references) ? (0, index_2.TransformDecode)(type, references, value) : value)],
|
|
37
|
+
['Default', (type, references, value) => (0, index_8.Default)(type, references, value)],
|
|
36
38
|
['Encode', (type, references, value) => ((0, index_2.HasTransform)(type, references) ? (0, index_2.TransformEncode)(type, references, value) : value)],
|
|
37
39
|
]);
|
|
38
40
|
// Deletes an entry from the registry
|
|
@@ -52,7 +54,7 @@ var ParseRegistry;
|
|
|
52
54
|
ParseRegistry.Get = Get;
|
|
53
55
|
})(ParseRegistry || (exports.ParseRegistry = ParseRegistry = {}));
|
|
54
56
|
// ------------------------------------------------------------------
|
|
55
|
-
// Default Parse
|
|
57
|
+
// Default Parse Pipeline
|
|
56
58
|
// ------------------------------------------------------------------
|
|
57
59
|
// prettier-ignore
|
|
58
60
|
exports.ParseDefault = [
|
|
@@ -69,7 +71,7 @@ exports.ParseDefault = [
|
|
|
69
71
|
function ParseValue(operations, type, references, value) {
|
|
70
72
|
return operations.reduce((value, operationKey) => {
|
|
71
73
|
const operation = ParseRegistry.Get(operationKey);
|
|
72
|
-
if ((0,
|
|
74
|
+
if ((0, index_9.IsUndefined)(operation))
|
|
73
75
|
throw new ParseError(`Unable to find Parse operation '${operationKey}'`);
|
|
74
76
|
return operation(type, references, value);
|
|
75
77
|
}, value);
|
|
@@ -78,7 +80,7 @@ function ParseValue(operations, type, references, value) {
|
|
|
78
80
|
function Parse(...args) {
|
|
79
81
|
// prettier-ignore
|
|
80
82
|
const [operations, schema, references, value] = (args.length === 4 ? [args[0], args[1], args[2], args[3]] :
|
|
81
|
-
args.length === 3 ? (0,
|
|
83
|
+
args.length === 3 ? (0, index_9.IsArray)(args[0]) ? [args[0], args[1], [], args[2]] : [exports.ParseDefault, args[0], args[1], args[2]] :
|
|
82
84
|
args.length === 2 ? [exports.ParseDefault, args[0], [], args[1]] :
|
|
83
85
|
(() => { throw new ParseError('Invalid Arguments'); })());
|
|
84
86
|
return ParseValue(operations, schema, references, value);
|
|
@@ -4,7 +4,7 @@ import { StaticDecode } from '../../type/static/index.mjs';
|
|
|
4
4
|
export declare class ParseError extends TypeBoxError {
|
|
5
5
|
constructor(message: string);
|
|
6
6
|
}
|
|
7
|
-
export type TParseOperation = '
|
|
7
|
+
export type TParseOperation = 'Assert' | 'Cast' | 'Clean' | 'Clone' | 'Convert' | 'Decode' | 'Default' | 'Encode' | ({} & string);
|
|
8
8
|
export type TParseFunction = (type: TSchema, references: TSchema[], value: unknown) => unknown;
|
|
9
9
|
export declare namespace ParseRegistry {
|
|
10
10
|
function Delete(key: string): void;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
2
2
|
import { TransformDecode, TransformEncode, HasTransform } from '../transform/index.mjs';
|
|
3
3
|
import { Assert } from '../assert/index.mjs';
|
|
4
|
-
import {
|
|
5
|
-
import { Convert } from '../convert/index.mjs';
|
|
4
|
+
import { Cast } from '../cast/index.mjs';
|
|
6
5
|
import { Clean } from '../clean/index.mjs';
|
|
7
6
|
import { Clone } from '../clone/index.mjs';
|
|
7
|
+
import { Convert } from '../convert/index.mjs';
|
|
8
|
+
import { Default } from '../default/index.mjs';
|
|
8
9
|
// ------------------------------------------------------------------
|
|
9
10
|
// Guards
|
|
10
11
|
// ------------------------------------------------------------------
|
|
@@ -21,12 +22,13 @@ export class ParseError extends TypeBoxError {
|
|
|
21
22
|
export var ParseRegistry;
|
|
22
23
|
(function (ParseRegistry) {
|
|
23
24
|
const registry = new Map([
|
|
24
|
-
['
|
|
25
|
+
['Assert', (type, references, value) => { Assert(type, references, value); return value; }],
|
|
26
|
+
['Cast', (type, references, value) => Cast(type, references, value)],
|
|
25
27
|
['Clean', (type, references, value) => Clean(type, references, value)],
|
|
26
|
-
['
|
|
28
|
+
['Clone', (_type, _references, value) => Clone(value)],
|
|
27
29
|
['Convert', (type, references, value) => Convert(type, references, value)],
|
|
28
|
-
['Assert', (type, references, value) => { Assert(type, references, value); return value; }],
|
|
29
30
|
['Decode', (type, references, value) => (HasTransform(type, references) ? TransformDecode(type, references, value) : value)],
|
|
31
|
+
['Default', (type, references, value) => Default(type, references, value)],
|
|
30
32
|
['Encode', (type, references, value) => (HasTransform(type, references) ? TransformEncode(type, references, value) : value)],
|
|
31
33
|
]);
|
|
32
34
|
// Deletes an entry from the registry
|
|
@@ -46,7 +48,7 @@ export var ParseRegistry;
|
|
|
46
48
|
ParseRegistry.Get = Get;
|
|
47
49
|
})(ParseRegistry || (ParseRegistry = {}));
|
|
48
50
|
// ------------------------------------------------------------------
|
|
49
|
-
// Default Parse
|
|
51
|
+
// Default Parse Pipeline
|
|
50
52
|
// ------------------------------------------------------------------
|
|
51
53
|
// prettier-ignore
|
|
52
54
|
export const ParseDefault = [
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -544,7 +544,7 @@ The following table lists the supported Json types. These types are fully compat
|
|
|
544
544
|
|
|
545
545
|
### JavaScript Types
|
|
546
546
|
|
|
547
|
-
TypeBox provides an extended type set that can be used to create schematics for common JavaScript constructs. These types can not be used with any standard Json Schema validator; but can be used to frame schematics for interfaces that may receive Json validated data. JavaScript types are prefixed with the `[JavaScript]`
|
|
547
|
+
TypeBox provides an extended type set that can be used to create schematics for common JavaScript constructs. These types can not be used with any standard Json Schema validator; but can be used to frame schematics for interfaces that may receive Json validated data. JavaScript types are prefixed with the `[JavaScript]` JSDoc comment for convenience. The following table lists the supported types.
|
|
548
548
|
|
|
549
549
|
```typescript
|
|
550
550
|
┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐
|
|
@@ -717,11 +717,11 @@ Object properties can be modified with Readonly and Optional. The following tabl
|
|
|
717
717
|
|
|
718
718
|
### Generic Types
|
|
719
719
|
|
|
720
|
-
Generic types can be created with generic functions
|
|
720
|
+
Generic types can be created with generic functions.
|
|
721
721
|
|
|
722
722
|
```typescript
|
|
723
723
|
const Nullable = <T extends TSchema>(T: T) => { // type Nullable<T> = T | null
|
|
724
|
-
return Type.Union([T, Type.Null())
|
|
724
|
+
return Type.Union([T, Type.Null()])
|
|
725
725
|
}
|
|
726
726
|
|
|
727
727
|
const T = Nullable(Type.String()) // type T = Nullable<string>
|
|
@@ -731,7 +731,7 @@ const T = Nullable(Type.String()) // type T = Nullable<string>
|
|
|
731
731
|
|
|
732
732
|
### Recursive Types
|
|
733
733
|
|
|
734
|
-
Use the Recursive function to create recursive types
|
|
734
|
+
Use the Recursive function to create recursive types.
|
|
735
735
|
|
|
736
736
|
```typescript
|
|
737
737
|
const Node = Type.Recursive(This => Type.Object({ // const Node = {
|
|
@@ -875,7 +875,7 @@ const C = Type.Index(T, Type.KeyOf(T)) // type C = T[keyof T]
|
|
|
875
875
|
|
|
876
876
|
### Mapped Types
|
|
877
877
|
|
|
878
|
-
TypeBox supports mapped types with the Mapped function. This function accepts two arguments, the first is a union type typically derived from KeyOf, the second is a mapping function that receives a mapping key `K` that can be used to index properties of a type. The following implements a mapped type that remaps each property to be `T | null
|
|
878
|
+
TypeBox supports mapped types with the Mapped function. This function accepts two arguments, the first is a union type typically derived from KeyOf, the second is a mapping function that receives a mapping key `K` that can be used to index properties of a type. The following implements a mapped type that remaps each property to be `T | null`.
|
|
879
879
|
|
|
880
880
|
```typescript
|
|
881
881
|
const T = Type.Object({ // type T = {
|
|
@@ -1123,7 +1123,7 @@ const Z = Value.Default(T, { x: 1 }) // const 'Z = { x: 1, y:
|
|
|
1123
1123
|
|
|
1124
1124
|
### Cast
|
|
1125
1125
|
|
|
1126
|
-
Use the Cast function to upcast a value into a target type. This function will retain as much
|
|
1126
|
+
Use the Cast function to upcast a value into a target type. This function will retain as much information as possible from the original value. The Cast function is intended to be used in data migration scenarios where existing values need to be upgraded to match a modified type.
|
|
1127
1127
|
|
|
1128
1128
|
```typescript
|
|
1129
1129
|
const T = Type.Object({ x: Type.Number(), y: Type.Number() }, { additionalProperties: false })
|
|
@@ -1203,7 +1203,7 @@ const R = Value.Equal( // const R = true
|
|
|
1203
1203
|
|
|
1204
1204
|
### Hash
|
|
1205
1205
|
|
|
1206
|
-
Use the Hash function to create a [FNV1A-64](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function) non
|
|
1206
|
+
Use the Hash function to create a [FNV1A-64](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function) non-cryptographic hash of a value.
|
|
1207
1207
|
|
|
1208
1208
|
```typescript
|
|
1209
1209
|
const A = Value.Hash({ x: 1, y: 2, z: 3 }) // const A = 2910466848807138541n
|
|
@@ -1383,7 +1383,7 @@ type BasisVectors = Static<typeof BasisVectors> // type BasisVectors = {
|
|
|
1383
1383
|
|
|
1384
1384
|
### Options
|
|
1385
1385
|
|
|
1386
|
-
Options can be passed via the last parameter
|
|
1386
|
+
Options can be passed via the last parameter.
|
|
1387
1387
|
|
|
1388
1388
|
```typescript
|
|
1389
1389
|
const T = Syntax(`number`, { minimum: 42 }) // const T = {
|
|
@@ -1455,7 +1455,7 @@ const B = Value.Check(T, 'bar') // const B = false
|
|
|
1455
1455
|
|
|
1456
1456
|
## TypeCheck
|
|
1457
1457
|
|
|
1458
|
-
TypeBox types target Json Schema Draft 7 and are compatible with any validator that supports this specification. TypeBox also provides a built
|
|
1458
|
+
TypeBox types target Json Schema Draft 7 and are compatible with any validator that supports this specification. TypeBox also provides a built-in type checking compiler designed specifically for TypeBox types that offers high performance compilation and value checking.
|
|
1459
1459
|
|
|
1460
1460
|
The following sections detail using Ajv and the TypeBox compiler infrastructure.
|
|
1461
1461
|
|
|
@@ -1574,7 +1574,7 @@ const C = TypeCompiler.Code(Type.String()) // const C = `return functi
|
|
|
1574
1574
|
|
|
1575
1575
|
## TypeMap
|
|
1576
1576
|
|
|
1577
|
-
TypeBox offers an external package for
|
|
1577
|
+
TypeBox offers an external package for bidirectional mapping between TypeBox, Valibot, and Zod type libraries. It also includes syntax parsing support for Valibot and Zod and supports the Standard Schema specification. For more details on TypeMap, refer to the project repository.
|
|
1578
1578
|
|
|
1579
1579
|
[TypeMap Repository](https://github.com/sinclairzx81/typemap)
|
|
1580
1580
|
|
|
@@ -1582,7 +1582,7 @@ TypeBox offers an external package for bi-directional mapping between TypeBox, V
|
|
|
1582
1582
|
|
|
1583
1583
|
### Usage
|
|
1584
1584
|
|
|
1585
|
-
TypeMap needs to be installed
|
|
1585
|
+
TypeMap needs to be installed separately
|
|
1586
1586
|
|
|
1587
1587
|
```bash
|
|
1588
1588
|
$ npm install @sinclair/typemap
|
|
@@ -1731,11 +1731,11 @@ The following is a list of community packages that offer general tooling, extend
|
|
|
1731
1731
|
| [json2typebox](https://github.com/hacxy/json2typebox) | Creating TypeBox code from Json Data |
|
|
1732
1732
|
| [nominal-typebox](https://github.com/Coder-Spirit/nominal/tree/main/%40coderspirit/nominal-typebox) | Allows devs to integrate nominal types into TypeBox schemas |
|
|
1733
1733
|
| [openapi-box](https://github.com/geut/openapi-box) | Generate TypeBox types from OpenApi IDL + Http client library |
|
|
1734
|
-
| [prismabox](https://github.com/m1212e/prismabox) | Converts a prisma.schema to
|
|
1734
|
+
| [prismabox](https://github.com/m1212e/prismabox) | Converts a prisma.schema to TypeBox schema matching the database models |
|
|
1735
1735
|
| [schema2typebox](https://github.com/xddq/schema2typebox) | Creating TypeBox code from Json Schemas |
|
|
1736
1736
|
| [sveltekit-superforms](https://github.com/ciscoheat/sveltekit-superforms) | A comprehensive SvelteKit form library for server and client validation |
|
|
1737
1737
|
| [ts2typebox](https://github.com/xddq/ts2typebox) | Creating TypeBox code from Typescript types |
|
|
1738
|
-
| [typebox-cli](https://github.com/gsuess/typebox-cli) | Generate Schema with
|
|
1738
|
+
| [typebox-cli](https://github.com/gsuess/typebox-cli) | Generate Schema with TypeBox from the CLI |
|
|
1739
1739
|
| [typebox-form-parser](https://github.com/jtlapp/typebox-form-parser) | Parses form and query data based on TypeBox schemas |
|
|
1740
1740
|
|
|
1741
1741
|
|