@sinclair/typebox 0.33.11 → 0.33.13

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.
@@ -66,7 +66,8 @@ function TryConvertBoolean(value) {
66
66
  return IsValueTrue(value) ? true : IsValueFalse(value) ? false : value;
67
67
  }
68
68
  function TryConvertBigInt(value) {
69
- return IsStringNumeric(value) ? BigInt(parseInt(value)) : (0, index_5.IsNumber)(value) ? BigInt(value | 0) : IsValueFalse(value) ? BigInt(0) : IsValueTrue(value) ? BigInt(1) : value;
69
+ const truncateInteger = (value) => value.split('.')[0];
70
+ return IsStringNumeric(value) ? BigInt(truncateInteger(value)) : (0, index_5.IsNumber)(value) ? BigInt(value | 0) : IsValueFalse(value) ? BigInt(0) : IsValueTrue(value) ? BigInt(1) : value;
70
71
  }
71
72
  function TryConvertString(value) {
72
73
  return IsValueToString(value) ? value.toString() : (0, index_5.IsSymbol)(value) && value.description !== undefined ? value.description.toString() : value;
@@ -198,12 +199,8 @@ function FromUnion(schema, references, value) {
198
199
  }
199
200
  return value;
200
201
  }
201
- function AddReference(references, schema) {
202
- references.push(schema);
203
- return references;
204
- }
205
202
  function Visit(schema, references, value) {
206
- const references_ = (0, index_5.IsString)(schema.$id) ? AddReference(references, schema) : references;
203
+ const references_ = (0, index_3.Pushref)(schema, references);
207
204
  const schema_ = schema;
208
205
  switch (schema[index_4.Kind]) {
209
206
  case 'Array':
@@ -387,12 +387,8 @@ function FromKind(schema, references) {
387
387
  throw new Error('User defined types must specify a default value');
388
388
  }
389
389
  }
390
- function AddReference(references, schema) {
391
- references.push(schema);
392
- return references;
393
- }
394
390
  function Visit(schema, references) {
395
- const references_ = (0, index_1.IsString)(schema.$id) ? AddReference(references, schema) : references;
391
+ const references_ = (0, index_4.Pushref)(schema, references);
396
392
  const schema_ = schema;
397
393
  switch (schema_[index_8.Kind]) {
398
394
  case 'Any':
@@ -119,18 +119,14 @@ function FromUnion(schema, references, value) {
119
119
  const defaulted = ValueOrDefault(schema, value);
120
120
  for (const inner of schema.anyOf) {
121
121
  const result = Visit(inner, references, (0, index_2.Clone)(defaulted));
122
- if ((0, index_1.Check)(inner, result)) {
122
+ if ((0, index_1.Check)(inner, references, result)) {
123
123
  return result;
124
124
  }
125
125
  }
126
126
  return defaulted;
127
127
  }
128
- function AddReference(references, schema) {
129
- references.push(schema);
130
- return references;
131
- }
132
128
  function Visit(schema, references, value) {
133
- const references_ = (0, index_5.IsString)(schema.$id) ? AddReference(references, schema) : references;
129
+ const references_ = (0, index_3.Pushref)(schema, references);
134
130
  const schema_ = schema;
135
131
  switch (schema_[index_4.Kind]) {
136
132
  case 'Array':
@@ -6,5 +6,7 @@ export declare class TypeDereferenceError extends TypeBoxError {
6
6
  readonly schema: TRef | TThis;
7
7
  constructor(schema: TRef | TThis);
8
8
  }
9
- /** Dereferences a schema from the references array or throws if not found */
9
+ /** `[Internal]` Pushes a schema onto references if the schema has an $id and does not exist on references */
10
+ export declare function Pushref(schema: TSchema, references: TSchema[]): TSchema[];
11
+ /** `[Internal]` Dereferences a schema from the references array or throws if not found */
10
12
  export declare function Deref(schema: TSchema, references: TSchema[]): TSchema;
@@ -2,12 +2,14 @@
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.TypeDereferenceError = void 0;
5
+ exports.Pushref = Pushref;
5
6
  exports.Deref = Deref;
6
7
  const index_1 = require("../../type/error/index");
7
8
  const index_2 = require("../../type/symbols/index");
9
+ const guard_1 = require("../guard/guard");
8
10
  class TypeDereferenceError extends index_1.TypeBoxError {
9
11
  constructor(schema) {
10
- super(`Unable to dereference schema with $id '${schema.$id}'`);
12
+ super(`Unable to dereference schema with $id '${schema.$ref}'`);
11
13
  this.schema = schema;
12
14
  }
13
15
  }
@@ -18,7 +20,14 @@ function Resolve(schema, references) {
18
20
  throw new TypeDereferenceError(schema);
19
21
  return Deref(target, references);
20
22
  }
21
- /** Dereferences a schema from the references array or throws if not found */
23
+ /** `[Internal]` Pushes a schema onto references if the schema has an $id and does not exist on references */
24
+ function Pushref(schema, references) {
25
+ if (!(0, guard_1.IsString)(schema.$id) || references.some((target) => target.$id === schema.$id))
26
+ return references;
27
+ references.push(schema);
28
+ return references;
29
+ }
30
+ /** `[Internal]` Dereferences a schema from the references array or throws if not found */
22
31
  function Deref(schema, references) {
23
32
  // prettier-ignore
24
33
  return (schema[index_2.Kind] === 'This' || schema[index_2.Kind] === 'Ref')
@@ -166,13 +166,9 @@ function FromUnion(schema, references, path, value) {
166
166
  }
167
167
  return Default(schema, path, value);
168
168
  }
169
- function AddReference(references, schema) {
170
- references.push(schema);
171
- return references;
172
- }
173
169
  // prettier-ignore
174
170
  function Visit(schema, references, path, value) {
175
- const references_ = typeof schema.$id === 'string' ? AddReference(references, schema) : references;
171
+ const references_ = (0, index_4.Pushref)(schema, references);
176
172
  const schema_ = schema;
177
173
  switch (schema[index_1.Kind]) {
178
174
  case 'Array':
@@ -178,13 +178,9 @@ function FromUnion(schema, references, path, value) {
178
178
  }
179
179
  return Default(schema, path, value);
180
180
  }
181
- function AddReference(references, schema) {
182
- references.push(schema);
183
- return references;
184
- }
185
181
  // prettier-ignore
186
182
  function Visit(schema, references, path, value) {
187
- const references_ = typeof schema.$id === 'string' ? AddReference(references, schema) : references;
183
+ const references_ = (0, index_4.Pushref)(schema, references);
188
184
  const schema_ = schema;
189
185
  switch (schema[index_1.Kind]) {
190
186
  case 'Array':
@@ -76,13 +76,9 @@ function FromTuple(schema, references) {
76
76
  function FromUnion(schema, references) {
77
77
  return (0, type_1.IsTransform)(schema) || schema.anyOf.some((schema) => Visit(schema, references));
78
78
  }
79
- function AddReference(references, schema) {
80
- references.push(schema);
81
- return references;
82
- }
83
79
  // prettier-ignore
84
80
  function Visit(schema, references) {
85
- const references_ = (0, index_3.IsString)(schema.$id) ? AddReference(references, schema) : references;
81
+ const references_ = (0, index_1.Pushref)(schema, references);
86
82
  const schema_ = schema;
87
83
  if (schema.$id && visited.has(schema.$id))
88
84
  return false;
@@ -1,6 +1,6 @@
1
1
  import { Clone } from '../clone/index.mjs';
2
2
  import { Check } from '../check/index.mjs';
3
- import { Deref } from '../deref/index.mjs';
3
+ import { Deref, Pushref } from '../deref/index.mjs';
4
4
  import { Kind } from '../../type/symbols/index.mjs';
5
5
  // ------------------------------------------------------------------
6
6
  // ValueGuard
@@ -62,7 +62,8 @@ function TryConvertBoolean(value) {
62
62
  return IsValueTrue(value) ? true : IsValueFalse(value) ? false : value;
63
63
  }
64
64
  function TryConvertBigInt(value) {
65
- return IsStringNumeric(value) ? BigInt(parseInt(value)) : IsNumber(value) ? BigInt(value | 0) : IsValueFalse(value) ? BigInt(0) : IsValueTrue(value) ? BigInt(1) : value;
65
+ const truncateInteger = (value) => value.split('.')[0];
66
+ return IsStringNumeric(value) ? BigInt(truncateInteger(value)) : IsNumber(value) ? BigInt(value | 0) : IsValueFalse(value) ? BigInt(0) : IsValueTrue(value) ? BigInt(1) : value;
66
67
  }
67
68
  function TryConvertString(value) {
68
69
  return IsValueToString(value) ? value.toString() : IsSymbol(value) && value.description !== undefined ? value.description.toString() : value;
@@ -194,12 +195,8 @@ function FromUnion(schema, references, value) {
194
195
  }
195
196
  return value;
196
197
  }
197
- function AddReference(references, schema) {
198
- references.push(schema);
199
- return references;
200
- }
201
198
  function Visit(schema, references, value) {
202
- const references_ = IsString(schema.$id) ? AddReference(references, schema) : references;
199
+ const references_ = Pushref(schema, references);
203
200
  const schema_ = schema;
204
201
  switch (schema[Kind]) {
205
202
  case 'Array':
@@ -1,7 +1,7 @@
1
- import { HasPropertyKey, IsString } from '../guard/index.mjs';
1
+ import { HasPropertyKey } from '../guard/index.mjs';
2
2
  import { Check } from '../check/index.mjs';
3
3
  import { Clone } from '../clone/index.mjs';
4
- import { Deref } from '../deref/index.mjs';
4
+ import { Deref, Pushref } from '../deref/index.mjs';
5
5
  import { TemplateLiteralGenerate, IsTemplateLiteralFinite } from '../../type/template-literal/index.mjs';
6
6
  import { PatternStringExact, PatternNumberExact } from '../../type/patterns/index.mjs';
7
7
  import { TypeRegistry } from '../../type/registry/index.mjs';
@@ -381,12 +381,8 @@ function FromKind(schema, references) {
381
381
  throw new Error('User defined types must specify a default value');
382
382
  }
383
383
  }
384
- function AddReference(references, schema) {
385
- references.push(schema);
386
- return references;
387
- }
388
384
  function Visit(schema, references) {
389
- const references_ = IsString(schema.$id) ? AddReference(references, schema) : references;
385
+ const references_ = Pushref(schema, references);
390
386
  const schema_ = schema;
391
387
  switch (schema_[Kind]) {
392
388
  case 'Any':
@@ -1,11 +1,11 @@
1
1
  import { Check } from '../check/index.mjs';
2
2
  import { Clone } from '../clone/index.mjs';
3
- import { Deref } from '../deref/index.mjs';
3
+ import { Deref, Pushref } from '../deref/index.mjs';
4
4
  import { Kind } from '../../type/symbols/index.mjs';
5
5
  // ------------------------------------------------------------------
6
6
  // ValueGuard
7
7
  // ------------------------------------------------------------------
8
- import { IsString, IsFunction, IsObject, IsArray, IsUndefined, HasPropertyKey } from '../guard/index.mjs';
8
+ import { IsFunction, IsObject, IsArray, IsUndefined, HasPropertyKey } from '../guard/index.mjs';
9
9
  // ------------------------------------------------------------------
10
10
  // TypeGuard
11
11
  // ------------------------------------------------------------------
@@ -115,18 +115,14 @@ function FromUnion(schema, references, value) {
115
115
  const defaulted = ValueOrDefault(schema, value);
116
116
  for (const inner of schema.anyOf) {
117
117
  const result = Visit(inner, references, Clone(defaulted));
118
- if (Check(inner, result)) {
118
+ if (Check(inner, references, result)) {
119
119
  return result;
120
120
  }
121
121
  }
122
122
  return defaulted;
123
123
  }
124
- function AddReference(references, schema) {
125
- references.push(schema);
126
- return references;
127
- }
128
124
  function Visit(schema, references, value) {
129
- const references_ = IsString(schema.$id) ? AddReference(references, schema) : references;
125
+ const references_ = Pushref(schema, references);
130
126
  const schema_ = schema;
131
127
  switch (schema_[Kind]) {
132
128
  case 'Array':
@@ -6,5 +6,7 @@ export declare class TypeDereferenceError extends TypeBoxError {
6
6
  readonly schema: TRef | TThis;
7
7
  constructor(schema: TRef | TThis);
8
8
  }
9
- /** Dereferences a schema from the references array or throws if not found */
9
+ /** `[Internal]` Pushes a schema onto references if the schema has an $id and does not exist on references */
10
+ export declare function Pushref(schema: TSchema, references: TSchema[]): TSchema[];
11
+ /** `[Internal]` Dereferences a schema from the references array or throws if not found */
10
12
  export declare function Deref(schema: TSchema, references: TSchema[]): TSchema;
@@ -1,8 +1,9 @@
1
1
  import { TypeBoxError } from '../../type/error/index.mjs';
2
2
  import { Kind } from '../../type/symbols/index.mjs';
3
+ import { IsString } from '../guard/guard.mjs';
3
4
  export class TypeDereferenceError extends TypeBoxError {
4
5
  constructor(schema) {
5
- super(`Unable to dereference schema with $id '${schema.$id}'`);
6
+ super(`Unable to dereference schema with $id '${schema.$ref}'`);
6
7
  this.schema = schema;
7
8
  }
8
9
  }
@@ -12,7 +13,14 @@ function Resolve(schema, references) {
12
13
  throw new TypeDereferenceError(schema);
13
14
  return Deref(target, references);
14
15
  }
15
- /** Dereferences a schema from the references array or throws if not found */
16
+ /** `[Internal]` Pushes a schema onto references if the schema has an $id and does not exist on references */
17
+ export function Pushref(schema, references) {
18
+ if (!IsString(schema.$id) || references.some((target) => target.$id === schema.$id))
19
+ return references;
20
+ references.push(schema);
21
+ return references;
22
+ }
23
+ /** `[Internal]` Dereferences a schema from the references array or throws if not found */
16
24
  export function Deref(schema, references) {
17
25
  // prettier-ignore
18
26
  return (schema[Kind] === 'This' || schema[Kind] === 'Ref')
@@ -2,7 +2,7 @@ import { TypeSystemPolicy } from '../../system/policy.mjs';
2
2
  import { Kind, TransformKind } from '../../type/symbols/index.mjs';
3
3
  import { TypeBoxError } from '../../type/error/index.mjs';
4
4
  import { KeyOfPropertyKeys, KeyOfPropertyEntries } from '../../type/keyof/index.mjs';
5
- import { Deref } from '../deref/index.mjs';
5
+ import { Deref, Pushref } from '../deref/index.mjs';
6
6
  import { Check } from '../check/index.mjs';
7
7
  // ------------------------------------------------------------------
8
8
  // ValueGuard
@@ -159,13 +159,9 @@ function FromUnion(schema, references, path, value) {
159
159
  }
160
160
  return Default(schema, path, value);
161
161
  }
162
- function AddReference(references, schema) {
163
- references.push(schema);
164
- return references;
165
- }
166
162
  // prettier-ignore
167
163
  function Visit(schema, references, path, value) {
168
- const references_ = typeof schema.$id === 'string' ? AddReference(references, schema) : references;
164
+ const references_ = Pushref(schema, references);
169
165
  const schema_ = schema;
170
166
  switch (schema[Kind]) {
171
167
  case 'Array':
@@ -2,7 +2,7 @@ import { TypeSystemPolicy } from '../../system/policy.mjs';
2
2
  import { Kind, TransformKind } from '../../type/symbols/index.mjs';
3
3
  import { TypeBoxError } from '../../type/error/index.mjs';
4
4
  import { KeyOfPropertyKeys, KeyOfPropertyEntries } from '../../type/keyof/index.mjs';
5
- import { Deref } from '../deref/index.mjs';
5
+ import { Deref, Pushref } from '../deref/index.mjs';
6
6
  import { Check } from '../check/index.mjs';
7
7
  // ------------------------------------------------------------------
8
8
  // ValueGuard
@@ -171,13 +171,9 @@ function FromUnion(schema, references, path, value) {
171
171
  }
172
172
  return Default(schema, path, value);
173
173
  }
174
- function AddReference(references, schema) {
175
- references.push(schema);
176
- return references;
177
- }
178
174
  // prettier-ignore
179
175
  function Visit(schema, references, path, value) {
180
- const references_ = typeof schema.$id === 'string' ? AddReference(references, schema) : references;
176
+ const references_ = Pushref(schema, references);
181
177
  const schema_ = schema;
182
178
  switch (schema[Kind]) {
183
179
  case 'Array':
@@ -1,4 +1,4 @@
1
- import { Deref } from '../deref/index.mjs';
1
+ import { Deref, Pushref } from '../deref/index.mjs';
2
2
  import { Kind } from '../../type/symbols/index.mjs';
3
3
  // ------------------------------------------------------------------
4
4
  // TypeGuard
@@ -7,7 +7,7 @@ import { IsTransform, IsSchema } from '../../type/guard/type.mjs';
7
7
  // ------------------------------------------------------------------
8
8
  // ValueGuard
9
9
  // ------------------------------------------------------------------
10
- import { IsString, IsUndefined } from '../guard/index.mjs';
10
+ import { IsUndefined } from '../guard/index.mjs';
11
11
  // prettier-ignore
12
12
  function FromArray(schema, references) {
13
13
  return IsTransform(schema) || Visit(schema.items, references);
@@ -72,13 +72,9 @@ function FromTuple(schema, references) {
72
72
  function FromUnion(schema, references) {
73
73
  return IsTransform(schema) || schema.anyOf.some((schema) => Visit(schema, references));
74
74
  }
75
- function AddReference(references, schema) {
76
- references.push(schema);
77
- return references;
78
- }
79
75
  // prettier-ignore
80
76
  function Visit(schema, references) {
81
- const references_ = IsString(schema.$id) ? AddReference(references, schema) : references;
77
+ const references_ = Pushref(schema, references);
82
78
  const schema_ = schema;
83
79
  if (schema.$id && visited.has(schema.$id))
84
80
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.33.11",
3
+ "version": "0.33.13",
4
4
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",