docusaurus-plugin-generate-schema-docs 1.2.0 → 1.3.0

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 (81) hide show
  1. package/README.md +42 -6
  2. package/__tests__/ExampleDataLayer.test.js +78 -155
  3. package/__tests__/__fixtures__/static/schemas/add-to-cart-event.json +4 -15
  4. package/__tests__/__fixtures__/static/schemas/choice-event.json +72 -0
  5. package/__tests__/__fixtures__/static/schemas/components/dataLayer.json +52 -54
  6. package/__tests__/__fixtures__/static/schemas/components/product.json +124 -210
  7. package/__tests__/__fixtures__/static/schemas/nested/child-event.json +10 -0
  8. package/__tests__/__fixtures__/static/schemas/nested/grandchild-a.json +9 -0
  9. package/__tests__/__fixtures__/static/schemas/nested/grandchild-b.json +9 -0
  10. package/__tests__/__fixtures__/static/schemas/nested/parent-event.json +7 -0
  11. package/__tests__/__fixtures__/static/schemas/root-any-of-event.json +34 -0
  12. package/__tests__/__fixtures__/static/schemas/root-choice-event.json +36 -0
  13. package/__tests__/__fixtures__/validateSchemas/circular-schema.json +6 -6
  14. package/__tests__/__fixtures__/validateSchemas/components/referenced.json +9 -7
  15. package/__tests__/__fixtures__/validateSchemas/invalid-example-schema.json +7 -7
  16. package/__tests__/__fixtures__/validateSchemas/main-schema-with-missing-ref.json +7 -7
  17. package/__tests__/__fixtures__/validateSchemas/main-schema-with-ref.json +7 -7
  18. package/__tests__/__fixtures__/validateSchemas/no-example-schema.json +11 -11
  19. package/__tests__/__fixtures__/validateSchemas/schema-A.json +5 -5
  20. package/__tests__/__fixtures__/validateSchemas/schema-B.json +5 -5
  21. package/__tests__/__fixtures__/validateSchemas/valid-schema.json +7 -7
  22. package/__tests__/__fixtures_versioned__/static/schemas/1.1.1/add-to-cart-event.json +44 -0
  23. package/__tests__/__fixtures_versioned__/static/schemas/1.1.1/components/dataLayer.json +56 -0
  24. package/__tests__/__fixtures_versioned__/static/schemas/1.1.1/components/product.json +125 -0
  25. package/__tests__/__fixtures_versioned__/static/schemas/next/add-to-cart-event.json +44 -0
  26. package/__tests__/__fixtures_versioned__/static/schemas/next/components/dataLayer.json +56 -0
  27. package/__tests__/__fixtures_versioned__/static/schemas/next/components/product.json +125 -0
  28. package/__tests__/__fixtures_versioned__/versions.json +1 -0
  29. package/__tests__/__snapshots__/ExampleDataLayer.test.js.snap +117 -0
  30. package/__tests__/__snapshots__/generateEventDocs.nested.test.js.snap +92 -0
  31. package/__tests__/__snapshots__/generateEventDocs.test.js.snap +113 -15
  32. package/__tests__/__snapshots__/generateEventDocs.versioned.test.js.snap +53 -0
  33. package/__tests__/components/FoldableRows.test.js +330 -0
  34. package/__tests__/components/PropertiesTable.test.js +31 -14
  35. package/__tests__/components/PropertyRow.test.js +471 -51
  36. package/__tests__/components/SchemaJsonViewer.test.js +23 -19
  37. package/__tests__/components/SchemaRows.test.js +96 -66
  38. package/__tests__/components/SchemaViewer.test.js +34 -17
  39. package/__tests__/components/TableHeader.test.js +12 -12
  40. package/__tests__/generateEventDocs.nested.test.js +80 -0
  41. package/__tests__/generateEventDocs.test.js +77 -71
  42. package/__tests__/generateEventDocs.versioned.test.js +69 -0
  43. package/__tests__/helpers/buildExampleFromSchema.test.js +160 -160
  44. package/__tests__/helpers/file-system.test.js +44 -0
  45. package/__tests__/helpers/getConstraints.test.js +48 -48
  46. package/__tests__/helpers/loadSchema.test.js +11 -5
  47. package/__tests__/helpers/path-helpers.test.js +34 -0
  48. package/__tests__/helpers/processSchema.test.js +42 -22
  49. package/__tests__/helpers/schema-processing.test.js +82 -0
  50. package/__tests__/helpers/schemaToExamples.test.js +56 -0
  51. package/__tests__/helpers/schemaToTableData.filtering.test.js +65 -0
  52. package/__tests__/helpers/schemaToTableData.hierarchicalLines.test.js +539 -0
  53. package/__tests__/helpers/schemaToTableData.test.js +222 -0
  54. package/__tests__/helpers/update-schema-ids.test.js +107 -0
  55. package/__tests__/update-schema-ids.test.js +39 -0
  56. package/__tests__/validateSchemas.test.js +125 -88
  57. package/components/ExampleDataLayer.js +59 -28
  58. package/components/FoldableRows.js +164 -0
  59. package/components/PropertiesTable.js +10 -7
  60. package/components/PropertyRow.js +169 -60
  61. package/components/SchemaJsonViewer.js +6 -6
  62. package/components/SchemaRows.css +236 -14
  63. package/components/SchemaRows.js +24 -41
  64. package/components/SchemaViewer.js +19 -13
  65. package/components/TableHeader.js +12 -12
  66. package/generateEventDocs.js +141 -61
  67. package/helpers/buildExampleFromSchema.js +58 -72
  68. package/helpers/choice-index-template.js +22 -0
  69. package/helpers/file-system.js +32 -0
  70. package/helpers/getConstraints.js +43 -44
  71. package/helpers/loadSchema.js +2 -2
  72. package/helpers/path-helpers.js +22 -0
  73. package/helpers/processSchema.js +19 -19
  74. package/helpers/{mdx-template.js → schema-doc-template.js} +12 -12
  75. package/helpers/schema-processing.js +75 -0
  76. package/helpers/schemaToExamples.js +99 -0
  77. package/helpers/schemaToTableData.js +311 -0
  78. package/helpers/update-schema-ids.js +47 -0
  79. package/index.js +143 -54
  80. package/package.json +1 -1
  81. package/validateSchemas.js +54 -71
