@sinclair/typebox 0.26.8 → 0.27.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/value/check.js CHANGED
@@ -315,13 +315,6 @@ var ValueCheck;
315
315
  const target = references[index];
316
316
  return Visit(target, references, value);
317
317
  }
318
- function Self(schema, references, value) {
319
- const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
320
- if (index === -1)
321
- throw new ValueCheckDereferenceError(schema);
322
- const target = references[index];
323
- return Visit(target, references, value);
324
- }
325
318
  function String(schema, references, value) {
326
319
  if (!IsString(value)) {
327
320
  return false;
@@ -353,6 +346,19 @@ var ValueCheck;
353
346
  }
354
347
  return true;
355
348
  }
349
+ function TemplateLiteral(schema, references, value) {
350
+ if (!IsString(value)) {
351
+ return false;
352
+ }
353
+ return new RegExp(schema.pattern).test(value);
354
+ }
355
+ function This(schema, references, value) {
356
+ const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
357
+ if (index === -1)
358
+ throw new ValueCheckDereferenceError(schema);
359
+ const target = references[index];
360
+ return Visit(target, references, value);
361
+ }
356
362
  function Tuple(schema, references, value) {
357
363
  if (!globalThis.Array.isArray(value)) {
358
364
  return false;
@@ -442,12 +448,14 @@ var ValueCheck;
442
448
  return Record(schema_, references_, value);
443
449
  case 'Ref':
444
450
  return Ref(schema_, references_, value);
445
- case 'Self':
446
- return Self(schema_, references_, value);
447
451
  case 'String':
448
452
  return String(schema_, references_, value);
449
453
  case 'Symbol':
450
454
  return Symbol(schema_, references_, value);
455
+ case 'TemplateLiteral':
456
+ return TemplateLiteral(schema_, references_, value);
457
+ case 'This':
458
+ return This(schema_, references_, value);
451
459
  case 'Tuple':
452
460
  return Tuple(schema_, references_, value);
453
461
  case 'Undefined':
@@ -4,8 +4,8 @@ export declare class ValueConvertUnknownTypeError extends Error {
4
4
  constructor(schema: Types.TSchema);
5
5
  }
6
6
  export declare class ValueConvertDereferenceError extends Error {
7
- readonly schema: Types.TRef | Types.TSelf;
8
- constructor(schema: Types.TRef | Types.TSelf);
7
+ readonly schema: Types.TRef | Types.TThis;
8
+ constructor(schema: Types.TRef | Types.TThis);
9
9
  }
10
10
  export declare namespace ValueConvert {
11
11
  function Visit(schema: Types.TSchema, references: Types.TSchema[], value: any): unknown;
package/value/convert.js CHANGED
@@ -252,19 +252,22 @@ var ValueConvert;
252
252
  const target = references[index];
253
253
  return Visit(target, references, value);
254
254
  }
255
- function Self(schema, references, value) {
256
- const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
257
- if (index === -1)
258
- throw new ValueConvertDereferenceError(schema);
259
- const target = references[index];
260
- return Visit(target, references, value);
261
- }
262
255
  function String(schema, references, value) {
263
256
  return TryConvertString(value);
264
257
  }
265
258
  function Symbol(schema, references, value) {
266
259
  return value;
267
260
  }
261
+ function TemplateLiteral(schema, references, value) {
262
+ return value;
263
+ }
264
+ function This(schema, references, value) {
265
+ const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
266
+ if (index === -1)
267
+ throw new ValueConvertDereferenceError(schema);
268
+ const target = references[index];
269
+ return Visit(target, references, value);
270
+ }
268
271
  function Tuple(schema, references, value) {
269
272
  if (IsArray(value) && schema.items !== undefined) {
270
273
  return value.map((value, index) => {
@@ -335,12 +338,14 @@ var ValueConvert;
335
338
  return Record(schema_, references_, value);
336
339
  case 'Ref':
337
340
  return Ref(schema_, references_, value);
338
- case 'Self':
339
- return Self(schema_, references_, value);
340
341
  case 'String':
341
342
  return String(schema_, references_, value);
342
343
  case 'Symbol':
343
344
  return Symbol(schema_, references_, value);
345
+ case 'TemplateLiteral':
346
+ return TemplateLiteral(schema_, references_, value);
347
+ case 'This':
348
+ return This(schema_, references_, value);
344
349
  case 'Tuple':
345
350
  return Tuple(schema_, references_, value);
346
351
  case 'Undefined':
package/value/create.d.ts CHANGED
@@ -11,9 +11,13 @@ export declare class ValueCreateIntersectTypeError extends Error {
11
11
  readonly schema: Types.TSchema;
12
12
  constructor(schema: Types.TSchema);
13
13
  }
14
+ export declare class ValueCreateTempateLiteralTypeError extends Error {
15
+ readonly schema: Types.TSchema;
16
+ constructor(schema: Types.TSchema);
17
+ }
14
18
  export declare class ValueCreateDereferenceError extends Error {
15
- readonly schema: Types.TRef | Types.TSelf;
16
- constructor(schema: Types.TRef | Types.TSelf);
19
+ readonly schema: Types.TRef | Types.TThis;
20
+ constructor(schema: Types.TRef | Types.TThis);
17
21
  }
18
22
  export declare namespace ValueCreate {
19
23
  /** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
package/value/create.js CHANGED
@@ -27,7 +27,7 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.ValueCreate = exports.ValueCreateDereferenceError = exports.ValueCreateIntersectTypeError = exports.ValueCreateNeverTypeError = exports.ValueCreateUnknownTypeError = void 0;
30
+ exports.ValueCreate = exports.ValueCreateDereferenceError = exports.ValueCreateTempateLiteralTypeError = exports.ValueCreateIntersectTypeError = exports.ValueCreateNeverTypeError = exports.ValueCreateUnknownTypeError = void 0;
31
31
  const Types = require("../typebox");
32
32
  const check_1 = require("./check");
33
33
  // --------------------------------------------------------------------------
@@ -54,6 +54,13 @@ class ValueCreateIntersectTypeError extends Error {
54
54
  }
55
55
  }
56
56
  exports.ValueCreateIntersectTypeError = ValueCreateIntersectTypeError;
57
+ class ValueCreateTempateLiteralTypeError extends Error {
58
+ constructor(schema) {
59
+ super('ValueCreate: Can only create template literal values from patterns that produce finite sequences. Consider using a default value.');
60
+ this.schema = schema;
61
+ }
62
+ }
63
+ exports.ValueCreateTempateLiteralTypeError = ValueCreateTempateLiteralTypeError;
57
64
  class ValueCreateDereferenceError extends Error {
58
65
  constructor(schema) {
59
66
  super(`ValueCreate: Unable to dereference schema with $id '${schema.$ref}'`);
@@ -241,7 +248,7 @@ var ValueCreate;
241
248
  if ('default' in schema) {
242
249
  return schema.default;
243
250
  }
244
- else if (!(keyPattern === '^.*$' || keyPattern === '^(0|[1-9][0-9]*)$')) {
251
+ else if (!(keyPattern === Types.PatternStringExact || keyPattern === Types.PatternNumberExact)) {
245
252
  const propertyKeys = keyPattern.slice(1, keyPattern.length - 1).split('|');
246
253
  return propertyKeys.reduce((acc, key) => {
247
254
  return { ...acc, [key]: Create(valueSchema, references) };
@@ -263,18 +270,6 @@ var ValueCreate;
263
270
  return Visit(target, references);
264
271
  }
265
272
  }
266
- function Self(schema, references) {
267
- if ('default' in schema) {
268
- return schema.default;
269
- }
270
- else {
271
- const index = references.findIndex((foreign) => foreign.$id === schema.$id);
272
- if (index === -1)
273
- throw new ValueCreateDereferenceError(schema);
274
- const target = references[index];
275
- return Visit(target, references);
276
- }
277
- }
278
273
  function String(schema, references) {
279
274
  if (schema.pattern !== undefined) {
280
275
  if (!('default' in schema)) {
@@ -317,6 +312,28 @@ var ValueCreate;
317
312
  return globalThis.Symbol();
318
313
  }
319
314
  }
315
+ function TemplateLiteral(schema, references) {
316
+ if ('default' in schema) {
317
+ return schema.default;
318
+ }
319
+ const expression = Types.TemplateLiteralParser.ParseExact(schema.pattern);
320
+ if (!Types.TemplateLiteralFinite.Check(expression))
321
+ throw new ValueCreateTempateLiteralTypeError(schema);
322
+ const sequence = Types.TemplateLiteralGenerator.Generate(expression);
323
+ return sequence.next().value;
324
+ }
325
+ function This(schema, references) {
326
+ if ('default' in schema) {
327
+ return schema.default;
328
+ }
329
+ else {
330
+ const index = references.findIndex((foreign) => foreign.$id === schema.$id);
331
+ if (index === -1)
332
+ throw new ValueCreateDereferenceError(schema);
333
+ const target = references[index];
334
+ return Visit(target, references);
335
+ }
336
+ }
320
337
  function Tuple(schema, references) {
321
338
  if ('default' in schema) {
322
339
  return schema.default;
@@ -423,12 +440,14 @@ var ValueCreate;
423
440
  return Record(schema_, references_);
424
441
  case 'Ref':
425
442
  return Ref(schema_, references_);
426
- case 'Self':
427
- return Self(schema_, references_);
428
443
  case 'String':
429
444
  return String(schema_, references_);
430
445
  case 'Symbol':
431
446
  return Symbol(schema_, references_);
447
+ case 'TemplateLiteral':
448
+ return TemplateLiteral(schema_, references_);
449
+ case 'This':
450
+ return This(schema_, references_);
432
451
  case 'Tuple':
433
452
  return Tuple(schema_, references_);
434
453
  case 'Undefined':
package/value/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { ValueError, ValueErrorIterator, ValueErrorType } from '../errors/index';
2
2
  export { ValueHash } from './hash';
3
3
  export { Edit, Insert, Update, Delete } from './delta';
4
+ export { Mutable } from './mutate';
4
5
  export * from './pointer';
5
6
  export * from './value';
@@ -0,0 +1,13 @@
1
+ export declare class ValueMutateTypeMismatchError extends Error {
2
+ constructor();
3
+ }
4
+ export declare class ValueMutateInvalidRootMutationError extends Error {
5
+ constructor();
6
+ }
7
+ export type Mutable = {
8
+ [key: string]: unknown;
9
+ } | unknown[];
10
+ export declare namespace ValueMutate {
11
+ /** Performs a deep mutable value assignment while retaining internal references. */
12
+ function Mutate(current: Mutable, next: Mutable): void;
13
+ }
@@ -0,0 +1,121 @@
1
+ "use strict";
2
+ /*--------------------------------------------------------------------------
3
+
4
+ @sinclair/typebox/value
5
+
6
+ The MIT License (MIT)
7
+
8
+ Copyright (c) 2017-2023 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.ValueMutate = exports.ValueMutateInvalidRootMutationError = exports.ValueMutateTypeMismatchError = void 0;
31
+ const is_1 = require("./is");
32
+ const pointer_1 = require("./pointer");
33
+ const clone_1 = require("./clone");
34
+ class ValueMutateTypeMismatchError extends Error {
35
+ constructor() {
36
+ super('ValueMutate: Cannot assign due type mismatch of assignable values');
37
+ }
38
+ }
39
+ exports.ValueMutateTypeMismatchError = ValueMutateTypeMismatchError;
40
+ class ValueMutateInvalidRootMutationError extends Error {
41
+ constructor() {
42
+ super('ValueMutate: Only object and array types can be mutated at the root level');
43
+ }
44
+ }
45
+ exports.ValueMutateInvalidRootMutationError = ValueMutateInvalidRootMutationError;
46
+ var ValueMutate;
47
+ (function (ValueMutate) {
48
+ function Object(root, path, current, next) {
49
+ if (!is_1.Is.Object(current)) {
50
+ pointer_1.ValuePointer.Set(root, path, clone_1.ValueClone.Clone(next));
51
+ }
52
+ else {
53
+ const currentKeys = globalThis.Object.keys(current);
54
+ const nextKeys = globalThis.Object.keys(next);
55
+ for (const currentKey of currentKeys) {
56
+ if (!nextKeys.includes(currentKey)) {
57
+ delete current[currentKey];
58
+ }
59
+ }
60
+ for (const nextKey of nextKeys) {
61
+ if (!currentKeys.includes(nextKey)) {
62
+ current[nextKey] = null;
63
+ }
64
+ }
65
+ for (const nextKey of nextKeys) {
66
+ Visit(root, `${path}/${nextKey}`, current[nextKey], next[nextKey]);
67
+ }
68
+ }
69
+ }
70
+ function Array(root, path, current, next) {
71
+ if (!is_1.Is.Array(current)) {
72
+ pointer_1.ValuePointer.Set(root, path, clone_1.ValueClone.Clone(next));
73
+ }
74
+ else {
75
+ for (let index = 0; index < next.length; index++) {
76
+ Visit(root, `${path}/${index}`, current[index], next[index]);
77
+ }
78
+ current.splice(next.length);
79
+ }
80
+ }
81
+ function TypedArray(root, path, current, next) {
82
+ if (is_1.Is.TypedArray(current) && current.length === next.length) {
83
+ for (let i = 0; i < current.length; i++) {
84
+ current[i] = next[i];
85
+ }
86
+ }
87
+ else {
88
+ pointer_1.ValuePointer.Set(root, path, clone_1.ValueClone.Clone(next));
89
+ }
90
+ }
91
+ function Value(root, path, current, next) {
92
+ if (current === next)
93
+ return;
94
+ pointer_1.ValuePointer.Set(root, path, next);
95
+ }
96
+ function Visit(root, path, current, next) {
97
+ if (is_1.Is.Array(next)) {
98
+ return Array(root, path, current, next);
99
+ }
100
+ else if (is_1.Is.TypedArray(next)) {
101
+ return TypedArray(root, path, current, next);
102
+ }
103
+ else if (is_1.Is.Object(next)) {
104
+ return Object(root, path, current, next);
105
+ }
106
+ else if (is_1.Is.Value(next)) {
107
+ return Value(root, path, current, next);
108
+ }
109
+ }
110
+ /** Performs a deep mutable value assignment while retaining internal references. */
111
+ function Mutate(current, next) {
112
+ if (is_1.Is.TypedArray(current) || is_1.Is.Value(current) || is_1.Is.TypedArray(next) || is_1.Is.Value(next)) {
113
+ throw new ValueMutateInvalidRootMutationError();
114
+ }
115
+ if ((is_1.Is.Object(current) && is_1.Is.Array(next)) || (is_1.Is.Array(current) && is_1.Is.Object(next))) {
116
+ throw new ValueMutateTypeMismatchError();
117
+ }
118
+ Visit(current, '', current, next);
119
+ }
120
+ ValueMutate.Mutate = Mutate;
121
+ })(ValueMutate = exports.ValueMutate || (exports.ValueMutate = {}));
package/value/value.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as Types from '../typebox';
2
2
  import { ValueErrorIterator } from '../errors/index';
3
+ import { Mutable } from './mutate';
3
4
  import { Edit } from './delta';
4
5
  /** Provides functions to perform structural updates to JavaScript values */
5
6
  export declare namespace Value {
@@ -33,4 +34,6 @@ export declare namespace Value {
33
34
  function Hash(value: unknown): bigint;
34
35
  /** Returns a new value with edits applied to the given value */
35
36
  function Patch<T = any>(current: unknown, edits: Edit[]): T;
37
+ /** Performs a deep mutable value assignment while retaining internal references. */
38
+ function Mutate(current: Mutable, next: Mutable): void;
36
39
  }
package/value/value.js CHANGED
@@ -29,6 +29,7 @@ THE SOFTWARE.
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
30
  exports.Value = void 0;
31
31
  const index_1 = require("../errors/index");
32
+ const mutate_1 = require("./mutate");
32
33
  const hash_1 = require("./hash");
33
34
  const equal_1 = require("./equal");
34
35
  const cast_1 = require("./cast");
@@ -90,4 +91,9 @@ var Value;
90
91
  return delta_1.ValueDelta.Patch(current, edits);
91
92
  }
92
93
  Value.Patch = Patch;
94
+ /** Performs a deep mutable value assignment while retaining internal references. */
95
+ function Mutate(current, next) {
96
+ mutate_1.ValueMutate.Mutate(current, next);
97
+ }
98
+ Value.Mutate = Mutate;
93
99
  })(Value = exports.Value || (exports.Value = {}));