Skip to content

Commit aeb151c

Browse files
authored
Merge pull request #31 from amuslija/chore/support-multiple-properties-types
Chore: Support multiple properties types
2 parents 79e5065 + 605162b commit aeb151c

8 files changed

Lines changed: 6277 additions & 8 deletions

package-lock.json

Lines changed: 6153 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "serverless-reqvalidator-plugin",
3-
"version": "1.0.3",
3+
"version": "3.0.0",
44
"description": "Serverless plugin for setting request validation",
55
"main": "src/index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "jest test"
88
},
99
"repository": {
1010
"type": "git",
@@ -23,5 +23,10 @@
2323
"bugs": {
2424
"url": "https://github.com/RafPe/serverless-reqvalidator-plugin/issues"
2525
},
26-
"homepage": "https://github.com/RafPe/serverless-reqvalidator-plugin#readme"
26+
"homepage": "https://github.com/RafPe/serverless-reqvalidator-plugin#readme",
27+
"devDependencies": {
28+
"ajv": "^8.11.2",
29+
"jest": "^29.3.1",
30+
"js-yaml": "^4.1.0"
31+
}
2732
}

src/index.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@
3333
* - https://www.snip2code.com/Snippet/1467589/adds-the-posibility-to-configure-AWS_IAM/
3434
*/
3535

36+
const ServerlessReqValidatorPluginConfigSchema = {
37+
properties: {
38+
reqValidatorName: {
39+
anyOf: [
40+
{ type: 'string' },
41+
{ type: 'object',
42+
properties: {
43+
'Fn::ImportValue': { type: 'string' }
44+
},
45+
required: ['Fn::ImportValue'],
46+
},
47+
]
48+
},
49+
},
50+
}
3651
class ServerlessReqValidatorPlugin {
3752
constructor(serverless, options) {
3853
this.serverless = serverless;
@@ -47,11 +62,7 @@ class ServerlessReqValidatorPlugin {
4762
this._beforeDeploy = this.beforeDeploy.bind(this)
4863

4964
// Create schema for your properties. For reference use https://github.com/ajv-validator/ajv
50-
serverless.configSchemaHandler.defineFunctionEventProperties('aws', 'http', {
51-
properties: {
52-
reqValidatorName: { type: 'string' },
53-
},
54-
});
65+
serverless.configSchemaHandler.defineFunctionEventProperties('aws', 'http', ServerlessReqValidatorPluginConfigSchema);
5566

5667
this.hooks = {
5768
'before:package:finalize': this._beforeDeploy
@@ -113,3 +124,4 @@ class ServerlessReqValidatorPlugin {
113124
}
114125

115126
module.exports = ServerlessReqValidatorPlugin;
127+
module.exports.ServerlessReqValidatorPluginConfigSchema = ServerlessReqValidatorPluginConfigSchema;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
plugins:
2+
- serverless-reqvalidator-plugin
3+
service: my-service-b
4+
functions:
5+
hello:
6+
handler: handler.myHandler
7+
events:
8+
- http:
9+
path: hello
10+
reqValidatorName:
11+
Fn::ImportValue: 'myReqValidator'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
plugins:
2+
- serverless-reqvalidator-plugin
3+
service: my-service-d
4+
functions:
5+
hello:
6+
handler: handler.myHandler
7+
events:
8+
- http:
9+
path: hello
10+
reqValidatorName:
11+
data: 123
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
plugins:
2+
- serverless-reqvalidator-plugin
3+
service: my-service-a
4+
functions:
5+
hello:
6+
handler: handler.myHandler
7+
events:
8+
- http:
9+
path: hello
10+
reqValidatorName: 'myReqValidator'
11+
12+
resources:
13+
Resources:
14+
xMyRequestValidator:
15+
Type: "AWS::ApiGateway::RequestValidator"
16+
Properties:
17+
Name: 'my-req-validator'
18+
RestApiId:
19+
Ref: ApiGatewayRestApi
20+
ValidateRequestBody: true
21+
ValidateRequestParameters: false
22+
Outputs:
23+
xMyRequestValidator:
24+
Value:
25+
Ref: my-req-validator
26+
Export:
27+
Name: myReqValidator
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
plugins:
2+
- serverless-reqvalidator-plugin
3+
service: my-service-c
4+
functions:
5+
hello:
6+
handler: handler.myHandler
7+
events:
8+
- http:
9+
path: hello

test/schema.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const Ajv = require('ajv');
2+
const yaml = require('js-yaml');
3+
const fs = require('fs');
4+
5+
const { ServerlessReqValidatorPluginConfigSchema } = require('../src/index');
6+
7+
const ajv = new Ajv();
8+
9+
const validateConfig = ajv.compile({
10+
type: 'object',
11+
...ServerlessReqValidatorPluginConfigSchema,
12+
});
13+
14+
describe('serverless-reqvalidator-plugin schema', () => {
15+
it('should validate a configuration without reqValidatorName property', () => {
16+
const config = yaml.load(fs.readFileSync('./test/fixtures/noValidatorFunction.yaml', 'utf8'));
17+
18+
const valid = validateConfig(config.functions.hello.events[0].http);
19+
expect(valid).toBeTruthy();
20+
});
21+
22+
it('should validate a configuration with a local reqValidatorName property', () => {
23+
const config = yaml.load(fs.readFileSync('./test/fixtures/localValidatorFunction.yaml', 'utf8'));
24+
25+
const valid = validateConfig(config.functions.hello.events[0].http);
26+
expect(valid).toBeTruthy();
27+
});
28+
29+
it('should validate a configuration with an external reqValidatorName property', () => {
30+
const config = yaml.load(fs.readFileSync('./test/fixtures/externalValidatorFunction.yaml', 'utf8'));
31+
32+
const valid = validateConfig(config.functions.hello.events[0].http);
33+
expect(valid).toBeTruthy();
34+
});
35+
36+
it('should not validate a configuration with an invalid reqValidatorName property', () => {
37+
const config = yaml.load(fs.readFileSync('./test/fixtures/invalidValidatorFunction.yaml', 'utf8'));
38+
const valid = validateConfig(config.functions.hello.events[0].http);
39+
expect(valid).toBeFalsy();
40+
});
41+
});

0 commit comments

Comments
 (0)