@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.24.40",
3
+ "version": "0.24.41",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
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 TKeyOf<T extends TObject> = {
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): TUnion<TKeyOf<T>>;
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
@@ -1,2 +1,3 @@
1
1
  export { ValueError, ValueErrorType } from '../errors/index';
2
+ export * from './pointer';
2
3
  export * from './value';
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);
@@ -1,11 +1,22 @@
1
- /** RFC6901 JsonPointer */
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: any, pointer: string, update: any): void;
5
- /** Deletes a value at the given pointer. */
6
- function Delete(value: any, pointer: string): any[] | undefined;
7
- /** True if a value exists at the given pointer */
8
- function Has(value: any, pointer: string): boolean;
9
- /** Gets the value at the given pointer */
10
- function Get(value: any, pointer: string): 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
- /** RFC6901 JsonPointer */
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 Format(pointer) {
35
- if (pointer === '/')
36
- return [''];
37
- const split = pointer.split('/');
38
- const filter = split.filter((part) => part.length > 0);
39
- return filter.map((part) => part.replace(/~0/g, `~`).replace(/~1/g, `/`));
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, pointer, update) {
43
- if (pointer === '')
44
- throw Error('Cannot set root value');
45
- const path = Format(pointer);
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 (path.length > 1) {
48
- const next = path.shift();
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[path.shift()] = update;
93
+ current[pointer.shift()] = update;
54
94
  }
55
95
  ValuePointer.Set = Set;
56
- /** Deletes a value at the given pointer. */
57
- function Delete(value, pointer) {
58
- if (pointer === '')
59
- throw Error('Cannot delete root value');
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 path = Format(pointer);
62
- while (path.length > 1) {
63
- const next = path.shift();
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(path.shift());
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 = path.shift();
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 pointer */
79
- function Has(value, pointer) {
80
- if (pointer === '')
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 path = Format(pointer);
84
- while (path.length > 1) {
85
- const next = path.shift();
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[path.shift()] !== undefined;
130
+ return current[pointer.shift()] !== undefined;
91
131
  }
92
132
  ValuePointer.Has = Has;
93
- /** Gets the value at the given pointer */
94
- function Get(value, pointer) {
95
- if (pointer === '')
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 path = Format(pointer);
99
- while (path.length > 1) {
100
- const next = path.shift();
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[path.shift()];
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
- /** The Value namespace provides type operations on values */
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
- /** The Value namespace provides type operations on values */
38
+ /** Value performs immutable operations on values */
39
39
  var Value;
40
40
  (function (Value) {
41
41
  function Cast(...args) {