@sinclair/typebox 0.23.4 → 0.24.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/compiler/compiler.d.ts +25 -0
- package/compiler/compiler.js +347 -0
- package/compiler/index.d.ts +1 -0
- package/compiler/index.js +40 -0
- package/license +1 -1
- package/package.json +6 -6
- package/readme.md +377 -361
- package/typebox.d.ts +306 -296
- package/typebox.js +247 -260
- package/value/check.d.ts +5 -0
- package/value/check.js +232 -0
- package/value/clone.d.ts +3 -0
- package/value/clone.js +94 -0
- package/value/create.d.ts +7 -0
- package/value/create.js +337 -0
- package/value/delta.d.ts +13 -0
- package/value/delta.js +191 -0
- package/value/index.d.ts +1 -0
- package/value/index.js +40 -0
- package/value/pointer.d.ts +12 -0
- package/value/pointer.js +110 -0
- package/value/reflect.d.ts +2 -0
- package/value/reflect.js +42 -0
- package/value/upcast.d.ts +4 -0
- package/value/upcast.js +247 -0
- package/value/value.d.ts +17 -0
- package/value/value.js +70 -0
package/value/check.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
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.CheckValue = void 0;
|
|
31
|
+
const Types = require("../typebox");
|
|
32
|
+
var CheckValue;
|
|
33
|
+
(function (CheckValue) {
|
|
34
|
+
const referenceMap = new Map();
|
|
35
|
+
function Any(schema, value) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
function Array(schema, value) {
|
|
39
|
+
if (typeof value !== 'object' || !globalThis.Array.isArray(value))
|
|
40
|
+
return false;
|
|
41
|
+
for (let i = 0; i < value.length; i++) {
|
|
42
|
+
if (!Visit(schema.items, value[i]))
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
function Boolean(schema, value) {
|
|
48
|
+
return typeof value === 'boolean';
|
|
49
|
+
}
|
|
50
|
+
function Constructor(schema, value) {
|
|
51
|
+
return Visit(schema.returns, value.prototype); // check return type only (as an object)
|
|
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;
|
|
59
|
+
}
|
|
60
|
+
function Function(schema, value) {
|
|
61
|
+
return typeof value === 'function';
|
|
62
|
+
}
|
|
63
|
+
function Integer(schema, value) {
|
|
64
|
+
return typeof value === 'number' && globalThis.Number.isInteger(value);
|
|
65
|
+
}
|
|
66
|
+
function Intersect(schema, value) {
|
|
67
|
+
return Object(schema, value);
|
|
68
|
+
}
|
|
69
|
+
function Literal(schema, value) {
|
|
70
|
+
return schema.const === value;
|
|
71
|
+
}
|
|
72
|
+
function Null(schema, value) {
|
|
73
|
+
return value === null;
|
|
74
|
+
}
|
|
75
|
+
function Number(schema, value) {
|
|
76
|
+
return typeof value === 'number';
|
|
77
|
+
}
|
|
78
|
+
function Object(schema, value) {
|
|
79
|
+
if (typeof value !== 'object' || value === null)
|
|
80
|
+
return false;
|
|
81
|
+
const propertyKeys = globalThis.Object.keys(schema.properties);
|
|
82
|
+
if (schema.additionalProperties === false) {
|
|
83
|
+
const valueKeys = globalThis.Object.keys(value);
|
|
84
|
+
for (const valueKey of valueKeys) {
|
|
85
|
+
if (!propertyKeys.includes(valueKey))
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
for (const propertyKey of propertyKeys) {
|
|
90
|
+
const propertySchema = schema.properties[propertyKey];
|
|
91
|
+
const propertyValue = value[propertyKey];
|
|
92
|
+
if (propertyValue === undefined && schema.required !== undefined && !schema.required.includes(propertyKey))
|
|
93
|
+
return true;
|
|
94
|
+
if (!Visit(propertySchema, propertyValue))
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
function Promise(schema, value) {
|
|
100
|
+
return typeof value === 'object' && typeof value['then'] === 'function';
|
|
101
|
+
}
|
|
102
|
+
function Record(schema, value) {
|
|
103
|
+
if (typeof value !== 'object' || value === null)
|
|
104
|
+
return false;
|
|
105
|
+
const propertySchema = globalThis.Object.values(schema.patternProperties)[0];
|
|
106
|
+
for (const key of globalThis.Object.keys(value)) {
|
|
107
|
+
const propertyValue = value[key];
|
|
108
|
+
if (!Visit(propertySchema, propertyValue))
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
function Recursive(schema, value) {
|
|
114
|
+
throw new Error('Cannot typeof recursive types');
|
|
115
|
+
}
|
|
116
|
+
function Ref(schema, value) {
|
|
117
|
+
throw new Error('Cannot typeof reference types');
|
|
118
|
+
}
|
|
119
|
+
function Self(schema, value) {
|
|
120
|
+
if (!referenceMap.has(schema.$ref))
|
|
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')
|
|
127
|
+
return false;
|
|
128
|
+
if (schema.pattern !== undefined) {
|
|
129
|
+
const regex = new RegExp(schema.pattern);
|
|
130
|
+
return value.match(regex) !== null;
|
|
131
|
+
}
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
function Tuple(schema, value) {
|
|
135
|
+
if (typeof value !== 'object' || !globalThis.Array.isArray(value))
|
|
136
|
+
return false;
|
|
137
|
+
if (schema.items === undefined && value.length === 0)
|
|
138
|
+
return true;
|
|
139
|
+
if (schema.items === undefined)
|
|
140
|
+
return false;
|
|
141
|
+
if (value.length < schema.minItems || value.length > schema.maxItems)
|
|
142
|
+
return false;
|
|
143
|
+
for (let i = 0; i < schema.items.length; i++) {
|
|
144
|
+
if (!Visit(schema.items[i], value[i]))
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
function Undefined(schema, value) {
|
|
150
|
+
return value === undefined;
|
|
151
|
+
}
|
|
152
|
+
function Union(schema, value) {
|
|
153
|
+
for (let i = 0; i < schema.anyOf.length; i++) {
|
|
154
|
+
if (Visit(schema.anyOf[i], value))
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
function Uint8Array(schema, value) {
|
|
160
|
+
return value instanceof globalThis.Uint8Array;
|
|
161
|
+
}
|
|
162
|
+
function Unknown(schema, value) {
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
function Void(schema, value) {
|
|
166
|
+
return value === null;
|
|
167
|
+
}
|
|
168
|
+
function Visit(schema, value) {
|
|
169
|
+
if (schema.$id !== undefined)
|
|
170
|
+
referenceMap.set(schema.$id, schema);
|
|
171
|
+
const anySchema = schema;
|
|
172
|
+
switch (anySchema[Types.Kind]) {
|
|
173
|
+
case 'Any':
|
|
174
|
+
return Any(anySchema, value);
|
|
175
|
+
case 'Array':
|
|
176
|
+
return Array(anySchema, value);
|
|
177
|
+
case 'Boolean':
|
|
178
|
+
return Boolean(anySchema, value);
|
|
179
|
+
case 'Constructor':
|
|
180
|
+
return Constructor(anySchema, value);
|
|
181
|
+
case 'Enum':
|
|
182
|
+
return Enum(anySchema, value);
|
|
183
|
+
case 'Function':
|
|
184
|
+
return Function(anySchema, value);
|
|
185
|
+
case 'Integer':
|
|
186
|
+
return Integer(anySchema, value);
|
|
187
|
+
case 'Intersect':
|
|
188
|
+
return Intersect(anySchema, value);
|
|
189
|
+
case 'Literal':
|
|
190
|
+
return Literal(anySchema, value);
|
|
191
|
+
case 'Null':
|
|
192
|
+
return Null(anySchema, value);
|
|
193
|
+
case 'Number':
|
|
194
|
+
return Number(anySchema, value);
|
|
195
|
+
case 'Object':
|
|
196
|
+
return Object(anySchema, value);
|
|
197
|
+
case 'Promise':
|
|
198
|
+
return Promise(anySchema, value);
|
|
199
|
+
case 'Record':
|
|
200
|
+
return Record(anySchema, value);
|
|
201
|
+
case 'Rec':
|
|
202
|
+
return Recursive(anySchema, value);
|
|
203
|
+
case 'Ref':
|
|
204
|
+
return Ref(anySchema, value);
|
|
205
|
+
case 'Self':
|
|
206
|
+
return Self(anySchema, value);
|
|
207
|
+
case 'String':
|
|
208
|
+
return String(anySchema, value);
|
|
209
|
+
case 'Tuple':
|
|
210
|
+
return Tuple(anySchema, value);
|
|
211
|
+
case 'Undefined':
|
|
212
|
+
return Undefined(anySchema, value);
|
|
213
|
+
case 'Union':
|
|
214
|
+
return Union(anySchema, value);
|
|
215
|
+
case 'Uint8Array':
|
|
216
|
+
return Uint8Array(anySchema, value);
|
|
217
|
+
case 'Unknown':
|
|
218
|
+
return Unknown(anySchema, value);
|
|
219
|
+
case 'Void':
|
|
220
|
+
return Void(anySchema, value);
|
|
221
|
+
default:
|
|
222
|
+
throw Error(`Unknown schema kind '${schema[Types.Kind]}'`);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
CheckValue.Visit = Visit;
|
|
226
|
+
function Check(schema, value) {
|
|
227
|
+
if (referenceMap.size > 0)
|
|
228
|
+
referenceMap.clear();
|
|
229
|
+
return Visit(schema, value);
|
|
230
|
+
}
|
|
231
|
+
CheckValue.Check = Check;
|
|
232
|
+
})(CheckValue = exports.CheckValue || (exports.CheckValue = {}));
|
package/value/clone.d.ts
ADDED
package/value/clone.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
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.CloneValue = void 0;
|
|
31
|
+
const reflect_1 = require("./reflect");
|
|
32
|
+
var CloneValue;
|
|
33
|
+
(function (CloneValue) {
|
|
34
|
+
function Undefined(_value) {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
function Null(_value) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
function Function(value) {
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
function Object(value) {
|
|
44
|
+
return globalThis.Object.entries(value).reduce((acc, [key, value]) => {
|
|
45
|
+
return { ...acc, [key]: Create(value) };
|
|
46
|
+
}, {});
|
|
47
|
+
}
|
|
48
|
+
function Array(value) {
|
|
49
|
+
return value.map((element) => Create(element));
|
|
50
|
+
}
|
|
51
|
+
function BigInt(value) {
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
function Symbol(value) {
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
function String(value) {
|
|
58
|
+
return value;
|
|
59
|
+
}
|
|
60
|
+
function Boolean(value) {
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
function Number(value) {
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
function Create(value) {
|
|
67
|
+
const typeName = (0, reflect_1.Reflect)(value);
|
|
68
|
+
switch (typeName) {
|
|
69
|
+
case 'array':
|
|
70
|
+
return Array(value);
|
|
71
|
+
case 'bigint':
|
|
72
|
+
return BigInt(value);
|
|
73
|
+
case 'boolean':
|
|
74
|
+
return Boolean(value);
|
|
75
|
+
case 'function':
|
|
76
|
+
return Function(value);
|
|
77
|
+
case 'null':
|
|
78
|
+
return Null(value);
|
|
79
|
+
case 'number':
|
|
80
|
+
return Number(value);
|
|
81
|
+
case 'object':
|
|
82
|
+
return Object(value);
|
|
83
|
+
case 'string':
|
|
84
|
+
return String(value);
|
|
85
|
+
case 'symbol':
|
|
86
|
+
return Symbol(value);
|
|
87
|
+
case 'undefined':
|
|
88
|
+
return Undefined(value);
|
|
89
|
+
default:
|
|
90
|
+
throw new Error(`Cannot clone from unknown typename ${typeName}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
CloneValue.Create = Create;
|
|
94
|
+
})(CloneValue = exports.CloneValue || (exports.CloneValue = {}));
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import * as Types from '../typebox';
|
|
2
|
+
export declare namespace CreateValue {
|
|
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
|
+
/** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
|
|
6
|
+
function Create<T extends Types.TSchema>(schema: T): Types.Static<T>;
|
|
7
|
+
}
|
package/value/create.js
ADDED
|
@@ -0,0 +1,337 @@
|
|
|
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.CreateValue = void 0;
|
|
31
|
+
const Types = require("../typebox");
|
|
32
|
+
var CreateValue;
|
|
33
|
+
(function (CreateValue) {
|
|
34
|
+
const referenceMap = new Map();
|
|
35
|
+
let recursionDepth = 0;
|
|
36
|
+
function Any(schema) {
|
|
37
|
+
if (schema.default !== undefined) {
|
|
38
|
+
return schema.default;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
return {};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function Array(schema) {
|
|
45
|
+
if (schema.default !== undefined) {
|
|
46
|
+
return schema.default;
|
|
47
|
+
}
|
|
48
|
+
else if (schema.minItems !== undefined) {
|
|
49
|
+
return globalThis.Array.from({ length: schema.minItems }).map((item) => {
|
|
50
|
+
return CreateValue.Create(schema.items);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
return [];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function Boolean(schema) {
|
|
58
|
+
if (schema.default !== undefined) {
|
|
59
|
+
return schema.default;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function Constructor(schema) {
|
|
66
|
+
if (schema.default !== undefined) {
|
|
67
|
+
return schema.default;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
const value = CreateValue.Create(schema.returns);
|
|
71
|
+
if (typeof value === 'object' && !globalThis.Array.isArray(value)) {
|
|
72
|
+
return class {
|
|
73
|
+
constructor() {
|
|
74
|
+
for (const [key, val] of globalThis.Object.entries(value)) {
|
|
75
|
+
const facade = this;
|
|
76
|
+
facade[key] = val;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
return class {
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function Enum(schema) {
|
|
88
|
+
if (schema.default !== undefined) {
|
|
89
|
+
return schema.default;
|
|
90
|
+
}
|
|
91
|
+
else if (schema.anyOf.length === 0) {
|
|
92
|
+
throw new Error('Cannot create default enum value as this enum has no items');
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
return schema.anyOf[0].const;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function Function(schema) {
|
|
99
|
+
if (schema.default !== undefined) {
|
|
100
|
+
return schema.default;
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
return () => CreateValue.Create(schema.returns);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function Integer(schema) {
|
|
107
|
+
if (schema.default !== undefined) {
|
|
108
|
+
return schema.default;
|
|
109
|
+
}
|
|
110
|
+
else if (schema.minimum !== undefined) {
|
|
111
|
+
return schema.minimum;
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
return 0;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function Intersect(schema) {
|
|
118
|
+
if (schema.default !== undefined) {
|
|
119
|
+
return schema.default;
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
return (schema.default ||
|
|
123
|
+
globalThis.Object.entries(schema.properties).reduce((acc, [key, schema]) => {
|
|
124
|
+
return { ...acc, [key]: CreateValue.Create(schema) };
|
|
125
|
+
}, {}));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function Literal(schema) {
|
|
129
|
+
return schema.const;
|
|
130
|
+
}
|
|
131
|
+
function Null(schema) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
function Number(schema) {
|
|
135
|
+
if (schema.default !== undefined) {
|
|
136
|
+
return schema.default;
|
|
137
|
+
}
|
|
138
|
+
else if (schema.minimum !== undefined) {
|
|
139
|
+
return schema.minimum;
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
return 0;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function Object(schema) {
|
|
146
|
+
if (schema.default !== undefined) {
|
|
147
|
+
return schema.default;
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
// StackOverflow Prevention
|
|
151
|
+
if (schema['$dynamicAnchor'] !== undefined) {
|
|
152
|
+
for (const [key, value] of globalThis.Object.entries(schema.properties)) {
|
|
153
|
+
if (value['$dynamicRef'] !== undefined && schema.required && schema.required.includes(key)) {
|
|
154
|
+
throw Error(`Cannot create recursive object with immediate recursive property`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const required = new Set(schema.required);
|
|
159
|
+
return (schema.default ||
|
|
160
|
+
globalThis.Object.entries(schema.properties).reduce((acc, [key, schema]) => {
|
|
161
|
+
return required.has(key) ? { ...acc, [key]: CreateValue.Create(schema) } : { ...acc };
|
|
162
|
+
}, {}));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
function Promise(schema) {
|
|
166
|
+
if (schema.default !== undefined) {
|
|
167
|
+
return schema.default;
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
return globalThis.Promise.resolve(CreateValue.Create(schema.item));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
function Record(schema) {
|
|
174
|
+
const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
|
|
175
|
+
if (schema.default !== undefined) {
|
|
176
|
+
return schema.default;
|
|
177
|
+
}
|
|
178
|
+
else if (!(keyPattern === '^.*$' || keyPattern === '^(0|[1-9][0-9]*)$')) {
|
|
179
|
+
const propertyKeys = keyPattern.slice(1, keyPattern.length - 1).split('|');
|
|
180
|
+
return propertyKeys.reduce((acc, key) => {
|
|
181
|
+
return { ...acc, [key]: Create(valueSchema) };
|
|
182
|
+
}, {});
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
return {};
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function Recursive(schema) {
|
|
189
|
+
if (schema.default !== undefined) {
|
|
190
|
+
return schema.default;
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
throw new Error('Rec types require a default value');
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
function Ref(schema) {
|
|
197
|
+
if (schema.default !== undefined) {
|
|
198
|
+
return schema.default;
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
throw new Error('Ref types require a default value');
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
function String(schema) {
|
|
205
|
+
if (schema.pattern !== undefined) {
|
|
206
|
+
if (schema.default === undefined) {
|
|
207
|
+
throw Error('String types with patterns must specify a default value');
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
return schema.default;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
if (schema.default !== undefined) {
|
|
215
|
+
return schema.default;
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
return '';
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function Tuple(schema) {
|
|
223
|
+
if (schema.default !== undefined) {
|
|
224
|
+
return schema.default;
|
|
225
|
+
}
|
|
226
|
+
if (schema.items === undefined) {
|
|
227
|
+
return [];
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
return globalThis.Array.from({ length: schema.minItems }).map((_, index) => CreateValue.Create(schema.items[index]));
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
function Undefined(schema) {
|
|
234
|
+
return undefined;
|
|
235
|
+
}
|
|
236
|
+
function Union(schema) {
|
|
237
|
+
if (schema.default !== undefined) {
|
|
238
|
+
return schema.default;
|
|
239
|
+
}
|
|
240
|
+
else if (schema.anyOf.length === 0) {
|
|
241
|
+
throw Error('Cannot generate Union with empty set');
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
return CreateValue.Create(schema.anyOf[0]);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
function Uint8Array(schema) {
|
|
248
|
+
if (schema.default !== undefined) {
|
|
249
|
+
return schema.default;
|
|
250
|
+
}
|
|
251
|
+
else if (schema.minByteLength) {
|
|
252
|
+
return new globalThis.Uint8Array(schema.minByteLength);
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
return new globalThis.Uint8Array(0);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
function Unknown(schema) {
|
|
259
|
+
if (schema.default !== undefined) {
|
|
260
|
+
return schema.default;
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
return {};
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function Void(schema) {
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
/** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
|
|
270
|
+
function Visit(schema) {
|
|
271
|
+
recursionDepth += 1;
|
|
272
|
+
if (recursionDepth >= 1000)
|
|
273
|
+
throw new Error('Cannot create value as schema contains a infinite expansion');
|
|
274
|
+
const anySchema = schema;
|
|
275
|
+
if (anySchema.$id !== undefined)
|
|
276
|
+
referenceMap.set(anySchema.$id, anySchema);
|
|
277
|
+
switch (anySchema[Types.Kind]) {
|
|
278
|
+
case 'Any':
|
|
279
|
+
return Any(anySchema);
|
|
280
|
+
case 'Array':
|
|
281
|
+
return Array(anySchema);
|
|
282
|
+
case 'Boolean':
|
|
283
|
+
return Boolean(anySchema);
|
|
284
|
+
case 'Constructor':
|
|
285
|
+
return Constructor(anySchema);
|
|
286
|
+
case 'Enum':
|
|
287
|
+
return Enum(anySchema);
|
|
288
|
+
case 'Function':
|
|
289
|
+
return Function(anySchema);
|
|
290
|
+
case 'Integer':
|
|
291
|
+
return Integer(anySchema);
|
|
292
|
+
case 'Intersect':
|
|
293
|
+
return Intersect(anySchema);
|
|
294
|
+
case 'Literal':
|
|
295
|
+
return Literal(anySchema);
|
|
296
|
+
case 'Null':
|
|
297
|
+
return Null(anySchema);
|
|
298
|
+
case 'Number':
|
|
299
|
+
return Number(anySchema);
|
|
300
|
+
case 'Object':
|
|
301
|
+
return Object(anySchema);
|
|
302
|
+
case 'Promise':
|
|
303
|
+
return Promise(anySchema);
|
|
304
|
+
case 'Record':
|
|
305
|
+
return Record(anySchema);
|
|
306
|
+
case 'Rec':
|
|
307
|
+
return Recursive(anySchema);
|
|
308
|
+
case 'Ref':
|
|
309
|
+
return Ref(anySchema);
|
|
310
|
+
case 'String':
|
|
311
|
+
return String(anySchema);
|
|
312
|
+
case 'Tuple':
|
|
313
|
+
return Tuple(anySchema);
|
|
314
|
+
case 'Undefined':
|
|
315
|
+
return Undefined(anySchema);
|
|
316
|
+
case 'Union':
|
|
317
|
+
return Union(anySchema);
|
|
318
|
+
case 'Uint8Array':
|
|
319
|
+
return Uint8Array(anySchema);
|
|
320
|
+
case 'Unknown':
|
|
321
|
+
return Unknown(anySchema);
|
|
322
|
+
case 'Void':
|
|
323
|
+
return Void(anySchema);
|
|
324
|
+
case 'Self':
|
|
325
|
+
return Visit(referenceMap.get(anySchema.$ref));
|
|
326
|
+
default:
|
|
327
|
+
throw Error(`Unknown schema kind '${schema[Types.Kind]}'`);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
CreateValue.Visit = Visit;
|
|
331
|
+
/** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
|
|
332
|
+
function Create(schema) {
|
|
333
|
+
recursionDepth = 0;
|
|
334
|
+
return Visit(schema);
|
|
335
|
+
}
|
|
336
|
+
CreateValue.Create = Create;
|
|
337
|
+
})(CreateValue = exports.CreateValue || (exports.CreateValue = {}));
|
package/value/delta.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare enum EditType {
|
|
2
|
+
Delete = 0,
|
|
3
|
+
Update = 1,
|
|
4
|
+
Insert = 2
|
|
5
|
+
}
|
|
6
|
+
export declare type Edit = Insert | Update | Delete;
|
|
7
|
+
export declare type Update = [EditType.Update, string, any];
|
|
8
|
+
export declare type Insert = [EditType.Insert, string, any];
|
|
9
|
+
export declare type Delete = [EditType.Delete, string];
|
|
10
|
+
export declare namespace DeltaValue {
|
|
11
|
+
function Diff(valueA: any, valueB: any): Edit[];
|
|
12
|
+
function Edit(valueA: any, operations: Edit[]): any;
|
|
13
|
+
}
|