@pdtf/schemas 3.2.1-14 → 3.2.1-16

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.
package/index.js CHANGED
@@ -61,14 +61,49 @@ const transactionSchemas = {
61
61
  };
62
62
 
63
63
  const combineMerge = (target, source, options) => {
64
+ // Special handling for 'required' arrays - combine them uniquely
65
+ if (target.some((item) => typeof item === "string")) {
66
+ return [...new Set([...target, ...source])];
67
+ }
68
+
69
+ // For other arrays, concatenate while preserving uniqueness for primitive values
70
+ if (!options.isMergeableObject(target[0])) {
71
+ return [...new Set([...target, ...source])];
72
+ }
73
+
74
+ // Special handling for oneOf arrays - merge matching schemas based on discriminator
75
+ if (target[0]?.properties && source[0]?.properties) {
76
+ // Find a common discriminator property (a property with an enum)
77
+ const findDiscriminator = (schema) => {
78
+ const props = schema.properties;
79
+ return Object.keys(props).find((key) => Array.isArray(props[key]?.enum));
80
+ };
81
+
82
+ const discriminator = findDiscriminator(target[0]);
83
+ if (discriminator) {
84
+ return target.map((targetSchema) => {
85
+ const targetEnum = targetSchema.properties?.[discriminator]?.enum || [];
86
+ const matchingSourceSchema = source.find((sourceSchema) => {
87
+ const sourceEnum =
88
+ sourceSchema.properties?.[discriminator]?.enum || [];
89
+ // Check if there's any overlap between the enum values
90
+ return sourceEnum.some((value) => targetEnum.includes(value));
91
+ });
92
+ if (matchingSourceSchema) {
93
+ return merge(targetSchema, matchingSourceSchema, options);
94
+ }
95
+ return targetSchema;
96
+ });
97
+ }
98
+ }
99
+
100
+ // For arrays of objects, merge by index and append remaining items
64
101
  const destination = target.slice();
65
102
  source.forEach((item, index) => {
66
103
  if (typeof destination[index] === "undefined") {
67
104
  destination[index] = options.cloneUnlessOtherwiseSpecified(item, options);
68
105
  } else if (options.isMergeableObject(item)) {
69
106
  destination[index] = merge(target[index], item, options);
70
- } else if (target.indexOf(item) === -1) {
71
- destination.push(item);
72
107
  }
73
108
  });
74
109
  return destination;
@@ -84,7 +119,12 @@ const getTransactionSchema = (
84
119
  if (!overlays || overlays.length < 1) return sourceSchema;
85
120
  let mergedSchema = sourceSchema;
86
121
  overlays.forEach((overlay) => {
87
- const overlaySchema = overlaysMap[schemaId][overlay] || {};
122
+ // Handle both string keys and direct overlay objects
123
+ const overlaySchema =
124
+ typeof overlay === "string"
125
+ ? overlaysMap[schemaId][overlay] || {}
126
+ : overlay;
127
+
88
128
  mergedSchema = merge(mergedSchema, overlaySchema, {
89
129
  customMerge: (key) => {
90
130
  if (key === "enum") {
@@ -97,6 +137,23 @@ const getTransactionSchema = (
97
137
  return mergedSchema;
98
138
  };
99
139
 
140
+ // Add helper function to generate cache key
141
+ const generateOverlayKey = (overlays) => {
142
+ if (!overlays) return "";
143
+ return overlays
144
+ .map((overlay) => {
145
+ if (typeof overlay === "string") return overlay;
146
+ // Generate a simple hash of the object for caching
147
+ return JSON.stringify(overlay)
148
+ .split("")
149
+ .reduce((hash, char) => {
150
+ return ((hash << 5) - hash + char.charCodeAt(0)) | 0;
151
+ }, 0)
152
+ .toString(36);
153
+ })
154
+ .join(".");
155
+ };
156
+
100
157
  const getValidator = (schemaId, overlays) => {
101
158
  return getSubschemaValidator("", schemaId, overlays);
102
159
  };
@@ -135,7 +192,7 @@ const isPathValid = (path, schemaId, overlays) => {
135
192
 
136
193
  const getSubschemaValidator = (path, schemaId, overlays) => {
137
194
  const subSchema = getSubschema(path, schemaId, overlays);
138
- const overlayKey = (overlays || []).join(".");
195
+ const overlayKey = generateOverlayKey(overlays);
139
196
  // see if we can retrieve the schema by path, schemaId and overlays
140
197
  const cacheKey = `${path}-${schemaId}-${overlayKey}`;
141
198
  let validator = ajv.getSchema(cacheKey);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pdtf/schemas",
3
- "version": "3.2.1-14",
3
+ "version": "3.2.1-16",
4
4
  "description": "Property Data Trust Framework Schemas and Utilities",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -0,0 +1,55 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "$id": "https://trust.propdata.org.uk/schemas/v3/overlays/custom.json",
4
+ "properties": {
5
+ "propertyPack": {
6
+ "customRef": "JKW",
7
+ "type": "object",
8
+ "required": ["specialistIssues"],
9
+ "properties": {
10
+ "specialistIssues": {
11
+ "customRef": "JKW.7",
12
+ "required": ["japaneseKnotweed"],
13
+ "properties": {
14
+ "japaneseKnotweed": {
15
+ "customRef": "JKW.7.8",
16
+ "discriminator": {
17
+ "propertyName": "yesNo"
18
+ },
19
+ "oneOf": [
20
+ {
21
+ "properties": {
22
+ "yesNo": {
23
+ "enum": ["No", "Not known"]
24
+ }
25
+ }
26
+ },
27
+ {
28
+ "properties": {
29
+ "yesNo": {
30
+ "enum": ["Yes"]
31
+ },
32
+ "managementPlanInPlace": {
33
+ "customRef": "JKW.7.8.2"
34
+ },
35
+ "attachments": {
36
+ "customRef": "JKW.7.8.3"
37
+ }
38
+ },
39
+ "required": ["managementPlanInPlace", "attachments"]
40
+ }
41
+ ],
42
+ "required": ["yesNo"],
43
+ "title": "Is the property affected by Japanese knotweed?",
44
+ "properties": {
45
+ "yesNo": {
46
+ "customRef": "JKW.7.8.1"
47
+ }
48
+ }
49
+ }
50
+ }
51
+ }
52
+ }
53
+ }
54
+ }
55
+ }
@@ -0,0 +1,93 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "$id": "https://trust.propdata.org.uk/schemas/v3/overlays/ta7.json",
4
+ "properties": {
5
+ "propertyPack": {
6
+ "macRef": "MAC",
7
+ "required": ["ownership"],
8
+ "properties": {
9
+ "ownership": {
10
+ "macRef": "MAC.1",
11
+ "required": ["ownershipsToBeTransferred"],
12
+ "properties": {
13
+ "ownershipsToBeTransferred": {
14
+ "macRef": "MAC.1",
15
+ "items": {
16
+ "discriminator": {
17
+ "propertyName": "ownershipType"
18
+ },
19
+ "oneOf": [
20
+ {
21
+ "properties": {
22
+ "ownershipType": {
23
+ "enum": ["Leasehold"]
24
+ },
25
+ "leaseholdInformation": {
26
+ "required": ["contactDetails"],
27
+ "properties": {
28
+ "contactDetails": {
29
+ "macRef": "MAC.4.1",
30
+ "required": ["contacts"],
31
+ "properties": {
32
+ "contacts": {
33
+ "macRef": "MAC.4.1",
34
+ "required": ["managingAgent"],
35
+ "properties": {
36
+ "managingAgent": {
37
+ "macRef": "MAC.4.1c",
38
+ "required": ["contact"],
39
+ "properties": {
40
+ "contact": {
41
+ "macRef": "MAC.4.1c",
42
+ "required": ["nameOrOrganisation"],
43
+ "properties": {
44
+ "nameOrOrganisation": {
45
+ "macRef": "MAC.4.1c.1"
46
+ },
47
+ "address": {
48
+ "macRef": "MAC.4.1c.0",
49
+ "properties": {
50
+ "line1": {
51
+ "macRef": "MAC.4.1c.2"
52
+ },
53
+ "line2": {
54
+ "macRef": "MAC.4.1c.3"
55
+ },
56
+ "line3": {
57
+ "macRef": "MAC.4.1c.4"
58
+ },
59
+ "town": {
60
+ "macRef": "MAC.4.1c.5"
61
+ },
62
+ "postcode": {
63
+ "macRef": "MAC.4.1c.6"
64
+ }
65
+ }
66
+ },
67
+ "telephone": {
68
+ "macRef": "MAC.4.1c.7"
69
+ },
70
+ "emailAddress": {
71
+ "macRef": "MAC.4.1c.8"
72
+ }
73
+ }
74
+ }
75
+ }
76
+ }
77
+ }
78
+ }
79
+ }
80
+ }
81
+ }
82
+ }
83
+ }
84
+ }
85
+ ]
86
+ }
87
+ }
88
+ }
89
+ }
90
+ }
91
+ }
92
+ }
93
+ }
@@ -1377,7 +1377,9 @@
1377
1377
  "Trunk road/motorway",
1378
1378
  "Airport",
1379
1379
  "Helipad",
1380
- "Other"
1380
+ "Other",
1381
+ "Tramway",
1382
+ "Underground/Subway"
1381
1383
  ]
1382
1384
  },
1383
1385
  "latitude": {
@@ -3954,6 +3956,7 @@
3954
3956
  }
3955
3957
  }
3956
3958
  },
3959
+ "ntsRequired": ["managedFreeholdOrCommonholdInformation"],
3957
3960
  "fme1Required": ["managedFreeholdOrCommonholdInformation"],
3958
3961
  "baspi4Required": [
3959
3962
  "managedFreeholdOrCommonholdInformation"
@@ -8954,6 +8957,7 @@
8954
8957
  }
8955
8958
  }
8956
8959
  },
8960
+ "ntsRequired": ["leaseholdInformation"],
8957
8961
  "lpe1Required": ["leaseholdInformation"],
8958
8962
  "baspi4Required": ["leaseholdInformation"],
8959
8963
  "baspi5Required": ["leaseholdInformation"],
@@ -275,7 +275,10 @@
275
275
  }
276
276
  }
