@unispechq/unispec-core 0.2.4 → 0.2.6

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.
@@ -9,13 +9,44 @@ 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
11
  const module_1 = require("module");
12
+ const url_1 = require("url");
12
13
  const unispec_schema_1 = require("@unispechq/unispec-schema");
13
- const ajv = new _2020_js_1.default({
14
- allErrors: true,
15
- strict: true,
16
- });
17
- // Register minimal URI format to satisfy UniSpec schemas (service.environments[*].baseUrl)
18
- ajv.addFormat("uri", true);
14
+ function getThisModuleUrl() {
15
+ if (typeof __filename === "string" && __filename.length > 0) {
16
+ return (0, url_1.pathToFileURL)(__filename).href;
17
+ }
18
+ const stack = new Error().stack ?? "";
19
+ const fileUrlMatch = stack.match(/file:\/\/\/[^\s)]+/);
20
+ if (fileUrlMatch?.[0]) {
21
+ return fileUrlMatch[0];
22
+ }
23
+ const winPathMatch = stack.match(/[A-Za-z]:\\[^\s)]+/);
24
+ if (winPathMatch?.[0]) {
25
+ return (0, url_1.pathToFileURL)(winPathMatch[0]).href;
26
+ }
27
+ throw new Error("Cannot determine current module URL for createRequire()");
28
+ }
29
+ function getLocalRequire() {
30
+ return (0, module_1.createRequire)(getThisModuleUrl());
31
+ }
32
+ function assertValidSchemaDir(schemaDir) {
33
+ const requiredFiles = [
34
+ path_1.default.join(schemaDir, "types", "service.schema.json"),
35
+ path_1.default.join(schemaDir, "unispec-tests.schema.json"),
36
+ ];
37
+ const missing = requiredFiles.filter((p) => !fs_1.default.existsSync(p));
38
+ if (missing.length === 0) {
39
+ return;
40
+ }
41
+ const hints = [
42
+ `UniSpec schema directory is invalid or incomplete: ${schemaDir}`,
43
+ `Missing schema files:`,
44
+ ...missing.map((p) => `- ${p}`),
45
+ `You can override the schema directory via ValidateOptions.schemaDir or UNISPEC_SCHEMA_DIR env var.`,
46
+ `In bundled environments (e.g. Next.js), module resolution can be altered; make sure @unispechq/unispec-schema is installed and accessible at runtime.`,
47
+ ];
48
+ throw new Error(hints.join("\n"));
49
+ }
19
50
  function findPackageRoot(startFilePath) {
20
51
  let dir = path_1.default.dirname(startFilePath);
21
52
  for (let i = 0; i < 50; i++) {
@@ -31,8 +62,12 @@ function findPackageRoot(startFilePath) {
31
62
  }
32
63
  throw new Error(`Cannot locate package.json for resolved path: ${startFilePath}`);
33
64
  }
34
- function getUniSpecSchemaDir() {
35
- const localRequire = typeof require !== "undefined" ? require : (0, module_1.createRequire)(path_1.default.join(process.cwd(), "package.json"));
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();
36
71
  try {
37
72
  const pkgJsonPath = localRequire.resolve("@unispechq/unispec-schema/package.json");
38
73
  return path_1.default.join(path_1.default.dirname(pkgJsonPath), "schema");
@@ -43,33 +78,44 @@ function getUniSpecSchemaDir() {
43
78
  return path_1.default.join(pkgRoot, "schema");
44
79
  }
45
80
  }
46
- // Register all UniSpec subschemas so that Ajv can resolve internal $ref links
47
- try {
48
- const schemaDir = getUniSpecSchemaDir();
81
+ const validatorCache = new Map();
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
+ }
89
+ const ajv = new _2020_js_1.default({
90
+ allErrors: true,
91
+ strict: true,
92
+ });
93
+ // Register minimal URI format to satisfy UniSpec schemas (service.environments[*].baseUrl)
94
+ ajv.addFormat("uri", true);
95
+ // Register all UniSpec subschemas so that Ajv can resolve internal $ref links
49
96
  const types = unispec_schema_1.manifest?.types ?? {};
50
97
  const typeSchemaPaths = Object.values(types).map((rel) => String(rel));
51
98
  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.
99
+ const filePath = path_1.default.join(schemaDir, relPath);
100
+ if (!fs_1.default.existsSync(filePath)) {
101
+ continue;
64
102
  }
103
+ const schema = JSON.parse(fs_1.default.readFileSync(filePath, "utf8"));
104
+ const normalizedRelPath = String(relPath).replace(/^\.\//, "");
105
+ const fallbackId = `https://unispec.dev/schema/${normalizedRelPath}`;
106
+ ajv.addSchema(schema, fallbackId);
65
107
  }
108
+ const validateFn = ajv.compile(unispec_schema_1.unispec);
109
+ const testsSchemaPath = path_1.default.join(schemaDir, "unispec-tests.schema.json");
110
+ const testsSchema = JSON.parse(fs_1.default.readFileSync(testsSchemaPath, "utf8"));
111
+ const validateTestsFn = ajv.compile(testsSchema);
112
+ const compiled = {
113
+ validateFn,
114
+ validateTestsFn,
115
+ };
116
+ validatorCache.set(schemaDir, compiled);
117
+ return compiled;
66
118
  }
67
- catch {
68
- // If subschemas cannot be loaded for some reason, validation will still work for
69
- // parts of the schema that do not rely on those $ref references.
70
- }
71
- const validateFn = ajv.compile(unispec_schema_1.unispec);
72
- let validateTestsFn;
73
119
  function mapAjvErrors(errors) {
74
120
  if (!errors)
75
121
  return [];
@@ -83,6 +129,7 @@ function mapAjvErrors(errors) {
83
129
  * Validate a UniSpec document against the UniSpec JSON Schema.
84
130
  */
85
131
  async function validateUniSpec(doc, _options = {}) {
132
+ const { validateFn } = getCompiledValidator(_options);
86
133
  const docForValidation = {
87
134
  unispecVersion: "0.0.0",
88
135
  service: {
@@ -110,12 +157,7 @@ async function validateUniSpec(doc, _options = {}) {
110
157
  * Validate a UniSpec Tests document against the UniSpec Tests JSON Schema.
111
158
  */
112
159
  async function validateUniSpecTests(doc, _options = {}) {
113
- if (!validateTestsFn) {
114
- const schemaDir = getUniSpecSchemaDir();
115
- const testsSchemaPath = path_1.default.join(schemaDir, "unispec-tests.schema.json");
116
- const testsSchema = JSON.parse(fs_1.default.readFileSync(testsSchemaPath, "utf8"));
117
- validateTestsFn = ajv.compile(testsSchema);
118
- }
160
+ const { validateTestsFn } = getCompiledValidator(_options);
119
161
  const valid = validateTestsFn(doc);
120
162
  if (valid) {
121
163
  return {
@@ -1,5 +1,6 @@
1
1
  import { UniSpecDocument, UniSpecTestsDocument, ValidationResult } from "../types";
2
2
  export interface ValidateOptions {
3
+ schemaDir?: string;
3
4
  }
4
5
  /**
5
6
  * Validate a UniSpec document against the UniSpec JSON Schema.
@@ -2,13 +2,44 @@ import Ajv2020 from "ajv/dist/2020.js";
2
2
  import fs from "fs";
3
3
  import path from "path";
4
4
  import { createRequire } from "module";
5
+ import { pathToFileURL } from "url";
5
6
  import { unispec as unispecSchema, manifest as unispecManifest } from "@unispechq/unispec-schema";
6
- const ajv = new Ajv2020({
7
- allErrors: true,
8
- strict: true,
9
- });
10
- // Register minimal URI format to satisfy UniSpec schemas (service.environments[*].baseUrl)
11
- ajv.addFormat("uri", true);
7
+ function getThisModuleUrl() {
8
+ if (typeof __filename === "string" && __filename.length > 0) {
9
+ return pathToFileURL(__filename).href;
10
+ }
11
+ const stack = new Error().stack ?? "";
12
+ const fileUrlMatch = stack.match(/file:\/\/\/[^\s)]+/);
13
+ if (fileUrlMatch?.[0]) {
14
+ return fileUrlMatch[0];
15
+ }
16
+ const winPathMatch = stack.match(/[A-Za-z]:\\[^\s)]+/);
17
+ if (winPathMatch?.[0]) {
18
+ return pathToFileURL(winPathMatch[0]).href;
19
+ }
20
+ throw new Error("Cannot determine current module URL for createRequire()");
21
+ }
22
+ function getLocalRequire() {
23
+ return createRequire(getThisModuleUrl());
24
+ }
25
+ function assertValidSchemaDir(schemaDir) {
26
+ const requiredFiles = [
27
+ path.join(schemaDir, "types", "service.schema.json"),
28
+ path.join(schemaDir, "unispec-tests.schema.json"),
29
+ ];
30
+ const missing = requiredFiles.filter((p) => !fs.existsSync(p));
31
+ if (missing.length === 0) {
32
+ return;
33
+ }
34
+ const hints = [
35
+ `UniSpec schema directory is invalid or incomplete: ${schemaDir}`,
36
+ `Missing schema files:`,
37
+ ...missing.map((p) => `- ${p}`),
38
+ `You can override the schema directory via ValidateOptions.schemaDir or UNISPEC_SCHEMA_DIR env var.`,
39
+ `In bundled environments (e.g. Next.js), module resolution can be altered; make sure @unispechq/unispec-schema is installed and accessible at runtime.`,
40
+ ];
41
+ throw new Error(hints.join("\n"));
42
+ }
12
43
  function findPackageRoot(startFilePath) {
13
44
  let dir = path.dirname(startFilePath);
14
45
  for (let i = 0; i < 50; i++) {
@@ -24,8 +55,12 @@ function findPackageRoot(startFilePath) {
24
55
  }
25
56
  throw new Error(`Cannot locate package.json for resolved path: ${startFilePath}`);
26
57
  }
27
- function getUniSpecSchemaDir() {
28
- const localRequire = typeof require !== "undefined" ? require : createRequire(path.join(process.cwd(), "package.json"));
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();
29
64
  try {
30
65
  const pkgJsonPath = localRequire.resolve("@unispechq/unispec-schema/package.json");
31
66
  return path.join(path.dirname(pkgJsonPath), "schema");
@@ -36,33 +71,44 @@ function getUniSpecSchemaDir() {
36
71
  return path.join(pkgRoot, "schema");
37
72
  }
38
73
  }
39
- // Register all UniSpec subschemas so that Ajv can resolve internal $ref links
40
- try {
41
- const schemaDir = getUniSpecSchemaDir();
74
+ const validatorCache = new Map();
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
+ }
82
+ const ajv = new Ajv2020({
83
+ allErrors: true,
84
+ strict: true,
85
+ });
86
+ // Register minimal URI format to satisfy UniSpec schemas (service.environments[*].baseUrl)
87
+ ajv.addFormat("uri", true);
88
+ // Register all UniSpec subschemas so that Ajv can resolve internal $ref links
42
89
  const types = unispecManifest?.types ?? {};
43
90
  const typeSchemaPaths = Object.values(types).map((rel) => String(rel));
44
91
  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.
92
+ const filePath = path.join(schemaDir, relPath);
93
+ if (!fs.existsSync(filePath)) {
94
+ continue;
57
95
  }
96
+ const schema = JSON.parse(fs.readFileSync(filePath, "utf8"));
97
+ const normalizedRelPath = String(relPath).replace(/^\.\//, "");
98
+ const fallbackId = `https://unispec.dev/schema/${normalizedRelPath}`;
99
+ ajv.addSchema(schema, fallbackId);
58
100
  }
101
+ const validateFn = ajv.compile(unispecSchema);
102
+ const testsSchemaPath = path.join(schemaDir, "unispec-tests.schema.json");
103
+ const testsSchema = JSON.parse(fs.readFileSync(testsSchemaPath, "utf8"));
104
+ const validateTestsFn = ajv.compile(testsSchema);
105
+ const compiled = {
106
+ validateFn,
107
+ validateTestsFn,
108
+ };
109
+ validatorCache.set(schemaDir, compiled);
110
+ return compiled;
59
111
  }
60
- catch {
61
- // If subschemas cannot be loaded for some reason, validation will still work for
62
- // parts of the schema that do not rely on those $ref references.
63
- }
64
- const validateFn = ajv.compile(unispecSchema);
65
- let validateTestsFn;
66
112
  function mapAjvErrors(errors) {
67
113
  if (!errors)
68
114
  return [];
@@ -76,6 +122,7 @@ function mapAjvErrors(errors) {
76
122
  * Validate a UniSpec document against the UniSpec JSON Schema.
77
123
  */
78
124
  export async function validateUniSpec(doc, _options = {}) {
125
+ const { validateFn } = getCompiledValidator(_options);
79
126
  const docForValidation = {
80
127
  unispecVersion: "0.0.0",
81
128
  service: {
@@ -103,12 +150,7 @@ export async function validateUniSpec(doc, _options = {}) {
103
150
  * Validate a UniSpec Tests document against the UniSpec Tests JSON Schema.
104
151
  */
105
152
  export async function validateUniSpecTests(doc, _options = {}) {
106
- if (!validateTestsFn) {
107
- const schemaDir = getUniSpecSchemaDir();
108
- const testsSchemaPath = path.join(schemaDir, "unispec-tests.schema.json");
109
- const testsSchema = JSON.parse(fs.readFileSync(testsSchemaPath, "utf8"));
110
- validateTestsFn = ajv.compile(testsSchema);
111
- }
153
+ const { validateTestsFn } = getCompiledValidator(_options);
112
154
  const valid = validateTestsFn(doc);
113
155
  if (valid) {
114
156
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unispechq/unispec-core",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "description": "Central UniSpec Core Engine providing parsing, validation, normalization, diffing, and conversion of UniSpec specs.",
5
5
  "license": "MIT",
6
6
  "repository": {