-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidateUtil.ts
More file actions
465 lines (434 loc) · 17.1 KB
/
validateUtil.ts
File metadata and controls
465 lines (434 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// SPDX-FileCopyrightText: 2025 diggsweden/rest-api-profil-lint-processor
//
// SPDX-License-Identifier: EUPL-1.2
import yaml from 'js-yaml';
import SwaggerParser from '@apidevtools/swagger-parser';
import { prettifySwaggerParserErrorToEditorStyle } from './prettifyOpenAPIErrors.js';
import { bundleAndLoadRuleset } from '@stoplight/spectral-ruleset-bundler/with-loader';
import * as SpectralCore from '@stoplight/spectral-core';
import { Document as SpectralDocument } from '@stoplight/spectral-core';
import * as fs2 from 'node:fs';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
import fs from 'fs/promises';
import os from 'os';
import path from 'path';
import { RapLPCustomSpectral } from './RapLPCustomSpectral.js';
import Parsers from '@stoplight/spectral-parsers';
import type { IParser } from '@stoplight/spectral-parsers';
import * as IssueHelper from './RapLPIssueHelpers.js';
import { SpecParseError} from './RapLPSpecParseError.js';
import { Issue } from './Issue.js';
/**
* Possible input types for specifications
* - filePath: Read from filesystem
* - raw: raw text (probably YAML or JSON)
* - base64: base64-coded content
* - parsed: allready parsed object (skip parsing)
*/
export type SpecInput =
| {filePath: string}
| {raw:string}
| {base64: string}
| {parsed: any};
/**
* Return result after successfull parsing
*/
export type ParseResult = {
format: 'json' | 'yaml';
raw: string;
parsed: any;
strictIssues?: Issue[];
}
export type ParseOptions = {
preferJsonError?: 'auto' | 'never' | 'always';
/**
* Limit how long the snippet in SpecParseError can be (optional).
*/
maxSnippetLength?: number;
disableSanity?: boolean;
strict?: boolean;
};
export type PreferJsonOption = 'auto' | 'never' | 'always';
/**
* detectSpecFormatPreference
* --------------------------------
* Function to determine how we should be interpret the format of the OpenAPI Specification.
* Normal case is either JSON,YAML or undefined format
* 'always' Expect JSON, give JSON-errorMessages
* 'never' Expect YAML, give YAML-errorMessages
* 'auto' ' Try to solve format base on content'
* Returns: PreferJsnOption
*/
export function detectSpecFormatPreference(
filePath?: string | null,
raw?: string | null,
defaultOption: PreferJsonOption = 'never',
): PreferJsonOption {
if (filePath) {
const ext = (filePath.split('.').pop() || '').toLowerCase();
if (ext === 'json') return 'always';
if (ext === 'yaml' || ext === 'yml') return 'never';
}
if (raw) {
const t = raw.trim();
if (t.startsWith('{') || t.startsWith('[')) return 'always';
if (/^\s*<\?xml|^\s*<[\w-]+[\s>]/i.test(t)) return 'never';
}
return defaultOption;
}
/**
*
* @param input :SpecInput // input type for specifications
* @param SpecInput,ParseOptions(Optional)
* @returns Promise<ParseResult>
* @throws SpecParseError // if the content is not valid JSON or YAML.
*/
export async function parseApiSpecInput(input: SpecInput,
opts: ParseOptions = {}
): Promise<ParseResult> {
const prefer = opts.preferJsonError ?? 'never'; // Standard is YAML focus
const maxSnippetLength = opts.maxSnippetLength ?? 5000;
const strict = opts.strict ?? false; // Strict flag is normally set to false
if ('parsed' in input) { // Checks if content is alreay parsed
const parsed = input.parsed;
if (!isOpenApiLike(parsed)) {
throw new SpecParseError('Det parsade objektet verkar inte vara en giltig OpenAPI-specifikation.', { source: 'unknown' });
}
const serialized = JSON.stringify(parsed, null, 2); // Serialize raw format
let strictIssues: Issue[] | undefined;
if (strict) {
try {
await runStrictValidationIfRequested(parsed);
}catch (err: any) {
const rawForMap = serialized;
const pretty = prettifySwaggerParserErrorToEditorStyle(
err?.message ?? String(err), rawForMap ?? '');
strictIssues = IssueHelper.parsePrettyLinesToIssues(pretty);
}
}
return { format: 'json', raw: serialized, parsed, strictIssues };
}
//Step one - Get rawtext from input
let raw: string;
try {
raw = getRawFromInput(input);
}catch (e: any) {
throw new SpecParseError(String(e.message || e), { source: 'unknown' });
}
//Step two - Create "stripped" variant (removes SPDX comments, empty lines, block comment at the beginning)
const stripped = stripLeadingCommentsAndWhitespace(raw).trimStart();
// Step three - Quick XML detection: If looks like XML -> reject
ensureIsNotXmlLike(stripped);
//Step four - Check if input looks like JSON or YAML
const jsonCandidate = looksLikeJson(stripped);
const yamlCandidate = looksLikeYamlOpenApi(stripped);
if (!jsonCandidate && !yamlCandidate) {
throw new SpecParseError('Innehållet verkar inte vara JSON eller YAML.', { source: 'unknown' });
}
// Step five - Decision logic when choosing parser:
// - prefer === 'never' => YAML-first (skipped JSON)
// - prefer === 'always' => JSON-first (fail-fast)
// - prefer === 'auto' => if looksLikeJson => JSON-first (fail-fast) else YAML-first
const shouldTryJson = (prefer === 'always') || (prefer === 'auto' && jsonCandidate);
const shouldTryYaml = (prefer === 'never' || prefer === 'auto' && yamlCandidate && !jsonCandidate);
let lastJsonError: SpecParseError | SyntaxError |undefined;
let parsedSpec: any;
let target: any;
// Step six - Try parse in choosed order, and return ParseResult or throw SpecParseError
if (shouldTryJson) {
try {
parsedSpec = tryParseJson(raw);
target = 'json';
} catch (e: any) {
if (e instanceof SpecParseError) { // Safecheck, should always be SpecParseError
lastJsonError = e;
//Fail fast for JSON
if (prefer === 'always') throw e;
}else {
//If error is unexpected - propagate up
throw e;
}
}
}else if (!parsedSpec && shouldTryYaml) {
//Step four - Try to parse input as yaml
try {
parsedSpec = tryParseYaml(raw,maxSnippetLength);
target = 'yaml';
}catch (yamlErr: any) {
//Check if error is allready a interpreted error (SpecParseError), throw it further
if (yamlErr instanceof SpecParseError) throw yamlErr;
// Fallback generiskt error - Is there a previous jsonSyntaxErr
if (lastJsonError) throw lastJsonError; // Tidigare fel
throw new SpecParseError('Kunde inte tolka innehållet som JSON eller YAML.', { source: 'unknown' });
}
}
//Make sure parsed specification is OpenAPI like
ensureIsOpenApiLike(parsedSpec,target);
let issues: Issue[] | undefined;
let prettyLines: string[] = [];
//If here - we have a parsed openapi object
if (strict) {
try {
//Run structural validation first (async run)
await runStrictValidationIfRequested(parsedSpec);
}catch (err:any) {
prettyLines = prettifySwaggerParserErrorToEditorStyle(
err?.message ?? String(err), raw ?? '');
}
let spectralDiagnostics: SpectralCore.ISpectralDiagnostic[] = [];
try {
// Build a Spectral Document from raw and the right parser so Spectral gets correct ranges
const parser: IParser<any> = (target === 'json' ? Parsers.Json : Parsers.Yaml) as unknown as IParser<any>;
const apiSpecDocument = new SpectralDocument(raw, parser, '');
//Run sematic validation second(async)
spectralDiagnostics = await semanticValidate(apiSpecDocument) as SpectralCore.ISpectralDiagnostic[];
}catch (e: any) {
// If semantic validation crashes, log it and continue parsing flow
console.error('Spectral semantic validation failed (non-fatal):', e?.message ?? String(e));
spectralDiagnostics = [];
}
const finalIssues = IssueHelper.buildIssuesFromPrettyAndSpectral(prettyLines ?? [], spectralDiagnostics, true /* addOneToLine */);
issues = Array.isArray(finalIssues) && finalIssues.length ? finalIssues : undefined;
issues = finalIssues && finalIssues.length ? finalIssues : undefined;
}
return {format: target, raw, parsed: parsedSpec, strictIssues: issues };
}
/**
* Helper function to decode Base64 encoded string
* @param base64YamlFile
* @returns decoded string
*/
export function decodeBase64String(base64YamlFile: string) {
// Import the necessary Node.js module (Buffer is built-in)
const atob = (b64String: string): string =>
Buffer.from(b64String, "base64").toString("utf-8");
// Decode the base64 string
const decodedYaml = atob(base64YamlFile);
return decodedYaml;
}
/**
* Helper function to determine if pased object is of type OpenAPI
*/
function isOpenApiLike(obj: any): boolean {
if (!obj || typeof obj !== 'object') return false;
if ('openapi' in obj) return true; // OpenAPI 3.x
if ('swagger' in obj) return true; // Swagger / OpenAPI 2.0
// optionally: also accept presence of 'paths' + 'info'
if ('paths' in obj && 'info' in obj) return true;
return false;
}
/**
* - Helper function to strip leading comments and withspaces
* - Removes lines starting with '#' (SPDX lines) or '//'
* - Removes initial blank lines
* - STOPS when it hits a non-comment line (preserving the rest)
*/
function stripLeadingCommentsAndWhitespace(raw: string): string {
const lines = raw.split(/\r?\n/);
let i = 0;
while (i < lines.length) {
const ln = lines[i].trim();
if (ln === '' || ln.startsWith('#') || ln.startsWith('//')) {
i++;
continue;
}
// hantera inledande /* ... */ blockkommentar (om filen inleds med det)
if (ln.startsWith('/*')) {
// hitta slutet på blockkommentaren
let j = i;
while (j < lines.length && !lines[j].includes('*/')) j++;
i = j + 1;
continue;
}
break;
}
return lines.slice(i).join('\n');
}
/**
* Helper function ( High level)
* Check: parsed object "looks like" OpenAPI (openapi, swagger eller paths+info).
* Återanvänds när caller redan parsat objektet (eller fått 'parsed' input).
*/
function ensureIsOpenApiLike(parsed: any, target: any) {
if (!isOpenApiLike(parsed)) {
throw new SpecParseError('Filen verkar inte vara en giltig OpenAPI-specifikation (saknar openapi/swagger eller paths+info).', { source: target });
}
}
/**
* Helper function ( High level)
* Check: parsed object "looks like" OpenAPI (openapi, swagger eller paths+info).
* @param stripped
*/
function ensureIsNotXmlLike(stripped: string) {
if (/^\s*<\?xml|^\s*<[\w-]+[\s>]/i.test(stripped)) {
throw SpecParseError.fromXmlNotice();
}
}
/**
* Helper function ( High level)
* Simple heuristic: does the text look like JSON?
* Run against stripped text (no SPDX header etc.). * Enkel heuristik: ser texten ut som JSON?
*/
function looksLikeJson(stripped: string): boolean {
const s = stripped.trimStart();
return s.startsWith('{') || s.startsWith('[');
}
/**
* Helper function ( High level)
* Simple heuristic: does the text look like JSON?
* Run against stripped text (no SPDX header etc.). * Enkel heuristik: ser texten ut som JSON?
*/
function looksLikeYamlOpenApi(stripped: string): boolean {
const s = stripped.trimStart();
if (s.startsWith('openapi:') || s.startsWith('swagger:') || s.startsWith('---') || s.startsWith('info:')) return true;
return /^[a-zA-Z0-9_-]+\s*:/.test(s);
}
/**
* Helper function JSON( high level )
* @param raw - raw json
* @returns return parsed json
*/
function tryParseJson(raw: string): any {
try {
return JSON.parse(raw);
} catch (e: any) {
// normal JSON parse error -> convert to SpecParseError
throw SpecParseError.fromJsonError(e);
}
}
/**
* Helper function to parse YAML( high level )
* @param raw - raw yaml
* @returns return parsed yaml
*/
function tryParseYaml(raw: string, maxSnippetLength: number): any {
try {
const parsed = yaml.load(raw);
return parsed;
} catch (e: any) {
if (e && typeof e === 'object' && (e.name === 'YAMLException' || e.mark)) {
const spe = SpecParseError.fromYamlError(e);
if (spe.snippet && spe.snippet.length > maxSnippetLength) {
spe.snippet = spe.snippet.slice(0, maxSnippetLength) + '...(truncated)';
}
throw spe; // Throw SpecParseError
}
// (un)normal JSON parse error -> convert to SpecParseError
throw new SpecParseError('Kunde inte tolka YAML-innehållet.', { source: 'yaml' });
}
}
function normalizeRaw(raw: string): string {
return raw
.replace(/^\uFEFF/, "") // ta bort BOM
.replace(/\r\n/g, "\n") // normalisera newline
.replace(/^\s*\n/, ""); // ta bort exakt EN ledande tomrad
}
/**
* Helper function ( high level ) to get raw from input
* @param input
* @returns
*/
export function getRawFromInput(input: SpecInput): string {
if ('filePath' in input) {
return fs2.readFileSync(input.filePath, 'utf8');
} else if ('raw' in input) {
return input.raw;
} else if ('base64' in input) {
return decodeBase64String(input.base64);
} else if ('parsed' in input) {
throw new Error('invalid variant to parse with');
}
throw new Error('Unexpected error - invalid input when trying to get raw from input');
}
/**
* Helper function (High level)
* Run strict validation with @apidevtools/swagger-parser.
*/
async function runStrictValidationIfRequested(parsed: any): Promise<void> {
try {
await SwaggerParser.validate(parsed);
} catch (e: any) {
// Encapsulate errors in SpecParseError so the rest of the system can handle them uniformly
const msg = e?.message ? String(e.message) : String(e);
throw new SpecParseError(`Strict validation failed: ${msg}`, { source: 'strict' });
}
}
/**
* R
* @returns
*/
async function ensureFetch(): Promise<typeof globalThis.fetch> {
if (typeof globalThis.fetch === 'function') return globalThis.fetch;
// dynamisk import av node-fetch endast om nödvändigt
const nodeFetch = (await import('node-fetch')).default;
return nodeFetch as unknown as typeof globalThis.fetch;
}
/**
* semanticValidate - minimal implementation (Alternativ 1)
*/
export async function semanticValidate(apiSpecDocument: SpectralDocument): Promise<SpectralCore.ISpectralDiagnostic[]> {
const runner = new RapLPCustomSpectral();
//Hardcoded rules to extend ( Should be able to config thoose)
let selectedRules: string[] = ['path-params', 'operation-operationId-unique', 'operation-parameters','oas3-schema'];
//let selectedRules: string[] = ['oas3-schema'];
// Start of ruleset string to extend ! ( Must look like this)
const rulesetYaml = `extends: spectral:oas
rules: {}`;
//Load rules temporary with bundle method
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'spectral-ruleset-'));
const tmpFile = path.join(tmpDir, '.spectral-temp.yaml');
await fs.writeFile(tmpFile, rulesetYaml, 'utf8');
try {
const fetchImpl = await ensureFetch();
const bundled = await bundleAndLoadRuleset(tmpFile, { fs: require('fs'), fetch: fetchImpl });
if (!bundled) throw new Error('bundleAndLoadRuleset returned nothing.');
// List existing keys and match expecting ones
const expectedKeys = Object.keys(bundled.rules);
//console.log('sematicKeys - ExpectedKeys:', expectedKeys);
const matchingKeys = selectedRules.filter(key => expectedKeys.includes(key));
//console.log('sematicKeys - MatchingKeys:', matchingKeys);
//Enable matching rules, disable thoose that dont match
for (const k of expectedKeys) {
if (matchingKeys.includes(k)) {
bundled.rules[k].enabled = true;
} else {
bundled.rules[k].enabled = false;
}
}
(runner as any).spectral.setRuleset(bundled);
let obj: any;
if ('source' in apiSpecDocument && apiSpecDocument.source) {
obj = apiSpecDocument.source; // ofta original YAML/JSON som POJO
} else {
obj = apiSpecDocument;
}
//Run async
const results = await runner.runSemanticValidation(apiSpecDocument);
/*console.log('RESULT SPECTRAL :', JSON.stringify(results,null,2));
console.log('DBG: spectral raw diagnostics (range.start.line may be 0- or 1-based):');
console.log(JSON.stringify(results.map(r => ({
code: r.code,
path: r.path,
rawRangeStart: r.range?.start?.line ?? null,
message: r.message
})), null, 2));
*/
return results;
} finally {
// Cleanup temporary files
try { await fs.rm(tmpDir, { recursive: true, force: true }); } catch {}
}
}
function normalizeSpecRaw(raw: string): string {
if (typeof raw !== 'string') return raw;
// 1) Remove BOM if present
if (raw.charCodeAt(0) === 0xFEFF) raw = raw.slice(1);
// 2) Normalize CRLF -> LF
raw = raw.replace(/\r\n/g, '\n');
// 3) Remove leading SPDX block comments (optional — keep if you strip them elsewhere)
// raw = raw.replace(/^\/\*![\s\S]*?\*\/\n*/, ''); // uncomment if used
// 4) Remove leading blank lines (one or many)
raw = raw.replace(/^\n+/, '');
return raw;
}