@@ -3,87 +3,70 @@ import path from 'path';
3
3
  import buildExampleFromSchema from './helpers/buildExampleFromSchema';
4
4
  import Ajv2020 from 'ajv/dist/2020.js';
5
5
  import addFormats from 'ajv-formats';
6
- import $RefParser from "@apidevtools/json-schema-ref-parser";
7
- import mergeJsonSchema from "json-schema-merge-allof";
6
+ import processSchema from './helpers/processSchema';
8
7
 
9
8
  const validateSchemas = async (schemaPath) => {
10
- const ajv = new Ajv2020();
11
- addFormats(ajv);
12
- ajv.addKeyword('x-gtm-clear');
9
+ const ajv = new Ajv2020();
10
+ addFormats(ajv);
11
+ ajv.addKeyword('x-gtm-clear');
13
12
 
14
- const getAllFiles = (dir, allFiles = []) => {
15
- const files = fs.readdirSync(dir);
13
+ const getAllFiles = (dir, allFiles = []) => {
14
+ const files = fs.readdirSync(dir);
16
15
 
17
- files.forEach(file => {
18
- const filePath = path.join(dir, file);
19
- if (fs.statSync(filePath).isDirectory())
20
- {
21
- getAllFiles(filePath, allFiles);
22
- } else
23
- {
24
- if (file.endsWith('.json'))
25
- {
26
- allFiles.push(filePath);
27
- }
28
- }
29
- });
30
- return allFiles;
31
- };
16
+ files.forEach((file) => {
17
+ const filePath = path.join(dir, file);
18
+ if (fs.statSync(filePath).isDirectory()) {
19
+ getAllFiles(filePath, allFiles);
20
+ } else {
21
+ if (file.endsWith('.json')) {
22
+ allFiles.push(filePath);
23
+ }
24
+ }
25
+ });
26
+ return allFiles;
27
+ };
32
28
 
33
- const allSchemaFiles = getAllFiles(schemaPath);
34
- for (const file of allSchemaFiles)
35
- {
36
- const schemaContent = fs.readFileSync(file, 'utf-8');
37
- const schema = JSON.parse(schemaContent);
38
- ajv.addSchema(schema);
39
- }
29
+ const allSchemaFiles = getAllFiles(schemaPath);
30
+ for (const file of allSchemaFiles) {
31
+ const schemaContent = fs.readFileSync(file, 'utf-8');
32
+ const schema = JSON.parse(schemaContent);
33
+ ajv.addSchema(schema);
34
+ }
40
35
 
41
- const schemaFiles = fs.readdirSync(schemaPath).filter(file => file.endsWith('.json'));
42
- let allValid = true;
43
- for (const file of schemaFiles)
44
- {
45
- const filePath = path.join(schemaPath, file);
36
+ const schemaFiles = fs
37
+ .readdirSync(schemaPath)
38
+ .filter((file) => file.endsWith('.json'));
39
+ let allValid = true;
40
+ for (const file of schemaFiles) {
41
+ const filePath = path.join(schemaPath, file);
46
42
 
47
- // Dereference and merge, same as in generateEventDocs.js
48
- const clonedSchema = await $RefParser.dereference(filePath, {
49
- mutateInputSchema: false, dereference: {
50
- circular: 'ignore'
51
- }
52
- });
53
- const mergedSchema = mergeJsonSchema(clonedSchema, {
54
- resolvers: {
55
- defaultResolver: mergeJsonSchema.options.resolvers.title
56
- }
57
- });
43
+ const mergedSchema = await processSchema(filePath);
58
44
 
59
- const example_data = buildExampleFromSchema(mergedSchema);
45
+ const example_data = buildExampleFromSchema(mergedSchema);
60
46
 
61
- if (!example_data)
62
- {
63
- console.error(`❌ Schema ${file} does not produce a valid example.`);
64
- allValid = false;
65
- } else
66
- {
67
- const originalSchema = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
68
- const validate = ajv.getSchema(originalSchema.$id);
69
- if (!validate)
70
- {
71
- console.error(`❌ Could not find compiled schema for ${originalSchema.$id}`);
72
- allValid = false;
73
- continue;
74
- }
75
- if (validate(example_data))
76
- {
77
- console.log(`✅ Schema ${file} produced a valid example.`);
78
- } else
79
- {
80
- console.error(`❌ Schema ${file} example data failed validation:`);
81
- console.error(validate.errors);
82
- allValid = false;
83
- }
84
- }
47
+ if (!example_data) {
48
+ console.error(`x Schema ${file} does not produce a valid example.`);
49
+ allValid = false;
50
+ } else {
51
+ const originalSchema = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
52
+ const validate = ajv.getSchema(originalSchema.$id);
53
+ if (!validate) {
54
+ console.error(
55
+ `x Could not find compiled schema for ${originalSchema.$id}`,
56
+ );
57
+ allValid = false;
58
+ continue;
59
+ }
60
+ if (validate(example_data)) {
61
+ console.log(`OK Schema ${file} produced a valid example.`);
62
+ } else {
63
+ console.error(`x Schema ${file} example data failed validation:`);
64
+ console.error(validate.errors);
65
+ allValid = false;
66
+ }
85
67
  }
86
- return allValid;
68
+ }
69
+ return allValid;
87
70
  };
88
71
 
89
72
  export default validateSchemas;