@sinclair/typebox 0.33.3 → 0.33.4
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 +2 -2
- package/build/cjs/compiler/compiler.js +1 -1
- package/build/cjs/value/assert/assert.d.ts +15 -0
- package/build/cjs/value/assert/assert.js +55 -0
- package/build/cjs/value/assert/index.d.ts +1 -0
- package/build/cjs/value/assert/index.js +18 -0
- package/build/cjs/value/cast/cast.js +1 -1
- package/build/cjs/value/clean/clean.js +6 -6
- package/build/cjs/value/clone/clone.js +21 -11
- package/build/cjs/value/convert/convert.d.ts +2 -2
- package/build/cjs/value/convert/convert.js +15 -18
- package/build/cjs/value/create/create.js +5 -1
- package/build/cjs/value/default/default.js +14 -16
- package/build/cjs/value/equal/equal.js +3 -3
- package/build/cjs/value/hash/hash.js +1 -1
- package/build/cjs/value/index.d.ts +2 -0
- package/build/cjs/value/index.js +2 -0
- package/build/cjs/value/mutate/mutate.js +4 -4
- package/build/cjs/value/parse/index.d.ts +1 -0
- package/build/cjs/value/parse/index.js +18 -0
- package/build/cjs/value/parse/parse.d.ts +6 -0
- package/build/cjs/value/parse/parse.js +29 -0
- package/build/cjs/value/transform/decode.js +8 -4
- package/build/cjs/value/transform/encode.js +9 -5
- package/build/cjs/value/transform/has.js +5 -1
- package/build/cjs/value/value/value.d.ts +10 -2
- package/build/cjs/value/value/value.js +38 -26
- package/build/esm/compiler/compiler.d.mts +2 -2
- package/build/esm/compiler/compiler.mjs +1 -1
- package/build/esm/value/assert/assert.d.mts +15 -0
- package/build/esm/value/assert/assert.mjs +49 -0
- package/build/esm/value/assert/index.d.mts +1 -0
- package/build/esm/value/assert/index.mjs +1 -0
- package/build/esm/value/cast/cast.mjs +2 -2
- package/build/esm/value/clean/clean.mjs +7 -7
- package/build/esm/value/clone/clone.mjs +22 -12
- package/build/esm/value/convert/convert.d.mts +2 -2
- package/build/esm/value/convert/convert.mjs +16 -19
- package/build/esm/value/create/create.mjs +5 -1
- package/build/esm/value/default/default.mjs +14 -16
- package/build/esm/value/equal/equal.mjs +4 -4
- package/build/esm/value/hash/hash.mjs +2 -2
- package/build/esm/value/index.d.mts +2 -0
- package/build/esm/value/index.mjs +2 -0
- package/build/esm/value/mutate/mutate.mjs +5 -5
- package/build/esm/value/parse/index.d.mts +1 -0
- package/build/esm/value/parse/index.mjs +1 -0
- package/build/esm/value/parse/parse.d.mts +6 -0
- package/build/esm/value/parse/parse.mjs +25 -0
- package/build/esm/value/transform/decode.mjs +9 -5
- package/build/esm/value/transform/encode.mjs +10 -6
- package/build/esm/value/transform/has.mjs +5 -1
- package/build/esm/value/value/value.d.mts +10 -2
- package/build/esm/value/value/value.mjs +11 -1
- package/package.json +1 -1
- package/readme.md +114 -130
|
@@ -62,7 +62,7 @@ function FromArray(schema, references, path, value) {
|
|
|
62
62
|
// prettier-ignore
|
|
63
63
|
function FromIntersect(schema, references, path, value) {
|
|
64
64
|
const defaulted = Default(schema, path, value);
|
|
65
|
-
if (!(0, index_6.
|
|
65
|
+
if (!(0, index_6.IsObject)(value) || (0, index_6.IsValueType)(value))
|
|
66
66
|
return defaulted;
|
|
67
67
|
const knownEntries = (0, index_3.KeyOfPropertyEntries)(schema);
|
|
68
68
|
const knownKeys = knownEntries.map(entry => entry[0]);
|
|
@@ -90,7 +90,7 @@ function FromNot(schema, references, path, value) {
|
|
|
90
90
|
// prettier-ignore
|
|
91
91
|
function FromObject(schema, references, path, value) {
|
|
92
92
|
const defaulted = Default(schema, path, value);
|
|
93
|
-
if (!(0, index_6.
|
|
93
|
+
if (!(0, index_6.IsObject)(defaulted))
|
|
94
94
|
return defaulted;
|
|
95
95
|
const knownKeys = (0, index_3.KeyOfPropertyKeys)(schema);
|
|
96
96
|
const knownProperties = { ...defaulted };
|
|
@@ -113,7 +113,7 @@ function FromObject(schema, references, path, value) {
|
|
|
113
113
|
// prettier-ignore
|
|
114
114
|
function FromRecord(schema, references, path, value) {
|
|
115
115
|
const defaulted = Default(schema, path, value);
|
|
116
|
-
if (!(0, index_6.
|
|
116
|
+
if (!(0, index_6.IsObject)(value))
|
|
117
117
|
return defaulted;
|
|
118
118
|
const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0];
|
|
119
119
|
const knownKeys = new RegExp(pattern);
|
|
@@ -123,7 +123,7 @@ function FromRecord(schema, references, path, value) {
|
|
|
123
123
|
knownProperties[key] = Visit(schema.patternProperties[pattern], references, `${path}/${key}`, knownProperties[key]);
|
|
124
124
|
}
|
|
125
125
|
if (!(0, type_1.IsSchema)(schema.additionalProperties)) {
|
|
126
|
-
return
|
|
126
|
+
return knownProperties;
|
|
127
127
|
}
|
|
128
128
|
const unknownKeys = Object.getOwnPropertyNames(knownProperties);
|
|
129
129
|
const additionalProperties = schema.additionalProperties;
|
|
@@ -169,9 +169,13 @@ function FromUnion(schema, references, path, value) {
|
|
|
169
169
|
}
|
|
170
170
|
return Default(schema, path, value);
|
|
171
171
|
}
|
|
172
|
+
function AddReference(references, schema) {
|
|
173
|
+
references.push(schema);
|
|
174
|
+
return references;
|
|
175
|
+
}
|
|
172
176
|
// prettier-ignore
|
|
173
177
|
function Visit(schema, references, path, value) {
|
|
174
|
-
const references_ = typeof schema.$id === 'string' ?
|
|
178
|
+
const references_ = typeof schema.$id === 'string' ? AddReference(references, schema) : references;
|
|
175
179
|
const schema_ = schema;
|
|
176
180
|
switch (schema[index_1.Kind]) {
|
|
177
181
|
case 'Array':
|
|
@@ -76,9 +76,13 @@ function FromTuple(schema, references) {
|
|
|
76
76
|
function FromUnion(schema, references) {
|
|
77
77
|
return (0, type_1.IsTransform)(schema) || schema.anyOf.some((schema) => Visit(schema, references));
|
|
78
78
|
}
|
|
79
|
+
function AddReference(references, schema) {
|
|
80
|
+
references.push(schema);
|
|
81
|
+
return references;
|
|
82
|
+
}
|
|
79
83
|
// prettier-ignore
|
|
80
84
|
function Visit(schema, references) {
|
|
81
|
-
const references_ = (0, index_3.IsString)(schema.$id) ?
|
|
85
|
+
const references_ = (0, index_3.IsString)(schema.$id) ? AddReference(references, schema) : references;
|
|
82
86
|
const schema_ = schema;
|
|
83
87
|
if (schema.$id && visited.has(schema.$id))
|
|
84
88
|
return false;
|
|
@@ -3,6 +3,10 @@ import { Edit } from '../delta/index';
|
|
|
3
3
|
import { ValueErrorIterator } from '../../errors/index';
|
|
4
4
|
import type { TSchema } from '../../type/schema/index';
|
|
5
5
|
import type { Static, StaticDecode, StaticEncode } from '../../type/static/index';
|
|
6
|
+
/** Asserts a value matches the given type or throws an `AssertError` if invalid. */
|
|
7
|
+
export declare function Assert<T extends TSchema, R = Static<T>>(schema: T, references: TSchema[], value: unknown): asserts value is R;
|
|
8
|
+
/** Asserts a value matches the given type or throws an `AssertError` if invalid. */
|
|
9
|
+
export declare function Assert<T extends TSchema, R = Static<T>>(schema: T, value: unknown): asserts value is R;
|
|
6
10
|
/** Casts a value into a given type. The return value will retain as much information of the original value as possible. */
|
|
7
11
|
export declare function Cast<T extends TSchema>(schema: T, references: TSchema[], value: unknown): Static<T>;
|
|
8
12
|
/** Casts a value into a given type. The return value will retain as much information of the original value as possible. */
|
|
@@ -19,9 +23,9 @@ export declare function Check<T extends TSchema>(schema: T, value: unknown): val
|
|
|
19
23
|
export declare function Clean(schema: TSchema, references: TSchema[], value: unknown): unknown;
|
|
20
24
|
/** `[Mutable]` Removes excess properties from a value and returns the result. This function does not check the value and returns an unknown type. You should Check the result before use. Clean is a mutable operation. To avoid mutation, Clone the value first. */
|
|
21
25
|
export declare function Clean(schema: TSchema, value: unknown): unknown;
|
|
22
|
-
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
26
|
+
/** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
23
27
|
export declare function Convert(schema: TSchema, references: TSchema[], value: unknown): unknown;
|
|
24
|
-
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
28
|
+
/** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
25
29
|
export declare function Convert(schema: TSchema, value: unknown): unknown;
|
|
26
30
|
/** Returns a structural clone of the given value */
|
|
27
31
|
export declare function Clone<T>(value: T): T;
|
|
@@ -37,6 +41,10 @@ export declare function Default(schema: TSchema, value: unknown): unknown;
|
|
|
37
41
|
export declare function Encode<T extends TSchema, R = StaticEncode<T>>(schema: T, references: TSchema[], value: unknown): R;
|
|
38
42
|
/** Encodes a value or throws if error */
|
|
39
43
|
export declare function Encode<T extends TSchema, R = StaticEncode<T>>(schema: T, value: unknown): R;
|
|
44
|
+
/** Parses a value or throws an `AssertError` if invalid. */
|
|
45
|
+
export declare function Parse<T extends TSchema, R = StaticDecode<T>>(schema: T, references: TSchema[], value: unknown): R;
|
|
46
|
+
/** Parses a value or throws an `AssertError` if invalid. */
|
|
47
|
+
export declare function Parse<T extends TSchema, R = StaticDecode<T>>(schema: T, value: unknown): R;
|
|
40
48
|
/** Returns an iterator for each error in this value. */
|
|
41
49
|
export declare function Errors<T extends TSchema>(schema: T, references: TSchema[], value: unknown): ValueErrorIterator;
|
|
42
50
|
/** Returns an iterator for each error in this value. */
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Assert = Assert;
|
|
4
5
|
exports.Cast = Cast;
|
|
5
6
|
exports.Create = Create;
|
|
6
7
|
exports.Check = Check;
|
|
@@ -10,6 +11,7 @@ exports.Clone = Clone;
|
|
|
10
11
|
exports.Decode = Decode;
|
|
11
12
|
exports.Default = Default;
|
|
12
13
|
exports.Encode = Encode;
|
|
14
|
+
exports.Parse = Parse;
|
|
13
15
|
exports.Errors = Errors;
|
|
14
16
|
exports.Equal = Equal;
|
|
15
17
|
exports.Diff = Diff;
|
|
@@ -17,41 +19,47 @@ exports.Hash = Hash;
|
|
|
17
19
|
exports.Patch = Patch;
|
|
18
20
|
exports.Mutate = Mutate;
|
|
19
21
|
const index_1 = require("../transform/index");
|
|
20
|
-
const index_2 = require("../
|
|
21
|
-
const index_3 = require("../
|
|
22
|
-
const index_4 = require("../
|
|
23
|
-
const index_5 = require("../
|
|
24
|
-
const index_6 = require("../
|
|
25
|
-
const index_7 = require("../
|
|
26
|
-
const index_8 = require("../
|
|
27
|
-
const index_9 = require("../
|
|
28
|
-
const index_10 = require("../
|
|
29
|
-
const index_11 = require("../
|
|
30
|
-
const index_12 = require("../
|
|
31
|
-
const index_13 = require("
|
|
22
|
+
const index_2 = require("../assert/index");
|
|
23
|
+
const index_3 = require("../mutate/index");
|
|
24
|
+
const index_4 = require("../hash/index");
|
|
25
|
+
const index_5 = require("../equal/index");
|
|
26
|
+
const index_6 = require("../cast/index");
|
|
27
|
+
const index_7 = require("../clone/index");
|
|
28
|
+
const index_8 = require("../convert/index");
|
|
29
|
+
const index_9 = require("../create/index");
|
|
30
|
+
const index_10 = require("../clean/index");
|
|
31
|
+
const index_11 = require("../check/index");
|
|
32
|
+
const index_12 = require("../parse/index");
|
|
33
|
+
const index_13 = require("../default/index");
|
|
34
|
+
const index_14 = require("../delta/index");
|
|
35
|
+
const index_15 = require("../../errors/index");
|
|
36
|
+
/** Asserts a value matches the given type or throws an `AssertError` if invalid. */
|
|
37
|
+
function Assert(...args) {
|
|
38
|
+
return index_2.Assert.apply(index_2.Assert, args);
|
|
39
|
+
}
|
|
32
40
|
/** Casts a value into a given type. The return value will retain as much information of the original value as possible. */
|
|
33
41
|
function Cast(...args) {
|
|
34
|
-
return
|
|
42
|
+
return index_6.Cast.apply(index_6.Cast, args);
|
|
35
43
|
}
|
|
36
44
|
/** Creates a value from the given type */
|
|
37
45
|
function Create(...args) {
|
|
38
|
-
return
|
|
46
|
+
return index_9.Create.apply(index_9.Create, args);
|
|
39
47
|
}
|
|
40
48
|
/** Returns true if the value matches the given type */
|
|
41
49
|
function Check(...args) {
|
|
42
|
-
return
|
|
50
|
+
return index_11.Check.apply(index_11.Check, args);
|
|
43
51
|
}
|
|
44
52
|
/** `[Mutable]` Removes excess properties from a value and returns the result. This function does not check the value and returns an unknown type. You should Check the result before use. Clean is a mutable operation. To avoid mutation, Clone the value first. */
|
|
45
53
|
function Clean(...args) {
|
|
46
|
-
return
|
|
54
|
+
return index_10.Clean.apply(index_10.Clean, args);
|
|
47
55
|
}
|
|
48
|
-
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
56
|
+
/** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
49
57
|
function Convert(...args) {
|
|
50
|
-
return
|
|
58
|
+
return index_8.Convert.apply(index_8.Convert, args);
|
|
51
59
|
}
|
|
52
60
|
/** Returns a structural clone of the given value */
|
|
53
61
|
function Clone(value) {
|
|
54
|
-
return (0,
|
|
62
|
+
return (0, index_7.Clone)(value);
|
|
55
63
|
}
|
|
56
64
|
/** Decodes a value or throws if error */
|
|
57
65
|
function Decode(...args) {
|
|
@@ -62,7 +70,7 @@ function Decode(...args) {
|
|
|
62
70
|
}
|
|
63
71
|
/** `[Mutable]` Generates missing properties on a value using default schema annotations if available. This function does not check the value and returns an unknown type. You should Check the result before use. Default is a mutable operation. To avoid mutation, Clone the value first. */
|
|
64
72
|
function Default(...args) {
|
|
65
|
-
return
|
|
73
|
+
return index_13.Default.apply(index_13.Default, args);
|
|
66
74
|
}
|
|
67
75
|
/** Encodes a value or throws if error */
|
|
68
76
|
function Encode(...args) {
|
|
@@ -72,27 +80,31 @@ function Encode(...args) {
|
|
|
72
80
|
throw new index_1.TransformEncodeCheckError(schema, encoded, Errors(schema, references, encoded).First());
|
|
73
81
|
return encoded;
|
|
74
82
|
}
|
|
83
|
+
/** Parses a value or throws an `AssertError` if invalid. */
|
|
84
|
+
function Parse(...args) {
|
|
85
|
+
return index_12.Parse.apply(index_12.Parse, args);
|
|
86
|
+
}
|
|
75
87
|
/** Returns an iterator for each error in this value. */
|
|
76
88
|
function Errors(...args) {
|
|
77
|
-
return
|
|
89
|
+
return index_15.Errors.apply(index_15.Errors, args);
|
|
78
90
|
}
|
|
79
91
|
/** Returns true if left and right values are structurally equal */
|
|
80
92
|
function Equal(left, right) {
|
|
81
|
-
return (0,
|
|
93
|
+
return (0, index_5.Equal)(left, right);
|
|
82
94
|
}
|
|
83
95
|
/** Returns edits to transform the current value into the next value */
|
|
84
96
|
function Diff(current, next) {
|
|
85
|
-
return (0,
|
|
97
|
+
return (0, index_14.Diff)(current, next);
|
|
86
98
|
}
|
|
87
99
|
/** Returns a FNV1A-64 non cryptographic hash of the given value */
|
|
88
100
|
function Hash(value) {
|
|
89
|
-
return (0,
|
|
101
|
+
return (0, index_4.Hash)(value);
|
|
90
102
|
}
|
|
91
103
|
/** Returns a new value with edits applied to the given value */
|
|
92
104
|
function Patch(current, edits) {
|
|
93
|
-
return (0,
|
|
105
|
+
return (0, index_14.Patch)(current, edits);
|
|
94
106
|
}
|
|
95
107
|
/** `[Mutable]` Performs a deep mutable value assignment while retaining internal references. */
|
|
96
108
|
function Mutate(current, next) {
|
|
97
|
-
(0,
|
|
109
|
+
(0, index_3.Mutate)(current, next);
|
|
98
110
|
}
|
|
@@ -17,9 +17,9 @@ export declare class TypeCheck<T extends TSchema> {
|
|
|
17
17
|
/** Returns true if the value matches the compiled type. */
|
|
18
18
|
Check(value: unknown): value is Static<T>;
|
|
19
19
|
/** Decodes a value or throws if error */
|
|
20
|
-
Decode(value: unknown):
|
|
20
|
+
Decode<R = StaticDecode<T>>(value: unknown): R;
|
|
21
21
|
/** Encodes a value or throws if error */
|
|
22
|
-
Encode(value: unknown):
|
|
22
|
+
Encode<R = StaticEncode<T>>(value: unknown): R;
|
|
23
23
|
}
|
|
24
24
|
export declare class TypeCompilerUnknownTypeError extends TypeBoxError {
|
|
25
25
|
readonly schema: TSchema;
|
|
@@ -44,7 +44,7 @@ export class TypeCheck {
|
|
|
44
44
|
Decode(value) {
|
|
45
45
|
if (!this.checkFunc(value))
|
|
46
46
|
throw new TransformDecodeCheckError(this.schema, value, this.Errors(value).First());
|
|
47
|
-
return this.hasTransform ? TransformDecode(this.schema, this.references, value) : value;
|
|
47
|
+
return (this.hasTransform ? TransformDecode(this.schema, this.references, value) : value);
|
|
48
48
|
}
|
|
49
49
|
/** Encodes a value or throws if error */
|
|
50
50
|
Encode(value) {
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ValueErrorIterator, ValueError } from '../../errors/index.mjs';
|
|
2
|
+
import { TypeBoxError } from '../../type/error/error.mjs';
|
|
3
|
+
import { TSchema } from '../../type/schema/index.mjs';
|
|
4
|
+
import { Static } from '../../type/static/index.mjs';
|
|
5
|
+
export declare class AssertError extends TypeBoxError {
|
|
6
|
+
#private;
|
|
7
|
+
error: ValueError | undefined;
|
|
8
|
+
constructor(iterator: ValueErrorIterator);
|
|
9
|
+
/** Returns an iterator for each error in this value. */
|
|
10
|
+
Errors(): ValueErrorIterator;
|
|
11
|
+
}
|
|
12
|
+
/** Asserts a value matches the given type or throws an `AssertError` if invalid */
|
|
13
|
+
export declare function Assert<T extends TSchema>(schema: T, references: TSchema[], value: unknown): asserts value is Static<T>;
|
|
14
|
+
/** Asserts a value matches the given type or throws an `AssertError` if invalid */
|
|
15
|
+
export declare function Assert<T extends TSchema>(schema: T, value: unknown): asserts value is Static<T>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _AssertError_instances, _AssertError_iterator, _AssertError_Iterator;
|
|
13
|
+
import { Errors, ValueErrorIterator } from '../../errors/index.mjs';
|
|
14
|
+
import { TypeBoxError } from '../../type/error/error.mjs';
|
|
15
|
+
import { Check } from '../check/check.mjs';
|
|
16
|
+
// ------------------------------------------------------------------
|
|
17
|
+
// AssertError
|
|
18
|
+
// ------------------------------------------------------------------
|
|
19
|
+
export class AssertError extends TypeBoxError {
|
|
20
|
+
constructor(iterator) {
|
|
21
|
+
const error = iterator.First();
|
|
22
|
+
super(error === undefined ? 'Invalid Value' : error.message);
|
|
23
|
+
_AssertError_instances.add(this);
|
|
24
|
+
_AssertError_iterator.set(this, void 0);
|
|
25
|
+
__classPrivateFieldSet(this, _AssertError_iterator, iterator, "f");
|
|
26
|
+
this.error = error;
|
|
27
|
+
}
|
|
28
|
+
/** Returns an iterator for each error in this value. */
|
|
29
|
+
Errors() {
|
|
30
|
+
return new ValueErrorIterator(__classPrivateFieldGet(this, _AssertError_instances, "m", _AssertError_Iterator).call(this));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
_AssertError_iterator = new WeakMap(), _AssertError_instances = new WeakSet(), _AssertError_Iterator = function* _AssertError_Iterator() {
|
|
34
|
+
if (this.error)
|
|
35
|
+
yield this.error;
|
|
36
|
+
yield* __classPrivateFieldGet(this, _AssertError_iterator, "f");
|
|
37
|
+
};
|
|
38
|
+
// ------------------------------------------------------------------
|
|
39
|
+
// AssertValue
|
|
40
|
+
// ------------------------------------------------------------------
|
|
41
|
+
function AssertValue(schema, references, value) {
|
|
42
|
+
if (Check(schema, references, value))
|
|
43
|
+
return;
|
|
44
|
+
throw new AssertError(Errors(schema, references, value));
|
|
45
|
+
}
|
|
46
|
+
/** Asserts a value matches the given type or throws an `AssertError` if invalid */
|
|
47
|
+
export function Assert(...args) {
|
|
48
|
+
return args.length === 3 ? AssertValue(args[0], args[1], args[2]) : AssertValue(args[0], [], args[1]);
|
|
49
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './assert.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './assert.mjs';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IsObject, IsArray, IsString, IsNumber, IsNull } from '../guard/index.mjs';
|
|
2
2
|
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
3
3
|
import { Kind } from '../../type/symbols/index.mjs';
|
|
4
4
|
import { Create } from '../create/index.mjs';
|
|
@@ -100,7 +100,7 @@ function FromConstructor(schema, references, value) {
|
|
|
100
100
|
}
|
|
101
101
|
function FromIntersect(schema, references, value) {
|
|
102
102
|
const created = Create(schema, references);
|
|
103
|
-
const mapped =
|
|
103
|
+
const mapped = IsObject(created) && IsObject(value) ? { ...created, ...value } : value;
|
|
104
104
|
return Check(schema, references, mapped) ? mapped : Create(schema, references);
|
|
105
105
|
}
|
|
106
106
|
function FromNever(schema, references, value) {
|
|
@@ -7,17 +7,17 @@ import { Kind } from '../../type/symbols/index.mjs';
|
|
|
7
7
|
// ValueGuard
|
|
8
8
|
// ------------------------------------------------------------------
|
|
9
9
|
// prettier-ignore
|
|
10
|
-
import { IsString, IsObject, IsArray, IsUndefined } from '../guard/index.mjs';
|
|
10
|
+
import { HasPropertyKey, IsString, IsObject, IsArray, IsUndefined } from '../guard/index.mjs';
|
|
11
11
|
// ------------------------------------------------------------------
|
|
12
12
|
// TypeGuard
|
|
13
13
|
// ------------------------------------------------------------------
|
|
14
14
|
// prettier-ignore
|
|
15
|
-
import {
|
|
15
|
+
import { IsKind } from '../../type/guard/kind.mjs';
|
|
16
16
|
// ------------------------------------------------------------------
|
|
17
17
|
// IsCheckable
|
|
18
18
|
// ------------------------------------------------------------------
|
|
19
19
|
function IsCheckable(schema) {
|
|
20
|
-
return
|
|
20
|
+
return IsKind(schema) && schema[Kind] !== 'Unsafe';
|
|
21
21
|
}
|
|
22
22
|
// ------------------------------------------------------------------
|
|
23
23
|
// Types
|
|
@@ -31,7 +31,7 @@ function FromIntersect(schema, references, value) {
|
|
|
31
31
|
const unevaluatedProperties = schema.unevaluatedProperties;
|
|
32
32
|
const intersections = schema.allOf.map((schema) => Visit(schema, references, Clone(value)));
|
|
33
33
|
const composite = intersections.reduce((acc, value) => (IsObject(value) ? { ...acc, ...value } : value), {});
|
|
34
|
-
if (!IsObject(value) || !IsObject(composite) || !
|
|
34
|
+
if (!IsObject(value) || !IsObject(composite) || !IsKind(unevaluatedProperties))
|
|
35
35
|
return composite;
|
|
36
36
|
const knownkeys = KeyOfPropertyKeys(schema);
|
|
37
37
|
for (const key of Object.getOwnPropertyNames(value)) {
|
|
@@ -48,11 +48,11 @@ function FromObject(schema, references, value) {
|
|
|
48
48
|
return value; // Check IsArray for AllowArrayObject configuration
|
|
49
49
|
const additionalProperties = schema.additionalProperties;
|
|
50
50
|
for (const key of Object.getOwnPropertyNames(value)) {
|
|
51
|
-
if (
|
|
51
|
+
if (HasPropertyKey(schema.properties, key)) {
|
|
52
52
|
value[key] = Visit(schema.properties[key], references, value[key]);
|
|
53
53
|
continue;
|
|
54
54
|
}
|
|
55
|
-
if (
|
|
55
|
+
if (IsKind(additionalProperties) && Check(additionalProperties, references, value[key])) {
|
|
56
56
|
value[key] = Visit(additionalProperties, references, value[key]);
|
|
57
57
|
continue;
|
|
58
58
|
}
|
|
@@ -72,7 +72,7 @@ function FromRecord(schema, references, value) {
|
|
|
72
72
|
value[key] = Visit(propertySchema, references, value[key]);
|
|
73
73
|
continue;
|
|
74
74
|
}
|
|
75
|
-
if (
|
|
75
|
+
if (IsKind(additionalProperties) && Check(additionalProperties, references, value[key])) {
|
|
76
76
|
value[key] = Visit(additionalProperties, references, value[key]);
|
|
77
77
|
continue;
|
|
78
78
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// ------------------------------------------------------------------
|
|
2
2
|
// ValueGuard
|
|
3
3
|
// ------------------------------------------------------------------
|
|
4
|
-
import { IsArray, IsDate,
|
|
4
|
+
import { IsArray, IsDate, IsMap, IsSet, IsObject, IsTypedArray, IsValueType } from '../guard/index.mjs';
|
|
5
5
|
// ------------------------------------------------------------------
|
|
6
6
|
// Clonable
|
|
7
7
|
// ------------------------------------------------------------------
|
|
8
|
-
function
|
|
8
|
+
function FromObject(value) {
|
|
9
9
|
const Acc = {};
|
|
10
10
|
for (const key of Object.getOwnPropertyNames(value)) {
|
|
11
11
|
Acc[key] = Clone(value[key]);
|
|
@@ -15,16 +15,22 @@ function ObjectType(value) {
|
|
|
15
15
|
}
|
|
16
16
|
return Acc;
|
|
17
17
|
}
|
|
18
|
-
function
|
|
18
|
+
function FromArray(value) {
|
|
19
19
|
return value.map((element) => Clone(element));
|
|
20
20
|
}
|
|
21
|
-
function
|
|
21
|
+
function FromTypedArray(value) {
|
|
22
22
|
return value.slice();
|
|
23
23
|
}
|
|
24
|
-
function
|
|
24
|
+
function FromMap(value) {
|
|
25
|
+
return new Map(Clone([...value.entries()]));
|
|
26
|
+
}
|
|
27
|
+
function FromSet(value) {
|
|
28
|
+
return new Set(Clone([...value.entries()]));
|
|
29
|
+
}
|
|
30
|
+
function FromDate(value) {
|
|
25
31
|
return new Date(value.toISOString());
|
|
26
32
|
}
|
|
27
|
-
function
|
|
33
|
+
function FromValue(value) {
|
|
28
34
|
return value;
|
|
29
35
|
}
|
|
30
36
|
// ------------------------------------------------------------------
|
|
@@ -33,14 +39,18 @@ function ValueType(value) {
|
|
|
33
39
|
/** Returns a clone of the given value */
|
|
34
40
|
export function Clone(value) {
|
|
35
41
|
if (IsArray(value))
|
|
36
|
-
return
|
|
42
|
+
return FromArray(value);
|
|
37
43
|
if (IsDate(value))
|
|
38
|
-
return
|
|
39
|
-
if (IsStandardObject(value))
|
|
40
|
-
return ObjectType(value);
|
|
44
|
+
return FromDate(value);
|
|
41
45
|
if (IsTypedArray(value))
|
|
42
|
-
return
|
|
46
|
+
return FromTypedArray(value);
|
|
47
|
+
if (IsMap(value))
|
|
48
|
+
return FromMap(value);
|
|
49
|
+
if (IsSet(value))
|
|
50
|
+
return FromSet(value);
|
|
51
|
+
if (IsObject(value))
|
|
52
|
+
return FromObject(value);
|
|
43
53
|
if (IsValueType(value))
|
|
44
|
-
return
|
|
54
|
+
return FromValue(value);
|
|
45
55
|
throw new Error('ValueClone: Unable to clone value');
|
|
46
56
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TSchema } from '../../type/schema/index.mjs';
|
|
2
|
-
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
2
|
+
/** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
3
3
|
export declare function Convert(schema: TSchema, references: TSchema[], value: unknown): unknown;
|
|
4
|
-
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
4
|
+
/** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
5
5
|
export declare function Convert(schema: TSchema, value: unknown): unknown;
|
|
@@ -5,7 +5,7 @@ import { Kind } from '../../type/symbols/index.mjs';
|
|
|
5
5
|
// ------------------------------------------------------------------
|
|
6
6
|
// ValueGuard
|
|
7
7
|
// ------------------------------------------------------------------
|
|
8
|
-
import { IsArray, IsObject, IsDate, IsUndefined, IsString, IsNumber, IsBoolean, IsBigInt, IsSymbol
|
|
8
|
+
import { IsArray, IsObject, IsDate, IsUndefined, IsString, IsNumber, IsBoolean, IsBigInt, IsSymbol } from '../guard/index.mjs';
|
|
9
9
|
// ------------------------------------------------------------------
|
|
10
10
|
// Conversions
|
|
11
11
|
// ------------------------------------------------------------------
|
|
@@ -56,7 +56,7 @@ function TryConvertLiteral(schema, value) {
|
|
|
56
56
|
return (IsString(schema.const) ? TryConvertLiteralString(value, schema.const) :
|
|
57
57
|
IsNumber(schema.const) ? TryConvertLiteralNumber(value, schema.const) :
|
|
58
58
|
IsBoolean(schema.const) ? TryConvertLiteralBoolean(value, schema.const) :
|
|
59
|
-
|
|
59
|
+
value);
|
|
60
60
|
}
|
|
61
61
|
function TryConvertBoolean(value) {
|
|
62
62
|
return IsValueTrue(value) ? true : IsValueFalse(value) ? false : value;
|
|
@@ -139,16 +139,12 @@ function FromNumber(schema, references, value) {
|
|
|
139
139
|
}
|
|
140
140
|
// prettier-ignore
|
|
141
141
|
function FromObject(schema, references, value) {
|
|
142
|
-
|
|
143
|
-
if (!isConvertable)
|
|
142
|
+
if (!IsObject(value))
|
|
144
143
|
return value;
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
result[key] = HasPropertyKey(schema.properties, key)
|
|
148
|
-
? Visit(schema.properties[key], references, value[key])
|
|
149
|
-
: value[key];
|
|
144
|
+
for (const key of Object.getOwnPropertyNames(schema.properties)) {
|
|
145
|
+
value[key] = Visit(schema.properties[key], references, value[key]);
|
|
150
146
|
}
|
|
151
|
-
return
|
|
147
|
+
return value;
|
|
152
148
|
}
|
|
153
149
|
function FromRecord(schema, references, value) {
|
|
154
150
|
const isConvertable = IsObject(value);
|
|
@@ -156,11 +152,10 @@ function FromRecord(schema, references, value) {
|
|
|
156
152
|
return value;
|
|
157
153
|
const propertyKey = Object.getOwnPropertyNames(schema.patternProperties)[0];
|
|
158
154
|
const property = schema.patternProperties[propertyKey];
|
|
159
|
-
const result = {};
|
|
160
155
|
for (const [propKey, propValue] of Object.entries(value)) {
|
|
161
|
-
|
|
156
|
+
value[propKey] = Visit(property, references, propValue);
|
|
162
157
|
}
|
|
163
|
-
return
|
|
158
|
+
return value;
|
|
164
159
|
}
|
|
165
160
|
function FromRef(schema, references, value) {
|
|
166
161
|
return Visit(Deref(schema, references), references, value);
|
|
@@ -190,15 +185,19 @@ function FromUndefined(schema, references, value) {
|
|
|
190
185
|
}
|
|
191
186
|
function FromUnion(schema, references, value) {
|
|
192
187
|
for (const subschema of schema.anyOf) {
|
|
193
|
-
const converted = Visit(subschema, references, value);
|
|
188
|
+
const converted = Visit(subschema, references, Clone(value));
|
|
194
189
|
if (!Check(subschema, references, converted))
|
|
195
190
|
continue;
|
|
196
191
|
return converted;
|
|
197
192
|
}
|
|
198
193
|
return value;
|
|
199
194
|
}
|
|
195
|
+
function AddReference(references, schema) {
|
|
196
|
+
references.push(schema);
|
|
197
|
+
return references;
|
|
198
|
+
}
|
|
200
199
|
function Visit(schema, references, value) {
|
|
201
|
-
const references_ = IsString(schema.$id) ?
|
|
200
|
+
const references_ = IsString(schema.$id) ? AddReference(references, schema) : references;
|
|
202
201
|
const schema_ = schema;
|
|
203
202
|
switch (schema[Kind]) {
|
|
204
203
|
case 'Array':
|
|
@@ -241,10 +240,8 @@ function Visit(schema, references, value) {
|
|
|
241
240
|
return Default(value);
|
|
242
241
|
}
|
|
243
242
|
}
|
|
244
|
-
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
243
|
+
/** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
245
244
|
// prettier-ignore
|
|
246
245
|
export function Convert(...args) {
|
|
247
|
-
return args.length === 3
|
|
248
|
-
? Visit(args[0], args[1], args[2])
|
|
249
|
-
: Visit(args[0], [], args[1]);
|
|
246
|
+
return args.length === 3 ? Visit(args[0], args[1], args[2]) : Visit(args[0], [], args[1]);
|
|
250
247
|
}
|
|
@@ -380,8 +380,12 @@ function FromKind(schema, references) {
|
|
|
380
380
|
throw new Error('User defined types must specify a default value');
|
|
381
381
|
}
|
|
382
382
|
}
|
|
383
|
+
function AddReference(references, schema) {
|
|
384
|
+
references.push(schema);
|
|
385
|
+
return references;
|
|
386
|
+
}
|
|
383
387
|
function Visit(schema, references) {
|
|
384
|
-
const references_ = IsString(schema.$id) ?
|
|
388
|
+
const references_ = IsString(schema.$id) ? AddReference(references, schema) : references;
|
|
385
389
|
const schema_ = schema;
|
|
386
390
|
switch (schema_[Kind]) {
|
|
387
391
|
case 'Any':
|