typebox 1.1.19 → 1.1.21

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.
Files changed (208) hide show
  1. package/build/compile/compile.mjs +1 -0
  2. package/build/compile/validator.mjs +1 -1
  3. package/build/error/errors.mjs +1 -0
  4. package/build/format/iri-reference.mjs +1 -0
  5. package/build/format/uri-reference.mjs +1 -0
  6. package/build/guard/globals.d.mts +3 -3
  7. package/build/guard/guard.d.mts +1 -1
  8. package/build/schema/build.mjs +3 -3
  9. package/build/schema/engine/_functions.mjs +1 -1
  10. package/build/schema/engine/_guard.d.mts +3 -3
  11. package/build/schema/engine/_guard.mjs +3 -3
  12. package/build/schema/engine/_refine.d.mts +3 -3
  13. package/build/schema/engine/_refine.mjs +3 -3
  14. package/build/schema/engine/additionalProperties.d.mts +2 -2
  15. package/build/schema/engine/additionalProperties.mjs +2 -2
  16. package/build/schema/engine/anyOf.mjs +1 -1
  17. package/build/schema/engine/boolean.d.mts +2 -2
  18. package/build/schema/engine/boolean.mjs +2 -2
  19. package/build/schema/engine/const.d.mts +2 -2
  20. package/build/schema/engine/const.mjs +2 -2
  21. package/build/schema/engine/dependentRequired.d.mts +3 -3
  22. package/build/schema/engine/dependentRequired.mjs +3 -3
  23. package/build/schema/engine/enum.d.mts +2 -2
  24. package/build/schema/engine/enum.mjs +2 -2
  25. package/build/schema/engine/exclusiveMaximum.d.mts +2 -2
  26. package/build/schema/engine/exclusiveMaximum.mjs +2 -2
  27. package/build/schema/engine/exclusiveMinimum.d.mts +2 -2
  28. package/build/schema/engine/exclusiveMinimum.mjs +2 -2
  29. package/build/schema/engine/format.d.mts +2 -2
  30. package/build/schema/engine/format.mjs +2 -2
  31. package/build/schema/engine/maxItems.d.mts +2 -2
  32. package/build/schema/engine/maxItems.mjs +2 -2
  33. package/build/schema/engine/maxLength.d.mts +2 -2
  34. package/build/schema/engine/maxLength.mjs +2 -2
  35. package/build/schema/engine/maxProperties.d.mts +2 -2
  36. package/build/schema/engine/maxProperties.mjs +2 -2
  37. package/build/schema/engine/maximum.d.mts +2 -2
  38. package/build/schema/engine/maximum.mjs +2 -2
  39. package/build/schema/engine/minItems.d.mts +2 -2
  40. package/build/schema/engine/minItems.mjs +2 -2
  41. package/build/schema/engine/minLength.d.mts +2 -2
  42. package/build/schema/engine/minLength.mjs +2 -2
  43. package/build/schema/engine/minProperties.d.mts +2 -2
  44. package/build/schema/engine/minProperties.mjs +2 -2
  45. package/build/schema/engine/minimum.d.mts +2 -2
  46. package/build/schema/engine/minimum.mjs +2 -2
  47. package/build/schema/engine/multipleOf.d.mts +2 -2
  48. package/build/schema/engine/multipleOf.mjs +2 -2
  49. package/build/schema/engine/pattern.d.mts +2 -2
  50. package/build/schema/engine/pattern.mjs +2 -2
  51. package/build/schema/engine/recursiveRef.d.mts +1 -1
  52. package/build/schema/engine/recursiveRef.mjs +1 -1
  53. package/build/schema/engine/ref.d.mts +1 -1
  54. package/build/schema/engine/ref.mjs +1 -1
  55. package/build/schema/engine/required.d.mts +3 -3
  56. package/build/schema/engine/required.mjs +3 -3
  57. package/build/schema/engine/type.mjs +2 -2
  58. package/build/schema/engine/uniqueItems.d.mts +3 -3
  59. package/build/schema/engine/uniqueItems.mjs +3 -3
  60. package/build/schema/resolve/ref.mjs +1 -1
  61. package/build/schema/static/additionalProperties.mjs +1 -0
  62. package/build/schema/static/ref.mjs +1 -0
  63. package/build/schema/static/required.mjs +1 -0
  64. package/build/schema/static/static.mjs +1 -0
  65. package/build/schema/static/type.mjs +1 -0
  66. package/build/schema/static/unevaluatedProperties.mjs +1 -0
  67. package/build/system/environment/environment.d.mts +1 -1
  68. package/build/system/environment/environment.mjs +1 -1
  69. package/build/system/environment/evaluate.d.mts +9 -0
  70. package/build/system/environment/evaluate.mjs +37 -0
  71. package/build/system/memory/assign.mjs +1 -0
  72. package/build/system/memory/create.mjs +2 -1
  73. package/build/system/memory/discard.mjs +1 -0
  74. package/build/type/action/awaited.mjs +1 -0
  75. package/build/type/action/capitalize.mjs +1 -0
  76. package/build/type/action/conditional.mjs +1 -0
  77. package/build/type/action/constructor-parameters.mjs +1 -0
  78. package/build/type/action/evaluate.mjs +1 -0
  79. package/build/type/action/exclude.mjs +1 -0
  80. package/build/type/action/extract.mjs +1 -0
  81. package/build/type/action/indexed.mjs +1 -0
  82. package/build/type/action/instance-type.mjs +1 -0
  83. package/build/type/action/interface.mjs +1 -0
  84. package/build/type/action/keyof.mjs +1 -0
  85. package/build/type/action/lowercase.mjs +1 -0
  86. package/build/type/action/mapped.mjs +1 -0
  87. package/build/type/action/module.mjs +1 -0
  88. package/build/type/action/non-nullable.mjs +1 -0
  89. package/build/type/action/omit.mjs +1 -0
  90. package/build/type/action/options.mjs +1 -0
  91. package/build/type/action/parameters.mjs +1 -0
  92. package/build/type/action/partial.mjs +1 -0
  93. package/build/type/action/pick.mjs +1 -0
  94. package/build/type/action/readonly-type.mjs +1 -0
  95. package/build/type/action/required.mjs +1 -0
  96. package/build/type/action/return-type.mjs +1 -0
  97. package/build/type/action/uncapitalize.mjs +1 -0
  98. package/build/type/action/uppercase.mjs +1 -0
  99. package/build/type/engine/constructor-parameters/instantiate.mjs +1 -0
  100. package/build/type/engine/cyclic/extends.mjs +4 -4
  101. package/build/type/engine/cyclic/instantiate.mjs +1 -0
  102. package/build/type/engine/evaluate/broaden.mjs +1 -1
  103. package/build/type/engine/evaluate/compare.mjs +1 -0
  104. package/build/type/engine/exclude/instantiate.mjs +1 -0
  105. package/build/type/engine/extract/instantiate.mjs +1 -0
  106. package/build/type/engine/helpers/union.mjs +1 -0
  107. package/build/type/engine/indexed/from-array.mjs +1 -0
  108. package/build/type/engine/indexed/from-tuple.mjs +1 -0
  109. package/build/type/engine/interface/instantiate.mjs +1 -1
  110. package/build/type/engine/mapped/instantiate.mjs +1 -0
  111. package/build/type/engine/module/instantiate.mjs +1 -0
  112. package/build/type/engine/non-nullable/instantiate.mjs +1 -0
  113. package/build/type/engine/object/from-intersect.mjs +1 -0
  114. package/build/type/engine/object/from-type.mjs +1 -0
  115. package/build/type/engine/parameters/instantiate.mjs +1 -0
  116. package/build/type/engine/partial/from-type.mjs +1 -0
  117. package/build/type/engine/pick/instantiate.mjs +1 -0
  118. package/build/type/engine/record/from-key-integer.d.mts +2 -2
  119. package/build/type/engine/record/from-key-integer.mjs +1 -1
  120. package/build/type/engine/record/from-key-number.d.mts +1 -1
  121. package/build/type/engine/record/from-key-number.mjs +1 -1
  122. package/build/type/engine/required/from-type.mjs +1 -0
  123. package/build/type/engine/template-literal/encode.mjs +1 -1
  124. package/build/type/engine/template-literal/is-finite.mjs +1 -1
  125. package/build/type/engine/tuple/to-object.mjs +1 -0
  126. package/build/type/extends/extends-right.d.mts +1 -1
  127. package/build/type/extends/extends-right.mjs +1 -1
  128. package/build/type/extends/inference.mjs +1 -0
  129. package/build/type/extends/object.mjs +1 -0
  130. package/build/type/types/_codec.d.mts +1 -1
  131. package/build/type/types/_codec.mjs +1 -0
  132. package/build/type/types/base.d.mts +2 -2
  133. package/build/type/types/base.mjs +2 -2
  134. package/build/type/types/call.mjs +1 -0
  135. package/build/type/types/never.mjs +1 -0
  136. package/build/type/types/properties.mjs +0 -1
  137. package/build/type/types/record.mjs +1 -0
  138. package/build/type/types/ref.mjs +1 -0
  139. package/build/value/clean/from-base.d.mts +1 -1
  140. package/build/value/clean/from-base.mjs +1 -1
  141. package/build/value/clean/from-union.mjs +2 -1
  142. package/build/value/codec/callback.mjs +2 -2
  143. package/build/value/codec/from-object.mjs +1 -1
  144. package/build/value/codec/from-union.mjs +5 -7
  145. package/build/value/convert/from-base.d.mts +1 -1
  146. package/build/value/convert/from-base.mjs +1 -1
  147. package/build/value/convert/from-bigint.d.mts +1 -1
  148. package/build/value/convert/from-bigint.mjs +1 -1
  149. package/build/value/convert/from-boolean.d.mts +1 -1
  150. package/build/value/convert/from-boolean.mjs +1 -1
  151. package/build/value/convert/from-integer.d.mts +1 -1
  152. package/build/value/convert/from-integer.mjs +1 -1
  153. package/build/value/convert/from-literal.mjs +4 -4
  154. package/build/value/convert/from-null.d.mts +1 -1
  155. package/build/value/convert/from-null.mjs +1 -1
  156. package/build/value/convert/from-number.d.mts +1 -1
  157. package/build/value/convert/from-number.mjs +1 -1
  158. package/build/value/convert/from-object.mjs +1 -1
  159. package/build/value/convert/from-string.d.mts +1 -1
  160. package/build/value/convert/from-string.mjs +1 -1
  161. package/build/value/convert/from-undefined.d.mts +1 -1
  162. package/build/value/convert/from-undefined.mjs +1 -1
  163. package/build/value/convert/from-void.d.mts +1 -1
  164. package/build/value/convert/from-void.mjs +1 -1
  165. package/build/value/create/from-async-iterator.d.mts +1 -1
  166. package/build/value/create/from-async-iterator.mjs +1 -1
  167. package/build/value/create/from-base.d.mts +1 -1
  168. package/build/value/create/from-base.mjs +1 -1
  169. package/build/value/create/from-bigint.d.mts +1 -1
  170. package/build/value/create/from-bigint.mjs +1 -1
  171. package/build/value/create/from-boolean.d.mts +1 -1
  172. package/build/value/create/from-boolean.mjs +1 -1
  173. package/build/value/create/from-default.d.mts +1 -1
  174. package/build/value/create/from-default.mjs +1 -1
  175. package/build/value/create/from-integer.d.mts +1 -1
  176. package/build/value/create/from-integer.mjs +1 -1
  177. package/build/value/create/from-iterator.d.mts +1 -1
  178. package/build/value/create/from-iterator.mjs +1 -1
  179. package/build/value/create/from-literal.d.mts +1 -1
  180. package/build/value/create/from-literal.mjs +1 -1
  181. package/build/value/create/from-never.d.mts +1 -1
  182. package/build/value/create/from-never.mjs +1 -1
  183. package/build/value/create/from-null.d.mts +1 -1
  184. package/build/value/create/from-null.mjs +1 -1
  185. package/build/value/create/from-number.d.mts +1 -1
  186. package/build/value/create/from-number.mjs +1 -1
  187. package/build/value/create/from-record.d.mts +1 -1
  188. package/build/value/create/from-record.mjs +1 -1
  189. package/build/value/create/from-string.d.mts +1 -1
  190. package/build/value/create/from-string.mjs +1 -1
  191. package/build/value/create/from-symbol.d.mts +1 -1
  192. package/build/value/create/from-symbol.mjs +1 -1
  193. package/build/value/create/from-undefined.d.mts +1 -1
  194. package/build/value/create/from-undefined.mjs +1 -1
  195. package/build/value/create/from-void.d.mts +1 -1
  196. package/build/value/create/from-void.mjs +1 -1
  197. package/build/value/index.d.mts +1 -0
  198. package/build/value/index.mjs +4 -0
  199. package/build/value/repair/from-union.mjs +3 -50
  200. package/build/value/shared/index.d.mts +2 -0
  201. package/build/value/shared/index.mjs +2 -0
  202. package/build/value/shared/union-priority-sort.d.mts +3 -0
  203. package/build/value/shared/union-priority-sort.mjs +36 -0
  204. package/build/value/shared/union-score-select.d.mts +3 -0
  205. package/build/value/shared/union-score-select.mjs +51 -0
  206. package/package.json +30 -30
  207. package/build/system/environment/can-evaluate.d.mts +0 -2
  208. package/build/system/environment/can-evaluate.mjs +0 -26
