@sinclair/typebox 0.34.38 → 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
  }
@@ -149,7 +149,7 @@ function FromNumber(schema, references, value) {
149
149
  }
150
150
  // prettier-ignore
151
151
  function FromObject(schema, references, value) {
152
- if (!(0, index_5.IsObject)(value))
152
+ if (!(0, index_5.IsObject)(value) || (0, index_5.IsArray)(value))
153
153
  return value;
154
154
  for (const propertyKey of Object.getOwnPropertyNames(schema.properties)) {
155
155
  if (!(0, index_5.HasPropertyKey)(value, propertyKey))
@@ -159,7 +159,7 @@ function FromObject(schema, references, value) {
159
159
  return value;
160
160
  }
161
161
  function FromRecord(schema, references, value) {
162
- const isConvertable = (0, index_5.IsObject)(value);
162
+ const isConvertable = (0, index_5.IsObject)(value) && !(0, index_5.IsArray)(value);
163
163
  if (!isConvertable)
164
164
  return value;
165
165
  const propertyKey = Object.getOwnPropertyNames(schema.patternProperties)[0];
@@ -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
  }
@@ -145,7 +145,7 @@ function FromNumber(schema, references, value) {
145
145
  }
146
146
  // prettier-ignore
147
147
  function FromObject(schema, references, value) {
148
- if (!IsObject(value))
148
+ if (!IsObject(value) || IsArray(value))
149
149
  return value;
150
150
  for (const propertyKey of Object.getOwnPropertyNames(schema.properties)) {
151
151
  if (!HasPropertyKey(value, propertyKey))
@@ -155,7 +155,7 @@ function FromObject(schema, references, value) {
155
155
  return value;
156
156
  }
157
157
  function FromRecord(schema, references, value) {
158
- const isConvertable = IsObject(value);
158
+ const isConvertable = IsObject(value) && !IsArray(value);
159
159
  if (!isConvertable)
160
160
  return value;
161
161
  const propertyKey = Object.getOwnPropertyNames(schema.patternProperties)[0];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.34.38",
3
+ "version": "0.34.40",
4
4
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",