@unispechq/unispec-core 0.2.3 → 0.2.4
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/dist/cjs/validator/index.js +45 -7
- package/dist/validator/index.js +45 -7
- package/package.json +1 -1
|
@@ -8,6 +8,7 @@ exports.validateUniSpecTests = validateUniSpecTests;
|
|
|
8
8
|
const _2020_js_1 = __importDefault(require("ajv/dist/2020.js"));
|
|
9
9
|
const fs_1 = __importDefault(require("fs"));
|
|
10
10
|
const path_1 = __importDefault(require("path"));
|
|
11
|
+
const module_1 = require("module");
|
|
11
12
|
const unispec_schema_1 = require("@unispechq/unispec-schema");
|
|
12
13
|
const ajv = new _2020_js_1.default({
|
|
13
14
|
allErrors: true,
|
|
@@ -15,16 +16,53 @@ const ajv = new _2020_js_1.default({
|
|
|
15
16
|
});
|
|
16
17
|
// Register minimal URI format to satisfy UniSpec schemas (service.environments[*].baseUrl)
|
|
17
18
|
ajv.addFormat("uri", true);
|
|
19
|
+
function findPackageRoot(startFilePath) {
|
|
20
|
+
let dir = path_1.default.dirname(startFilePath);
|
|
21
|
+
for (let i = 0; i < 50; i++) {
|
|
22
|
+
const pkgJsonPath = path_1.default.join(dir, "package.json");
|
|
23
|
+
if (fs_1.default.existsSync(pkgJsonPath)) {
|
|
24
|
+
return dir;
|
|
25
|
+
}
|
|
26
|
+
const parent = path_1.default.dirname(dir);
|
|
27
|
+
if (parent === dir) {
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
dir = parent;
|
|
31
|
+
}
|
|
32
|
+
throw new Error(`Cannot locate package.json for resolved path: ${startFilePath}`);
|
|
33
|
+
}
|
|
34
|
+
function getUniSpecSchemaDir() {
|
|
35
|
+
const localRequire = typeof require !== "undefined" ? require : (0, module_1.createRequire)(path_1.default.join(process.cwd(), "package.json"));
|
|
36
|
+
try {
|
|
37
|
+
const pkgJsonPath = localRequire.resolve("@unispechq/unispec-schema/package.json");
|
|
38
|
+
return path_1.default.join(path_1.default.dirname(pkgJsonPath), "schema");
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
const entryPath = localRequire.resolve("@unispechq/unispec-schema");
|
|
42
|
+
const pkgRoot = findPackageRoot(entryPath);
|
|
43
|
+
return path_1.default.join(pkgRoot, "schema");
|
|
44
|
+
}
|
|
45
|
+
}
|
|
18
46
|
// Register all UniSpec subschemas so that Ajv can resolve internal $ref links
|
|
19
47
|
try {
|
|
20
|
-
const schemaDir =
|
|
48
|
+
const schemaDir = getUniSpecSchemaDir();
|
|
21
49
|
const types = unispec_schema_1.manifest?.types ?? {};
|
|
22
50
|
const typeSchemaPaths = Object.values(types).map((rel) => String(rel));
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
51
|
+
for (const relPath of typeSchemaPaths) {
|
|
52
|
+
try {
|
|
53
|
+
const filePath = path_1.default.join(schemaDir, relPath);
|
|
54
|
+
if (!fs_1.default.existsSync(filePath)) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const schema = JSON.parse(fs_1.default.readFileSync(filePath, "utf8"));
|
|
58
|
+
const normalizedRelPath = String(relPath).replace(/^\.\//, "");
|
|
59
|
+
const fallbackId = `https://unispec.dev/schema/${normalizedRelPath}`;
|
|
60
|
+
ajv.addSchema(schema, fallbackId);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
// Ignore individual schema registration failures to keep the validator usable.
|
|
64
|
+
}
|
|
65
|
+
}
|
|
28
66
|
}
|
|
29
67
|
catch {
|
|
30
68
|
// If subschemas cannot be loaded for some reason, validation will still work for
|
|
@@ -73,7 +111,7 @@ async function validateUniSpec(doc, _options = {}) {
|
|
|
73
111
|
*/
|
|
74
112
|
async function validateUniSpecTests(doc, _options = {}) {
|
|
75
113
|
if (!validateTestsFn) {
|
|
76
|
-
const schemaDir =
|
|
114
|
+
const schemaDir = getUniSpecSchemaDir();
|
|
77
115
|
const testsSchemaPath = path_1.default.join(schemaDir, "unispec-tests.schema.json");
|
|
78
116
|
const testsSchema = JSON.parse(fs_1.default.readFileSync(testsSchemaPath, "utf8"));
|
|
79
117
|
validateTestsFn = ajv.compile(testsSchema);
|
package/dist/validator/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import Ajv2020 from "ajv/dist/2020.js";
|
|
2
2
|
import fs from "fs";
|
|
3
3
|
import path from "path";
|
|
4
|
+
import { createRequire } from "module";
|
|
4
5
|
import { unispec as unispecSchema, manifest as unispecManifest } from "@unispechq/unispec-schema";
|
|
5
6
|
const ajv = new Ajv2020({
|
|
6
7
|
allErrors: true,
|
|
@@ -8,16 +9,53 @@ const ajv = new Ajv2020({
|
|
|
8
9
|
});
|
|
9
10
|
// Register minimal URI format to satisfy UniSpec schemas (service.environments[*].baseUrl)
|
|
10
11
|
ajv.addFormat("uri", true);
|
|
12
|
+
function findPackageRoot(startFilePath) {
|
|
13
|
+
let dir = path.dirname(startFilePath);
|
|
14
|
+
for (let i = 0; i < 50; i++) {
|
|
15
|
+
const pkgJsonPath = path.join(dir, "package.json");
|
|
16
|
+
if (fs.existsSync(pkgJsonPath)) {
|
|
17
|
+
return dir;
|
|
18
|
+
}
|
|
19
|
+
const parent = path.dirname(dir);
|
|
20
|
+
if (parent === dir) {
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
dir = parent;
|
|
24
|
+
}
|
|
25
|
+
throw new Error(`Cannot locate package.json for resolved path: ${startFilePath}`);
|
|
26
|
+
}
|
|
27
|
+
function getUniSpecSchemaDir() {
|
|
28
|
+
const localRequire = typeof require !== "undefined" ? require : createRequire(path.join(process.cwd(), "package.json"));
|
|
29
|
+
try {
|
|
30
|
+
const pkgJsonPath = localRequire.resolve("@unispechq/unispec-schema/package.json");
|
|
31
|
+
return path.join(path.dirname(pkgJsonPath), "schema");
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
const entryPath = localRequire.resolve("@unispechq/unispec-schema");
|
|
35
|
+
const pkgRoot = findPackageRoot(entryPath);
|
|
36
|
+
return path.join(pkgRoot, "schema");
|
|
37
|
+
}
|
|
38
|
+
}
|
|
11
39
|
// Register all UniSpec subschemas so that Ajv can resolve internal $ref links
|
|
12
40
|
try {
|
|
13
|
-
const schemaDir =
|
|
41
|
+
const schemaDir = getUniSpecSchemaDir();
|
|
14
42
|
const types = unispecManifest?.types ?? {};
|
|
15
43
|
const typeSchemaPaths = Object.values(types).map((rel) => String(rel));
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
44
|
+
for (const relPath of typeSchemaPaths) {
|
|
45
|
+
try {
|
|
46
|
+
const filePath = path.join(schemaDir, relPath);
|
|
47
|
+
if (!fs.existsSync(filePath)) {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const schema = JSON.parse(fs.readFileSync(filePath, "utf8"));
|
|
51
|
+
const normalizedRelPath = String(relPath).replace(/^\.\//, "");
|
|
52
|
+
const fallbackId = `https://unispec.dev/schema/${normalizedRelPath}`;
|
|
53
|
+
ajv.addSchema(schema, fallbackId);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
// Ignore individual schema registration failures to keep the validator usable.
|
|
57
|
+
}
|
|
58
|
+
}
|
|
21
59
|
}
|
|
22
60
|
catch {
|
|
23
61
|
// If subschemas cannot be loaded for some reason, validation will still work for
|
|
@@ -66,7 +104,7 @@ export async function validateUniSpec(doc, _options = {}) {
|
|
|
66
104
|
*/
|
|
67
105
|
export async function validateUniSpecTests(doc, _options = {}) {
|
|
68
106
|
if (!validateTestsFn) {
|
|
69
|
-
const schemaDir =
|
|
107
|
+
const schemaDir = getUniSpecSchemaDir();
|
|
70
108
|
const testsSchemaPath = path.join(schemaDir, "unispec-tests.schema.json");
|
|
71
109
|
const testsSchema = JSON.parse(fs.readFileSync(testsSchemaPath, "utf8"));
|
|
72
110
|
validateTestsFn = ajv.compile(testsSchema);
|
package/package.json
CHANGED