-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec-validator.ts
156 lines (136 loc) · 4.4 KB
/
spec-validator.ts
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
import * as fs from 'fs';
import * as jsYaml from 'js-yaml';
import * as colors from 'colors';
import * as Debug from 'debug';
import { OpenApiValidator, OpenApiDocument } from 'express-openapi-validate';
import {
Operation,
PathItemObject,
OperationObject,
} from 'express-openapi-validate/dist/OpenApiDocument';
import { EndPointValidator } from './endpoint-validator';
import { OperationConfig } from './operation-config';
import { ParamParser } from './param-parser';
const debug = Debug('oa3-def');
/**
* A class encapsulating the boilerplate required to call the EndpointValidator.
*
* Simply instantiate a new instance of this class with a file path to the spec file,
* and the root URL of the API to test against, and then call the validate() method.
*/
export class SpecValidator {
constructor(
specPath: string,
apiUrl: string,
auth?: string,
endPointValidator?: EndPointValidator, // simple DI
) {
this.specPath = specPath;
this.apiUrl = apiUrl;
this.auth = auth ? auth : undefined;
if (endPointValidator) {
this.endPointValidator = endPointValidator;
}
this._document = this.loadOpenApiSpec();
this._oa3Validator = new OpenApiValidator(this.document, {
ajvOptions: { allErrors: true, verbose: true },
});
}
public endPointValidator: any | undefined;
private specPath: string;
private apiUrl: string;
private auth: string | undefined;
private _document: OpenApiDocument;
get document(): OpenApiDocument {
return this._document;
}
private _oa3Validator: OpenApiValidator;
get oa3Validator(): OpenApiValidator {
return this._oa3Validator;
}
/**
* Parses an OA3 spec using jsYaml & fs
*/
public loadOpenApiSpec(): OpenApiDocument | never {
let doc: OpenApiDocument | undefined;
try {
debug(`Trying to parse spec: ${this.specPath}...`);
doc = jsYaml.safeLoad(
fs.readFileSync(this.specPath, 'utf-8'),
) as OpenApiDocument;
debug('Success!');
return doc;
} catch (err) {
if (err.code === 'ENOENT') {
throw new Error(
`${colors.red(
`No such file or directory: ${colors.red.bold(err.path)}`,
)}`,
);
} else {
throw err;
}
}
}
/**
* Gathers paths & their descendant HTTP Operations from the validator's currently parsed
* OA3 spec and passes then to the static EndpointValidator.validate method.
*/
public validateSpec(): void {
if (!this.document) {
throw new Error('No OA3 document found!');
} else {
const pathsToValidate: string[] = Object.keys(this.document.paths);
debug(`Found ${pathsToValidate.length} paths to validate...`);
pathsToValidate.forEach(path => {
const pathObj: PathItemObject = this.document.paths[path];
const operations = this.getDefinedHttpOperations(pathObj);
const reqBody = {};
operations.forEach(opConfig => {
const paramaterisedPath = ParamParser.generateParamaterisedPath(
path,
opConfig,
);
// very rudimentary DI
if (this.endPointValidator) {
debug(`opConfig: ${JSON.stringify(opConfig, null, 2)}`);
this.endPointValidator.validate(
this._oa3Validator,
opConfig,
path,
this.apiUrl,
);
} else {
EndPointValidator.validate(
this._oa3Validator,
opConfig,
path,
paramaterisedPath,
this.apiUrl,
reqBody,
this.auth,
);
}
});
});
}
}
/**
* Returns an array of Operations from a PathItemObject.
* (currently only supports **get** | **post** | **delete** | **put** | **patch**)
*/
public getDefinedHttpOperations(pathItem: PathItemObject): OperationConfig[] {
const operations: OperationConfig[] = [];
if (pathItem.get)
operations.push({ operation: 'get', config: pathItem.get });
if (pathItem.post)
operations.push({ operation: 'post', config: pathItem.post });
if (pathItem.delete)
operations.push({ operation: 'delete', config: pathItem.delete });
if (pathItem.put)
operations.push({ operation: 'put', config: pathItem.put });
if (pathItem.patch)
operations.push({ operation: 'patch', config: pathItem.patch });
return operations;
}
}