@sinclair/typebox 0.24.7 → 0.24.10
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 +6 -5
- package/compiler/compiler.js +108 -101
- package/compiler/index.d.ts +1 -1
- package/compiler/index.js +0 -1
- package/package.json +1 -1
- package/readme.md +32 -19
- package/typebox.d.ts +1 -3
- package/typebox.js +1 -1
- package/value/cast.d.ts +5 -0
- package/value/cast.js +249 -0
- package/value/check.d.ts +2 -3
- package/value/check.js +181 -119
- package/value/create.d.ts +3 -4
- package/value/create.js +91 -101
- package/value/errors.d.ts +10 -0
- package/{compiler → value}/errors.js +77 -92
- package/value/index.d.ts +1 -0
- 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,249 @@
|
|
|
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
|
+
function Any(schema, references, value) {
|
|
76
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
77
|
+
}
|
|
78
|
+
function Array(schema, references, value) {
|
|
79
|
+
if (check_1.ValueCheck.Check(schema, references, value))
|
|
80
|
+
return value;
|
|
81
|
+
if (!globalThis.Array.isArray(value))
|
|
82
|
+
return create_1.ValueCreate.Create(schema, references);
|
|
83
|
+
return value.map((val) => Visit(schema.items, references, val));
|
|
84
|
+
}
|
|
85
|
+
function Boolean(schema, references, value) {
|
|
86
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
87
|
+
}
|
|
88
|
+
function Constructor(schema, references, value) {
|
|
89
|
+
if (check_1.ValueCheck.Check(schema, references, value))
|
|
90
|
+
return create_1.ValueCreate.Create(schema, references);
|
|
91
|
+
const required = new Set(schema.returns.required || []);
|
|
92
|
+
const result = function () { };
|
|
93
|
+
for (const [key, property] of globalThis.Object.entries(schema.returns.properties)) {
|
|
94
|
+
if (!required.has(key) && value.prototype[key] === undefined)
|
|
95
|
+
continue;
|
|
96
|
+
result.prototype[key] = Visit(property, references, value.prototype[key]);
|
|
97
|
+
}
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
function Enum(schema, references, value) {
|
|
101
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
102
|
+
}
|
|
103
|
+
function Function(schema, references, value) {
|
|
104
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
105
|
+
}
|
|
106
|
+
function Integer(schema, references, value) {
|
|
107
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
108
|
+
}
|
|
109
|
+
function Literal(schema, references, value) {
|
|
110
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
111
|
+
}
|
|
112
|
+
function Null(schema, references, value) {
|
|
113
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
114
|
+
}
|
|
115
|
+
function Number(schema, references, value) {
|
|
116
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
117
|
+
}
|
|
118
|
+
function Object(schema, references, value) {
|
|
119
|
+
if (check_1.ValueCheck.Check(schema, references, value))
|
|
120
|
+
return value;
|
|
121
|
+
if (value === null || typeof value !== 'object')
|
|
122
|
+
return create_1.ValueCreate.Create(schema, references);
|
|
123
|
+
const required = new Set(schema.required || []);
|
|
124
|
+
const result = {};
|
|
125
|
+
for (const [key, property] of globalThis.Object.entries(schema.properties)) {
|
|
126
|
+
if (!required.has(key) && value[key] === undefined)
|
|
127
|
+
continue;
|
|
128
|
+
result[key] = Visit(property, references, value[key]);
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
function Promise(schema, references, value) {
|
|
133
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
134
|
+
}
|
|
135
|
+
function Record(schema, references, value) {
|
|
136
|
+
if (check_1.ValueCheck.Check(schema, references, value))
|
|
137
|
+
return value;
|
|
138
|
+
if (value === null || typeof value !== 'object' || globalThis.Array.isArray(value))
|
|
139
|
+
return create_1.ValueCreate.Create(schema, references);
|
|
140
|
+
const subschemaKey = globalThis.Object.keys(schema.patternProperties)[0];
|
|
141
|
+
const subschema = schema.patternProperties[subschemaKey];
|
|
142
|
+
const result = {};
|
|
143
|
+
for (const [propKey, propValue] of globalThis.Object.entries(value)) {
|
|
144
|
+
result[propKey] = Visit(subschema, references, propValue);
|
|
145
|
+
}
|
|
146
|
+
return result;
|
|
147
|
+
}
|
|
148
|
+
function Recursive(schema, references, value) {
|
|
149
|
+
throw new Error('CastValue.Recursive: Cannot cast recursive schemas');
|
|
150
|
+
}
|
|
151
|
+
function Ref(schema, references, value) {
|
|
152
|
+
const reference = references.find((reference) => reference.$id === schema.$ref);
|
|
153
|
+
if (reference === undefined)
|
|
154
|
+
throw new Error(`CastValue.Ref: Cannot find schema with $id '${schema.$ref}'.`);
|
|
155
|
+
return Visit(reference, references, value);
|
|
156
|
+
}
|
|
157
|
+
function Self(schema, references, value) {
|
|
158
|
+
const reference = references.find((reference) => reference.$id === schema.$ref);
|
|
159
|
+
if (reference === undefined)
|
|
160
|
+
throw new Error(`CastValue.Self: Cannot find schema with $id '${schema.$ref}'.`);
|
|
161
|
+
return Visit(reference, references, value);
|
|
162
|
+
}
|
|
163
|
+
function String(schema, references, value) {
|
|
164
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
165
|
+
}
|
|
166
|
+
function Tuple(schema, references, value) {
|
|
167
|
+
if (check_1.ValueCheck.Check(schema, references, value))
|
|
168
|
+
return value;
|
|
169
|
+
if (!globalThis.Array.isArray(value))
|
|
170
|
+
return create_1.ValueCreate.Create(schema, references);
|
|
171
|
+
if (schema.items === undefined)
|
|
172
|
+
return [];
|
|
173
|
+
return schema.items.map((schema, index) => Visit(schema, references, value[index]));
|
|
174
|
+
}
|
|
175
|
+
function Undefined(schema, references, value) {
|
|
176
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
177
|
+
}
|
|
178
|
+
function Union(schema, references, value) {
|
|
179
|
+
return UnionValueCast.Create(schema, references, value);
|
|
180
|
+
}
|
|
181
|
+
function Uint8Array(schema, references, value) {
|
|
182
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
183
|
+
}
|
|
184
|
+
function Unknown(schema, references, value) {
|
|
185
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
186
|
+
}
|
|
187
|
+
function Void(schema, references, value) {
|
|
188
|
+
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
189
|
+
}
|
|
190
|
+
function Visit(schema, references, value) {
|
|
191
|
+
const anyReferences = schema.$id === undefined ? references : [schema, ...references];
|
|
192
|
+
const anySchema = schema;
|
|
193
|
+
switch (schema[Types.Kind]) {
|
|
194
|
+
case 'Any':
|
|
195
|
+
return Any(anySchema, anyReferences, value);
|
|
196
|
+
case 'Array':
|
|
197
|
+
return Array(anySchema, anyReferences, value);
|
|
198
|
+
case 'Boolean':
|
|
199
|
+
return Boolean(anySchema, anyReferences, value);
|
|
200
|
+
case 'Constructor':
|
|
201
|
+
return Constructor(anySchema, anyReferences, value);
|
|
202
|
+
case 'Enum':
|
|
203
|
+
return Enum(anySchema, anyReferences, value);
|
|
204
|
+
case 'Function':
|
|
205
|
+
return Function(anySchema, anyReferences, value);
|
|
206
|
+
case 'Integer':
|
|
207
|
+
return Integer(anySchema, anyReferences, value);
|
|
208
|
+
case 'Literal':
|
|
209
|
+
return Literal(anySchema, anyReferences, value);
|
|
210
|
+
case 'Null':
|
|
211
|
+
return Null(anySchema, anyReferences, value);
|
|
212
|
+
case 'Number':
|
|
213
|
+
return Number(anySchema, anyReferences, value);
|
|
214
|
+
case 'Object':
|
|
215
|
+
return Object(anySchema, anyReferences, value);
|
|
216
|
+
case 'Promise':
|
|
217
|
+
return Promise(anySchema, anyReferences, value);
|
|
218
|
+
case 'Record':
|
|
219
|
+
return Record(anySchema, anyReferences, value);
|
|
220
|
+
case 'Rec':
|
|
221
|
+
return Recursive(anySchema, anyReferences, value);
|
|
222
|
+
case 'Ref':
|
|
223
|
+
return Ref(anySchema, anyReferences, value);
|
|
224
|
+
case 'Self':
|
|
225
|
+
return Self(anySchema, anyReferences, value);
|
|
226
|
+
case 'String':
|
|
227
|
+
return String(anySchema, anyReferences, value);
|
|
228
|
+
case 'Tuple':
|
|
229
|
+
return Tuple(anySchema, anyReferences, value);
|
|
230
|
+
case 'Undefined':
|
|
231
|
+
return Undefined(anySchema, anyReferences, value);
|
|
232
|
+
case 'Union':
|
|
233
|
+
return Union(anySchema, anyReferences, value);
|
|
234
|
+
case 'Uint8Array':
|
|
235
|
+
return Uint8Array(anySchema, anyReferences, value);
|
|
236
|
+
case 'Unknown':
|
|
237
|
+
return Unknown(anySchema, anyReferences, value);
|
|
238
|
+
case 'Void':
|
|
239
|
+
return Void(anySchema, anyReferences, value);
|
|
240
|
+
default:
|
|
241
|
+
throw Error(`Unknown schema kind '${schema[Types.Kind]}'`);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
ValueCast.Visit = Visit;
|
|
245
|
+
function Cast(schema, references, value) {
|
|
246
|
+
return schema.$id === undefined ? Visit(schema, references, value) : Visit(schema, [schema, ...references], value);
|
|
247
|
+
}
|
|
248
|
+
ValueCast.Cast = Cast;
|
|
249
|
+
})(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,268 @@ 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
|
|
46
|
+
function Constructor(schema, references, value) {
|
|
47
|
+
return Visit(schema.returns, references, value);
|
|
52
48
|
}
|
|
53
|
-
function
|
|
54
|
-
for (const subschema of schema.anyOf) {
|
|
55
|
-
if (subschema.const === value)
|
|
56
|
-
return true;
|
|
57
|
-
}
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
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))) {
|
|
150
|
+
return false;
|
|
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))) {
|
|
104
155
|
return false;
|
|
105
|
-
|
|
106
|
-
for (const
|
|
107
|
-
|
|
108
|
-
if (!Visit(propertySchema, propertyValue))
|
|
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
|
-
|
|
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);
|
|
115
168
|
}
|
|
116
|
-
function
|
|
117
|
-
|
|
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);
|
|
118
174
|
}
|
|
119
|
-
function
|
|
120
|
-
if (!
|
|
121
|
-
throw new Error(`Check: Cannot locate schema with $id '${schema.$id}' for referenced type`);
|
|
122
|
-
const referenced = referenceMap.get(schema.$ref);
|
|
123
|
-
return Visit(referenced, value);
|
|
124
|
-
}
|
|
125
|
-
function String(schema, value) {
|
|
126
|
-
if (typeof value !== 'string')
|
|
175
|
+
function String(schema, references, value) {
|
|
176
|
+
if (!(typeof value === 'string')) {
|
|
127
177
|
return false;
|
|
178
|
+
}
|
|
179
|
+
if (schema.minLength !== undefined) {
|
|
180
|
+
if (!(value.length >= schema.minLength))
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
if (schema.maxLength !== undefined) {
|
|
184
|
+
if (!(value.length <= schema.maxLength))
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
128
187
|
if (schema.pattern !== undefined) {
|
|
129
188
|
const regex = new RegExp(schema.pattern);
|
|
130
|
-
|
|
189
|
+
if (!regex.test(value))
|
|
190
|
+
return false;
|
|
131
191
|
}
|
|
132
192
|
return true;
|
|
133
193
|
}
|
|
134
|
-
function Tuple(schema, value) {
|
|
135
|
-
if (
|
|
194
|
+
function Tuple(schema, references, value) {
|
|
195
|
+
if (!global.Array.isArray(value)) {
|
|
136
196
|
return false;
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (schema.items === undefined)
|
|
197
|
+
}
|
|
198
|
+
if (schema.items === undefined && !(value.length === 0)) {
|
|
140
199
|
return false;
|
|
141
|
-
|
|
200
|
+
}
|
|
201
|
+
if (!(value.length === schema.maxItems)) {
|
|
142
202
|
return false;
|
|
203
|
+
}
|
|
204
|
+
if (!schema.items) {
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
143
207
|
for (let i = 0; i < schema.items.length; i++) {
|
|
144
|
-
if (!Visit(schema.items[i], value[i]))
|
|
208
|
+
if (!Visit(schema.items[i], references, value[i]))
|
|
145
209
|
return false;
|
|
146
210
|
}
|
|
147
211
|
return true;
|
|
148
212
|
}
|
|
149
|
-
function Undefined(schema, value) {
|
|
213
|
+
function Undefined(schema, references, value) {
|
|
150
214
|
return value === undefined;
|
|
151
215
|
}
|
|
152
|
-
function Union(schema, value) {
|
|
153
|
-
|
|
154
|
-
if (Visit(schema.anyOf[i], value))
|
|
155
|
-
return true;
|
|
156
|
-
}
|
|
157
|
-
return false;
|
|
216
|
+
function Union(schema, references, value) {
|
|
217
|
+
return schema.anyOf.some((inner) => Visit(inner, references, value));
|
|
158
218
|
}
|
|
159
|
-
function Uint8Array(schema, value) {
|
|
160
|
-
|
|
219
|
+
function Uint8Array(schema, references, value) {
|
|
220
|
+
if (!(value instanceof globalThis.Uint8Array)) {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
if (schema.maxByteLength && !(value.length <= schema.maxByteLength)) {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
if (schema.minByteLength && !(value.length >= schema.minByteLength)) {
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
return true;
|
|
161
230
|
}
|
|
162
|
-
function Unknown(schema, value) {
|
|
231
|
+
function Unknown(schema, references, value) {
|
|
163
232
|
return true;
|
|
164
233
|
}
|
|
165
|
-
function Void(schema, value) {
|
|
234
|
+
function Void(schema, references, value) {
|
|
166
235
|
return value === null;
|
|
167
236
|
}
|
|
168
|
-
function Visit(schema, value) {
|
|
169
|
-
|
|
170
|
-
referenceMap.set(schema.$id, schema);
|
|
237
|
+
function Visit(schema, references, value) {
|
|
238
|
+
const anyReferences = schema.$id === undefined ? references : [schema, ...references];
|
|
171
239
|
const anySchema = schema;
|
|
172
240
|
switch (anySchema[Types.Kind]) {
|
|
173
241
|
case 'Any':
|
|
174
|
-
return Any(anySchema, value);
|
|
242
|
+
return Any(anySchema, anyReferences, value);
|
|
175
243
|
case 'Array':
|
|
176
|
-
return Array(anySchema, value);
|
|
244
|
+
return Array(anySchema, anyReferences, value);
|
|
177
245
|
case 'Boolean':
|
|
178
|
-
return Boolean(anySchema, value);
|
|
246
|
+
return Boolean(anySchema, anyReferences, value);
|
|
179
247
|
case 'Constructor':
|
|
180
|
-
return Constructor(anySchema, value);
|
|
181
|
-
case 'Enum':
|
|
182
|
-
return Enum(anySchema, value);
|
|
248
|
+
return Constructor(anySchema, anyReferences, value);
|
|
183
249
|
case 'Function':
|
|
184
|
-
return Function(anySchema, value);
|
|
250
|
+
return Function(anySchema, anyReferences, value);
|
|
185
251
|
case 'Integer':
|
|
186
|
-
return Integer(anySchema, value);
|
|
187
|
-
case 'Intersect':
|
|
188
|
-
return Intersect(anySchema, value);
|
|
252
|
+
return Integer(anySchema, anyReferences, value);
|
|
189
253
|
case 'Literal':
|
|
190
|
-
return Literal(anySchema, value);
|
|
254
|
+
return Literal(anySchema, anyReferences, value);
|
|
191
255
|
case 'Null':
|
|
192
|
-
return Null(anySchema, value);
|
|
256
|
+
return Null(anySchema, anyReferences, value);
|
|
193
257
|
case 'Number':
|
|
194
|
-
return Number(anySchema, value);
|
|
258
|
+
return Number(anySchema, anyReferences, value);
|
|
195
259
|
case 'Object':
|
|
196
|
-
return Object(anySchema, value);
|
|
260
|
+
return Object(anySchema, anyReferences, value);
|
|
197
261
|
case 'Promise':
|
|
198
|
-
return Promise(anySchema, value);
|
|
262
|
+
return Promise(anySchema, anyReferences, value);
|
|
199
263
|
case 'Record':
|
|
200
|
-
return Record(anySchema, value);
|
|
201
|
-
case 'Rec':
|
|
202
|
-
return Recursive(anySchema, value);
|
|
264
|
+
return Record(anySchema, anyReferences, value);
|
|
203
265
|
case 'Ref':
|
|
204
|
-
return Ref(anySchema, value);
|
|
266
|
+
return Ref(anySchema, anyReferences, value);
|
|
205
267
|
case 'Self':
|
|
206
|
-
return Self(anySchema, value);
|
|
268
|
+
return Self(anySchema, anyReferences, value);
|
|
207
269
|
case 'String':
|
|
208
|
-
return String(anySchema, value);
|
|
270
|
+
return String(anySchema, anyReferences, value);
|
|
209
271
|
case 'Tuple':
|
|
210
|
-
return Tuple(anySchema, value);
|
|
272
|
+
return Tuple(anySchema, anyReferences, value);
|
|
211
273
|
case 'Undefined':
|
|
212
|
-
return Undefined(anySchema, value);
|
|
274
|
+
return Undefined(anySchema, anyReferences, value);
|
|
213
275
|
case 'Union':
|
|
214
|
-
return Union(anySchema, value);
|
|
276
|
+
return Union(anySchema, anyReferences, value);
|
|
215
277
|
case 'Uint8Array':
|
|
216
|
-
return Uint8Array(anySchema, value);
|
|
278
|
+
return Uint8Array(anySchema, anyReferences, value);
|
|
217
279
|
case 'Unknown':
|
|
218
|
-
return Unknown(anySchema, value);
|
|
280
|
+
return Unknown(anySchema, anyReferences, value);
|
|
219
281
|
case 'Void':
|
|
220
|
-
return Void(anySchema, value);
|
|
282
|
+
return Void(anySchema, anyReferences, value);
|
|
221
283
|
default:
|
|
222
|
-
throw Error(`Unknown schema kind '${schema[Types.Kind]}'`);
|
|
284
|
+
throw new Error(`CheckValue: Unknown schema kind '${schema[Types.Kind]}'`);
|
|
223
285
|
}
|
|
224
286
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
return Visit(schema, value);
|
|
287
|
+
// -------------------------------------------------------------------------
|
|
288
|
+
// Check
|
|
289
|
+
// -------------------------------------------------------------------------
|
|
290
|
+
function Check(schema, references, value) {
|
|
291
|
+
return schema.$id === undefined ? Visit(schema, references, value) : Visit(schema, [schema, ...references], value);
|
|
230
292
|
}
|
|
231
|
-
|
|
232
|
-
})(
|
|
293
|
+
ValueCheck.Check = Check;
|
|
294
|
+
})(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
|
}
|