@sinclair/typebox 0.24.7 → 0.24.8
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 +5 -5
- package/compiler/compiler.js +98 -98
- package/compiler/index.d.ts +1 -1
- package/compiler/index.js +1 -1
- package/package.json +1 -1
- package/readme.md +30 -17
- package/typebox.d.ts +1 -3
- package/value/cast.d.ts +5 -0
- package/value/cast.js +251 -0
- package/value/check.d.ts +2 -3
- package/value/check.js +172 -118
- package/value/create.d.ts +3 -4
- package/value/create.js +79 -94
- package/value/errors.d.ts +10 -0
- package/{compiler → value}/errors.js +70 -91
- package/value/value.d.ts +17 -13
- package/value/value.js +20 -32
- package/compiler/errors.d.ts +0 -10
- package/value/clone.d.ts +0 -3
- package/value/clone.js +0 -94
- package/value/delta.d.ts +0 -13
- package/value/delta.js +0 -191
- package/value/pointer.d.ts +0 -12
- package/value/pointer.js +0 -110
- package/value/reflect.d.ts +0 -2
- package/value/reflect.js +0 -42
- package/value/upcast.d.ts +0 -4
- package/value/upcast.js +0 -247
package/value/cast.js
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*--------------------------------------------------------------------------
|
|
3
|
+
|
|
4
|
+
@sinclair/typebox/value
|
|
5
|
+
|
|
6
|
+
The MIT License (MIT)
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in
|
|
18
|
+
all copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
+
THE SOFTWARE.
|
|
27
|
+
|
|
28
|
+
---------------------------------------------------------------------------*/
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.ValueCast = void 0;
|
|
31
|
+
const Types = require("../typebox");
|
|
32
|
+
const create_1 = require("./create");
|
|
33
|
+
const check_1 = require("./check");
|
|
34
|
+
// --------------------------------------------------------------------------
|
|
35
|
+
// Specialized Union Patch. Because a value can be one of many different
|
|
36
|
+
// unions with properties potentially overlapping, we need a strategy
|
|
37
|
+
// in which to resolve the appropriate schema to patch from.
|
|
38
|
+
//
|
|
39
|
+
// The following will score each union type found within the types anyOf
|
|
40
|
+
// array. Typically this is executed for objects only, so the score is a
|
|
41
|
+
// essentially a tally of how many properties are valid. The reasoning
|
|
42
|
+
// here is the discriminator field would tip the scales in favor of that
|
|
43
|
+
// union if other properties overlap and match.
|
|
44
|
+
// --------------------------------------------------------------------------
|
|
45
|
+
var UnionValueCast;
|
|
46
|
+
(function (UnionValueCast) {
|
|
47
|
+
function Score(schema, references, value) {
|
|
48
|
+
let score = 0;
|
|
49
|
+
if (schema[Types.Kind] === 'Object' && typeof value === 'object' && value !== null) {
|
|
50
|
+
const objectSchema = schema;
|
|
51
|
+
const entries = globalThis.Object.entries(objectSchema.properties);
|
|
52
|
+
score += entries.reduce((acc, [key, schema]) => acc + (check_1.ValueCheck.Check(schema, references, value[key]) ? 1 : 0), 0);
|
|
53
|
+
}
|
|
54
|
+
return score;
|
|
55
|
+
}
|
|
56
|
+
function Select(schema, references, value) {
|
|
57
|
+
let select = schema.anyOf[0];
|
|
58
|
+
let best = 0;
|
|
59
|
+
for (const subschema of schema.anyOf) {
|
|
60
|
+
const score = Score(subschema, references, value);
|
|
61
|
+
if (score > best) {
|
|
62
|
+
select = subschema;
|
|
63
|
+
best = score;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return select;
|
|
67
|
+
}
|
|
68
|
+
function Create(schema, references, value) {
|
|
69
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : ValueCast.Cast(Select(schema, references, value), references, value);
|
|
70
|
+
}
|
|
71
|
+
UnionValueCast.Create = Create;
|
|
72
|
+
})(UnionValueCast || (UnionValueCast = {}));
|
|
73
|
+
var ValueCast;
|
|
74
|
+
(function (ValueCast) {
|
|
75
|
+
const ids = new Map();
|
|
76
|
+
function Any(schema, references, value) {
|
|
77
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
78
|
+
}
|
|
79
|
+
function Array(schema, references, value) {
|
|
80
|
+
if (check_1.ValueCheck.Check(schema, references, value))
|
|
81
|
+
return value;
|
|
82
|
+
if (!globalThis.Array.isArray(value))
|
|
83
|
+
return create_1.ValueCreate.Create(schema, references);
|
|
84
|
+
return value.map((val) => Visit(schema.items, references, val));
|
|
85
|
+
}
|
|
86
|
+
function Boolean(schema, references, value) {
|
|
87
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
88
|
+
}
|
|
89
|
+
function Constructor(schema, references, value) {
|
|
90
|
+
if (check_1.ValueCheck.Check(schema, references, value))
|
|
91
|
+
return create_1.ValueCreate.Create(schema, references);
|
|
92
|
+
const required = new Set(schema.returns.required || []);
|
|
93
|
+
const result = function () { };
|
|
94
|
+
for (const [key, property] of globalThis.Object.entries(schema.returns.properties)) {
|
|
95
|
+
if (!required.has(key) && value.prototype[key] === undefined)
|
|
96
|
+
continue;
|
|
97
|
+
result.prototype[key] = Visit(property, references, value.prototype[key]);
|
|
98
|
+
}
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
function Enum(schema, references, value) {
|
|
102
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
103
|
+
}
|
|
104
|
+
function Function(schema, references, value) {
|
|
105
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
106
|
+
}
|
|
107
|
+
function Integer(schema, references, value) {
|
|
108
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
109
|
+
}
|
|
110
|
+
function Literal(schema, references, value) {
|
|
111
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
112
|
+
}
|
|
113
|
+
function Null(schema, references, value) {
|
|
114
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
115
|
+
}
|
|
116
|
+
function Number(schema, references, value) {
|
|
117
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
118
|
+
}
|
|
119
|
+
function Object(schema, references, value) {
|
|
120
|
+
if (check_1.ValueCheck.Check(schema, references, value))
|
|
121
|
+
return value;
|
|
122
|
+
if (value === null || typeof value !== 'object')
|
|
123
|
+
return create_1.ValueCreate.Create(schema, references);
|
|
124
|
+
ids.set(schema.$id, schema);
|
|
125
|
+
const required = new Set(schema.required || []);
|
|
126
|
+
const result = {};
|
|
127
|
+
for (const [key, property] of globalThis.Object.entries(schema.properties)) {
|
|
128
|
+
if (!required.has(key) && value[key] === undefined)
|
|
129
|
+
continue;
|
|
130
|
+
result[key] = Visit(property, references, value[key]);
|
|
131
|
+
}
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
function Promise(schema, references, value) {
|
|
135
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
136
|
+
}
|
|
137
|
+
function Record(schema, references, value) {
|
|
138
|
+
if (check_1.ValueCheck.Check(schema, references, value))
|
|
139
|
+
return value;
|
|
140
|
+
if (value === null || typeof value !== 'object' || globalThis.Array.isArray(value))
|
|
141
|
+
return create_1.ValueCreate.Create(schema, references);
|
|
142
|
+
const subschemaKey = globalThis.Object.keys(schema.patternProperties)[0];
|
|
143
|
+
const subschema = schema.patternProperties[subschemaKey];
|
|
144
|
+
const result = {};
|
|
145
|
+
for (const [propKey, propValue] of globalThis.Object.entries(value)) {
|
|
146
|
+
result[propKey] = Visit(subschema, references, propValue);
|
|
147
|
+
}
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
function Recursive(schema, references, value) {
|
|
151
|
+
throw Error('Cannot patch recursive schemas');
|
|
152
|
+
}
|
|
153
|
+
function Ref(schema, references, value) {
|
|
154
|
+
const reference = references.find((reference) => reference.$id === schema.$ref);
|
|
155
|
+
if (reference === undefined)
|
|
156
|
+
throw new Error(`CastValue.Ref: Cannot find schema with $id '${schema.$ref}'.`);
|
|
157
|
+
return Visit(reference, references, value);
|
|
158
|
+
}
|
|
159
|
+
function Self(schema, references, value) {
|
|
160
|
+
const reference = references.find((reference) => reference.$id === schema.$ref);
|
|
161
|
+
if (reference === undefined)
|
|
162
|
+
throw new Error(`CastValue.Self: Cannot find schema with $id '${schema.$ref}'.`);
|
|
163
|
+
return Visit(reference, references, value);
|
|
164
|
+
}
|
|
165
|
+
function String(schema, references, value) {
|
|
166
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
167
|
+
}
|
|
168
|
+
function Tuple(schema, references, value) {
|
|
169
|
+
if (check_1.ValueCheck.Check(schema, references, value))
|
|
170
|
+
return value;
|
|
171
|
+
if (!globalThis.Array.isArray(value))
|
|
172
|
+
return create_1.ValueCreate.Create(schema, references);
|
|
173
|
+
if (schema.items === undefined)
|
|
174
|
+
return [];
|
|
175
|
+
return schema.items.map((schema, index) => Visit(schema, references, value[index]));
|
|
176
|
+
}
|
|
177
|
+
function Undefined(schema, references, value) {
|
|
178
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
179
|
+
}
|
|
180
|
+
function Union(schema, references, value) {
|
|
181
|
+
return UnionValueCast.Create(schema, references, value);
|
|
182
|
+
}
|
|
183
|
+
function Uint8Array(schema, references, value) {
|
|
184
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
185
|
+
}
|
|
186
|
+
function Unknown(schema, references, value) {
|
|
187
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
188
|
+
}
|
|
189
|
+
function Void(schema, references, value) {
|
|
190
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
191
|
+
}
|
|
192
|
+
function Visit(schema, references, value) {
|
|
193
|
+
const anyReferences = schema.$id === undefined ? references : [schema, ...references];
|
|
194
|
+
const anySchema = schema;
|
|
195
|
+
switch (schema[Types.Kind]) {
|
|
196
|
+
case 'Any':
|
|
197
|
+
return Any(anySchema, anyReferences, value);
|
|
198
|
+
case 'Array':
|
|
199
|
+
return Array(anySchema, anyReferences, value);
|
|
200
|
+
case 'Boolean':
|
|
201
|
+
return Boolean(anySchema, anyReferences, value);
|
|
202
|
+
case 'Constructor':
|
|
203
|
+
return Constructor(anySchema, anyReferences, value);
|
|
204
|
+
case 'Enum':
|
|
205
|
+
return Enum(anySchema, anyReferences, value);
|
|
206
|
+
case 'Function':
|
|
207
|
+
return Function(anySchema, anyReferences, value);
|
|
208
|
+
case 'Integer':
|
|
209
|
+
return Integer(anySchema, anyReferences, value);
|
|
210
|
+
case 'Literal':
|
|
211
|
+
return Literal(anySchema, anyReferences, value);
|
|
212
|
+
case 'Null':
|
|
213
|
+
return Null(anySchema, anyReferences, value);
|
|
214
|
+
case 'Number':
|
|
215
|
+
return Number(anySchema, anyReferences, value);
|
|
216
|
+
case 'Object':
|
|
217
|
+
return Object(anySchema, anyReferences, value);
|
|
218
|
+
case 'Promise':
|
|
219
|
+
return Promise(anySchema, anyReferences, value);
|
|
220
|
+
case 'Record':
|
|
221
|
+
return Record(anySchema, anyReferences, value);
|
|
222
|
+
case 'Rec':
|
|
223
|
+
return Recursive(anySchema, anyReferences, value);
|
|
224
|
+
case 'Ref':
|
|
225
|
+
return Ref(anySchema, anyReferences, value);
|
|
226
|
+
case 'Self':
|
|
227
|
+
return Self(anySchema, anyReferences, value);
|
|
228
|
+
case 'String':
|
|
229
|
+
return String(anySchema, anyReferences, value);
|
|
230
|
+
case 'Tuple':
|
|
231
|
+
return Tuple(anySchema, anyReferences, value);
|
|
232
|
+
case 'Undefined':
|
|
233
|
+
return Undefined(anySchema, anyReferences, value);
|
|
234
|
+
case 'Union':
|
|
235
|
+
return Union(anySchema, anyReferences, value);
|
|
236
|
+
case 'Uint8Array':
|
|
237
|
+
return Uint8Array(anySchema, anyReferences, value);
|
|
238
|
+
case 'Unknown':
|
|
239
|
+
return Unknown(anySchema, anyReferences, value);
|
|
240
|
+
case 'Void':
|
|
241
|
+
return Void(anySchema, anyReferences, value);
|
|
242
|
+
default:
|
|
243
|
+
throw Error(`Unknown schema kind '${schema[Types.Kind]}'`);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
ValueCast.Visit = Visit;
|
|
247
|
+
function Cast(schema, references, value) {
|
|
248
|
+
return schema.$id === undefined ? Visit(schema, references, value) : Visit(schema, [schema, ...references], value);
|
|
249
|
+
}
|
|
250
|
+
ValueCast.Cast = Cast;
|
|
251
|
+
})(ValueCast = exports.ValueCast || (exports.ValueCast = {}));
|
package/value/check.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import * as Types from '../typebox';
|
|
2
|
-
export declare namespace
|
|
3
|
-
function
|
|
4
|
-
function Check<T extends Types.TSchema>(schema: T, value: any): boolean;
|
|
2
|
+
export declare namespace ValueCheck {
|
|
3
|
+
function Check<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: any): boolean;
|
|
5
4
|
}
|
package/value/check.js
CHANGED
|
@@ -27,206 +27,260 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.
|
|
30
|
+
exports.ValueCheck = void 0;
|
|
31
31
|
const Types = require("../typebox");
|
|
32
|
-
var
|
|
33
|
-
(function (
|
|
34
|
-
|
|
35
|
-
function Any(schema, value) {
|
|
32
|
+
var ValueCheck;
|
|
33
|
+
(function (ValueCheck) {
|
|
34
|
+
function Any(schema, references, value) {
|
|
36
35
|
return true;
|
|
37
36
|
}
|
|
38
|
-
function Array(schema, value) {
|
|
39
|
-
if (
|
|
37
|
+
function Array(schema, references, value) {
|
|
38
|
+
if (!globalThis.Array.isArray(value)) {
|
|
40
39
|
return false;
|
|
41
|
-
for (let i = 0; i < value.length; i++) {
|
|
42
|
-
if (!Visit(schema.items, value[i]))
|
|
43
|
-
return false;
|
|
44
40
|
}
|
|
45
|
-
return
|
|
41
|
+
return value.every((val) => Visit(schema.items, references, val));
|
|
46
42
|
}
|
|
47
|
-
function Boolean(schema, value) {
|
|
43
|
+
function Boolean(schema, references, value) {
|
|
48
44
|
return typeof value === 'boolean';
|
|
49
45
|
}
|
|
50
|
-
function Constructor(schema, value) {
|
|
51
|
-
return Visit(schema.returns, value
|
|
52
|
-
}
|
|
53
|
-
function Enum(schema, value) {
|
|
54
|
-
for (const subschema of schema.anyOf) {
|
|
55
|
-
if (subschema.const === value)
|
|
56
|
-
return true;
|
|
57
|
-
}
|
|
58
|
-
return false;
|
|
46
|
+
function Constructor(schema, references, value) {
|
|
47
|
+
return Visit(schema.returns, references, value);
|
|
59
48
|
}
|
|
60
|
-
function Function(schema, value) {
|
|
49
|
+
function Function(schema, references, value) {
|
|
61
50
|
return typeof value === 'function';
|
|
62
51
|
}
|
|
63
|
-
function Integer(schema, value) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
52
|
+
function Integer(schema, references, value) {
|
|
53
|
+
if (!(typeof value === 'number')) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
if (!globalThis.Number.isInteger(value)) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
if (schema.multipleOf && !(value % schema.multipleOf === 0)) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
if (schema.exclusiveMinimum && !(value > schema.exclusiveMinimum)) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
if (schema.exclusiveMaximum && !(value < schema.exclusiveMaximum)) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
if (schema.minimum && !(value >= schema.minimum)) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
if (schema.maximum && !(value <= schema.maximum)) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
return true;
|
|
68
75
|
}
|
|
69
|
-
function Literal(schema, value) {
|
|
70
|
-
return schema.const
|
|
76
|
+
function Literal(schema, references, value) {
|
|
77
|
+
return value === schema.const;
|
|
71
78
|
}
|
|
72
|
-
function Null(schema, value) {
|
|
79
|
+
function Null(schema, references, value) {
|
|
73
80
|
return value === null;
|
|
74
81
|
}
|
|
75
|
-
function Number(schema, value) {
|
|
76
|
-
|
|
82
|
+
function Number(schema, references, value) {
|
|
83
|
+
if (!(typeof value === 'number')) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
if (schema.multipleOf && !(value % schema.multipleOf === 0)) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
if (schema.exclusiveMinimum && !(value > schema.exclusiveMinimum)) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
if (schema.exclusiveMaximum && !(value < schema.exclusiveMaximum)) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
if (schema.minimum && !(value >= schema.minimum)) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
if (schema.maximum && !(value <= schema.maximum)) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
return true;
|
|
77
102
|
}
|
|
78
|
-
function Object(schema, value) {
|
|
79
|
-
if (typeof value
|
|
103
|
+
function Object(schema, references, value) {
|
|
104
|
+
if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
if (schema.minProperties !== undefined && !(globalThis.Object.keys(value).length >= schema.minProperties)) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
if (schema.maxProperties !== undefined && !(globalThis.Object.keys(value).length <= schema.maxProperties)) {
|
|
80
111
|
return false;
|
|
112
|
+
}
|
|
81
113
|
const propertyKeys = globalThis.Object.keys(schema.properties);
|
|
82
114
|
if (schema.additionalProperties === false) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
115
|
+
// optimization: If the property key length matches the required keys length
|
|
116
|
+
// then we only need check that the values property key length matches that
|
|
117
|
+
// of the property key length. This is because exhaustive testing for values
|
|
118
|
+
// will occur in subsequent property tests.
|
|
119
|
+
if (schema.required && schema.required.length === propertyKeys.length && !(globalThis.Object.keys(value).length === propertyKeys.length)) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
if (!globalThis.Object.keys(value).every((key) => propertyKeys.includes(key))) {
|
|
86
124
|
return false;
|
|
125
|
+
}
|
|
87
126
|
}
|
|
88
127
|
}
|
|
89
128
|
for (const propertyKey of propertyKeys) {
|
|
90
129
|
const propertySchema = schema.properties[propertyKey];
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
130
|
+
if (schema.required && schema.required.includes(propertyKey)) {
|
|
131
|
+
if (!Visit(propertySchema, references, value[propertyKey])) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
if (value[propertyKey] !== undefined) {
|
|
137
|
+
if (!Visit(propertySchema, references, value[propertyKey])) {
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
96
142
|
}
|
|
97
143
|
return true;
|
|
98
144
|
}
|
|
99
|
-
function Promise(schema, value) {
|
|
100
|
-
return typeof value === 'object' && typeof value
|
|
145
|
+
function Promise(schema, references, value) {
|
|
146
|
+
return typeof value === 'object' && typeof value.then === 'function';
|
|
101
147
|
}
|
|
102
|
-
function Record(schema, value) {
|
|
103
|
-
if (typeof value
|
|
148
|
+
function Record(schema, references, value) {
|
|
149
|
+
if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
|
|
104
150
|
return false;
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
151
|
+
}
|
|
152
|
+
const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
|
|
153
|
+
const regex = new RegExp(keyPattern);
|
|
154
|
+
if (!globalThis.Object.keys(value).every((key) => regex.test(key))) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
for (const propValue of globalThis.Object.values(value)) {
|
|
158
|
+
if (!Visit(valueSchema, references, propValue))
|
|
109
159
|
return false;
|
|
110
160
|
}
|
|
111
161
|
return true;
|
|
112
162
|
}
|
|
113
|
-
function
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
163
|
+
function Ref(schema, references, value) {
|
|
164
|
+
const reference = references.find((reference) => reference.$id === schema.$ref);
|
|
165
|
+
if (reference === undefined)
|
|
166
|
+
throw new Error(`CheckValue.Ref: Cannot find schema with $id '${schema.$ref}'.`);
|
|
167
|
+
return Visit(reference, references, value);
|
|
118
168
|
}
|
|
119
|
-
function Self(schema, value) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
return Visit(
|
|
169
|
+
function Self(schema, references, value) {
|
|
170
|
+
const reference = references.find((reference) => reference.$id === schema.$ref);
|
|
171
|
+
if (reference === undefined)
|
|
172
|
+
throw new Error(`CheckValue.Self: Cannot find schema with $id '${schema.$ref}'.`);
|
|
173
|
+
return Visit(reference, references, value);
|
|
124
174
|
}
|
|
125
|
-
function String(schema, value) {
|
|
126
|
-
if (typeof value
|
|
175
|
+
function String(schema, references, value) {
|
|
176
|
+
if (!(typeof value === 'string')) {
|
|
127
177
|
return false;
|
|
178
|
+
}
|
|
128
179
|
if (schema.pattern !== undefined) {
|
|
129
180
|
const regex = new RegExp(schema.pattern);
|
|
130
|
-
|
|
181
|
+
if (!regex.test(value))
|
|
182
|
+
return false;
|
|
131
183
|
}
|
|
132
184
|
return true;
|
|
133
185
|
}
|
|
134
|
-
function Tuple(schema, value) {
|
|
135
|
-
if (
|
|
186
|
+
function Tuple(schema, references, value) {
|
|
187
|
+
if (!global.Array.isArray(value)) {
|
|
136
188
|
return false;
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (schema.items === undefined)
|
|
189
|
+
}
|
|
190
|
+
if (schema.items === undefined && !(value.length === 0)) {
|
|
140
191
|
return false;
|
|
141
|
-
|
|
192
|
+
}
|
|
193
|
+
if (!(value.length === schema.maxItems)) {
|
|
142
194
|
return false;
|
|
195
|
+
}
|
|
196
|
+
if (!schema.items) {
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
143
199
|
for (let i = 0; i < schema.items.length; i++) {
|
|
144
|
-
if (!Visit(schema.items[i], value[i]))
|
|
200
|
+
if (!Visit(schema.items[i], references, value[i]))
|
|
145
201
|
return false;
|
|
146
202
|
}
|
|
147
203
|
return true;
|
|
148
204
|
}
|
|
149
|
-
function Undefined(schema, value) {
|
|
205
|
+
function Undefined(schema, references, value) {
|
|
150
206
|
return value === undefined;
|
|
151
207
|
}
|
|
152
|
-
function Union(schema, value) {
|
|
153
|
-
|
|
154
|
-
if (Visit(schema.anyOf[i], value))
|
|
155
|
-
return true;
|
|
156
|
-
}
|
|
157
|
-
return false;
|
|
208
|
+
function Union(schema, references, value) {
|
|
209
|
+
return schema.anyOf.some((inner) => Visit(inner, references, value));
|
|
158
210
|
}
|
|
159
|
-
function Uint8Array(schema, value) {
|
|
160
|
-
|
|
211
|
+
function Uint8Array(schema, references, value) {
|
|
212
|
+
if (!(value instanceof globalThis.Uint8Array)) {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
if (schema.maxByteLength && !(value.length <= schema.maxByteLength)) {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
if (schema.minByteLength && !(value.length >= schema.minByteLength)) {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
return true;
|
|
161
222
|
}
|
|
162
|
-
function Unknown(schema, value) {
|
|
223
|
+
function Unknown(schema, references, value) {
|
|
163
224
|
return true;
|
|
164
225
|
}
|
|
165
|
-
function Void(schema, value) {
|
|
226
|
+
function Void(schema, references, value) {
|
|
166
227
|
return value === null;
|
|
167
228
|
}
|
|
168
|
-
function Visit(schema, value) {
|
|
169
|
-
|
|
170
|
-
referenceMap.set(schema.$id, schema);
|
|
229
|
+
function Visit(schema, references, value) {
|
|
230
|
+
const anyReferences = schema.$id === undefined ? references : [schema, ...references];
|
|
171
231
|
const anySchema = schema;
|
|
172
232
|
switch (anySchema[Types.Kind]) {
|
|
173
233
|
case 'Any':
|
|
174
|
-
return Any(anySchema, value);
|
|
234
|
+
return Any(anySchema, anyReferences, value);
|
|
175
235
|
case 'Array':
|
|
176
|
-
return Array(anySchema, value);
|
|
236
|
+
return Array(anySchema, anyReferences, value);
|
|
177
237
|
case 'Boolean':
|
|
178
|
-
return Boolean(anySchema, value);
|
|
238
|
+
return Boolean(anySchema, anyReferences, value);
|
|
179
239
|
case 'Constructor':
|
|
180
|
-
return Constructor(anySchema, value);
|
|
181
|
-
case 'Enum':
|
|
182
|
-
return Enum(anySchema, value);
|
|
240
|
+
return Constructor(anySchema, anyReferences, value);
|
|
183
241
|
case 'Function':
|
|
184
|
-
return Function(anySchema, value);
|
|
242
|
+
return Function(anySchema, anyReferences, value);
|
|
185
243
|
case 'Integer':
|
|
186
|
-
return Integer(anySchema, value);
|
|
187
|
-
case 'Intersect':
|
|
188
|
-
return Intersect(anySchema, value);
|
|
244
|
+
return Integer(anySchema, anyReferences, value);
|
|
189
245
|
case 'Literal':
|
|
190
|
-
return Literal(anySchema, value);
|
|
246
|
+
return Literal(anySchema, anyReferences, value);
|
|
191
247
|
case 'Null':
|
|
192
|
-
return Null(anySchema, value);
|
|
248
|
+
return Null(anySchema, anyReferences, value);
|
|
193
249
|
case 'Number':
|
|
194
|
-
return Number(anySchema, value);
|
|
250
|
+
return Number(anySchema, anyReferences, value);
|
|
195
251
|
case 'Object':
|
|
196
|
-
return Object(anySchema, value);
|
|
252
|
+
return Object(anySchema, anyReferences, value);
|
|
197
253
|
case 'Promise':
|
|
198
|
-
return Promise(anySchema, value);
|
|
254
|
+
return Promise(anySchema, anyReferences, value);
|
|
199
255
|
case 'Record':
|
|
200
|
-
return Record(anySchema, value);
|
|
201
|
-
case 'Rec':
|
|
202
|
-
return Recursive(anySchema, value);
|
|
256
|
+
return Record(anySchema, anyReferences, value);
|
|
203
257
|
case 'Ref':
|
|
204
|
-
return Ref(anySchema, value);
|
|
258
|
+
return Ref(anySchema, anyReferences, value);
|
|
205
259
|
case 'Self':
|
|
206
|
-
return Self(anySchema, value);
|
|
260
|
+
return Self(anySchema, anyReferences, value);
|
|
207
261
|
case 'String':
|
|
208
|
-
return String(anySchema, value);
|
|
262
|
+
return String(anySchema, anyReferences, value);
|
|
209
263
|
case 'Tuple':
|
|
210
|
-
return Tuple(anySchema, value);
|
|
264
|
+
return Tuple(anySchema, anyReferences, value);
|
|
211
265
|
case 'Undefined':
|
|
212
|
-
return Undefined(anySchema, value);
|
|
266
|
+
return Undefined(anySchema, anyReferences, value);
|
|
213
267
|
case 'Union':
|
|
214
|
-
return Union(anySchema, value);
|
|
268
|
+
return Union(anySchema, anyReferences, value);
|
|
215
269
|
case 'Uint8Array':
|
|
216
|
-
return Uint8Array(anySchema, value);
|
|
270
|
+
return Uint8Array(anySchema, anyReferences, value);
|
|
217
271
|
case 'Unknown':
|
|
218
|
-
return Unknown(anySchema, value);
|
|
272
|
+
return Unknown(anySchema, anyReferences, value);
|
|
219
273
|
case 'Void':
|
|
220
|
-
return Void(anySchema, value);
|
|
274
|
+
return Void(anySchema, anyReferences, value);
|
|
221
275
|
default:
|
|
222
276
|
throw Error(`Unknown schema kind '${schema[Types.Kind]}'`);
|
|
223
277
|
}
|
|
224
278
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
return Visit(schema, value);
|
|
279
|
+
// -------------------------------------------------------------------------
|
|
280
|
+
// Check
|
|
281
|
+
// -------------------------------------------------------------------------
|
|
282
|
+
function Check(schema, references, value) {
|
|
283
|
+
return schema.$id === undefined ? Visit(schema, references, value) : Visit(schema, [schema, ...references], value);
|
|
230
284
|
}
|
|
231
|
-
|
|
232
|
-
})(
|
|
285
|
+
ValueCheck.Check = Check;
|
|
286
|
+
})(ValueCheck = exports.ValueCheck || (exports.ValueCheck = {}));
|
package/value/create.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import * as Types from '../typebox';
|
|
2
|
-
export declare namespace
|
|
2
|
+
export declare namespace ValueCreate {
|
|
3
3
|
/** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
|
|
4
|
-
function Visit<T extends Types.TSchema>(schema: T): Types.Static<T>;
|
|
5
|
-
|
|
6
|
-
function Create<T extends Types.TSchema>(schema: T): Types.Static<T>;
|
|
4
|
+
function Visit<T extends Types.TSchema>(schema: T, references: Types.TSchema[]): Types.Static<T>;
|
|
5
|
+
function Create<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R]): Types.Static<T>;
|
|
7
6
|
}
|