@unispechq/unispec-core 0.2.6 → 0.2.9
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 +119 -99
- package/dist/validator/index.d.ts +14 -3
- package/dist/validator/index.js +86 -99
- package/package.json +1 -1
|
@@ -1,4 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
2
35
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
37
|
};
|
|
@@ -6,115 +39,90 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
39
|
exports.validateUniSpec = validateUniSpec;
|
|
7
40
|
exports.validateUniSpecTests = validateUniSpecTests;
|
|
8
41
|
const _2020_js_1 = __importDefault(require("ajv/dist/2020.js"));
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
return
|
|
42
|
+
let cached = null;
|
|
43
|
+
async function tryCreateNodeSchemaProvider() {
|
|
44
|
+
try {
|
|
45
|
+
const fs = await Promise.resolve().then(() => __importStar(require("node:fs/promises")));
|
|
46
|
+
const path = await Promise.resolve().then(() => __importStar(require("node:path")));
|
|
47
|
+
const module = await Promise.resolve().then(() => __importStar(require("node:module")));
|
|
48
|
+
const requireBasePath = typeof __filename === "string" && __filename.length > 0
|
|
49
|
+
? __filename
|
|
50
|
+
: path.join(process.cwd(), "__unispec_core_require__.js");
|
|
51
|
+
const require = module.createRequire(requireBasePath);
|
|
52
|
+
const schemaIndexPath = require.resolve("@unispechq/unispec-schema/schema");
|
|
53
|
+
const schemaRoot = path.dirname(schemaIndexPath);
|
|
54
|
+
return {
|
|
55
|
+
getSchema: async (schemaPath) => {
|
|
56
|
+
const fullPath = path.join(schemaRoot, schemaPath);
|
|
57
|
+
const json = await fs.readFile(fullPath, "utf8");
|
|
58
|
+
return JSON.parse(json);
|
|
59
|
+
},
|
|
60
|
+
};
|
|
22
61
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return (0, url_1.pathToFileURL)(winPathMatch[0]).href;
|
|
62
|
+
catch {
|
|
63
|
+
return undefined;
|
|
26
64
|
}
|
|
27
|
-
throw new Error("Cannot determine current module URL for createRequire()");
|
|
28
|
-
}
|
|
29
|
-
function getLocalRequire() {
|
|
30
|
-
return (0, module_1.createRequire)(getThisModuleUrl());
|
|
31
65
|
}
|
|
32
|
-
function
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const missing = requiredFiles.filter((p) => !fs_1.default.existsSync(p));
|
|
38
|
-
if (missing.length === 0) {
|
|
39
|
-
return;
|
|
66
|
+
async function loadDefaultSchemas() {
|
|
67
|
+
const schemaProvider = await tryCreateNodeSchemaProvider();
|
|
68
|
+
if (!schemaProvider) {
|
|
69
|
+
throw new Error("UniSpec validator: default schema loading is not available in this environment. " +
|
|
70
|
+
"Provide ValidateOptions.schemas (recommended for browsers/edge runtimes) or ValidateOptions.schemaProvider.");
|
|
40
71
|
}
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
];
|
|
48
|
-
throw new Error(hints.join("\n"));
|
|
49
|
-
}
|
|
50
|
-
function findPackageRoot(startFilePath) {
|
|
51
|
-
let dir = path_1.default.dirname(startFilePath);
|
|
52
|
-
for (let i = 0; i < 50; i++) {
|
|
53
|
-
const pkgJsonPath = path_1.default.join(dir, "package.json");
|
|
54
|
-
if (fs_1.default.existsSync(pkgJsonPath)) {
|
|
55
|
-
return dir;
|
|
72
|
+
const { manifest, unispec } = (await Promise.resolve().then(() => __importStar(require("@unispechq/unispec-schema"))));
|
|
73
|
+
const types = manifest?.types ?? {};
|
|
74
|
+
const typeSchemaPaths = Object.values(types).map((rel) => String(rel));
|
|
75
|
+
const subschemas = await Promise.all(typeSchemaPaths.map(async (relPath) => {
|
|
76
|
+
try {
|
|
77
|
+
return await schemaProvider.getSchema(relPath);
|
|
56
78
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
break;
|
|
79
|
+
catch {
|
|
80
|
+
return null;
|
|
60
81
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
throw new Error(`Cannot locate package.json for resolved path: ${startFilePath}`);
|
|
64
|
-
}
|
|
65
|
-
function getUniSpecSchemaDir(options = {}) {
|
|
66
|
-
const override = options.schemaDir ?? process.env.UNISPEC_SCHEMA_DIR;
|
|
67
|
-
if (override) {
|
|
68
|
-
return override;
|
|
69
|
-
}
|
|
70
|
-
const localRequire = getLocalRequire();
|
|
82
|
+
}));
|
|
83
|
+
let unispecTests;
|
|
71
84
|
try {
|
|
72
|
-
|
|
73
|
-
return path_1.default.join(path_1.default.dirname(pkgJsonPath), "schema");
|
|
85
|
+
unispecTests = await schemaProvider.getSchema("unispec-tests.schema.json");
|
|
74
86
|
}
|
|
75
87
|
catch {
|
|
76
|
-
|
|
77
|
-
const pkgRoot = findPackageRoot(entryPath);
|
|
78
|
-
return path_1.default.join(pkgRoot, "schema");
|
|
88
|
+
unispecTests = undefined;
|
|
79
89
|
}
|
|
90
|
+
return {
|
|
91
|
+
unispec: unispec,
|
|
92
|
+
unispecTests,
|
|
93
|
+
subschemas: subschemas.filter((s) => Boolean(s)),
|
|
94
|
+
};
|
|
80
95
|
}
|
|
81
|
-
|
|
82
|
-
function getCompiledValidator(options = {}) {
|
|
83
|
-
const schemaDir = getUniSpecSchemaDir(options);
|
|
84
|
-
assertValidSchemaDir(schemaDir);
|
|
85
|
-
const cached = validatorCache.get(schemaDir);
|
|
86
|
-
if (cached) {
|
|
87
|
-
return cached;
|
|
88
|
-
}
|
|
96
|
+
function createAjv(options) {
|
|
89
97
|
const ajv = new _2020_js_1.default({
|
|
90
98
|
allErrors: true,
|
|
91
99
|
strict: true,
|
|
100
|
+
...(options.ajvOptions ?? {}),
|
|
92
101
|
});
|
|
93
|
-
// Register minimal URI format to satisfy UniSpec schemas (service.environments[*].baseUrl)
|
|
94
102
|
ajv.addFormat("uri", true);
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
const normalizedRelPath = String(relPath).replace(/^\.\//, "");
|
|
105
|
-
const fallbackId = `https://unispec.dev/schema/${normalizedRelPath}`;
|
|
106
|
-
ajv.addSchema(schema, fallbackId);
|
|
103
|
+
return ajv;
|
|
104
|
+
}
|
|
105
|
+
async function getValidator(options = {}) {
|
|
106
|
+
if (cached && !options.schemas && !options.schemaProvider && !options.ajvOptions)
|
|
107
|
+
return cached;
|
|
108
|
+
const schemas = options.schemas ?? (await loadDefaultSchemas());
|
|
109
|
+
const ajv = createAjv(options);
|
|
110
|
+
if (schemas.subschemas?.length) {
|
|
111
|
+
ajv.addSchema(schemas.subschemas);
|
|
107
112
|
}
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
113
|
+
const validateUniSpecFn = ajv.compile(schemas.unispec);
|
|
114
|
+
let validateUniSpecTestsFn;
|
|
115
|
+
if (schemas.unispecTests) {
|
|
116
|
+
validateUniSpecTestsFn = ajv.compile(schemas.unispecTests);
|
|
117
|
+
}
|
|
118
|
+
const result = {
|
|
119
|
+
validateUniSpecFn,
|
|
120
|
+
validateUniSpecTestsFn,
|
|
115
121
|
};
|
|
116
|
-
|
|
117
|
-
|
|
122
|
+
if (!options.schemas && !options.schemaProvider && !options.ajvOptions) {
|
|
123
|
+
cached = result;
|
|
124
|
+
}
|
|
125
|
+
return result;
|
|
118
126
|
}
|
|
119
127
|
function mapAjvErrors(errors) {
|
|
120
128
|
if (!errors)
|
|
@@ -128,8 +136,8 @@ function mapAjvErrors(errors) {
|
|
|
128
136
|
/**
|
|
129
137
|
* Validate a UniSpec document against the UniSpec JSON Schema.
|
|
130
138
|
*/
|
|
131
|
-
async function validateUniSpec(doc,
|
|
132
|
-
const {
|
|
139
|
+
async function validateUniSpec(doc, options = {}) {
|
|
140
|
+
const { validateUniSpecFn } = await getValidator(options);
|
|
133
141
|
const docForValidation = {
|
|
134
142
|
unispecVersion: "0.0.0",
|
|
135
143
|
service: {
|
|
@@ -141,7 +149,7 @@ async function validateUniSpec(doc, _options = {}) {
|
|
|
141
149
|
},
|
|
142
150
|
extensions: doc.extensions,
|
|
143
151
|
};
|
|
144
|
-
const valid =
|
|
152
|
+
const valid = validateUniSpecFn(docForValidation);
|
|
145
153
|
if (valid) {
|
|
146
154
|
return {
|
|
147
155
|
valid: true,
|
|
@@ -150,15 +158,27 @@ async function validateUniSpec(doc, _options = {}) {
|
|
|
150
158
|
}
|
|
151
159
|
return {
|
|
152
160
|
valid: false,
|
|
153
|
-
errors: mapAjvErrors(
|
|
161
|
+
errors: mapAjvErrors(validateUniSpecFn.errors),
|
|
154
162
|
};
|
|
155
163
|
}
|
|
156
164
|
/**
|
|
157
165
|
* Validate a UniSpec Tests document against the UniSpec Tests JSON Schema.
|
|
158
166
|
*/
|
|
159
|
-
async function validateUniSpecTests(doc,
|
|
160
|
-
const {
|
|
161
|
-
|
|
167
|
+
async function validateUniSpecTests(doc, options = {}) {
|
|
168
|
+
const { validateUniSpecTestsFn } = await getValidator(options);
|
|
169
|
+
if (!validateUniSpecTestsFn) {
|
|
170
|
+
return {
|
|
171
|
+
valid: false,
|
|
172
|
+
errors: [
|
|
173
|
+
{
|
|
174
|
+
message: "UniSpec tests validation schema is not available. Provide ValidateOptions.schemas.unispecTests or a schemaProvider that can load it.",
|
|
175
|
+
path: "",
|
|
176
|
+
code: "schema_missing",
|
|
177
|
+
},
|
|
178
|
+
],
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
const valid = validateUniSpecTestsFn(doc);
|
|
162
182
|
if (valid) {
|
|
163
183
|
return {
|
|
164
184
|
valid: true,
|
|
@@ -167,6 +187,6 @@ async function validateUniSpecTests(doc, _options = {}) {
|
|
|
167
187
|
}
|
|
168
188
|
return {
|
|
169
189
|
valid: false,
|
|
170
|
-
errors: mapAjvErrors(
|
|
190
|
+
errors: mapAjvErrors(validateUniSpecTestsFn.errors),
|
|
171
191
|
};
|
|
172
192
|
}
|
|
@@ -1,12 +1,23 @@
|
|
|
1
|
+
import Ajv2020 from "ajv/dist/2020.js";
|
|
1
2
|
import { UniSpecDocument, UniSpecTestsDocument, ValidationResult } from "../types";
|
|
3
|
+
export interface SchemaProvider {
|
|
4
|
+
getSchema: (schemaPath: string) => Promise<object>;
|
|
5
|
+
}
|
|
6
|
+
export interface ValidatorSchemas {
|
|
7
|
+
unispec: object;
|
|
8
|
+
unispecTests?: object;
|
|
9
|
+
subschemas?: object[];
|
|
10
|
+
}
|
|
2
11
|
export interface ValidateOptions {
|
|
3
|
-
|
|
12
|
+
schemas?: ValidatorSchemas;
|
|
13
|
+
schemaProvider?: SchemaProvider;
|
|
14
|
+
ajvOptions?: ConstructorParameters<typeof Ajv2020>[0];
|
|
4
15
|
}
|
|
5
16
|
/**
|
|
6
17
|
* Validate a UniSpec document against the UniSpec JSON Schema.
|
|
7
18
|
*/
|
|
8
|
-
export declare function validateUniSpec(doc: UniSpecDocument,
|
|
19
|
+
export declare function validateUniSpec(doc: UniSpecDocument, options?: ValidateOptions): Promise<ValidationResult>;
|
|
9
20
|
/**
|
|
10
21
|
* Validate a UniSpec Tests document against the UniSpec Tests JSON Schema.
|
|
11
22
|
*/
|
|
12
|
-
export declare function validateUniSpecTests(doc: UniSpecTestsDocument,
|
|
23
|
+
export declare function validateUniSpecTests(doc: UniSpecTestsDocument, options?: ValidateOptions): Promise<ValidationResult>;
|
package/dist/validator/index.js
CHANGED
|
@@ -1,113 +1,88 @@
|
|
|
1
1
|
import Ajv2020 from "ajv/dist/2020.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return
|
|
2
|
+
let cached = null;
|
|
3
|
+
async function tryCreateNodeSchemaProvider() {
|
|
4
|
+
try {
|
|
5
|
+
const fs = await import("node:fs/promises");
|
|
6
|
+
const path = await import("node:path");
|
|
7
|
+
const module = await import("node:module");
|
|
8
|
+
const requireBasePath = typeof __filename === "string" && __filename.length > 0
|
|
9
|
+
? __filename
|
|
10
|
+
: path.join(process.cwd(), "__unispec_core_require__.js");
|
|
11
|
+
const require = module.createRequire(requireBasePath);
|
|
12
|
+
const schemaIndexPath = require.resolve("@unispechq/unispec-schema/schema");
|
|
13
|
+
const schemaRoot = path.dirname(schemaIndexPath);
|
|
14
|
+
return {
|
|
15
|
+
getSchema: async (schemaPath) => {
|
|
16
|
+
const fullPath = path.join(schemaRoot, schemaPath);
|
|
17
|
+
const json = await fs.readFile(fullPath, "utf8");
|
|
18
|
+
return JSON.parse(json);
|
|
19
|
+
},
|
|
20
|
+
};
|
|
15
21
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return pathToFileURL(winPathMatch[0]).href;
|
|
22
|
+
catch {
|
|
23
|
+
return undefined;
|
|
19
24
|
}
|
|
20
|
-
throw new Error("Cannot determine current module URL for createRequire()");
|
|
21
|
-
}
|
|
22
|
-
function getLocalRequire() {
|
|
23
|
-
return createRequire(getThisModuleUrl());
|
|
24
25
|
}
|
|
25
|
-
function
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const missing = requiredFiles.filter((p) => !fs.existsSync(p));
|
|
31
|
-
if (missing.length === 0) {
|
|
32
|
-
return;
|
|
26
|
+
async function loadDefaultSchemas() {
|
|
27
|
+
const schemaProvider = await tryCreateNodeSchemaProvider();
|
|
28
|
+
if (!schemaProvider) {
|
|
29
|
+
throw new Error("UniSpec validator: default schema loading is not available in this environment. " +
|
|
30
|
+
"Provide ValidateOptions.schemas (recommended for browsers/edge runtimes) or ValidateOptions.schemaProvider.");
|
|
33
31
|
}
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
];
|
|
41
|
-
throw new Error(hints.join("\n"));
|
|
42
|
-
}
|
|
43
|
-
function findPackageRoot(startFilePath) {
|
|
44
|
-
let dir = path.dirname(startFilePath);
|
|
45
|
-
for (let i = 0; i < 50; i++) {
|
|
46
|
-
const pkgJsonPath = path.join(dir, "package.json");
|
|
47
|
-
if (fs.existsSync(pkgJsonPath)) {
|
|
48
|
-
return dir;
|
|
32
|
+
const { manifest, unispec } = (await import("@unispechq/unispec-schema"));
|
|
33
|
+
const types = manifest?.types ?? {};
|
|
34
|
+
const typeSchemaPaths = Object.values(types).map((rel) => String(rel));
|
|
35
|
+
const subschemas = await Promise.all(typeSchemaPaths.map(async (relPath) => {
|
|
36
|
+
try {
|
|
37
|
+
return await schemaProvider.getSchema(relPath);
|
|
49
38
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
break;
|
|
39
|
+
catch {
|
|
40
|
+
return null;
|
|
53
41
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
throw new Error(`Cannot locate package.json for resolved path: ${startFilePath}`);
|
|
57
|
-
}
|
|
58
|
-
function getUniSpecSchemaDir(options = {}) {
|
|
59
|
-
const override = options.schemaDir ?? process.env.UNISPEC_SCHEMA_DIR;
|
|
60
|
-
if (override) {
|
|
61
|
-
return override;
|
|
62
|
-
}
|
|
63
|
-
const localRequire = getLocalRequire();
|
|
42
|
+
}));
|
|
43
|
+
let unispecTests;
|
|
64
44
|
try {
|
|
65
|
-
|
|
66
|
-
return path.join(path.dirname(pkgJsonPath), "schema");
|
|
45
|
+
unispecTests = await schemaProvider.getSchema("unispec-tests.schema.json");
|
|
67
46
|
}
|
|
68
47
|
catch {
|
|
69
|
-
|
|
70
|
-
const pkgRoot = findPackageRoot(entryPath);
|
|
71
|
-
return path.join(pkgRoot, "schema");
|
|
48
|
+
unispecTests = undefined;
|
|
72
49
|
}
|
|
50
|
+
return {
|
|
51
|
+
unispec: unispec,
|
|
52
|
+
unispecTests,
|
|
53
|
+
subschemas: subschemas.filter((s) => Boolean(s)),
|
|
54
|
+
};
|
|
73
55
|
}
|
|
74
|
-
|
|
75
|
-
function getCompiledValidator(options = {}) {
|
|
76
|
-
const schemaDir = getUniSpecSchemaDir(options);
|
|
77
|
-
assertValidSchemaDir(schemaDir);
|
|
78
|
-
const cached = validatorCache.get(schemaDir);
|
|
79
|
-
if (cached) {
|
|
80
|
-
return cached;
|
|
81
|
-
}
|
|
56
|
+
function createAjv(options) {
|
|
82
57
|
const ajv = new Ajv2020({
|
|
83
58
|
allErrors: true,
|
|
84
59
|
strict: true,
|
|
60
|
+
...(options.ajvOptions ?? {}),
|
|
85
61
|
});
|
|
86
|
-
// Register minimal URI format to satisfy UniSpec schemas (service.environments[*].baseUrl)
|
|
87
62
|
ajv.addFormat("uri", true);
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
63
|
+
return ajv;
|
|
64
|
+
}
|
|
65
|
+
async function getValidator(options = {}) {
|
|
66
|
+
if (cached && !options.schemas && !options.schemaProvider && !options.ajvOptions)
|
|
67
|
+
return cached;
|
|
68
|
+
const schemas = options.schemas ?? (await loadDefaultSchemas());
|
|
69
|
+
const ajv = createAjv(options);
|
|
70
|
+
if (schemas.subschemas?.length) {
|
|
71
|
+
ajv.addSchema(schemas.subschemas);
|
|
72
|
+
}
|
|
73
|
+
const validateUniSpecFn = ajv.compile(schemas.unispec);
|
|
74
|
+
let validateUniSpecTestsFn;
|
|
75
|
+
if (schemas.unispecTests) {
|
|
76
|
+
validateUniSpecTestsFn = ajv.compile(schemas.unispecTests);
|
|
100
77
|
}
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
const validateTestsFn = ajv.compile(testsSchema);
|
|
105
|
-
const compiled = {
|
|
106
|
-
validateFn,
|
|
107
|
-
validateTestsFn,
|
|
78
|
+
const result = {
|
|
79
|
+
validateUniSpecFn,
|
|
80
|
+
validateUniSpecTestsFn,
|
|
108
81
|
};
|
|
109
|
-
|
|
110
|
-
|
|
82
|
+
if (!options.schemas && !options.schemaProvider && !options.ajvOptions) {
|
|
83
|
+
cached = result;
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
111
86
|
}
|
|
112
87
|
function mapAjvErrors(errors) {
|
|
113
88
|
if (!errors)
|
|
@@ -121,8 +96,8 @@ function mapAjvErrors(errors) {
|
|
|
121
96
|
/**
|
|
122
97
|
* Validate a UniSpec document against the UniSpec JSON Schema.
|
|
123
98
|
*/
|
|
124
|
-
export async function validateUniSpec(doc,
|
|
125
|
-
const {
|
|
99
|
+
export async function validateUniSpec(doc, options = {}) {
|
|
100
|
+
const { validateUniSpecFn } = await getValidator(options);
|
|
126
101
|
const docForValidation = {
|
|
127
102
|
unispecVersion: "0.0.0",
|
|
128
103
|
service: {
|
|
@@ -134,7 +109,7 @@ export async function validateUniSpec(doc, _options = {}) {
|
|
|
134
109
|
},
|
|
135
110
|
extensions: doc.extensions,
|
|
136
111
|
};
|
|
137
|
-
const valid =
|
|
112
|
+
const valid = validateUniSpecFn(docForValidation);
|
|
138
113
|
if (valid) {
|
|
139
114
|
return {
|
|
140
115
|
valid: true,
|
|
@@ -143,15 +118,27 @@ export async function validateUniSpec(doc, _options = {}) {
|
|
|
143
118
|
}
|
|
144
119
|
return {
|
|
145
120
|
valid: false,
|
|
146
|
-
errors: mapAjvErrors(
|
|
121
|
+
errors: mapAjvErrors(validateUniSpecFn.errors),
|
|
147
122
|
};
|
|
148
123
|
}
|
|
149
124
|
/**
|
|
150
125
|
* Validate a UniSpec Tests document against the UniSpec Tests JSON Schema.
|
|
151
126
|
*/
|
|
152
|
-
export async function validateUniSpecTests(doc,
|
|
153
|
-
const {
|
|
154
|
-
|
|
127
|
+
export async function validateUniSpecTests(doc, options = {}) {
|
|
128
|
+
const { validateUniSpecTestsFn } = await getValidator(options);
|
|
129
|
+
if (!validateUniSpecTestsFn) {
|
|
130
|
+
return {
|
|
131
|
+
valid: false,
|
|
132
|
+
errors: [
|
|
133
|
+
{
|
|
134
|
+
message: "UniSpec tests validation schema is not available. Provide ValidateOptions.schemas.unispecTests or a schemaProvider that can load it.",
|
|
135
|
+
path: "",
|
|
136
|
+
code: "schema_missing",
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
const valid = validateUniSpecTestsFn(doc);
|
|
155
142
|
if (valid) {
|
|
156
143
|
return {
|
|
157
144
|
valid: true,
|
|
@@ -160,6 +147,6 @@ export async function validateUniSpecTests(doc, _options = {}) {
|
|
|
160
147
|
}
|
|
161
148
|
return {
|
|
162
149
|
valid: false,
|
|
163
|
-
errors: mapAjvErrors(
|
|
150
|
+
errors: mapAjvErrors(validateUniSpecTestsFn.errors),
|
|
164
151
|
};
|
|
165
152
|
}
|
package/package.json
CHANGED