@sinclair/typebox 0.24.40 → 0.24.42
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/readme.md +16 -0
- package/typebox.d.ts +8 -6
- 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 -6
- package/value/pointer.js +79 -45
- package/value/value.d.ts +1 -1
- package/value/value.js +1 -1
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -76,6 +76,7 @@ License MIT
|
|
|
76
76
|
- [Diff](#values-diff)
|
|
77
77
|
- [Patch](#values-patch)
|
|
78
78
|
- [Errors](#values-errors)
|
|
79
|
+
- [Pointer](#values-pointer)
|
|
79
80
|
- [TypeCheck](#typecheck)
|
|
80
81
|
- [Ajv](#typecheck-ajv)
|
|
81
82
|
- [Compiler](#typecheck-compiler)
|
|
@@ -870,6 +871,21 @@ const R = [...Value.Errors(T, { x: '42' })] // const R = [{
|
|
|
870
871
|
// }]
|
|
871
872
|
```
|
|
872
873
|
|
|
874
|
+
<a name='values-pointer'></a>
|
|
875
|
+
|
|
876
|
+
### Pointer
|
|
877
|
+
|
|
878
|
+
Use ValuePointer to perform mutable updates on existing values using [RFC6901](https://www.rfc-editor.org/rfc/rfc6901) Json Pointers.
|
|
879
|
+
|
|
880
|
+
```typescript
|
|
881
|
+
import { ValuePointer } from '@sinclair/typebox/value'
|
|
882
|
+
|
|
883
|
+
const A = { x: 0, y: 0, z: 0 }
|
|
884
|
+
|
|
885
|
+
ValuePointer.Set(A, '/x', 1) // const A = { x: 1, y: 0, z: 0 }
|
|
886
|
+
ValuePointer.Set(A, '/y', 1) // const A = { x: 1, y: 1, z: 0 }
|
|
887
|
+
ValuePointer.Set(A, '/z', 1) // const A = { x: 1, y: 1, z: 1 }
|
|
888
|
+
```
|
|
873
889
|
<a name='typecheck'></a>
|
|
874
890
|
|
|
875
891
|
## TypeCheck
|
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';
|
|
@@ -229,15 +231,15 @@ export interface TRequired<T extends TObject | TRef<TObject>> extends TObject {
|
|
|
229
231
|
static: Required<Static<T, this['params']>>;
|
|
230
232
|
}
|
|
231
233
|
export declare type StringFormatOption = 'date-time' | 'time' | 'date' | 'email' | 'idn-email' | 'hostname' | 'idn-hostname' | 'ipv4' | 'ipv6' | 'uri' | 'uri-reference' | 'iri' | 'uuid' | 'iri-reference' | 'uri-template' | 'json-pointer' | 'relative-json-pointer' | 'regex';
|
|
232
|
-
export interface StringOptions<
|
|
234
|
+
export interface StringOptions<Format extends string> extends SchemaOptions {
|
|
233
235
|
minLength?: number;
|
|
234
236
|
maxLength?: number;
|
|
235
237
|
pattern?: string;
|
|
236
|
-
format?:
|
|
238
|
+
format?: Format;
|
|
237
239
|
contentEncoding?: '7bit' | '8bit' | 'binary' | 'quoted-printable' | 'base64';
|
|
238
240
|
contentMediaType?: string;
|
|
239
241
|
}
|
|
240
|
-
export interface TString extends TSchema, StringOptions<
|
|
242
|
+
export interface TString<Format extends string = string> extends TSchema, StringOptions<Format> {
|
|
241
243
|
[Kind]: 'String';
|
|
242
244
|
static: string;
|
|
243
245
|
type: 'string';
|
|
@@ -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 */
|
|
@@ -371,7 +373,7 @@ export declare class TypeBuilder {
|
|
|
371
373
|
/** Removes Kind and Modifier symbol property keys from this schema */
|
|
372
374
|
Strict<T extends TSchema>(schema: T): T;
|
|
373
375
|
/** Creates a string type */
|
|
374
|
-
String<
|
|
376
|
+
String<Format extends string>(options?: StringOptions<StringFormatOption | Format>): TString<Format>;
|
|
375
377
|
/** Creates a tuple type */
|
|
376
378
|
Tuple<T extends TSchema[]>(items: [...T], options?: SchemaOptions): TTuple<T>;
|
|
377
379
|
/** Creates a undefined 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,10 +1,23 @@
|
|
|
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
|
-
/**
|
|
4
|
-
function
|
|
5
|
-
/**
|
|
6
|
-
function
|
|
7
|
-
/**
|
|
14
|
+
/** Formats the given pointer into navigable key components */
|
|
15
|
+
function Format(pointer: string): IterableIterator<string>;
|
|
16
|
+
/** Sets the value at the given pointer. If the value at the pointer does not exist it is created */
|
|
17
|
+
function Set(value: any, pointer: string, update: unknown): void;
|
|
18
|
+
/** Deletes a value at the given pointer */
|
|
19
|
+
function Delete(value: any, pointer: string): void;
|
|
20
|
+
/** Returns true if a value exists at the given pointer */
|
|
8
21
|
function Has(value: any, pointer: string): boolean;
|
|
9
22
|
/** Gets the value at the given pointer */
|
|
10
23
|
function Get(value: any, pointer: string): any;
|
package/value/pointer.js
CHANGED
|
@@ -27,67 +27,103 @@ 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
|
-
function
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
51
|
+
function Escape(component) {
|
|
52
|
+
return component.indexOf('~') === -1 ? component : component.replace(/~1/g, '/').replace(/~0/g, '~');
|
|
53
|
+
}
|
|
54
|
+
/** Formats the given pointer into navigable key components */
|
|
55
|
+
function* Format(pointer) {
|
|
56
|
+
if (pointer === '')
|
|
57
|
+
return;
|
|
58
|
+
let [start, end] = [0, 0];
|
|
59
|
+
for (let i = 0; i < pointer.length; i++) {
|
|
60
|
+
const char = pointer.charAt(i);
|
|
61
|
+
if (char === '/') {
|
|
62
|
+
if (i === 0) {
|
|
63
|
+
start = i + 1;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
end = i;
|
|
67
|
+
yield Escape(pointer.slice(start, end));
|
|
68
|
+
start = i + 1;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
end = i;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
yield Escape(pointer.slice(start));
|
|
40
76
|
}
|
|
41
|
-
|
|
77
|
+
ValuePointer.Format = Format;
|
|
78
|
+
/** Sets the value at the given pointer. If the value at the pointer does not exist it is created */
|
|
42
79
|
function Set(value, pointer, update) {
|
|
43
80
|
if (pointer === '')
|
|
44
|
-
throw
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
81
|
+
throw new ValuePointerRootSetError(value, pointer, update);
|
|
82
|
+
let [owner, next, key] = [null, value, ''];
|
|
83
|
+
for (const component of Format(pointer)) {
|
|
84
|
+
if (next[component] === undefined)
|
|
85
|
+
next[component] = {};
|
|
86
|
+
owner = next;
|
|
87
|
+
next = next[component];
|
|
88
|
+
key = component;
|
|
52
89
|
}
|
|
53
|
-
|
|
90
|
+
owner[key] = update;
|
|
54
91
|
}
|
|
55
92
|
ValuePointer.Set = Set;
|
|
56
|
-
/** Deletes a value at the given pointer
|
|
93
|
+
/** Deletes a value at the given pointer */
|
|
57
94
|
function Delete(value, pointer) {
|
|
58
95
|
if (pointer === '')
|
|
59
|
-
throw
|
|
60
|
-
let
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
const next = path.shift();
|
|
64
|
-
if (current[next] === undefined)
|
|
96
|
+
throw new ValuePointerRootDeleteError(value, pointer);
|
|
97
|
+
let [owner, next, key] = [null, value, ''];
|
|
98
|
+
for (const component of Format(pointer)) {
|
|
99
|
+
if (next[component] === undefined || next[component] === null)
|
|
65
100
|
return;
|
|
66
|
-
|
|
101
|
+
owner = next;
|
|
102
|
+
next = next[component];
|
|
103
|
+
key = component;
|
|
67
104
|
}
|
|
68
|
-
if (Array.isArray(
|
|
69
|
-
const index = parseInt(
|
|
70
|
-
|
|
105
|
+
if (globalThis.Array.isArray(owner)) {
|
|
106
|
+
const index = parseInt(key);
|
|
107
|
+
owner.splice(index, 1);
|
|
71
108
|
}
|
|
72
109
|
else {
|
|
73
|
-
|
|
74
|
-
delete current[key];
|
|
110
|
+
delete owner[key];
|
|
75
111
|
}
|
|
76
112
|
}
|
|
77
113
|
ValuePointer.Delete = Delete;
|
|
78
|
-
/**
|
|
114
|
+
/** Returns true if a value exists at the given pointer */
|
|
79
115
|
function Has(value, pointer) {
|
|
80
116
|
if (pointer === '')
|
|
81
117
|
return true;
|
|
82
|
-
let
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
const next = path.shift();
|
|
86
|
-
if (current[next] === undefined)
|
|
118
|
+
let [owner, next, key] = [null, value, ''];
|
|
119
|
+
for (const component of Format(pointer)) {
|
|
120
|
+
if (next[component] === undefined)
|
|
87
121
|
return false;
|
|
88
|
-
|
|
122
|
+
owner = next;
|
|
123
|
+
next = next[component];
|
|
124
|
+
key = component;
|
|
89
125
|
}
|
|
90
|
-
return
|
|
126
|
+
return globalThis.Object.getOwnPropertyNames(owner).includes(key);
|
|
91
127
|
}
|
|
92
128
|
ValuePointer.Has = Has;
|
|
93
129
|
/** Gets the value at the given pointer */
|
|
@@ -95,14 +131,12 @@ var ValuePointer;
|
|
|
95
131
|
if (pointer === '')
|
|
96
132
|
return value;
|
|
97
133
|
let current = value;
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
const next = path.shift();
|
|
101
|
-
if (current[next] === undefined)
|
|
134
|
+
for (const component of Format(pointer)) {
|
|
135
|
+
if (current[component] === undefined)
|
|
102
136
|
return undefined;
|
|
103
|
-
current = current[
|
|
137
|
+
current = current[component];
|
|
104
138
|
}
|
|
105
|
-
return current
|
|
139
|
+
return current;
|
|
106
140
|
}
|
|
107
141
|
ValuePointer.Get = Get;
|
|
108
142
|
})(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) {
|