@sinclair/typebox 0.32.28 → 0.32.30

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.
@@ -1,3 +1,4 @@
1
1
  export * from './keyof-from-mapped-result';
2
+ export * from './keyof-property-entries';
2
3
  export * from './keyof-property-keys';
3
4
  export * from './keyof';
@@ -16,5 +16,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  };
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
18
  __exportStar(require("./keyof-from-mapped-result"), exports);
19
+ __exportStar(require("./keyof-property-entries"), exports);
19
20
  __exportStar(require("./keyof-property-keys"), exports);
20
21
  __exportStar(require("./keyof"), exports);
@@ -0,0 +1,7 @@
1
+ import { TSchema } from '../schema/index';
2
+ /**
3
+ * `[Utility]` Resolves an array of keys and schemas from the given schema. This method is faster
4
+ * than obtaining the keys and resolving each individually via indexing. This method was written
5
+ * accellerate Intersect and Union encoding.
6
+ */
7
+ export declare function KeyOfPropertyEntries(schema: TSchema): [key: string, schema: TSchema][];
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.KeyOfPropertyEntries = void 0;
5
+ const indexed_1 = require("../indexed/indexed");
6
+ const keyof_property_keys_1 = require("./keyof-property-keys");
7
+ /**
8
+ * `[Utility]` Resolves an array of keys and schemas from the given schema. This method is faster
9
+ * than obtaining the keys and resolving each individually via indexing. This method was written
10
+ * accellerate Intersect and Union encoding.
11
+ */
12
+ function KeyOfPropertyEntries(schema) {
13
+ const keys = (0, keyof_property_keys_1.KeyOfPropertyKeys)(schema);
14
+ const schemas = (0, indexed_1.IndexFromPropertyKeys)(schema, keys);
15
+ return keys.map((_, index) => [keys[index], schemas[index]]);
16
+ }
17
+ exports.KeyOfPropertyEntries = KeyOfPropertyEntries;
@@ -20,7 +20,7 @@ exports.IsIterator = IsIterator;
20
20
  // --------------------------------------------------------------------------
21
21
  /** Returns true if this value is not an instance of a class */
22
22
  function IsStandardObject(value) {
23
- return IsObject(value) && !IsArray(value) && IsFunction(value.constructor) && value.constructor.name === 'Object';
23
+ return IsObject(value) && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null);
24
24
  }
25
25
  exports.IsStandardObject = IsStandardObject;
26
26
  /** Returns true if this value is an instance of a class */
@@ -5,13 +5,12 @@ exports.TransformDecode = exports.TransformDecodeError = exports.TransformDecode
5
5
  const index_1 = require("../../type/symbols/index");
6
6
  const index_2 = require("../../type/error/index");
7
7
  const index_3 = require("../../type/keyof/index");
8
- const index_4 = require("../../type/indexed/index");
9
- const index_5 = require("../deref/index");
10
- const index_6 = require("../check/index");
8
+ const index_4 = require("../deref/index");
9
+ const index_5 = require("../check/index");
11
10
  // ------------------------------------------------------------------
12
11
  // ValueGuard
13
12
  // ------------------------------------------------------------------
14
- const index_7 = require("../guard/index");
13
+ const index_6 = require("../guard/index");
15
14
  // ------------------------------------------------------------------
16
15
  // TypeGuard
17
16
  // ------------------------------------------------------------------
@@ -55,19 +54,20 @@ function Default(schema, path, value) {
55
54
  }
56
55
  // prettier-ignore
57
56
  function FromArray(schema, references, path, value) {
58
- return ((0, index_7.IsArray)(value))
57
+ return ((0, index_6.IsArray)(value))
59
58
  ? Default(schema, path, value.map((value, index) => Visit(schema.items, references, `${path}/${index}`, value)))
60
59
  : Default(schema, path, value);
61
60
  }
62
61
  // prettier-ignore
