@sinclair/typebox 0.34.39 → 0.34.40

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.
@@ -21,23 +21,23 @@ class ValueCastError extends index_2.TypeBoxError {
21
21
  }
22
22
  exports.ValueCastError = ValueCastError;
23
23
  // ------------------------------------------------------------------
24
- // The following will score a schema against a value. For objects,
25
- // the score is the tally of points awarded for each property of
26
- // the value. Property points are (1.0 / propertyCount) to prevent
27
- // large property counts biasing results. Properties that match
28
- // literal values are maximally awarded as literals are typically
29
- // used as union discriminator fields.
24
+ // The following logic assigns a score to a schema based on how well
25
+ // it matches a given value. For object types, the score is calculated
26
+ // by evaluating each property of the value against the schema's
27
+ // properties. To avoid bias towards objects with many properties,
28
+ // each property contributes equally to the total score. Properties
29
+ // that exactly match literal values receive the highest possible
30
+ // score, as literals are often used as discriminators in union types.
30
31
  // ------------------------------------------------------------------
31
32
  function ScoreUnion(schema, references, value) {
32
33
  if (schema[index_3.Kind] === 'Object' && typeof value === 'object' && !(0, index_1.IsNull)(value)) {
33
34
  const object = schema;
34
35
  const keys = Object.getOwnPropertyNames(value);
35
36
  const entries = Object.entries(object.properties);
36
- const [point, max] = [1 / entries.length, entries.length];
37
37
  return entries.reduce((acc, [key, schema]) => {
38
- const literal = schema[index_3.Kind] === 'Literal' && schema.const === value[key] ? max : 0;
39
- const checks = (0, index_5.Check)(schema, references, value[key]) ? point : 0;
40
- const exists = keys.includes(key) ? point : 0;
38
+ const literal = schema[index_3.Kind] === 'Literal' && schema.const === value[key] ? 100 : 0;
39
+ const checks = (0, index_5.Check)(schema, references, value[key]) ? 10 : 0;
40
+ const exists = keys.includes(key) ? 1 : 0;
41
41
  return acc + (literal + checks + exists);
42
42
  }, 0);
43
43
  }
@@ -15,23 +15,23 @@ export class ValueCastError extends TypeBoxError {
15
15
  }
16
16
  }
17
17
  // ------------------------------------------------------------------
18
- // The following will score a schema against a value. For objects,
19
- // the score is the tally of points awarded for each property of
20
- // the value. Property points are (1.0 / propertyCount) to prevent
21
- // large property counts biasing results. Properties that match
22
- // literal values are maximally awarded as literals are typically
23
- // used as union discriminator fields.
18
+ // The following logic assigns a score to a schema based on how well
19
+ // it matches a given value. For object types, the score is calculated
20
+ // by evaluating each property of the value against the schema's
21
+ // properties. To avoid bias towards objects with many properties,
22
+ // each property contributes equally to the total score. Properties
23
+ // that exactly match literal values receive the highest possible
24
+ // score, as literals are often used as discriminators in union types.
24
25
  // ------------------------------------------------------------------
25
26
  function ScoreUnion(schema, references, value) {
26
27
  if (schema[Kind] === 'Object' && typeof value === 'object' && !IsNull(value)) {
27
28
  const object = schema;
28
29
  const keys = Object.getOwnPropertyNames(value);
29
30
  const entries = Object.entries(object.properties);
30
- const [point, max] = [1 / entries.length, entries.length];
31
31
  return entries.reduce((acc, [key, schema]) => {
32
- const literal = schema[Kind] === 'Literal' && schema.const === value[key] ? max : 0;
33
- const checks = Check(schema, references, value[key]) ? point : 0;
34
- const exists = keys.includes(key) ? point : 0;
32
+ const literal = schema[Kind] === 'Literal' && schema.const === value[key] ? 100 : 0;
33
+ const checks = Check(schema, references, value[key]) ? 10 : 0;
34
+ const exists = keys.includes(key) ? 1 : 0;
35
35
  return acc + (literal + checks + exists);
36
36
  }, 0);
37
37
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.34.39",
3
+ "version": "0.34.40",
4
4
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",