@sinclair/typebox 0.33.1 → 0.33.3

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.
@@ -69,6 +69,8 @@ function FromProperties(K, T) {
69
69
  }
70
70
  // prettier-ignore
71
71
  function FromSchemaType(K, T) {
72
+ // required to retain user defined options for mapped type
73
+ const options = { ...T };
72
74
  return (
73
75
  // unevaluated modifier types
74
76
  (0, kind_1.IsOptional)(T) ? (0, index_12.Optional)(FromSchemaType(K, (0, index_2.Discard)(T, [index_1.OptionalKind]))) :
@@ -77,16 +79,16 @@ function FromSchemaType(K, T) {
77
79
  (0, kind_1.IsMappedResult)(T) ? FromMappedResult(K, T.properties) :
78
80
  (0, kind_1.IsMappedKey)(T) ? FromMappedKey(K, T.keys) :
79
81
  // unevaluated types
80
- (0, kind_1.IsConstructor)(T) ? (0, index_5.Constructor)(FromRest(K, T.parameters), FromSchemaType(K, T.returns)) :
81
- (0, kind_1.IsFunction)(T) ? (0, index_6.Function)(FromRest(K, T.parameters), FromSchemaType(K, T.returns)) :
82
- (0, kind_1.IsAsyncIterator)(T) ? (0, index_4.AsyncIterator)(FromSchemaType(K, T.items)) :
83
- (0, kind_1.IsIterator)(T) ? (0, index_9.Iterator)(FromSchemaType(K, T.items)) :
84
- (0, kind_1.IsIntersect)(T) ? (0, index_8.Intersect)(FromRest(K, T.allOf)) :
85
- (0, kind_1.IsUnion)(T) ? (0, index_16.Union)(FromRest(K, T.anyOf)) :
86
- (0, kind_1.IsTuple)(T) ? (0, index_15.Tuple)(FromRest(K, T.items ?? [])) :
87
- (0, kind_1.IsObject)(T) ? (0, index_11.Object)(FromProperties(K, T.properties)) :
88
- (0, kind_1.IsArray)(T) ? (0, index_3.Array)(FromSchemaType(K, T.items)) :
89
- (0, kind_1.IsPromise)(T) ? (0, index_13.Promise)(FromSchemaType(K, T.item)) :
82
+ (0, kind_1.IsConstructor)(T) ? (0, index_5.Constructor)(FromRest(K, T.parameters), FromSchemaType(K, T.returns), options) :
83
+ (0, kind_1.IsFunction)(T) ? (0, index_6.Function)(FromRest(K, T.parameters), FromSchemaType(K, T.returns), options) :
84
+ (0, kind_1.IsAsyncIterator)(T) ? (0, index_4.AsyncIterator)(FromSchemaType(K, T.items), options) :
85
+ (0, kind_1.IsIterator)(T) ? (0, index_9.Iterator)(FromSchemaType(K, T.items), options) :
86
+ (0, kind_1.IsIntersect)(T) ? (0, index_8.Intersect)(FromRest(K, T.allOf), options) :
87
+ (0, kind_1.IsUnion)(T) ? (0, index_16.Union)(FromRest(K, T.anyOf), options) :
88
+ (0, kind_1.IsTuple)(T) ? (0, index_15.Tuple)(FromRest(K, T.items ?? []), options) :
89
+ (0, kind_1.IsObject)(T) ? (0, index_11.Object)(FromProperties(K, T.properties), options) :
90
+ (0, kind_1.IsArray)(T) ? (0, index_3.Array)(FromSchemaType(K, T.items), options) :
91
+ (0, kind_1.IsPromise)(T) ? (0, index_13.Promise)(FromSchemaType(K, T.item), options) :
90
92
  T);
91
93
  }
92
94
  // prettier-ignore
@@ -42,7 +42,7 @@ function FromProperties(T, K) {
42
42
  function OmitResolve(T, K) {
43
43
  return ((0, kind_1.IsIntersect)(T) ? (0, index_1.Intersect)(FromIntersect(T.allOf, K)) :
44
44
  (0, kind_1.IsUnion)(T) ? (0, index_2.Union)(FromUnion(T.anyOf, K)) :
45
- (0, kind_1.IsObject)(T) ? (0, index_3.Object)(FromProperties(T.properties, K)) :
45
+ (0, kind_1.IsObject)(T) ? (0, index_3.Object)(FromProperties(T.properties, K), (0, index_5.Discard)(T, [index_6.TransformKind, '$id', 'required'])) :
46
46
  (0, index_3.Object)({}));
47
47
  }
48
48
  function Omit(T, K, options) {
@@ -53,7 +53,5 @@ function Omit(T, K, options) {
53
53
  return (0, omit_from_mapped_result_1.OmitFromMappedResult)(T, K, options);
54
54
  // non-mapped
55
55
  const I = (0, kind_1.IsSchema)(K) ? (0, index_4.IndexPropertyKeys)(K) : K;
56
- const D = (0, index_5.Discard)(T, [index_6.TransformKind, '$id', 'required']);
57
- const R = OmitResolve(T, I);
58
- return (0, type_1.CreateType)({ ...D, ...R }, options);
56
+ return (0, type_1.CreateType)(OmitResolve(T, I), options);
59
57
  }
@@ -37,7 +37,7 @@ function FromProperties(T, K) {
37
37
  function PickResolve(T, K) {
38
38
  return ((0, kind_1.IsIntersect)(T) ? (0, index_1.Intersect)(FromIntersect(T.allOf, K)) :
39
39
  (0, kind_1.IsUnion)(T) ? (0, index_2.Union)(FromUnion(T.anyOf, K)) :
40
- (0, kind_1.IsObject)(T) ? (0, index_3.Object)(FromProperties(T.properties, K)) :
40
+ (0, kind_1.IsObject)(T) ? (0, index_3.Object)(FromProperties(T.properties, K), (0, index_5.Discard)(T, [index_6.TransformKind, '$id', 'required'])) :
41
41
  (0, index_3.Object)({}));
42
42
  }
43
43
  function Pick(T, K, options) {
@@ -48,7 +48,5 @@ function Pick(T, K, options) {
48
48
  return (0, pick_from_mapped_result_1.PickFromMappedResult)(T, K, options);
49
49
  // non-mapped
50
50
  const I = (0, kind_1.IsSchema)(K) ? (0, index_4.IndexPropertyKeys)(K) : K;
51
- const D = (0, index_5.Discard)(T, [index_6.TransformKind, '$id', 'required']);
52
- const R = PickResolve(T, I);
53
- return (0, type_1.CreateType)({ ...D, ...R }, options);
51
+ return (0, type_1.CreateType)(PickResolve(T, I), options);
54
52
  }
@@ -24,13 +24,9 @@ export declare const Delete: TObject<{
24
24
  }>;
25
25
  export type Edit = Static<typeof Edit>;
26
26
  export declare const Edit: TUnion<[typeof Insert, typeof Update, typeof Delete]>;
27
- export declare class ValueDeltaError extends TypeBoxError {
27
+ export declare class ValueDiffError extends TypeBoxError {
28
28
  readonly value: unknown;
29
29
  constructor(value: unknown, message: string);
30
30
  }
31
- export declare class ValueDeltaSymbolError extends ValueDeltaError {
32
- readonly value: unknown;
33
- constructor(value: unknown);
34
- }
35
31
  export declare function Diff(current: unknown, next: unknown): Edit[];
36
32
  export declare function Patch<T = any>(current: unknown, edits: Edit[]): T;
@@ -1,12 +1,13 @@
1
1
  "use strict";
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.ValueDeltaSymbolError = exports.ValueDeltaError = exports.Edit = exports.Delete = exports.Update = exports.Insert = void 0;
4
+ exports.ValueDiffError = exports.Edit = exports.Delete = exports.Update = exports.Insert = void 0;
5
5
  exports.Diff = Diff;
6
6
  exports.Patch = Patch;
7
7
  const index_1 = require("../guard/index");
8
8
  const index_2 = require("../pointer/index");
9
9
  const index_3 = require("../clone/index");
10
+ const equal_1 = require("../equal/equal");
10
11
  const index_4 = require("../../type/error/index");
11
12
  const index_5 = require("../../type/literal/index");
12
13
  const index_6 = require("../../type/object/index");
@@ -31,20 +32,13 @@ exports.Edit = (0, index_9.Union)([exports.Insert, exports.Update, exports.Delet
31
32
  // ------------------------------------------------------------------
32
33
  // Errors
33
34
  // ------------------------------------------------------------------
34
- class ValueDeltaError extends index_4.TypeBoxError {
35
+ class ValueDiffError extends index_4.TypeBoxError {
35
36
  constructor(value, message) {
36
37
  super(message);
37
38
  this.value = value;
38
39
  }
39
40
  }
40
- exports.ValueDeltaError = ValueDeltaError;
41
- class ValueDeltaSymbolError extends ValueDeltaError {
42
- constructor(value) {
43
- super(value, 'Cannot diff objects with symbol keys');
44
- this.value = value;
45
- }
46
- }
47
- exports.ValueDeltaSymbolError = ValueDeltaSymbolError;
41
+ exports.ValueDiffError = ValueDiffError;
48
42
  // ------------------------------------------------------------------
49
43
  // Command Factory
50
44
  // ------------------------------------------------------------------
@@ -58,37 +52,47 @@ function CreateDelete(path) {
58
52
  return { type: 'delete', path };
59
53
  }
60
54
  // ------------------------------------------------------------------
55
+ // AssertDiffable
56
+ // ------------------------------------------------------------------
57
+ function AssertDiffable(value) {
58
+ if (globalThis.Object.getOwnPropertySymbols(value).length > 0)
59
+ throw new ValueDiffError(value, 'Cannot diff objects with symbols');
60
+ }
61
+ // ------------------------------------------------------------------
61
62
  // Diffing Generators
62
63
  // ------------------------------------------------------------------
63
64
  function* ObjectType(path, current, next) {
65
+ AssertDiffable(current);
66
+ AssertDiffable(next);
64
67
  if (!(0, index_1.IsStandardObject)(next))
65
68
  return yield CreateUpdate(path, next);
66
- const currentKeys = [...globalThis.Object.keys(current), ...globalThis.Object.getOwnPropertySymbols(current)];
67
- const nextKeys = [...globalThis.Object.keys(next), ...globalThis.Object.getOwnPropertySymbols(next)];
68
- for (const key of currentKeys) {
69
- if ((0, index_1.IsSymbol)(key))
70
- throw new ValueDeltaSymbolError(key);
71
- if ((0, index_1.IsUndefined)(next[key]) && nextKeys.includes(key))
72
- yield CreateUpdate(`${path}/${globalThis.String(key)}`, undefined);
73
- }
69
+ const currentKeys = globalThis.Object.getOwnPropertyNames(current);
70
+ const nextKeys = globalThis.Object.getOwnPropertyNames(next);
71
+ // ----------------------------------------------------------------
72
+ // inserts
73
+ // ----------------------------------------------------------------
74
74
  for (const key of nextKeys) {
75
- if ((0, index_1.IsUndefined)(current[key]) || (0, index_1.IsUndefined)(next[key]))
75
+ if ((0, index_1.HasPropertyKey)(current, key))
76
76
  continue;
77
- if ((0, index_1.IsSymbol)(key))
78
- throw new ValueDeltaSymbolError(key);
79
- yield* Visit(`${path}/${globalThis.String(key)}`, current[key], next[key]);
77
+ yield CreateInsert(`${path}/${key}`, next[key]);
80
78
  }
81
- for (const key of nextKeys) {
82
- if ((0, index_1.IsSymbol)(key))
83
- throw new ValueDeltaSymbolError(key);
84
- if ((0, index_1.IsUndefined)(current[key]))
85
- yield CreateInsert(`${path}/${globalThis.String(key)}`, next[key]);
79
+ // ----------------------------------------------------------------
80
+ // updates
81
+ // ----------------------------------------------------------------
82
+ for (const key of currentKeys) {
83
+ if (!(0, index_1.HasPropertyKey)(next, key))
84
+ continue;
85
+ if ((0, equal_1.Equal)(current, next))
86
+ continue;
87
+ yield* Visit(`${path}/${key}`, current[key], next[key]);
86
88
  }
87
- for (const key of currentKeys.reverse()) {
88
- if ((0, index_1.IsSymbol)(key))
89
- throw new ValueDeltaSymbolError(key);
90
- if ((0, index_1.IsUndefined)(next[key]) && !nextKeys.includes(key))
91
- yield CreateDelete(`${path}/${globalThis.String(key)}`);
89
+ // ----------------------------------------------------------------
90
+ // deletes
91
+ // ----------------------------------------------------------------
92
+ for (const key of currentKeys) {
93
+ if ((0, index_1.HasPropertyKey)(next, key))
94
+ continue;
95
+ yield CreateDelete(`${path}/${key}`);
92
96
  }
93
97
  }
94
98
  function* ArrayType(path, current, next) {
@@ -129,7 +133,7 @@ function* Visit(path, current, next) {
129
133
  return yield* TypedArrayType(path, current, next);
130
134
  if ((0, index_1.IsValueType)(current))
131
135
  return yield* ValueType(path, current, next);
132
- throw new ValueDeltaError(current, 'Unable to create diff edits for unknown value');
136
+ throw new ValueDiffError(current, 'Unable to diff value');
133
137
  }
134
138
  // ------------------------------------------------------------------
135
139
  // Diff
@@ -64,6 +64,8 @@ function FromProperties(K, T) {
64
64
  }
65
65
  // prettier-ignore
66
66
  function FromSchemaType(K, T) {
67
+ // required to retain user defined options for mapped type
68
+ const options = { ...T };
67
69
  return (
68
70
  // unevaluated modifier types
69
71
  IsOptional(T) ? Optional(FromSchemaType(K, Discard(T, [OptionalKind]))) :
@@ -72,16 +74,16 @@ function FromSchemaType(K, T) {
72
74
  IsMappedResult(T) ? FromMappedResult(K, T.properties) :
73
75
  IsMappedKey(T) ? FromMappedKey(K, T.keys) :
74
76
  // unevaluated types
75
- IsConstructor(T) ? Constructor(FromRest(K, T.parameters), FromSchemaType(K, T.returns)) :
76
- IsFunction(T) ? FunctionType(FromRest(K, T.parameters), FromSchemaType(K, T.returns)) :
77
- IsAsyncIterator(T) ? AsyncIterator(FromSchemaType(K, T.items)) :
78
- IsIterator(T) ? Iterator(FromSchemaType(K, T.items)) :
79
- IsIntersect(T) ? Intersect(FromRest(K, T.allOf)) :
80
- IsUnion(T) ? Union(FromRest(K, T.anyOf)) :
81
- IsTuple(T) ? Tuple(FromRest(K, T.items ?? [])) :
82
- IsObject(T) ? Object(FromProperties(K, T.properties)) :
83
- IsArray(T) ? Array(FromSchemaType(K, T.items)) :
84
- IsPromise(T) ? Promise(FromSchemaType(K, T.item)) :
77
+ IsConstructor(T) ? Constructor(FromRest(K, T.parameters), FromSchemaType(K, T.returns), options) :
78
+ IsFunction(T) ? FunctionType(FromRest(K, T.parameters), FromSchemaType(K, T.returns), options) :
79
+ IsAsyncIterator(T) ? AsyncIterator(FromSchemaType(K, T.items), options) :
80
+ IsIterator(T) ? Iterator(FromSchemaType(K, T.items), options) :
81
+ IsIntersect(T) ? Intersect(FromRest(K, T.allOf), options) :
82
+ IsUnion(T) ? Union(FromRest(K, T.anyOf), options) :
83
+ IsTuple(T) ? Tuple(FromRest(K, T.items ?? []), options) :
84
+ IsObject(T) ? Object(FromProperties(K, T.properties), options) :
85
+ IsArray(T) ? Array(FromSchemaType(K, T.items), options) :
86
+ IsPromise(T) ? Promise(FromSchemaType(K, T.item), options) :
85
87
  T);
86
88
  }
87
89
  // prettier-ignore
@@ -38,7 +38,7 @@ function FromProperties(T, K) {
38
38
  function OmitResolve(T, K) {
39
39
  return (IsIntersect(T) ? Intersect(FromIntersect(T.allOf, K)) :
40
40
  IsUnion(T) ? Union(FromUnion(T.anyOf, K)) :
41
- IsObject(T) ? Object(FromProperties(T.properties, K)) :
41
+ IsObject(T) ? Object(FromProperties(T.properties, K), Discard(T, [TransformKind, '$id', 'required'])) :
42
42
  Object({}));
43
43
  }
44
44
  export function Omit(T, K, options) {
@@ -49,7 +49,5 @@ export function Omit(T, K, options) {
49
49
  return OmitFromMappedResult(T, K, options);
50
50
  // non-mapped
51
51
  const I = IsSchema(K) ? IndexPropertyKeys(K) : K;
52
- const D = Discard(T, [TransformKind, '$id', 'required']);
53
- const R = OmitResolve(T, I);
54
- return CreateType({ ...D, ...R }, options);
52
+ return CreateType(OmitResolve(T, I), options);
55
53
  }
@@ -33,7 +33,7 @@ function FromProperties(T, K) {
33
33
  function PickResolve(T, K) {
34
34
  return (IsIntersect(T) ? Intersect(FromIntersect(T.allOf, K)) :
35
35
  IsUnion(T) ? Union(FromUnion(T.anyOf, K)) :
36
- IsObject(T) ? Object(FromProperties(T.properties, K)) :
36
+ IsObject(T) ? Object(FromProperties(T.properties, K), Discard(T, [TransformKind, '$id', 'required'])) :
37
37
  Object({}));
38
38
  }
39
39
  export function Pick(T, K, options) {
@@ -44,7 +44,5 @@ export function Pick(T, K, options) {
44
44
  return PickFromMappedResult(T, K, options);
45
45
  // non-mapped
46
46
  const I = IsSchema(K) ? IndexPropertyKeys(K) : K;
47
- const D = Discard(T, [TransformKind, '$id', 'required']);
48
- const R = PickResolve(T, I);
49
- return CreateType({ ...D, ...R }, options);
47
+ return CreateType(PickResolve(T, I), options);
50
48
  }
@@ -24,13 +24,9 @@ export declare const Delete: TObject<{
24
24
  }>;
25
25
  export type Edit = Static<typeof Edit>;
26
26
  export declare const Edit: TUnion<[typeof Insert, typeof Update, typeof Delete]>;
27
- export declare class ValueDeltaError extends TypeBoxError {
27
+ export declare class ValueDiffError extends TypeBoxError {
28
28
  readonly value: unknown;
29
29
  constructor(value: unknown, message: string);
30
30
  }
31
- export declare class ValueDeltaSymbolError extends ValueDeltaError {
32
- readonly value: unknown;
33
- constructor(value: unknown);
34
- }
35
31
  export declare function Diff(current: unknown, next: unknown): Edit[];
36
32
  export declare function Patch<T = any>(current: unknown, edits: Edit[]): T;
@@ -1,6 +1,7 @@
1
- import { IsStandardObject, IsArray, IsTypedArray, IsValueType, IsSymbol, IsUndefined } from '../guard/index.mjs';
1
+ import { HasPropertyKey, IsStandardObject, IsArray, IsTypedArray, IsValueType } from '../guard/index.mjs';
2
2
  import { ValuePointer } from '../pointer/index.mjs';
3
3
  import { Clone } from '../clone/index.mjs';
4
+ import { Equal } from '../equal/equal.mjs';
4
5
  import { TypeBoxError } from '../../type/error/index.mjs';
5
6
  import { Literal } from '../../type/literal/index.mjs';
6
7
  import { Object } from '../../type/object/index.mjs';
@@ -25,18 +26,12 @@ export const Edit = Union([Insert, Update, Delete]);
25
26
  // ------------------------------------------------------------------
26
27
  // Errors
27
28
  // ------------------------------------------------------------------
28
- export class ValueDeltaError extends TypeBoxError {
29
+ export class ValueDiffError extends TypeBoxError {
29
30
  constructor(value, message) {
30
31
  super(message);
31
32
  this.value = value;
32
33
  }
33
34
  }
34
- export class ValueDeltaSymbolError extends ValueDeltaError {
35
- constructor(value) {
36
- super(value, 'Cannot diff objects with symbol keys');
37
- this.value = value;
38
- }
39
- }
40
35
  // ------------------------------------------------------------------
41
36
  // Command Factory
42
37
  // ------------------------------------------------------------------
@@ -50,37 +45,47 @@ function CreateDelete(path) {
50
45
  return { type: 'delete', path };
51
46
  }
52
47
  // ------------------------------------------------------------------
48
+ // AssertDiffable
49
+ // ------------------------------------------------------------------
50
+ function AssertDiffable(value) {
51
+ if (globalThis.Object.getOwnPropertySymbols(value).length > 0)
52
+ throw new ValueDiffError(value, 'Cannot diff objects with symbols');
53
+ }
54
+ // ------------------------------------------------------------------
53
55
  // Diffing Generators
54
56
  // ------------------------------------------------------------------
55
57
  function* ObjectType(path, current, next) {
58
+ AssertDiffable(current);
59
+ AssertDiffable(next);
56
60
  if (!IsStandardObject(next))
57
61
  return yield CreateUpdate(path, next);
58
- const currentKeys = [...globalThis.Object.keys(current), ...globalThis.Object.getOwnPropertySymbols(current)];
59
- const nextKeys = [...globalThis.Object.keys(next), ...globalThis.Object.getOwnPropertySymbols(next)];
60
- for (const key of currentKeys) {
61
- if (IsSymbol(key))
62
- throw new ValueDeltaSymbolError(key);
63
- if (IsUndefined(next[key]) && nextKeys.includes(key))
64
- yield CreateUpdate(`${path}/${globalThis.String(key)}`, undefined);
65
- }
62
+ const currentKeys = globalThis.Object.getOwnPropertyNames(current);
63
+ const nextKeys = globalThis.Object.getOwnPropertyNames(next);
64
+ // ----------------------------------------------------------------
65
+ // inserts
66
+ // ----------------------------------------------------------------
66
67
  for (const key of nextKeys) {
67
- if (IsUndefined(current[key]) || IsUndefined(next[key]))
68
+ if (HasPropertyKey(current, key))
68
69
  continue;
69
- if (IsSymbol(key))
70
- throw new ValueDeltaSymbolError(key);
71
- yield* Visit(`${path}/${globalThis.String(key)}`, current[key], next[key]);
70
+ yield CreateInsert(`${path}/${key}`, next[key]);
72
71
  }
73
- for (const key of nextKeys) {
74
- if (IsSymbol(key))
75
- throw new ValueDeltaSymbolError(key);
76
- if (IsUndefined(current[key]))
77
- yield CreateInsert(`${path}/${globalThis.String(key)}`, next[key]);
72
+ // ----------------------------------------------------------------
73
+ // updates
74
+ // ----------------------------------------------------------------
75
+ for (const key of currentKeys) {
76
+ if (!HasPropertyKey(next, key))
77
+ continue;
78
+ if (Equal(current, next))
79
+ continue;
80
+ yield* Visit(`${path}/${key}`, current[key], next[key]);
78
81
  }
79
- for (const key of currentKeys.reverse()) {
80
- if (IsSymbol(key))
81
- throw new ValueDeltaSymbolError(key);
82
- if (IsUndefined(next[key]) && !nextKeys.includes(key))
83
- yield CreateDelete(`${path}/${globalThis.String(key)}`);
82
+ // ----------------------------------------------------------------
83
+ // deletes
84
+ // ----------------------------------------------------------------
85
+ for (const key of currentKeys) {
86
+ if (HasPropertyKey(next, key))
87
+ continue;
88
+ yield CreateDelete(`${path}/${key}`);
84
89
  }
85
90
  }
86
91
  function* ArrayType(path, current, next) {
@@ -121,7 +126,7 @@ function* Visit(path, current, next) {
121
126
  return yield* TypedArrayType(path, current, next);
122
127
  if (IsValueType(current))
123
128
  return yield* ValueType(path, current, next);
124
- throw new ValueDeltaError(current, 'Unable to create diff edits for unknown value');
129
+ throw new ValueDiffError(current, 'Unable to diff value');
125
130
  }
126
131
  // ------------------------------------------------------------------
127
132
  // Diff
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.33.1",
3
+ "version": "0.33.3",
4
4
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",