UNPKG

1.38 kBJavaScriptView Raw
1const jsYml = require('js-yaml');
2const fs = require('fs');
3const path = require('path');
4const merge = require('lodash.merge')
5
6class AddValidation {
7 invoke(options, content, serverless) {
8 try {
9 if (!options.validation) {
10 return content;
11 }
12
13 const validators = this.readTemplate(serverless);
14 if (!content.hasOwnProperty('x-amazon-apigateway-request-validators')) {
15 content = merge(content, validators);
16 }
17
18 return content
19 } catch (e) {
20 console.log(e);
21 }
22 }
23
24 readTemplate(serverless) {
25 const templatePath = path.resolve(process.cwd(), 'openapi-integration/request-validator.yml')
26 try {
27 if (fs.existsSync(templatePath)) {
28 serverless.cli.log(
29 `Process custom VALIDATION template`,
30 'OpenApi Integration Plugin'
31 );
32 return jsYml.load(fs.readFileSync(templatePath))
33 }
34 } catch (err) {
35 console.error(err)
36 }
37
38 serverless.cli.log(
39 `Process default VALIDATION template`,
40 'OpenApi Integration Plugin'
41 );
42 return jsYml.load(fs.readFileSync(__dirname + '/../../templates/validator/request-validator.yml'));
43 }
44}
45
46module.exports = AddValidation