277
277
  }
278
- }
278
+ },
279
+ "required": [
280
+ "managedFreeholdOrCommonholdInformation"
281
+ ]
279
282
  },
280
283
  {
281
284
  "properties": {
@@ -524,7 +527,10 @@
524
527
  }
525
528
  }
526
529
  }
527
- }
530
+ },
531
+ "required": [
532
+ "leaseholdInformation"
533
+ ]
528
534
  },
529
535
  {
530
536
  "properties": {
@@ -1095,7 +1095,9 @@
1095
1095
  "Trunk road/motorway",
1096
1096
  "Airport",
1097
1097
  "Helipad",
1098
- "Other"
1098
+ "Other",
1099
+ "Tramway",
1100
+ "Underground/Subway"
1099
1101
  ]
1100
1102
  },
1101
1103
  "latitude": {
@@ -8,6 +8,7 @@ const {
8
8
  getValidator,
9
9
  getSubschemaValidator,
10
10
  getTitleAtPath,
11
+ overlaysMap,
11
12
  } = require("../../../index.js");
12
13
 
13
14
  const exampleTransaction = require("../../examples/v3/exampleTransaction.json");
@@ -90,6 +91,18 @@ test("sample with missing dependent required fields is invalid", () => {
90
91
  expect(isValid).toBe(false);
91
92
  });
92
93
 
94
+ test("sample with missing dependent required fields with NTS overlay is invalid", () => {
95
+ // Previously a required field was missing from the NTS overlay
96
+ const clonedExampleTransaction = JSON.parse(
97
+ JSON.stringify(exampleTransaction)
98
+ );
99
+ clonedExampleTransaction.propertyPack.ownership.ownershipsToBeTransferred[0].leaseholdInformation =
100
+ undefined;
101
+ const validator = getValidator(schemaId, ["nts2023"]);
102
+ const isValid = validator(clonedExampleTransaction);
103
+ expect(isValid).toBe(false);
104
+ });
105
+
93
106
  test("correctly gets a top level subschema", () => {
94
107
  const subschema = getSubschema("/propertyPack");
95
108
  expect(subschema.title).toBe("Property Pack");
@@ -403,3 +416,227 @@ test("validates a valid contract", () => {
403
416
  const isValid = validator(data);
404
417
  expect(isValid).toBe(true);
405
418
  });
419
+
420
+ test("overlay as key and as object produce identical results", () => {
421
+ // Get the NTS overlay object directly from the overlays map
422
+ const ntsOverlayObject = { ...overlaysMap[schemaId]["nts2023"] };
423
+ // Remove the $id to prevent schema registration conflicts
424
+ ntsOverlayObject.$id =
425
+ "https://trust.propdata.org.uk/schemas/v3/overlays/nts2024.json";
426
+
427
+ // Get schema using the key
428
+ const schemaWithKey = getTransactionSchema(schemaId, ["nts2023"]);
429
+
430
+ // Get schema using the object
431
+ const schemaWithObject = getTransactionSchema(schemaId, [ntsOverlayObject]);
432
+
433
+ // Create copies without $id for comparison
434
+ const schemaWithKeyNoId = { ...schemaWithKey };
435
+ const schemaWithObjectNoId = { ...schemaWithObject };
436
+ delete schemaWithKeyNoId.$id;
437
+ delete schemaWithObjectNoId.$id;
438
+
439
+ // Deep compare the results (excluding $id)
440
+ expect(schemaWithObjectNoId).toEqual(schemaWithKeyNoId);
441
+
442
+ // Test that validation produces the same errors
443
+ const validatorWithKey = getValidator(schemaId, ["nts2023"]);
444
+ const validatorWithObject = getValidator(schemaId, [ntsOverlayObject]);
445
+
446
+ validatorWithKey(exampleTransaction);
447
+ validatorWithObject(exampleTransaction);
448
+
449
+ // Compare validation errors
450
+ expect(validatorWithObject.errors).toEqual(validatorWithKey.errors);
451
+ });
452
+
453
+ test("correctly merges custom Japanese Knotweed overlay with base schema", () => {
454
+ // Load the custom overlay
455
+ const customOverlay = require("../../examples/v3/exampleCustomOverlay.json");
456
+
457
+ // Get base schema and schema with overlay
458
+ const baseSchema = getTransactionSchema(schemaId);
459
+ const schemaWithOverlay = getTransactionSchema(schemaId, [customOverlay]);
460
+
461
+ // Verify that other properties in propertyPack are preserved
462
+ expect(
463
+ Object.keys(schemaWithOverlay.properties.propertyPack.properties)
464
+ ).toEqual(Object.keys(baseSchema.properties.propertyPack.properties));
465
+
466
+ // Verify that other properties in specialistIssues are preserved
467
+ const baseSpecialistIssues =
468
+ baseSchema.properties.propertyPack.properties.specialistIssues;
469
+ const overlaidSpecialistIssues =
470
+ schemaWithOverlay.properties.propertyPack.properties.specialistIssues;
471
+ expect(Object.keys(overlaidSpecialistIssues.properties)).toEqual(
472
+ Object.keys(baseSpecialistIssues.properties)
473
+ );
474
+
475
+ // Verify that Japanese Knotweed section is updated
476
+ const japaneseKnotweed = overlaidSpecialistIssues.properties.japaneseKnotweed;
477
+ expect(japaneseKnotweed.title).toBe(
478
+ "Is the property affected by Japanese knotweed?"
479
+ );
480
+ expect(japaneseKnotweed.required).toEqual(["yesNo"]);
481
+
482
+ // Verify the oneOf conditions are updated
483
+ expect(japaneseKnotweed.oneOf).toHaveLength(2);
484
+ expect(japaneseKnotweed.oneOf[0].properties.yesNo.enum).toEqual([
485
+ "No",
486
+ "Not known",
487
+ ]);
488
+ expect(japaneseKnotweed.oneOf[1].properties.yesNo.enum).toEqual(["Yes"]);
489
+ expect(japaneseKnotweed.oneOf[1].required).toEqual([
490
+ "managementPlanInPlace",
491
+ "attachments",
492
+ ]);
493
+ });
494
+
495
+ test("correctly merges multiple overlays using both key and object references", () => {
496
+ // Load the custom overlay
497
+ const customOverlay = require("../../examples/v3/exampleCustomOverlay.json");
498
+
499
+ // Get schema with both overlays
500
+ const schemaWithOverlays = getTransactionSchema(schemaId, [
501
+ "nts2023",
502
+ customOverlay,
503
+ ]);
504
+
505
+ // Get individual overlay schemas for comparison
506
+ const ntsSchema = getTransactionSchema(schemaId, ["nts2023"]);
507
+ const customSchema = getTransactionSchema(schemaId, [customOverlay]);
508
+
509
+ // Verify propertyPack structure is preserved
510
+ const propertyPack = schemaWithOverlays.properties.propertyPack;
511
+ expect(Object.keys(propertyPack.properties)).toEqual(
512
+ expect.arrayContaining(
513
+ Object.keys(ntsSchema.properties.propertyPack.properties)
514
+ )
515
+ );
516
+
517
+ // Verify Japanese Knotweed changes from custom overlay are present
518
+ const japaneseKnotweed =
519
+ propertyPack.properties.specialistIssues.properties.japaneseKnotweed;
520
+ expect(japaneseKnotweed.title).toBe(
521
+ "Is the property affected by Japanese knotweed?"
522
+ );
523
+ expect(japaneseKnotweed.oneOf[1].required).toEqual([
524
+ "managementPlanInPlace",
525
+ "attachments",
526
+ ]);
527
+
528
+ // Verify customRef properties are correctly merged
529
+ expect(propertyPack.customRef).toBe("JKW");
530
+ expect(propertyPack.properties.specialistIssues.customRef).toBe("JKW.7");
531
+ expect(japaneseKnotweed.customRef).toBe("JKW.7.8");
532
+ expect(
533
+ japaneseKnotweed.oneOf[1].properties.managementPlanInPlace.customRef
534
+ ).toBe("JKW.7.8.2");
535
+ expect(japaneseKnotweed.oneOf[1].properties.attachments.customRef).toBe(
536
+ "JKW.7.8.3"
537
+ );
538
+ expect(japaneseKnotweed.properties.yesNo.customRef).toBe("JKW.7.8.1");
539
+
540
+ // Verify NTS specific changes are present (e.g., simplified enums)
541
+ const mainsElectricity =
542
+ propertyPack.properties.electricity.properties.mainsElectricity;
543
+ expect(mainsElectricity.properties.yesNo.enum).toEqual(["Yes", "No"]);
544
+
545
+ // Verify required fields from both overlays are merged
546
+ expect(propertyPack.required).toEqual(
547
+ expect.arrayContaining([
548
+ ...ntsSchema.properties.propertyPack.required,
549
+ ...customSchema.properties.propertyPack.required,
550
+ ])
551
+ );
552
+ });
553
+
554
+ test("correctly merges NTS and both custom overlays with proper required fields", () => {
555
+ // Load both custom overlays
556
+ const japaneseKnoweedOverlay = require("../../examples/v3/exampleCustomOverlay.json");
557
+ const managingAgentOverlay = require("../../examples/v3/exampleCustomOverlay2.json");
558
+
559
+ // Get schema with all overlays
560
+ const schemaWithOverlays = getTransactionSchema(schemaId, [
561
+ "nts2023",
562
+ japaneseKnoweedOverlay,
563
+ managingAgentOverlay,
564
+ ]);
565
+
566
+ // Navigate to the leasehold section in the oneOf array
567
+ const leaseholdSchema =
568
+ schemaWithOverlays.properties.propertyPack.properties.ownership.properties.ownershipsToBeTransferred.items.oneOf.find(
569
+ (schema) => schema.properties.ownershipType.enum[0] === "Leasehold"
570
+ );
571
+
572
+ // Verify the leasehold information structure
573
+ const leaseholdInfo = leaseholdSchema.properties.leaseholdInformation;
574
+
575
+ // Check required fields - should include both NTS and custom overlay requirements
576
+ expect(leaseholdInfo.required).toContain("contactDetails");
577
+ expect(leaseholdInfo.required).toContain("leaseTerm"); // from NTS
578
+ expect(leaseholdInfo.required).toContain("groundRent"); // from NTS
579
+
580
+ // Verify managing agent contact structure and refs
581
+ const managingAgent =
582
+ leaseholdInfo.properties.contactDetails.properties.contacts.properties
583
+ .managingAgent;
584
+ expect(managingAgent.macRef).toBe("MAC.4.1c");
585
+
586
+ // Verify contact details structure
587
+ const contact = managingAgent.properties.contact;
588
+ expect(contact.properties.nameOrOrganisation.macRef).toBe("MAC.4.1c.1");
589
+ expect(contact.properties.address.macRef).toBe("MAC.4.1c.0");
590
+ expect(contact.properties.telephone.macRef).toBe("MAC.4.1c.7");
591
+ expect(contact.properties.emailAddress.macRef).toBe("MAC.4.1c.8");
592
+
593
+ // Verify Japanese Knotweed section is still present and correct
594
+ const japaneseKnotweed =
595
+ schemaWithOverlays.properties.propertyPack.properties.specialistIssues
596
+ .properties.japaneseKnotweed;
597
+ expect(japaneseKnotweed.customRef).toBe("JKW.7.8");
598
+
599
+ // Verify NTS changes are still present
600
+ const mainsElectricity =
601
+ schemaWithOverlays.properties.propertyPack.properties.electricity.properties
602
+ .mainsElectricity;
603
+ expect(mainsElectricity.properties.yesNo.enum).toEqual(["Yes", "No"]);
604
+ });
605
+
606
+ test("ta7 overlay as key and as object produce identical results at leasehold info level", () => {
607
+ // Get the TA7 overlay object directly from the overlays map
608
+ const ta7OverlayObject = { ...overlaysMap[schemaId]["ta7ed3"] };
609
+ // Remove the $id to prevent schema registration conflicts
610
+ ta7OverlayObject.$id =
611
+ "https://trust.propdata.org.uk/schemas/v3/overlays/ta7ed3-copy.json";
612
+
613
+ // Get schemas using both methods
614
+ const schemaWithKey = getTransactionSchema(schemaId, ["ta7ed3"]);
615
+ const schemaWithObject = getTransactionSchema(schemaId, [ta7OverlayObject]);
616
+
617
+ // Navigate to the leasehold information section in both schemas
618
+ const getLeaseholdInfo = (schema) => {
619
+ const ownershipsToBeTransferred =
620
+ schema.properties.propertyPack.properties.ownership.properties
621
+ .ownershipsToBeTransferred;
622
+ const leaseholdSchema = ownershipsToBeTransferred.items.oneOf.find(
623
+ (schema) => schema.properties.ownershipType.enum[0] === "Leasehold"
624
+ );
625
+ return leaseholdSchema.properties.leaseholdInformation;
626
+ };
627
+
628
+ const leaseholdInfoWithKey = getLeaseholdInfo(schemaWithKey);
629
+ const leaseholdInfoWithObject = getLeaseholdInfo(schemaWithObject);
630
+
631
+ // Deep compare the leasehold information sections
632
+ expect(leaseholdInfoWithObject).toEqual(leaseholdInfoWithKey);
633
+
634
+ // Verify specific TA7 requirements are present in both
635
+ expect(leaseholdInfoWithKey.required).toContain("contactDetails");
636
+ expect(leaseholdInfoWithObject.required).toContain("contactDetails");
637
+
638
+ // Verify TA7 refs are present and identical
639
+ const contactDetails = leaseholdInfoWithKey.properties.contactDetails;
640
+ expect(contactDetails.ta7Ref).toBe("4.1");
641
+ expect(leaseholdInfoWithObject.properties.contactDetails.ta7Ref).toBe("4.1");
642
+ });