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
@@ -0,0 +1,82 @@
1
+ import { processOneOfSchema } from '../../helpers/schema-processing';
2
+ import path from 'path';
3
+
4
+ describe('schema-processing', () => {
5
+ describe('processOneOfSchema', () => {
6
+ it('should merge oneOf options with the root schema', async () => {
7
+ const rootSchema = {
8
+ title: 'Root',
9
+ description: 'Root description',
10
+ oneOf: [
11
+ {
12
+ title: 'Option 1',
13
+ description: 'Description for Option 1',
14
+ properties: {
15
+ prop1: { type: 'string' },
16
+ },
17
+ },
18
+ {
19
+ title: 'Option 2',
20
+ properties: {
21
+ prop2: { type: 'number' },
22
+ },
23
+ },
24
+ ],
25
+ properties: {
26
+ commonProp: { type: 'boolean' },
27
+ },
28
+ };
29
+
30
+ const filePath = '/path/to/schema.json';
31
+ const result = await processOneOfSchema(rootSchema, filePath);
32
+
33
+ expect(result).toHaveLength(2);
34
+
35
+ // Check the first merged schema
36
+ expect(result[0].schema.title).toBe('Option 1');
37
+ expect(result[0].schema.description).toBe('Description for Option 1');
38
+ expect(result[0].schema.properties.commonProp).toEqual({
39
+ type: 'boolean',
40
+ });
41
+ expect(result[0].schema.properties.prop1).toEqual({ type: 'string' });
42
+ expect(result[0].schema.oneOf).toBeUndefined();
43
+
44
+ // Check the second merged schema
45
+ expect(result[1].schema.title).toBe('Option 2');
46
+ expect(result[1].schema.description).toBe('Root description'); // Fallback
47
+ expect(result[1].schema.properties.prop2).toEqual({ type: 'number' });
48
+ expect(result[1].slug).toBe('option-2');
49
+ });
50
+
51
+ it('should use option $id when available', async () => {
52
+ const rootSchema = {
53
+ $id: 'root.json',
54
+ title: 'Root',
55
+ oneOf: [
56
+ {
57
+ $id: 'option1.json',
58
+ title: 'Option 1',
59
+ },
60
+ ],
61
+ };
62
+ const filePath = '/path/to/schema.json';
63
+ const result = await processOneOfSchema(rootSchema, filePath);
64
+ expect(result[0].schema.$id).toBe('option1.json');
65
+ });
66
+
67
+ it('should generate a new $id when option $id is not available', async () => {
68
+ const rootSchema = {
69
+ $id: 'root.json',
70
+ title: 'Root',
71
+ oneOf: [
72
+ {
73
+ title: 'Option 1',
74
+ },
75
+ ],
76
+ };
77
+ const filePath = '/path/to/schema.json';
78
+ const result = await processOneOfSchema(rootSchema, filePath);
79
+ expect(result[0].schema.$id).toBe('root.json#option-1');
80
+ });
81
+ });
82
+ });
@@ -0,0 +1,56 @@
1
+ import { schemaToExamples } from '../../helpers/schemaToExamples';
2
+ import choiceEventSchema from '../__fixtures__/static/schemas/choice-event.json';
3
+
4
+ describe('schemaToExamples', () => {
5
+ it('should generate examples for all options in a complex schema', () => {
6
+ const exampleGroups = schemaToExamples(choiceEventSchema);
7
+
8
+ // Should find two choice points: user_id and payment_method
9
+ expect(exampleGroups).toHaveLength(2);
10
+
11
+ // Find the user_id group
12
+ const userIdGroup = exampleGroups.find(
13
+ (group) => group.property === 'user_id',
14
+ );
15
+ expect(userIdGroup).toBeDefined();
16
+ expect(userIdGroup.options).toHaveLength(2); // String and Integer
17
+
18
+ // Find the payment_method group
19
+ const paymentMethodGroup = exampleGroups.find(
20
+ (group) => group.property === 'payment_method',
21
+ );
22
+ expect(paymentMethodGroup).toBeDefined();
23
+ expect(paymentMethodGroup.options).toHaveLength(2); // Credit Card and PayPal
24
+
25
+ // Check the Credit Card example
26
+ const creditCardOption = paymentMethodGroup.options.find(
27
+ (opt) => opt.title === 'Credit Card',
28
+ );
29
+ expect(creditCardOption).toBeDefined();
30
+ expect(creditCardOption.example).toHaveProperty('payment_method');
31
+ expect(creditCardOption.example.payment_method).toHaveProperty(
32
+ 'card_number',
33
+ );
34
+
35
+ // Check the PayPal example
36
+ const payPalOption = paymentMethodGroup.options.find(
37
+ (opt) => opt.title === 'PayPal',
38
+ );
39
+ expect(payPalOption).toBeDefined();
40
+ expect(payPalOption.example).toHaveProperty('payment_method');
41
+ expect(payPalOption.example.payment_method).toHaveProperty('email');
42
+
43
+ // Check the user_id examples
44
+ const userIdStringOption = userIdGroup.options.find(
45
+ (opt) => opt.title === 'User ID as String',
46
+ );
47
+ expect(userIdStringOption).toBeDefined();
48
+ expect(userIdStringOption.example).toHaveProperty('payment_method');
49
+
50
+ const userIdIntegerOption = userIdGroup.options.find(
51
+ (opt) => opt.title === 'User ID as Integer',
52
+ );
53
+ expect(userIdIntegerOption).toBeDefined();
54
+ expect(userIdIntegerOption.example).toHaveProperty('payment_method');
55
+ });
56
+ });
@@ -0,0 +1,65 @@
1
+ import { schemaToTableData } from '../../helpers/schemaToTableData';
2
+
3
+ describe('schemaToTableData filtering', () => {
4
+ it('filters out a property with x-gtm-clear that is effectively empty', () => {
5
+ const schema = {
6
+ properties: {
7
+ empty_object: {
8
+ 'x-gtm-clear': true,
9
+ type: 'object',
10
+ properties: {},
11
+ },
12
+ should_appear: {
13
+ type: 'string',
14
+ },
15
+ },
16
+ };
17
+
18
+ const tableData = schemaToTableData(schema);
19
+
20
+ expect(tableData).toHaveLength(1);
21
+ expect(tableData[0].name).toBe('should_appear');
22
+ });
23
+
24
+ it('does NOT filter a property with x-gtm-clear if it contains data', () => {
25
+ const schema = {
26
+ properties: {
27
+ non_empty_object: {
28
+ 'x-gtm-clear': true,
29
+ type: 'object',
30
+ properties: {
31
+ child_prop: {
32
+ type: 'string', // This is "data"
33
+ },
34
+ },
35
+ },
36
+ },
37
+ };
38
+
39
+ const tableData = schemaToTableData(schema);
40
+
41
+ // Both the parent and child should appear
42
+ expect(tableData).toHaveLength(2);
43
+ expect(tableData[0].name).toBe('non_empty_object');
44
+ expect(tableData[1].name).toBe('child_prop');
45
+ });
46
+
47
+ it('does NOT filter a property if x-gtm-clear is not exactly true', () => {
48
+ const schema = {
49
+ properties: {
50
+ empty_object_false: {
51
+ 'x-gtm-clear': false,
52
+ type: 'object',
53
+ properties: {},
54
+ },
55
+ empty_object_missing: {
56
+ type: 'object',
57
+ properties: {},
58
+ },
59
+ },
60
+ };
61
+
62
+ const tableData = schemaToTableData(schema);
63
+ expect(tableData).toHaveLength(2);
64
+ });
65
+ });