typebox 1.1.18 → 1.1.19

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.
@@ -2,27 +2,57 @@
2
2
  import { Guard } from '../../guard/index.mjs';
3
3
  import { FromType } from './from-type.mjs';
4
4
  import { Callback } from './callback.mjs';
5
+ import { Clone } from '../clone/index.mjs';
6
+ import { Clean } from '../clean/index.mjs';
7
+ // ------------------------------------------------------------------
8
+ // MergeInteriors
9
+ //
10
+ // Merges all interior operand results into a single object. Each
11
+ // subsequent operand's properties override those of prior operands.
12
+ //
13
+ // ------------------------------------------------------------------
14
+ function MergeInteriors(interiors) {
15
+ return interiors.reduce((results, interior) => ({ ...results, ...interior }), {});
16
+ }
17
+ // ------------------------------------------------------------------
18
+ // NonMatchingInterior
19
+ //
20
+ // Used when Intersect operands do not all produce Objects. Returns
21
+ // the first interior result that differs from the original value,
22
+ // indicating a Codec has transformed the data. If no operand
23
+ // produced a change, defaults to the first interior result.
24
+ //
25
+ // ------------------------------------------------------------------
26
+ function NonMatchingInterior(value, interiors) {
27
+ for (const interior of interiors)
28
+ if (!Guard.IsDeepEqual(value, interior))
29
+ return interior;
30
+ return value; // value-unchanged
31
+ }
5
32
  // ------------------------------------------------------------------
6
33
  // Decode
7
34
  // ------------------------------------------------------------------
8
35
  function Decode(direction, context, type, value) {
9
- for (const schema of type.allOf) {
10
- value = FromType(direction, context, schema, value);
11
- }
12
- return Callback(direction, context, type, value);
36
+ if (Guard.IsEqual(type.allOf.length, 0))
37
+ return Callback(direction, context, type, value);
38
+ const interiors = type.allOf.map((schema) => FromType(direction, context, schema, Clean(schema, Clone(value))));
39
+ const structural = interiors.every((result) => Guard.IsObject(result));
40
+ const exterior = structural ? MergeInteriors(interiors) : NonMatchingInterior(value, interiors);
41
+ return Callback(direction, context, type, exterior);
13
42
  }
14
43
  // ------------------------------------------------------------------
15
44
  // Encode
16
45
  // ------------------------------------------------------------------
17
46
  function Encode(direction, context, type, value) {
18
- let exterior = Callback(direction, context, type, value);
19
- for (const schema of type.allOf) {
20
- exterior = FromType(direction, context, schema, exterior);
21
- }
22
- return exterior;
47
+ if (Guard.IsEqual(type.allOf.length, 0))
48
+ return Callback(direction, context, type, value);
49
+ const exterior = Callback(direction, context, type, value);
50
+ const interiors = type.allOf.map((schema) => FromType(direction, context, schema, Clean(schema, Clone(exterior))));
51
+ const structural = interiors.every((result) => Guard.IsObject(result));
52
+ if (structural)
53
+ return MergeInteriors(interiors);
54
+ return NonMatchingInterior(exterior, interiors);
23
55
  }
24
56
  export function FromIntersect(direction, context, type, value) {
25
- return Guard.IsEqual(direction, 'Decode')
26
- ? Decode(direction, context, type, value)
27
- : Encode(direction, context, type, value);
57
+ return Guard.IsEqual(direction, 'Decode') ? Decode(direction, context, type, value) : Encode(direction, context, type, value);
28
58
  }
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.18",
4
+ "version": "1.1.19",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "jsonschema"