63
62
  function FromIntersect(schema, references, path, value) {
64
- if (!(0, index_7.IsStandardObject)(value) || (0, index_7.IsValueType)(value))
63
+ if (!(0, index_6.IsStandardObject)(value) || (0, index_6.IsValueType)(value))
65
64
  return Default(schema, path, value);
66
- const knownKeys = (0, index_3.KeyOfPropertyKeys)(schema);
65
+ const knownEntries = (0, index_3.KeyOfPropertyEntries)(schema);
66
+ const knownKeys = knownEntries.map(entry => entry[0]);
67
67
  const knownProperties = { ...value };
68
- for (const key of knownKeys)
69
- if (key in knownProperties) {
70
- knownProperties[key] = Visit((0, index_4.Index)(schema, [key]), references, `${path}/${key}`, knownProperties[key]);
68
+ for (const [knownKey, knownSchema] of knownEntries)
69
+ if (knownKey in knownProperties) {
70
+ knownProperties[knownKey] = Visit(knownSchema, references, `${path}/${knownKey}`, knownProperties[knownKey]);
71
71
  }
72
72
  if (!(0, type_1.IsTransform)(schema.unevaluatedProperties)) {
73
73
  return Default(schema, path, knownProperties);
@@ -86,7 +86,7 @@ function FromNot(schema, references, path, value) {
86
86
  }
87
87
  // prettier-ignore
88
88
  function FromObject(schema, references, path, value) {
89
- if (!(0, index_7.IsStandardObject)(value))
89
+ if (!(0, index_6.IsStandardObject)(value))
90
90
  return Default(schema, path, value);
91
91
  const knownKeys = (0, index_3.KeyOfPropertyKeys)(schema);
92
92
  const knownProperties = { ...value };
@@ -108,7 +108,7 @@ function FromObject(schema, references, path, value) {
108
108
  }
109
109
  // prettier-ignore
110
110
  function FromRecord(schema, references, path, value) {
111
- if (!(0, index_7.IsStandardObject)(value))
111
+ if (!(0, index_6.IsStandardObject)(value))
112
112
  return Default(schema, path, value);
113
113
  const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0];
114
114
  const knownKeys = new RegExp(pattern);
@@ -131,24 +131,24 @@ function FromRecord(schema, references, path, value) {
131
131
  }
132
132
  // prettier-ignore
133
133
  function FromRef(schema, references, path, value) {
134
- const target = (0, index_5.Deref)(schema, references);
134
+ const target = (0, index_4.Deref)(schema, references);
135
135
  return Default(schema, path, Visit(target, references, path, value));
136
136
  }
137
137
  // prettier-ignore
138
138
  function FromThis(schema, references, path, value) {
139
- const target = (0, index_5.Deref)(schema, references);
139
+ const target = (0, index_4.Deref)(schema, references);
140
140
  return Default(schema, path, Visit(target, references, path, value));
141
141
  }
142
142
  // prettier-ignore
143
143
  function FromTuple(schema, references, path, value) {
144
- return ((0, index_7.IsArray)(value) && (0, index_7.IsArray)(schema.items))
144
+ return ((0, index_6.IsArray)(value) && (0, index_6.IsArray)(schema.items))
145
145
  ? Default(schema, path, schema.items.map((schema, index) => Visit(schema, references, `${path}/${index}`, value[index])))
146
146
  : Default(schema, path, value);
147
147
  }
148
148
  // prettier-ignore
149
149
  function FromUnion(schema, references, path, value) {
150
150
  for (const subschema of schema.anyOf) {
151
- if (!(0, index_6.Check)(subschema, references, value))
151
+ if (!(0, index_5.Check)(subschema, references, value))
152
152
  continue;
153
153
  // note: ensure interior is decoded first
154
154
  const decoded = Visit(subschema, references, path, value);
@@ -5,13 +5,12 @@ exports.TransformEncode = exports.TransformEncodeError = exports.TransformEncode
5
5
  const index_1 = require("../../type/symbols/index");
6
6
  const index_2 = require("../../type/error/index");
7
7
  const index_3 = require("../../type/keyof/index");
8
- const index_4 = require("../../type/indexed/index");
9
- const index_5 = require("../deref/index");
10
- const index_6 = require("../check/index");
8
+ const index_4 = require("../deref/index");
9
+ const index_5 = require("../check/index");
11
10
  // ------------------------------------------------------------------
12
11
  // ValueGuard
13
12
  // ------------------------------------------------------------------
14
- const index_7 = require("../guard/index");
13
+ const index_6 = require("../guard/index");
15
14
  // ------------------------------------------------------------------
16
15
  // TypeGuard
17
16
  // ------------------------------------------------------------------
@@ -55,20 +54,21 @@ function Default(schema, path, value) {
55
54
  // prettier-ignore
56
55
  function FromArray(schema, references, path, value) {
57
56
  const defaulted = Default(schema, path, value);
58
- return (0, index_7.IsArray)(defaulted)
57
+ return (0, index_6.IsArray)(defaulted)
59
58
  ? defaulted.map((value, index) => Visit(schema.items, references, `${path}/${index}`, value))
60
59
  : defaulted;
61
60
  }
62
61
  // prettier-ignore
63
62
  function FromIntersect(schema, references, path, value) {
64
63
  const defaulted = Default(schema, path, value);
65
- if (!(0, index_7.IsStandardObject)(value) || (0, index_7.IsValueType)(value))
64
+ if (!(0, index_6.IsStandardObject)(value) || (0, index_6.IsValueType)(value))
66
65
  return defaulted;
67
- const knownKeys = (0, index_3.KeyOfPropertyKeys)(schema);
66
+ const knownEntries = (0, index_3.KeyOfPropertyEntries)(schema);
67
+ const knownKeys = knownEntries.map(entry => entry[0]);
68
68
  const knownProperties = { ...defaulted };
69
- for (const key of knownKeys)
70
- if (key in knownProperties) {
71
- knownProperties[key] = Visit((0, index_4.Index)(schema, [key]), references, `${path}/${key}`, knownProperties[key]);
69
+ for (const [knownKey, knownSchema] of knownEntries)
70
+ if (knownKey in knownProperties) {
71
+ knownProperties[knownKey] = Visit(knownSchema, references, `${path}/${knownKey}`, knownProperties[knownKey]);
72
72
  }
73
73
  if (!(0, type_1.IsTransform)(schema.unevaluatedProperties)) {
74
74
  return Default(schema, path, knownProperties);
@@ -89,7 +89,7 @@ function FromNot(schema, references, path, value) {
89
89
  // prettier-ignore
90
90
  function FromObject(schema, references, path, value) {
91
91
  const defaulted = Default(schema, path, value);
92
- if (!(0, index_7.IsStandardObject)(defaulted))
92
+ if (!(0, index_6.IsStandardObject)(defaulted))
93
93
  return defaulted;
94
94
  const knownKeys = (0, index_3.KeyOfPropertyKeys)(schema);
95
95
  const knownProperties = { ...defaulted };
@@ -112,7 +112,7 @@ function FromObject(schema, references, path, value) {
112
112
  // prettier-ignore
113
113
  function FromRecord(schema, references, path, value) {
114
114
  const defaulted = Default(schema, path, value);
115
- if (!(0, index_7.IsStandardObject)(value))
115
+ if (!(0, index_6.IsStandardObject)(value))
116
116
  return defaulted;
117
117
  const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0];
118
118
  const knownKeys = new RegExp(pattern);
@@ -135,26 +135,26 @@ function FromRecord(schema, references, path, value) {
135
135
  }
136
136
  // prettier-ignore
137
137
  function FromRef(schema, references, path, value) {
138
- const target = (0, index_5.Deref)(schema, references);
138
+ const target = (0, index_4.Deref)(schema, references);
139
139
  const resolved = Visit(target, references, path, value);
140
140
  return Default(schema, path, resolved);
141
141
  }
142
142
  // prettier-ignore
143
143
  function FromThis(schema, references, path, value) {
144
- const target = (0, index_5.Deref)(schema, references);
144
+ const target = (0, index_4.Deref)(schema, references);
145
145
  const resolved = Visit(target, references, path, value);
146
146
  return Default(schema, path, resolved);
147
147
  }
148
148
  // prettier-ignore
149
149
  function FromTuple(schema, references, path, value) {
150
150
  const value1 = Default(schema, path, value);
151
- return (0, index_7.IsArray)(schema.items) ? schema.items.map((schema, index) => Visit(schema, references, `${path}/${index}`, value1[index])) : [];
151
+ return (0, index_6.IsArray)(schema.items) ? schema.items.map((schema, index) => Visit(schema, references, `${path}/${index}`, value1[index])) : [];
152
152
  }
153
153
  // prettier-ignore
154
154
  function FromUnion(schema, references, path, value) {
155
155
  // test value against union variants
156
156
  for (const subschema of schema.anyOf) {
157
- if (!(0, index_6.Check)(subschema, references, value))
157
+ if (!(0, index_5.Check)(subschema, references, value))
158
158
  continue;
159
159
  const value1 = Visit(subschema, references, path, value);
160
160
  return Default(schema, path, value1);
@@ -162,7 +162,7 @@ function FromUnion(schema, references, path, value) {
162
162
  // test transformed value against union variants
163
163
  for (const subschema of schema.anyOf) {
164
164
  const value1 = Visit(subschema, references, path, value);
165
- if (!(0, index_6.Check)(schema, references, value1))
165
+ if (!(0, index_5.Check)(schema, references, value1))
166
166
  continue;
167
167
  return Default(schema, path, value1);
168
168
  }
@@ -50,7 +50,7 @@ function Decode(...args) {
50
50
  const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]];
51
51
  if (!Check(schema, references, value))
52
52
  throw new index_1.TransformDecodeCheckError(schema, value, Errors(schema, references, value).First());
53
- return (0, index_1.TransformDecode)(schema, references, value);
53
+ return (0, index_1.HasTransform)(schema, references) ? (0, index_1.TransformDecode)(schema, references, value) : value;
54
54
  }
55
55
  exports.Decode = Decode;
56
56
  /** `[Mutable]` Generates missing properties on a value using default schema annotations if available. This function does not check the value and returns an unknown type. You should Check the result before use. Default is a mutable operation. To avoid mutation, Clone the value first. */
@@ -61,7 +61,7 @@ exports.Default = Default;
61
61
  /** Encodes a value or throws if error */
62
62
  function Encode(...args) {
63
63
  const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]];
64
- const encoded = (0, index_1.TransformEncode)(schema, references, value);
64
+ const encoded = (0, index_1.HasTransform)(schema, references) ? (0, index_1.TransformEncode)(schema, references, value) : value;
65
65
  if (!Check(schema, references, encoded))
66
66
  throw new index_1.TransformEncodeCheckError(schema, encoded, Errors(schema, references, encoded).First());
67
67
  return encoded;
@@ -1,3 +1,4 @@
1
1
  export * from './keyof-from-mapped-result.mjs';
2
+ export * from './keyof-property-entries.mjs';
2
3
  export * from './keyof-property-keys.mjs';
3
4
  export * from './keyof.mjs';
@@ -1,3 +1,4 @@
1
1
  export * from './keyof-from-mapped-result.mjs';
2
+ export * from './keyof-property-entries.mjs';
2
3
  export * from './keyof-property-keys.mjs';
3
4
  export * from './keyof.mjs';
@@ -0,0 +1,7 @@
1
+ import { TSchema } from '../schema/index.mjs';
2
+ /**
3
+ * `[Utility]` Resolves an array of keys and schemas from the given schema. This method is faster
4
+ * than obtaining the keys and resolving each individually via indexing. This method was written
5
+ * accellerate Intersect and Union encoding.
6
+ */
7
+ export declare function KeyOfPropertyEntries(schema: TSchema): [key: string, schema: TSchema][];
@@ -0,0 +1,12 @@
1
+ import { IndexFromPropertyKeys } from '../indexed/indexed.mjs';
2
+ import { KeyOfPropertyKeys } from './keyof-property-keys.mjs';
3
+ /**
4
+ * `[Utility]` Resolves an array of keys and schemas from the given schema. This method is faster
5
+ * than obtaining the keys and resolving each individually via indexing. This method was written
6
+ * accellerate Intersect and Union encoding.
7
+ */
8
+ export function KeyOfPropertyEntries(schema) {
9
+ const keys = KeyOfPropertyKeys(schema);
10
+ const schemas = IndexFromPropertyKeys(schema, keys);
11
+ return keys.map((_, index) => [keys[index], schemas[index]]);
12
+ }
@@ -14,7 +14,7 @@ export function IsIterator(value) {
14
14
  // --------------------------------------------------------------------------
15
15
  /** Returns true if this value is not an instance of a class */
16
16
  export function IsStandardObject(value) {
17
- return IsObject(value) && !IsArray(value) && IsFunction(value.constructor) && value.constructor.name === 'Object';
17
+ return IsObject(value) && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null);
18
18
  }
19
19
  /** Returns true if this value is an instance of a class */
20
20
  export function IsInstanceObject(value) {
@@ -1,7 +1,6 @@
1
1
  import { Kind, TransformKind } from '../../type/symbols/index.mjs';
2
2
  import { TypeBoxError } from '../../type/error/index.mjs';
3
- import { KeyOfPropertyKeys } from '../../type/keyof/index.mjs';
4
- import { Index } from '../../type/indexed/index.mjs';
3
+ import { KeyOfPropertyKeys, KeyOfPropertyEntries } from '../../type/keyof/index.mjs';
5
4
  import { Deref } from '../deref/index.mjs';
6
5
  import { Check } from '../check/index.mjs';
7
6
  // ------------------------------------------------------------------
@@ -64,11 +63,12 @@ function FromArray(schema, references, path, value) {
64
63
  function FromIntersect(schema, references, path, value) {
65
64
  if (!IsStandardObject(value) || IsValueType(value))
66
65
  return Default(schema, path, value);
67
- const knownKeys = KeyOfPropertyKeys(schema);
66
+ const knownEntries = KeyOfPropertyEntries(schema);
67
+ const knownKeys = knownEntries.map(entry => entry[0]);
68
68
  const knownProperties = { ...value };
69
- for (const key of knownKeys)
70
- if (key in knownProperties) {
71
- knownProperties[key] = Visit(Index(schema, [key]), references, `${path}/${key}`, knownProperties[key]);
69
+ for (const [knownKey, knownSchema] of knownEntries)
70
+ if (knownKey in knownProperties) {
71
+ knownProperties[knownKey] = Visit(knownSchema, references, `${path}/${knownKey}`, knownProperties[knownKey]);
72
72
  }
73
73
  if (!IsTransform(schema.unevaluatedProperties)) {
74
74
  return Default(schema, path, knownProperties);
@@ -1,7 +1,6 @@
1
1
  import { Kind, TransformKind } from '../../type/symbols/index.mjs';
2
2
  import { TypeBoxError } from '../../type/error/index.mjs';
3
- import { KeyOfPropertyKeys } from '../../type/keyof/index.mjs';
4
- import { Index } from '../../type/indexed/index.mjs';
3
+ import { KeyOfPropertyKeys, KeyOfPropertyEntries } from '../../type/keyof/index.mjs';
5
4
  import { Deref } from '../deref/index.mjs';
6
5
  import { Check } from '../check/index.mjs';
7
6
  // ------------------------------------------------------------------
@@ -65,11 +64,12 @@ function FromIntersect(schema, references, path, value) {
65
64
  const defaulted = Default(schema, path, value);
66
65
  if (!IsStandardObject(value) || IsValueType(value))
67
66
  return defaulted;
68
- const knownKeys = KeyOfPropertyKeys(schema);
67
+ const knownEntries = KeyOfPropertyEntries(schema);
68
+ const knownKeys = knownEntries.map(entry => entry[0]);
69
69
  const knownProperties = { ...defaulted };
70
- for (const key of knownKeys)
71
- if (key in knownProperties) {
72
- knownProperties[key] = Visit(Index(schema, [key]), references, `${path}/${key}`, knownProperties[key]);
70
+ for (const [knownKey, knownSchema] of knownEntries)
71
+ if (knownKey in knownProperties) {
72
+ knownProperties[knownKey] = Visit(knownSchema, references, `${path}/${knownKey}`, knownProperties[knownKey]);
73
73
  }
74
74
  if (!IsTransform(schema.unevaluatedProperties)) {
75
75
  return Default(schema, path, knownProperties);
@@ -1,4 +1,4 @@
1
- import { TransformDecode, TransformEncode, TransformDecodeCheckError, TransformEncodeCheckError } from '../transform/index.mjs';
1
+ import { HasTransform, TransformDecode, TransformEncode, TransformDecodeCheckError, TransformEncodeCheckError } from '../transform/index.mjs';
2
2
  import { Mutate as MutateValue } from '../mutate/index.mjs';
3
3
  import { Hash as HashValue } from '../hash/index.mjs';
4
4
  import { Equal as EqualValue } from '../equal/index.mjs';
@@ -40,7 +40,7 @@ export function Decode(...args) {
40
40
  const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]];
41
41
  if (!Check(schema, references, value))
42
42
  throw new TransformDecodeCheckError(schema, value, Errors(schema, references, value).First());
43
- return TransformDecode(schema, references, value);
43
+ return HasTransform(schema, references) ? TransformDecode(schema, references, value) : value;
44
44
  }
45
45
  /** `[Mutable]` Generates missing properties on a value using default schema annotations if available. This function does not check the value and returns an unknown type. You should Check the result before use. Default is a mutable operation. To avoid mutation, Clone the value first. */
46
46
  export function Default(...args) {
@@ -49,7 +49,7 @@ export function Default(...args) {
49
49
  /** Encodes a value or throws if error */
50
50
  export function Encode(...args) {
51
51
  const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]];
52
- const encoded = TransformEncode(schema, references, value);
52
+ const encoded = HasTransform(schema, references) ? TransformEncode(schema, references, value) : value;
53
53
  if (!Check(schema, references, encoded))
54
54
  throw new TransformEncodeCheckError(schema, encoded, Errors(schema, references, encoded).First());
55
55
  return encoded;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.32.28",
3
+ "version": "0.32.30",
4
4
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",