@sinclair/typebox 0.30.3 → 0.31.0-dev-1
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/compiler/compiler.d.ts +22 -14
- package/compiler/compiler.js +135 -107
- package/compiler/index.d.ts +1 -1
- package/compiler/index.js +2 -1
- package/errors/errors.d.ts +56 -61
- package/errors/errors.js +222 -279
- package/package.json +5 -3
- package/readme.md +592 -511
- package/system/system.d.ts +34 -8
- package/system/system.js +214 -11
- package/typebox.d.ts +180 -106
- package/typebox.js +794 -907
- package/value/cast.d.ts +6 -12
- package/value/cast.js +81 -163
- package/value/check.d.ts +1 -5
- package/value/check.js +59 -103
- package/value/clone.js +6 -29
- package/value/convert.d.ts +2 -5
- package/value/convert.js +55 -106
- package/value/create.d.ts +6 -10
- package/value/create.js +54 -68
- package/value/delta.js +22 -22
- package/value/deref.d.ts +7 -0
- package/value/deref.js +46 -0
- package/value/equal.js +10 -10
- package/value/guard.js +1 -1
- package/value/hash.js +14 -14
- package/value/mutate.js +17 -17
- package/value/pointer.js +2 -2
- package/value/transform.d.ts +42 -0
- package/value/transform.js +512 -0
- package/value/value.d.ts +8 -0
- package/value/value.js +18 -0
package/value/hash.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/*--------------------------------------------------------------------------
|
|
3
3
|
|
|
4
|
-
@sinclair/typebox/
|
|
4
|
+
@sinclair/typebox/value
|
|
5
5
|
|
|
6
6
|
The MIT License (MIT)
|
|
7
7
|
|
|
@@ -28,13 +28,13 @@ THE SOFTWARE.
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
30
|
exports.Hash = exports.ByteMarker = exports.ValueHashError = void 0;
|
|
31
|
-
const
|
|
31
|
+
const guard_1 = require("./guard");
|
|
32
32
|
// --------------------------------------------------------------------------
|
|
33
33
|
// Errors
|
|
34
34
|
// --------------------------------------------------------------------------
|
|
35
35
|
class ValueHashError extends Error {
|
|
36
36
|
constructor(value) {
|
|
37
|
-
super(`
|
|
37
|
+
super(`Unable to hash value`);
|
|
38
38
|
this.value = value;
|
|
39
39
|
}
|
|
40
40
|
}
|
|
@@ -126,27 +126,27 @@ function UndefinedType(value) {
|
|
|
126
126
|
return FNV1A64(ByteMarker.Undefined);
|
|
127
127
|
}
|
|
128
128
|
function Visit(value) {
|
|
129
|
-
if (
|
|
129
|
+
if ((0, guard_1.IsArray)(value))
|
|
130
130
|
return ArrayType(value);
|
|
131
|
-
if (
|
|
131
|
+
if ((0, guard_1.IsBoolean)(value))
|
|
132
132
|
return BooleanType(value);
|
|
133
|
-
if (
|
|
133
|
+
if ((0, guard_1.IsBigInt)(value))
|
|
134
134
|
return BigIntType(value);
|
|
135
|
-
if (
|
|
135
|
+
if ((0, guard_1.IsDate)(value))
|
|
136
136
|
return DateType(value);
|
|
137
|
-
if (
|
|
137
|
+
if ((0, guard_1.IsNull)(value))
|
|
138
138
|
return NullType(value);
|
|
139
|
-
if (
|
|
139
|
+
if ((0, guard_1.IsNumber)(value))
|
|
140
140
|
return NumberType(value);
|
|
141
|
-
if (
|
|
141
|
+
if ((0, guard_1.IsPlainObject)(value))
|
|
142
142
|
return ObjectType(value);
|
|
143
|
-
if (
|
|
143
|
+
if ((0, guard_1.IsString)(value))
|
|
144
144
|
return StringType(value);
|
|
145
|
-
if (
|
|
145
|
+
if ((0, guard_1.IsSymbol)(value))
|
|
146
146
|
return SymbolType(value);
|
|
147
|
-
if (
|
|
147
|
+
if ((0, guard_1.IsUint8Array)(value))
|
|
148
148
|
return Uint8ArrayType(value);
|
|
149
|
-
if (
|
|
149
|
+
if ((0, guard_1.IsUndefined)(value))
|
|
150
150
|
return UndefinedType(value);
|
|
151
151
|
throw new ValueHashError(value);
|
|
152
152
|
}
|
package/value/mutate.js
CHANGED
|
@@ -28,27 +28,27 @@ THE SOFTWARE.
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
30
|
exports.Mutate = exports.ValueMutateInvalidRootMutationError = exports.ValueMutateTypeMismatchError = void 0;
|
|
31
|
+
const guard_1 = require("./guard");
|
|
31
32
|
const pointer_1 = require("./pointer");
|
|
32
|
-
const
|
|
33
|
-
const ValueGuard = require("./guard");
|
|
33
|
+
const clone_1 = require("./clone");
|
|
34
34
|
// --------------------------------------------------------------------------
|
|
35
35
|
// Errors
|
|
36
36
|
// --------------------------------------------------------------------------
|
|
37
37
|
class ValueMutateTypeMismatchError extends Error {
|
|
38
38
|
constructor() {
|
|
39
|
-
super('
|
|
39
|
+
super('Cannot assign due type mismatch of assignable values');
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
exports.ValueMutateTypeMismatchError = ValueMutateTypeMismatchError;
|
|
43
43
|
class ValueMutateInvalidRootMutationError extends Error {
|
|
44
44
|
constructor() {
|
|
45
|
-
super('
|
|
45
|
+
super('Only object and array types can be mutated at the root level');
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
exports.ValueMutateInvalidRootMutationError = ValueMutateInvalidRootMutationError;
|
|
49
49
|
function ObjectType(root, path, current, next) {
|
|
50
|
-
if (!
|
|
51
|
-
pointer_1.ValuePointer.Set(root, path,
|
|
50
|
+
if (!(0, guard_1.IsPlainObject)(current)) {
|
|
51
|
+
pointer_1.ValuePointer.Set(root, path, (0, clone_1.Clone)(next));
|
|
52
52
|
}
|
|
53
53
|
else {
|
|
54
54
|
const currentKeys = Object.keys(current);
|
|
@@ -69,8 +69,8 @@ function ObjectType(root, path, current, next) {
|
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
function ArrayType(root, path, current, next) {
|
|
72
|
-
if (!
|
|
73
|
-
pointer_1.ValuePointer.Set(root, path,
|
|
72
|
+
if (!(0, guard_1.IsArray)(current)) {
|
|
73
|
+
pointer_1.ValuePointer.Set(root, path, (0, clone_1.Clone)(next));
|
|
74
74
|
}
|
|
75
75
|
else {
|
|
76
76
|
for (let index = 0; index < next.length; index++) {
|
|
@@ -80,13 +80,13 @@ function ArrayType(root, path, current, next) {
|
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
function TypedArrayType(root, path, current, next) {
|
|
83
|
-
if (
|
|
83
|
+
if ((0, guard_1.IsTypedArray)(current) && current.length === next.length) {
|
|
84
84
|
for (let i = 0; i < current.length; i++) {
|
|
85
85
|
current[i] = next[i];
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
88
|
else {
|
|
89
|
-
pointer_1.ValuePointer.Set(root, path,
|
|
89
|
+
pointer_1.ValuePointer.Set(root, path, (0, clone_1.Clone)(next));
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
function ValueType(root, path, current, next) {
|
|
@@ -95,25 +95,25 @@ function ValueType(root, path, current, next) {
|
|
|
95
95
|
pointer_1.ValuePointer.Set(root, path, next);
|
|
96
96
|
}
|
|
97
97
|
function Visit(root, path, current, next) {
|
|
98
|
-
if (
|
|
98
|
+
if ((0, guard_1.IsArray)(next))
|
|
99
99
|
return ArrayType(root, path, current, next);
|
|
100
|
-
if (
|
|
100
|
+
if ((0, guard_1.IsTypedArray)(next))
|
|
101
101
|
return TypedArrayType(root, path, current, next);
|
|
102
|
-
if (
|
|
102
|
+
if ((0, guard_1.IsPlainObject)(next))
|
|
103
103
|
return ObjectType(root, path, current, next);
|
|
104
|
-
if (
|
|
104
|
+
if ((0, guard_1.IsValueType)(next))
|
|
105
105
|
return ValueType(root, path, current, next);
|
|
106
106
|
}
|
|
107
107
|
// --------------------------------------------------------------------------
|
|
108
108
|
// Mutate
|
|
109
109
|
// --------------------------------------------------------------------------
|
|
110
110
|
function IsNonMutableValue(value) {
|
|
111
|
-
return
|
|
111
|
+
return (0, guard_1.IsTypedArray)(value) || (0, guard_1.IsValueType)(value);
|
|
112
112
|
}
|
|
113
113
|
function IsMismatchedValue(current, next) {
|
|
114
114
|
// prettier-ignore
|
|
115
|
-
return ((
|
|
116
|
-
(
|
|
115
|
+
return (((0, guard_1.IsPlainObject)(current) && (0, guard_1.IsArray)(next)) ||
|
|
116
|
+
((0, guard_1.IsArray)(current) && (0, guard_1.IsPlainObject)(next)));
|
|
117
117
|
}
|
|
118
118
|
// --------------------------------------------------------------------------
|
|
119
119
|
// Mutate
|
package/value/pointer.js
CHANGED
|
@@ -33,7 +33,7 @@ exports.ValuePointer = exports.ValuePointerRootDeleteError = exports.ValuePointe
|
|
|
33
33
|
// --------------------------------------------------------------------------
|
|
34
34
|
class ValuePointerRootSetError extends Error {
|
|
35
35
|
constructor(value, path, update) {
|
|
36
|
-
super('
|
|
36
|
+
super('Cannot set root value');
|
|
37
37
|
this.value = value;
|
|
38
38
|
this.path = path;
|
|
39
39
|
this.update = update;
|
|
@@ -42,7 +42,7 @@ class ValuePointerRootSetError extends Error {
|
|
|
42
42
|
exports.ValuePointerRootSetError = ValuePointerRootSetError;
|
|
43
43
|
class ValuePointerRootDeleteError extends Error {
|
|
44
44
|
constructor(value, path) {
|
|
45
|
-
super('
|
|
45
|
+
super('Cannot delete root value');
|
|
46
46
|
this.value = value;
|
|
47
47
|
this.path = path;
|
|
48
48
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ValueError } from '../errors/errors';
|
|
2
|
+
import * as Types from '../typebox';
|
|
3
|
+
export type CheckFunction = (schema: Types.TSchema, references: Types.TSchema[], value: unknown) => boolean;
|
|
4
|
+
export declare class TransformUnknownTypeError extends Types.TypeBoxError {
|
|
5
|
+
readonly schema: Types.TRef | Types.TThis;
|
|
6
|
+
constructor(schema: Types.TRef | Types.TThis);
|
|
7
|
+
}
|
|
8
|
+
export declare class TransformDecodeCheckError extends Types.TypeBoxError {
|
|
9
|
+
readonly schema: Types.TSchema;
|
|
10
|
+
readonly value: unknown;
|
|
11
|
+
readonly error: ValueError;
|
|
12
|
+
constructor(schema: Types.TSchema, value: unknown, error: ValueError);
|
|
13
|
+
}
|
|
14
|
+
export declare class TransformEncodeCheckError extends Types.TypeBoxError {
|
|
15
|
+
readonly schema: Types.TSchema;
|
|
16
|
+
readonly value: unknown;
|
|
17
|
+
readonly error: ValueError;
|
|
18
|
+
constructor(schema: Types.TSchema, value: unknown, error: ValueError);
|
|
19
|
+
}
|
|
20
|
+
export declare class TransformDecodeError extends Types.TypeBoxError {
|
|
21
|
+
readonly schema: Types.TSchema;
|
|
22
|
+
readonly value: unknown;
|
|
23
|
+
constructor(schema: Types.TSchema, value: unknown, error: any);
|
|
24
|
+
}
|
|
25
|
+
export declare class TransformEncodeError extends Types.TypeBoxError {
|
|
26
|
+
readonly schema: Types.TSchema;
|
|
27
|
+
readonly value: unknown;
|
|
28
|
+
constructor(schema: Types.TSchema, value: unknown, error: any);
|
|
29
|
+
}
|
|
30
|
+
/** Recursively checks a schema for transform codecs */
|
|
31
|
+
export declare namespace HasTransform {
|
|
32
|
+
/** Returns true if this schema contains a transform codec */
|
|
33
|
+
function Has(schema: Types.TSchema, references: Types.TSchema[]): boolean;
|
|
34
|
+
}
|
|
35
|
+
/** Decodes a value using transform decoders if available. Does not ensure correct results. */
|
|
36
|
+
export declare namespace DecodeTransform {
|
|
37
|
+
function Decode(schema: Types.TSchema, references: Types.TSchema[], value: unknown, check: CheckFunction): unknown;
|
|
38
|
+
}
|
|
39
|
+
/** Encodes a value using transform encoders if available. Does not ensure correct results. */
|
|
40
|
+
export declare namespace EncodeTransform {
|
|
41
|
+
function Encode(schema: Types.TSchema, references: Types.TSchema[], value: unknown, check: CheckFunction): unknown;
|
|
42
|
+
}
|