@@ -1,4 +1,4 @@
1
1
  // deno-fmt-ignore-file
2
- export function FromLiteral(context, type) {
2
+ export function FromLiteral(_context, type) {
3
3
  return type.const;
4
4
  }
@@ -1,2 +1,2 @@
1
1
  import type { TProperties, TNever } from '../../type/index.mjs';
2
- export declare function FromNever(context: TProperties, type: TNever): unknown;
2
+ export declare function FromNever(_context: TProperties, type: TNever): unknown;
@@ -1,5 +1,5 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { CreateError } from './error.mjs';
3
- export function FromNever(context, type) {
3
+ export function FromNever(_context, type) {
4
4
  throw new CreateError(type, 'Cannot create TNever types');
5
5
  }
@@ -1,2 +1,2 @@
1
1
  import type { TProperties, TNull } from '../../type/index.mjs';
2
- export declare function FromNull(context: TProperties, type: TNull): unknown;
2
+ export declare function FromNull(_context: TProperties, _type: TNull): unknown;
@@ -1,4 +1,4 @@
1
1
  // deno-fmt-ignore-file
2
- export function FromNull(context, type) {
2
+ export function FromNull(_context, _type) {
3
3
  return null;
4
4
  }
@@ -1,2 +1,2 @@
1
1
  import type { TProperties, TNumber } from '../../type/index.mjs';
2
- export declare function FromNumber(context: TProperties, type: TNumber): unknown;
2
+ export declare function FromNumber(_context: TProperties, type: TNumber): unknown;
@@ -1,7 +1,7 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { Guard } from '../../guard/index.mjs';
3
3
  import { IsExclusiveMinimum, IsMinimum } from '../../schema/types/index.mjs';
4
- export function FromNumber(context, type) {
4
+ export function FromNumber(_context, type) {
5
5
  return (IsExclusiveMinimum(type) && Guard.IsNumber(type.exclusiveMinimum) ? type.exclusiveMinimum + 1 :
6
6
  IsMinimum(type) ? type.minimum :
7
7
  0);
@@ -1,2 +1,2 @@
1
1
  import type { TProperties, TRecord } from '../../type/index.mjs';
2
- export declare function FromRecord(context: TProperties, type: TRecord): unknown;
2
+ export declare function FromRecord(_context: TProperties, type: TRecord): unknown;
@@ -1,7 +1,7 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { IsMinProperties, IsDefault } from '../../schema/types/index.mjs';
3
3
  import { CreateError } from './error.mjs';
4
- export function FromRecord(context, type) {
4
+ export function FromRecord(_context, type) {
5
5
  if (IsMinProperties(type) && !IsDefault(type))
6
6
  throw new CreateError(type, 'Record with the minProperties constraint must have a default annotation');
7
7
  return {};
@@ -1,2 +1,2 @@
1
1
  import type { TProperties, TString } from '../../type/index.mjs';
2
- export declare function FromString(context: TProperties, type: TString): unknown;
2
+ export declare function FromString(_context: TProperties, type: TString): unknown;
@@ -1,6 +1,6 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { IsPattern, IsFormat, IsDefault, IsMinLength } from '../../schema/types/index.mjs';
3
- export function FromString(context, type) {
3
+ export function FromString(_context, type) {
4
4
  const needsDefault = (IsPattern(type) || IsFormat(type)) && !IsDefault(type);
5
5
  if (needsDefault)
6
6
  throw Error('Strings with format or pattern constraints must specify default');
@@ -1,2 +1,2 @@
1
1
  import type { TProperties, TSymbol } from '../../type/index.mjs';
2
- export declare function FromSymbol(context: TProperties, type: TSymbol): unknown;
2
+ export declare function FromSymbol(_context: TProperties, _type: TSymbol): unknown;
@@ -1,4 +1,4 @@
1
1
  // deno-fmt-ignore-file
2
- export function FromSymbol(context, type) {
2
+ export function FromSymbol(_context, _type) {
3
3
  return Symbol();
4
4
  }
@@ -1,2 +1,2 @@
1
1
  import type { TProperties, TUndefined } from '../../type/index.mjs';
2
- export declare function FromUndefined(context: TProperties, type: TUndefined): unknown;
2
+ export declare function FromUndefined(_context: TProperties, _type: TUndefined): unknown;
@@ -1,4 +1,4 @@
1
1
  // deno-fmt-ignore-file
2
- export function FromUndefined(context, type) {
2
+ export function FromUndefined(_context, _type) {
3
3
  return undefined;
4
4
  }
@@ -1,2 +1,2 @@
1
1
  import type { TProperties, TVoid } from '../../type/index.mjs';
2
- export declare function FromVoid(context: TProperties, type: TVoid): unknown;
2
+ export declare function FromVoid(_context: TProperties, _type: TVoid): unknown;
@@ -1,4 +1,4 @@
1
1
  // deno-fmt-ignore-file
2
- export function FromVoid(context, type) {
2
+ export function FromVoid(_context, _type) {
3
3
  return void 0;
4
4
  }
@@ -15,6 +15,7 @@ export * from './delta/index.mjs';
15
15
  export * from './pipeline/index.mjs';
16
16
  export * from './pointer/index.mjs';
17
17
  export * from './repair/index.mjs';
18
+ export * from './shared/index.mjs';
18
19
  import * as Value from './value.mjs';
19
20
  export * as Value from './value.mjs';
20
21
  export default Value;
@@ -19,6 +19,10 @@ export * from './pipeline/index.mjs';
19
19
  export * from './pointer/index.mjs';
20
20
  export * from './repair/index.mjs';
21
21
  // ------------------------------------------------------------------
22
+ // Shared
23
+ // ------------------------------------------------------------------
24
+ export * from './shared/index.mjs';
25
+ // ------------------------------------------------------------------
22
26
  // Default
23
27
  // ------------------------------------------------------------------
24
28
  import * as Value from './value.mjs';
@@ -1,65 +1,18 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { IsDefault } from '../../schema/types/index.mjs';
3
- import { Union, IsLiteral, IsObject, IsRef } from '../../type/index.mjs';
3
+ import { Union } from '../../type/index.mjs';
4
4
  import { Flatten } from '../../type/engine/evaluate/index.mjs';
5
- import { Guard } from '../../guard/index.mjs';
6
5
  import { Check } from '../check/index.mjs';
7
6
  import { Clone } from '../clone/index.mjs';
8
7
  import { Create } from '../create/index.mjs';
9
- import { RepairError } from './error.mjs';
10
8
  import { FromType } from './from-type.mjs';
11
- // ------------------------------------------------------------------
12
- // Deref
13
- // ------------------------------------------------------------------
14
- function Deref(context, type, value) {
15
- return IsRef(type)
16
- ? Guard.HasPropertyKey(context, type.$ref)
17
- ? Deref(context, context[type.$ref], value)
18
- : (() => { throw new RepairError(context, type, value, 'Unable to Deref target on Union repair'); })()
19
- : type;
20
- }
21
- // ------------------------------------------------------------------
22
- // The following will score a schema against a value. For objects,
23
- // the score is the tally of points awarded for each property of
24
- // the value. Property points are (1.0 / propertyCount) to prevent
25
- // large property counts biasing results. Properties that match
26
- // literal values are maximally awarded as literals are typically
27
- // used as union discriminator fields.
28
- // ------------------------------------------------------------------
29
- function ScoreVariant(context, type, value) {
30
- // scoring is only possible for object types.
31
- if (!(IsObject(type) && Guard.IsObject(value)))
32
- return 0;
33
- const keys = Guard.Keys(value);
34
- const entries = Guard.Entries(type.properties);
35
- return entries.reduce((result, [key, schema]) => {
36
- const literal = IsLiteral(schema) && Guard.IsEqual(schema.const, value[key]) ? 100 : 0;
37
- const checks = Check(context, schema, value[key]) ? 10 : 0;
38
- const exists = keys.includes(key) ? 1 : 0;
39
- return result + (literal + checks + exists);
40
- }, 0);
41
- }
42
- // ------------------------------------------------------------------
43
- // SelectVariant
44
- // ------------------------------------------------------------------
45
- function SelectVariant(context, type, value) {
46
- const schemas = type.anyOf.map((schema) => Deref(context, schema, value));
47
- let [select, best] = [schemas[0], 0];
48
- for (const schema of schemas) {
49
- const score = ScoreVariant(context, schema, value);
50
- if (score > best) {
51
- select = schema;
52
- best = score;
53
- }
54
- }
55
- return select;
56
- }
9
+ import { UnionScoreSelect } from '../shared/union-score-select.mjs';
57
10
  // ------------------------------------------------------------------
58
11
  // RepairUnion
59
12
  // ------------------------------------------------------------------
60
13
  function RepairUnion(context, type, value) {
61
14
  const union = Union(Flatten(type.anyOf));
62
- const schema = SelectVariant(context, union, value);
15
+ const schema = UnionScoreSelect(context, union, value);
63
16
  return FromType(context, schema, value);
64
17
  }
65
18
  export function FromUnion(context, type, value) {
@@ -1 +1,3 @@
1
1
  export * from './optional-undefined.mjs';
2
+ export * from './union-priority-sort.mjs';
3
+ export * from './union-score-select.mjs';
@@ -1,2 +1,4 @@
1
1
  // deno-fmt-ignore-file
2
2
  export * from './optional-undefined.mjs';
3
+ export * from './union-priority-sort.mjs';
4
+ export * from './union-score-select.mjs';
@@ -0,0 +1,3 @@
1
+ import { type TSchema } from '../../type/index.mjs';
2
+ /** Deterministically sorts schemas by structural relationship (narrow to broad) */
3
+ export declare function UnionPrioritySort(types: TSchema[], order?: number): TSchema[];
@@ -0,0 +1,36 @@
1
+ // deno-fmt-ignore-file
2
+ import { Guard } from '../../guard/index.mjs';
3
+ import { Compare } from '../../type/index.mjs';
4
+ // ------------------------------------------------------------------
5
+ // DeterministicCompare
6
+ //
7
+ // Provides a deterministic tie-break for schemas. This is used when
8
+ // schemas are structurally disjoint or mutually inclusive. While
9
+ // JSON serialization incurs a performance overhead, it serves as a
10
+ // reliable mechanism to ensure stable ordering and preserves the
11
+ // alphabetical alignment of named constants.
12
+ //
13
+ // ------------------------------------------------------------------
14
+ function DeterministicCompare(left, right) {
15
+ return JSON.stringify(left).localeCompare(JSON.stringify(right));
16
+ }
17
+ // ------------------------------------------------------------------
18
+ // UnionPrioritySort
19
+ //
20
+ // Performs a deterministic sort on Union members. By default, this
21
+ // function ensures that narrow (more specific) types precede broader
22
+ // types in the resulting array. The order can be reversed by setting
23
+ // the order property to -1 which will reverse unions from broader
24
+ // to more narrow.
25
+ //
26
+ // ------------------------------------------------------------------
27
+ /** Deterministically sorts schemas by structural relationship (narrow to broad) */
28
+ export function UnionPrioritySort(types, order = 1) {
29
+ return types.sort((left, right) => {
30
+ const result = Compare(left, right);
31
+ return (Guard.IsEqual(result, 'disjoint') ? DeterministicCompare(left, right) :
32
+ Guard.IsEqual(result, 'right-inside') ? 1 :
33
+ Guard.IsEqual(result, 'left-inside') ? -1 :
34
+ DeterministicCompare(left, right)) * order;
35
+ });
36
+ }
@@ -0,0 +1,3 @@
1
+ import { type TProperties, type TSchema, type TUnion } from '../../type/index.mjs';
2
+ /** Scores Union variants and returns the best match for the given value */
3
+ export declare function UnionScoreSelect(context: TProperties, type: TUnion, value: unknown): TSchema;
@@ -0,0 +1,51 @@
1
+ // deno-fmt-ignore-file
2
+ import { IsLiteral, IsObject, IsRef } from '../../type/index.mjs';
3
+ import { Guard } from '../../guard/index.mjs';
4
+ import { Check } from '../check/index.mjs';
5
+ // ------------------------------------------------------------------
6
+ // Deref
7
+ // ------------------------------------------------------------------
8
+ function Deref(context, type, value) {
9
+ return IsRef(type)
10
+ ? Guard.HasPropertyKey(context, type.$ref)
11
+ ? Deref(context, context[type.$ref], value)
12
+ : (() => { throw new Error('Unable to Deref target'); })()
13
+ : type;
14
+ }
15
+ // ------------------------------------------------------------------
16
+ // The following will score a schema against a value. For objects,
17
+ // the score is the tally of points awarded for each property of
18
+ // the value. Property points are (1.0 / propertyCount) to prevent
19
+ // large property counts biasing results. Properties that match
20
+ // literal values are maximally awarded as literals are typically
21
+ // used as union discriminator fields.
22
+ // ------------------------------------------------------------------
23
+ function ScoreVariant(context, type, value) {
24
+ // scoring is only possible for object types.
25
+ if (!(IsObject(type) && Guard.IsObject(value)))
26
+ return 0;
27
+ const keys = Guard.Keys(value);
28
+ const entries = Guard.Entries(type.properties);
29
+ return entries.reduce((result, [key, schema]) => {
30
+ const literal = IsLiteral(schema) && Guard.IsEqual(schema.const, value[key]) ? 100 : 0;
31
+ const checks = Check(context, schema, value[key]) ? 10 : 0;
32
+ const exists = keys.includes(key) ? 1 : 0;
33
+ return result + (literal + checks + exists);
34
+ }, 0);
35
+ }
36
+ // ------------------------------------------------------------------
37
+ // UnionScoreSelect
38
+ // ------------------------------------------------------------------
39
+ /** Scores Union variants and returns the best match for the given value */
40
+ export function UnionScoreSelect(context, type, value) {
41
+ const schemas = type.anyOf.map((schema) => Deref(context, schema, value));
42
+ let [select, best] = [schemas[0], 0];
43
+ for (const schema of schemas) {
44
+ const score = ScoreVariant(context, schema, value);
45
+ if (score > best) {
46
+ select = schema;
47
+ best = score;
48
+ }
49
+ }
50
+ return select;
51
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "typebox",
3
3
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
4
- "version": "1.1.19",
4
+ "version": "1.1.21",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "jsonschema"
@@ -16,29 +16,21 @@
16
16
  "types": "./build/index.d.mts",
17
17
  "module": "./build/index.mjs",
18
18
  "exports": {
19
- "./system": {
20
- "import": "./build/system/index.mjs",
21
- "default": "./build/system/index.mjs"
22
- },
23
- "./compile": {
24
- "import": "./build/compile/index.mjs",
25
- "default": "./build/compile/index.mjs"
26
- },
27
- "./value": {
28
- "import": "./build/value/index.mjs",
29
- "default": "./build/value/index.mjs"
19
+ "./guard": {
20
+ "import": "./build/guard/index.mjs",
21
+ "default": "./build/guard/index.mjs"
30
22
  },
31
23
  "./error": {
32
24
  "import": "./build/error/index.mjs",
33
25
  "default": "./build/error/index.mjs"
34
26
  },
35
- "./guard": {
36
- "import": "./build/guard/index.mjs",
37
- "default": "./build/guard/index.mjs"
27
+ "./compile": {
28
+ "import": "./build/compile/index.mjs",
29
+ "default": "./build/compile/index.mjs"
38
30
  },
39
- "./schema": {
40
- "import": "./build/schema/index.mjs",
41
- "default": "./build/schema/index.mjs"
31
+ "./system": {
32
+ "import": "./build/system/index.mjs",
33
+ "default": "./build/system/index.mjs"
42
34
  },
43
35
  "./format": {
44
36
  "import": "./build/format/index.mjs",
@@ -48,6 +40,14 @@
48
40
  "import": "./build/type/index.mjs",
49
41
  "default": "./build/type/index.mjs"
50
42
  },
43
+ "./schema": {
44
+ "import": "./build/schema/index.mjs",
45
+ "default": "./build/schema/index.mjs"
46
+ },
47
+ "./value": {
48
+ "import": "./build/value/index.mjs",
49
+ "default": "./build/value/index.mjs"
50
+ },
51
51
  ".": {
52
52
  "import": "./build/index.mjs",
53
53
  "default": "./build/index.mjs"
@@ -55,23 +55,17 @@
55
55
  },
56
56
  "typesVersions": {
57
57
  "*": {
58
- "system": [
59
- "./build/system/index.d.mts"
60
- ],
61
- "compile": [
62
- "./build/compile/index.d.mts"
63
- ],
64
- "value": [
65
- "./build/value/index.d.mts"
58
+ "guard": [
59
+ "./build/guard/index.d.mts"
66
60
  ],
67
61
  "error": [
68
62
  "./build/error/index.d.mts"
69
63
  ],
70
- "guard": [
71
- "./build/guard/index.d.mts"
64
+ "compile": [
65
+ "./build/compile/index.d.mts"
72
66
  ],
73
- "schema": [
74
- "./build/schema/index.d.mts"
67
+ "system": [
68
+ "./build/system/index.d.mts"
75
69
  ],
76
70
  "format": [
77
71
  "./build/format/index.d.mts"
@@ -79,6 +73,12 @@
79
73
  "type": [
80
74
  "./build/type/index.d.mts"
81
75
  ],
76
+ "schema": [
77
+ "./build/schema/index.d.mts"
78
+ ],
79
+ "value": [
80
+ "./build/value/index.d.mts"
81
+ ],
82
82
  ".": [
83
83
  "./build/index.d.mts"
84
84
  ]
@@ -1,2 +0,0 @@
1
- /** Returns true if the environment supports dynamic JavaScript evaluation */
2
- export declare function CanAccelerate(): boolean;
@@ -1,26 +0,0 @@
1
- import { Settings } from '../settings/index.mjs';
2
- import { Guard } from '../../guard/index.mjs';
3
- let supported = undefined;
4
- // ------------------------------------------------------------------
5
- // TryEval
6
- // ------------------------------------------------------------------
7
- // deno-coverage-ignore-start - catch is environment dependent
8
- function TryEval() {
9
- try {
10
- new globalThis.Function('null')();
11
- return true;
12
- }
13
- catch {
14
- return false;
15
- }
16
- }
17
- // deno-coverage-ignore-stop
18
- // ------------------------------------------------------------------
19
- // CanEvaluate
20
- // ------------------------------------------------------------------
21
- /** Returns true if the environment supports dynamic JavaScript evaluation */
22
- export function CanAccelerate() {
23
- if (Guard.IsUndefined(supported))
24
- supported = TryEval();
25
- return supported && Settings.Get().useAcceleration;
26
- }