@sinclair/typebox 0.24.40 → 0.24.41
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/package.json +1 -1
- package/typebox.d.ts +4 -2
- package/value/cast.js +7 -6
- package/value/index.d.ts +1 -0
- package/value/index.js +1 -0
- package/value/pointer.d.ts +19 -8
- package/value/pointer.js +79 -39
- package/value/value.d.ts +1 -1
- package/value/value.js +1 -1
package/package.json
CHANGED
package/typebox.d.ts
CHANGED
|
@@ -116,9 +116,11 @@ export declare type UnionToTuple<U, L = UnionLast<U>> = [U] extends [never] ? []
|
|
|
116
116
|
export declare type UnionStringLiteralToTuple<T> = T extends TUnion<infer L> ? {
|
|
117
117
|
[I in keyof L]: L[I] extends TLiteral<infer C> ? C : never;
|
|
118
118
|
} : never;
|
|
119
|
-
export declare type
|
|
119
|
+
export declare type UnionLiteralsFromObject<T extends TObject> = {
|
|
120
120
|
[K in ObjectPropertyKeys<T>]: TLiteral<K>;
|
|
121
121
|
} extends infer R ? UnionToTuple<R[keyof R]> : never;
|
|
122
|
+
export interface TKeyOf<T extends TObject> extends TUnion<UnionLiteralsFromObject<T>> {
|
|
123
|
+
}
|
|
122
124
|
export declare type TLiteralValue = string | number | boolean;
|
|
123
125
|
export interface TLiteral<T extends TLiteralValue = TLiteralValue> extends TSchema {
|
|
124
126
|
[Kind]: 'Literal';
|
|
@@ -329,7 +331,7 @@ export declare class TypeBuilder {
|
|
|
329
331
|
/** Creates a intersect type. */
|
|
330
332
|
Intersect<T extends TObject[]>(objects: [...T], options?: ObjectOptions): TIntersect<T>;
|
|
331
333
|
/** Creates a keyof type */
|
|
332
|
-
KeyOf<T extends TObject>(object: T, options?: SchemaOptions):
|
|
334
|
+
KeyOf<T extends TObject>(object: T, options?: SchemaOptions): TKeyOf<T>;
|
|
333
335
|
/** Creates a literal type. */
|
|
334
336
|
Literal<T extends TLiteralValue>(value: T, options?: SchemaOptions): TLiteral<T>;
|
|
335
337
|
/** Creates a never type */
|
package/value/cast.js
CHANGED
|
@@ -31,6 +31,7 @@ exports.ValueCast = exports.ValueCastUnknownTypeError = exports.ValueCastRecursi
|
|
|
31
31
|
const Types = require("../typebox");
|
|
32
32
|
const create_1 = require("./create");
|
|
33
33
|
const check_1 = require("./check");
|
|
34
|
+
const clone_1 = require("./clone");
|
|
34
35
|
var UnionValueCast;
|
|
35
36
|
(function (UnionValueCast) {
|
|
36
37
|
// ----------------------------------------------------------------------------------------------
|
|
@@ -68,7 +69,7 @@ var UnionValueCast;
|
|
|
68
69
|
return select;
|
|
69
70
|
}
|
|
70
71
|
function Create(union, references, value) {
|
|
71
|
-
return check_1.ValueCheck.Check(union, references, value) ? value : ValueCast.Cast(Select(union, references, value), references, value);
|
|
72
|
+
return check_1.ValueCheck.Check(union, references, value) ? clone_1.ValueClone.Clone(value) : ValueCast.Cast(Select(union, references, value), references, value);
|
|
72
73
|
}
|
|
73
74
|
UnionValueCast.Create = Create;
|
|
74
75
|
})(UnionValueCast || (UnionValueCast = {}));
|
|
@@ -166,8 +167,8 @@ var ValueCast;
|
|
|
166
167
|
}
|
|
167
168
|
function Array(schema, references, value) {
|
|
168
169
|
if (check_1.ValueCheck.Check(schema, references, value))
|
|
169
|
-
return value;
|
|
170
|
-
const created = IsArray(value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
170
|
+
return clone_1.ValueClone.Clone(value);
|
|
171
|
+
const created = IsArray(value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references);
|
|
171
172
|
const minimum = IsNumber(schema.minItems) && created.length < schema.minItems ? [...created, ...globalThis.Array.from({ length: schema.minItems - created.length }, () => null)] : created;
|
|
172
173
|
const maximum = IsNumber(schema.maxItems) && minimum.length > schema.maxItems ? minimum.slice(0, schema.maxItems) : minimum;
|
|
173
174
|
const casted = maximum.map((value) => Visit(schema.items, references, value));
|
|
@@ -219,7 +220,7 @@ var ValueCast;
|
|
|
219
220
|
}
|
|
220
221
|
function Object(schema, references, value) {
|
|
221
222
|
if (check_1.ValueCheck.Check(schema, references, value))
|
|
222
|
-
return value;
|
|
223
|
+
return clone_1.ValueClone.Clone(value);
|
|
223
224
|
if (value === null || typeof value !== 'object')
|
|
224
225
|
return create_1.ValueCreate.Create(schema, references);
|
|
225
226
|
const required = new Set(schema.required || []);
|
|
@@ -236,7 +237,7 @@ var ValueCast;
|
|
|
236
237
|
}
|
|
237
238
|
function Record(schema, references, value) {
|
|
238
239
|
if (check_1.ValueCheck.Check(schema, references, value))
|
|
239
|
-
return value;
|
|
240
|
+
return clone_1.ValueClone.Clone(value);
|
|
240
241
|
if (value === null || typeof value !== 'object' || globalThis.Array.isArray(value))
|
|
241
242
|
return create_1.ValueCreate.Create(schema, references);
|
|
242
243
|
const subschemaKey = globalThis.Object.keys(schema.patternProperties)[0];
|
|
@@ -268,7 +269,7 @@ var ValueCast;
|
|
|
268
269
|
}
|
|
269
270
|
function Tuple(schema, references, value) {
|
|
270
271
|
if (check_1.ValueCheck.Check(schema, references, value))
|
|
271
|
-
return value;
|
|
272
|
+
return clone_1.ValueClone.Clone(value);
|
|
272
273
|
if (!globalThis.Array.isArray(value))
|
|
273
274
|
return create_1.ValueCreate.Create(schema, references);
|
|
274
275
|
if (schema.items === undefined)
|
package/value/index.d.ts
CHANGED
package/value/index.js
CHANGED
|
@@ -44,4 +44,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
44
44
|
exports.ValueErrorType = void 0;
|
|
45
45
|
var index_1 = require("../errors/index");
|
|
46
46
|
Object.defineProperty(exports, "ValueErrorType", { enumerable: true, get: function () { return index_1.ValueErrorType; } });
|
|
47
|
+
__exportStar(require("./pointer"), exports);
|
|
47
48
|
__exportStar(require("./value"), exports);
|
package/value/pointer.d.ts
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
1
|
-
|
|
1
|
+
export declare class ValuePointerRootSetError extends Error {
|
|
2
|
+
readonly value: unknown;
|
|
3
|
+
readonly path: string;
|
|
4
|
+
readonly update: unknown;
|
|
5
|
+
constructor(value: unknown, path: string, update: unknown);
|
|
6
|
+
}
|
|
7
|
+
export declare class ValuePointerRootDeleteError extends Error {
|
|
8
|
+
readonly value: unknown;
|
|
9
|
+
readonly path: string;
|
|
10
|
+
constructor(value: unknown, path: string);
|
|
11
|
+
}
|
|
12
|
+
/** ValuePointer performs mutable operations on values using RFC6901 Json Pointers */
|
|
2
13
|
export declare namespace ValuePointer {
|
|
3
14
|
/** Sets the value at the given pointer. If the value at the pointer does not exist it is created. */
|
|
4
|
-
function Set(value:
|
|
5
|
-
/** Deletes a value at the given
|
|
6
|
-
function Delete(value: any,
|
|
7
|
-
/** True if a value exists at the given
|
|
8
|
-
function Has(value: any,
|
|
9
|
-
/** Gets the value at the given
|
|
10
|
-
function Get(value: any,
|
|
15
|
+
function Set(value: unknown, path: string, update: unknown): void;
|
|
16
|
+
/** Deletes a value at the given path. */
|
|
17
|
+
function Delete(value: any, path: string): any[] | undefined;
|
|
18
|
+
/** True if a value exists at the given path */
|
|
19
|
+
function Has(value: any, path: string): boolean;
|
|
20
|
+
/** Gets the value at the given path */
|
|
21
|
+
function Get(value: any, path: string): any;
|
|
11
22
|
}
|
package/value/pointer.js
CHANGED
|
@@ -27,82 +27,122 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.ValuePointer = void 0;
|
|
31
|
-
|
|
30
|
+
exports.ValuePointer = exports.ValuePointerRootDeleteError = exports.ValuePointerRootSetError = void 0;
|
|
31
|
+
class ValuePointerRootSetError extends Error {
|
|
32
|
+
constructor(value, path, update) {
|
|
33
|
+
super('ValuePointer: Cannot set root value');
|
|
34
|
+
this.value = value;
|
|
35
|
+
this.path = path;
|
|
36
|
+
this.update = update;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.ValuePointerRootSetError = ValuePointerRootSetError;
|
|
40
|
+
class ValuePointerRootDeleteError extends Error {
|
|
41
|
+
constructor(value, path) {
|
|
42
|
+
super('ValuePointer: Cannot delete root value');
|
|
43
|
+
this.value = value;
|
|
44
|
+
this.path = path;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.ValuePointerRootDeleteError = ValuePointerRootDeleteError;
|
|
48
|
+
/** ValuePointer performs mutable operations on values using RFC6901 Json Pointers */
|
|
32
49
|
var ValuePointer;
|
|
33
50
|
(function (ValuePointer) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
51
|
+
/** Formats the path into navigable components */
|
|
52
|
+
function* Format(path) {
|
|
53
|
+
function clear(chars) {
|
|
54
|
+
while (chars.length > 0)
|
|
55
|
+
chars.shift();
|
|
56
|
+
}
|
|
57
|
+
const chars = [];
|
|
58
|
+
for (let i = 0; i < path.length; i++) {
|
|
59
|
+
const char = path.charAt(i);
|
|
60
|
+
if (char === '/') {
|
|
61
|
+
if (i !== 0) {
|
|
62
|
+
yield chars.join('');
|
|
63
|
+
clear(chars);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else if (char === '~' && path.charAt(i + 1) === '0' && (path.charAt(i + 2) === '/' || i !== path.length - 1)) {
|
|
67
|
+
chars.push('~');
|
|
68
|
+
i += 1;
|
|
69
|
+
}
|
|
70
|
+
else if (char === '~' && path.charAt(i + 1) === '1' && (path.charAt(i + 2) === '/' || i !== path.length - 1)) {
|
|
71
|
+
chars.push('/');
|
|
72
|
+
i += 1;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
chars.push(char);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
yield chars.join('');
|
|
79
|
+
clear(chars);
|
|
40
80
|
}
|
|
41
81
|
/** Sets the value at the given pointer. If the value at the pointer does not exist it is created. */
|
|
42
|
-
function Set(value,
|
|
43
|
-
if (
|
|
44
|
-
throw
|
|
45
|
-
const
|
|
82
|
+
function Set(value, path, update) {
|
|
83
|
+
if (path === '')
|
|
84
|
+
throw new ValuePointerRootSetError(value, path, update);
|
|
85
|
+
const pointer = [...Format(path)];
|
|
46
86
|
let current = value;
|
|
47
|
-
while (
|
|
48
|
-
const next =
|
|
87
|
+
while (pointer.length > 1) {
|
|
88
|
+
const next = pointer.shift();
|
|
49
89
|
if (current[next] === undefined)
|
|
50
90
|
current[next] = {};
|
|
51
91
|
current = current[next];
|
|
52
92
|
}
|
|
53
|
-
current[
|
|
93
|
+
current[pointer.shift()] = update;
|
|
54
94
|
}
|
|
55
95
|
ValuePointer.Set = Set;
|
|
56
|
-
/** Deletes a value at the given
|
|
57
|
-
function Delete(value,
|
|
58
|
-
if (
|
|
59
|
-
throw
|
|
96
|
+
/** Deletes a value at the given path. */
|
|
97
|
+
function Delete(value, path) {
|
|
98
|
+
if (path === '')
|
|
99
|
+
throw new ValuePointerRootDeleteError(value, path);
|
|
60
100
|
let current = value;
|
|
61
|
-
const
|
|
62
|
-
while (
|
|
63
|
-
const next =
|
|
101
|
+
const pointer = [...Format(path)];
|
|
102
|
+
while (pointer.length > 1) {
|
|
103
|
+
const next = pointer.shift();
|
|
64
104
|
if (current[next] === undefined)
|
|
65
105
|
return;
|
|
66
106
|
current = current[next];
|
|
67
107
|
}
|
|
68
|
-
if (Array.isArray(current)) {
|
|
69
|
-
const index = parseInt(
|
|
108
|
+
if (globalThis.Array.isArray(current)) {
|
|
109
|
+
const index = parseInt(pointer.shift());
|
|
70
110
|
return current.splice(index, 1);
|
|
71
111
|
}
|
|
72
112
|
else {
|
|
73
|
-
const key =
|
|
113
|
+
const key = pointer.shift();
|
|
74
114
|
delete current[key];
|
|
75
115
|
}
|
|
76
116
|
}
|
|
77
117
|
ValuePointer.Delete = Delete;
|
|
78
|
-
/** True if a value exists at the given
|
|
79
|
-
function Has(value,
|
|
80
|
-
if (
|
|
118
|
+
/** True if a value exists at the given path */
|
|
119
|
+
function Has(value, path) {
|
|
120
|
+
if (path === '')
|
|
81
121
|
return true;
|
|
82
122
|
let current = value;
|
|
83
|
-
const
|
|
84
|
-
while (
|
|
85
|
-
const next =
|
|
123
|
+
const pointer = [...Format(path)];
|
|
124
|
+
while (pointer.length > 1) {
|
|
125
|
+
const next = pointer.shift();
|
|
86
126
|
if (current[next] === undefined)
|
|
87
127
|
return false;
|
|
88
128
|
current = current[next];
|
|
89
129
|
}
|
|
90
|
-
return current[
|
|
130
|
+
return current[pointer.shift()] !== undefined;
|
|
91
131
|
}
|
|
92
132
|
ValuePointer.Has = Has;
|
|
93
|
-
/** Gets the value at the given
|
|
94
|
-
function Get(value,
|
|
95
|
-
if (
|
|
133
|
+
/** Gets the value at the given path */
|
|
134
|
+
function Get(value, path) {
|
|
135
|
+
if (path === '')
|
|
96
136
|
return value;
|
|
97
137
|
let current = value;
|
|
98
|
-
const
|
|
99
|
-
while (
|
|
100
|
-
const next =
|
|
138
|
+
const pointer = [...Format(path)];
|
|
139
|
+
while (pointer.length > 1) {
|
|
140
|
+
const next = pointer.shift();
|
|
101
141
|
if (current[next] === undefined)
|
|
102
142
|
return undefined;
|
|
103
143
|
current = current[next];
|
|
104
144
|
}
|
|
105
|
-
return current[
|
|
145
|
+
return current[pointer.shift()];
|
|
106
146
|
}
|
|
107
147
|
ValuePointer.Get = Get;
|
|
108
148
|
})(ValuePointer = exports.ValuePointer || (exports.ValuePointer = {}));
|
package/value/value.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import * as Types from '../typebox';
|
|
|
2
2
|
import { ValueError } from '../errors/index';
|
|
3
3
|
import { Edit } from './delta';
|
|
4
4
|
export type { Edit } from './delta';
|
|
5
|
-
/**
|
|
5
|
+
/** Value performs immutable operations on values */
|
|
6
6
|
export declare namespace Value {
|
|
7
7
|
/** Casts a value into a given type. The return value will retain as much information of the original value as possible. Cast will convert string, number and boolean values if a reasonable conversion is possible. */
|
|
8
8
|
function Cast<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): Types.Static<T>;
|
package/value/value.js
CHANGED
|
@@ -35,7 +35,7 @@ const clone_1 = require("./clone");
|
|
|
35
35
|
const create_1 = require("./create");
|
|
36
36
|
const check_1 = require("./check");
|
|
37
37
|
const delta_1 = require("./delta");
|
|
38
|
-
/**
|
|
38
|
+
/** Value performs immutable operations on values */
|
|
39
39
|
var Value;
|
|
40
40
|
(function (Value) {
|
|
41
41
|
function Cast(...args) {
|