@schalkneethling/miyagi-core 4.4.0 → 4.4.2

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.
@@ -1,5 +1,6 @@
1
1
  // @ts-check
2
2
 
3
+ import path from "path";
3
4
  import deepMerge from "deepmerge";
4
5
 
5
6
  const DEFAULT_SCHEMA_VALIDATION_MODE = "collect-all";
@@ -18,6 +19,38 @@ export function getSchemaValidationMode() {
18
19
  return mode;
19
20
  }
20
21
 
22
+ /**
23
+ * Resolves global schema definitions from the root of the components folder.
24
+ * The file must have the same name as configured in `files.schema` (JSON or YAML)
25
+ * and contain an array of JSON Schema definition objects.
26
+ * @returns {Array<{schema: object, globalSchemaFile: string}>}
27
+ */
28
+ function loadGlobalSchemas() {
29
+ const { components, files } = global.config;
30
+
31
+ if (!components?.folder || !files?.schema) {
32
+ return [];
33
+ }
34
+
35
+ const schemaFileName = `${files.schema.name}.${files.schema.extension}`;
36
+ const globalSchemaFile = path.join(
37
+ process.cwd(),
38
+ components.folder,
39
+ schemaFileName,
40
+ );
41
+
42
+ const content = global.state?.fileContents?.[globalSchemaFile];
43
+ if (!content) {
44
+ return [];
45
+ }
46
+
47
+ const defs = Array.isArray(content) ? content : [content];
48
+
49
+ return defs
50
+ .filter((def) => def && typeof def === "object")
51
+ .map((schema) => ({ schema: structuredClone(schema), globalSchemaFile }));
52
+ }
53
+
21
54
  /**
22
55
  * @param {object} options
23
56
  * @param {Array<object>} [options.components]
@@ -38,6 +71,31 @@ export function validateSchemas({ components } = {}) {
38
71
  ),
39
72
  );
40
73
 
74
+ // Register global schema definitions before compiling component schemas so
75
+ // that component $ref entries targeting global $id values can resolve.
76
+ for (const [index, { schema, globalSchemaFile }] of loadGlobalSchemas().entries()) {
77
+ if (!schema.$id) {
78
+ schema.$id = `miyagi-global:${index}`;
79
+ }
80
+ try {
81
+ validator.addSchema(schema);
82
+ validSchemas.push({
83
+ component: "$global",
84
+ schemaFile: globalSchemaFile,
85
+ schema,
86
+ });
87
+ } catch (error) {
88
+ errors.push(
89
+ buildSchemaValidationError({
90
+ error,
91
+ component: "$global",
92
+ schemaFile: globalSchemaFile,
93
+ rawSchema: schema,
94
+ }),
95
+ );
96
+ }
97
+ }
98
+
41
99
  let pendingSchemas = componentRoutes
42
100
  .map((component, index) => {
43
101
  // Absolute schema file path.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@schalkneethling/miyagi-core",
3
- "version": "4.4.0",
3
+ "version": "4.4.2",
4
4
  "description": "miyagi is a component development tool for JavaScript template engines.",
5
5
  "main": "index.js",
6
6
  "author": "Schalk Neethling <schalkneethling@duck.com>, Michael Großklaus <mail@mgrossklaus.de> (https://www.mgrossklaus.de)",