Skip to content

Commit

Permalink
Merge pull request #31 from amuslija/chore/support-multiple-propertie…
Browse files Browse the repository at this point in the history
…s-types

Chore: Support multiple properties types
  • Loading branch information
RafPe authored Dec 27, 2022
2 parents 79e5065 + 605162b commit aeb151c
Show file tree
Hide file tree
Showing 8 changed files with 6,277 additions and 8 deletions.
6,153 changes: 6,153 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "serverless-reqvalidator-plugin",
"version": "1.0.3",
"version": "3.0.0",
"description": "Serverless plugin for setting request validation",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest test"
},
"repository": {
"type": "git",
Expand All @@ -23,5 +23,10 @@
"bugs": {
"url": "https://github.com/RafPe/serverless-reqvalidator-plugin/issues"
},
"homepage": "https://github.com/RafPe/serverless-reqvalidator-plugin#readme"
"homepage": "https://github.com/RafPe/serverless-reqvalidator-plugin#readme",
"devDependencies": {
"ajv": "^8.11.2",
"jest": "^29.3.1",
"js-yaml": "^4.1.0"
}
}
22 changes: 17 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@
* - https://www.snip2code.com/Snippet/1467589/adds-the-posibility-to-configure-AWS_IAM/
*/

const ServerlessReqValidatorPluginConfigSchema = {
properties: {
reqValidatorName: {
anyOf: [
{ type: 'string' },
{ type: 'object',
properties: {
'Fn::ImportValue': { type: 'string' }
},
required: ['Fn::ImportValue'],
},
]
},
},
}
class ServerlessReqValidatorPlugin {
constructor(serverless, options) {
this.serverless = serverless;
Expand All @@ -47,11 +62,7 @@ class ServerlessReqValidatorPlugin {
this._beforeDeploy = this.beforeDeploy.bind(this)

// Create schema for your properties. For reference use https://github.com/ajv-validator/ajv
serverless.configSchemaHandler.defineFunctionEventProperties('aws', 'http', {
properties: {
reqValidatorName: { type: 'string' },
},
});
serverless.configSchemaHandler.defineFunctionEventProperties('aws', 'http', ServerlessReqValidatorPluginConfigSchema);

this.hooks = {
'before:package:finalize': this._beforeDeploy
Expand Down Expand Up @@ -113,3 +124,4 @@ class ServerlessReqValidatorPlugin {
}

module.exports = ServerlessReqValidatorPlugin;
module.exports.ServerlessReqValidatorPluginConfigSchema = ServerlessReqValidatorPluginConfigSchema;
11 changes: 11 additions & 0 deletions test/fixtures/externalValidatorFunction.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins:
- serverless-reqvalidator-plugin
service: my-service-b
functions:
hello:
handler: handler.myHandler
events:
- http:
path: hello
reqValidatorName:
Fn::ImportValue: 'myReqValidator'
11 changes: 11 additions & 0 deletions test/fixtures/invalidValidatorFunction.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins:
- serverless-reqvalidator-plugin
service: my-service-d
functions:
hello:
handler: handler.myHandler
events:
- http:
path: hello
reqValidatorName:
data: 123
27 changes: 27 additions & 0 deletions test/fixtures/localValidatorFunction.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
plugins:
- serverless-reqvalidator-plugin
service: my-service-a
functions:
hello:
handler: handler.myHandler
events:
- http:
path: hello
reqValidatorName: 'myReqValidator'

resources:
Resources:
xMyRequestValidator:
Type: "AWS::ApiGateway::RequestValidator"
Properties:
Name: 'my-req-validator'
RestApiId:
Ref: ApiGatewayRestApi
ValidateRequestBody: true
ValidateRequestParameters: false
Outputs:
xMyRequestValidator:
Value:
Ref: my-req-validator
Export:
Name: myReqValidator
9 changes: 9 additions & 0 deletions test/fixtures/noValidatorFunction.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins:
- serverless-reqvalidator-plugin
service: my-service-c
functions:
hello:
handler: handler.myHandler
events:
- http:
path: hello
41 changes: 41 additions & 0 deletions test/schema.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const Ajv = require('ajv');
const yaml = require('js-yaml');
const fs = require('fs');

const { ServerlessReqValidatorPluginConfigSchema } = require('../src/index');

const ajv = new Ajv();

const validateConfig = ajv.compile({
type: 'object',
...ServerlessReqValidatorPluginConfigSchema,
});

describe('serverless-reqvalidator-plugin schema', () => {
it('should validate a configuration without reqValidatorName property', () => {
const config = yaml.load(fs.readFileSync('./test/fixtures/noValidatorFunction.yaml', 'utf8'));

const valid = validateConfig(config.functions.hello.events[0].http);
expect(valid).toBeTruthy();
});

it('should validate a configuration with a local reqValidatorName property', () => {
const config = yaml.load(fs.readFileSync('./test/fixtures/localValidatorFunction.yaml', 'utf8'));

const valid = validateConfig(config.functions.hello.events[0].http);
expect(valid).toBeTruthy();
});

it('should validate a configuration with an external reqValidatorName property', () => {
const config = yaml.load(fs.readFileSync('./test/fixtures/externalValidatorFunction.yaml', 'utf8'));

const valid = validateConfig(config.functions.hello.events[0].http);
expect(valid).toBeTruthy();
});

it('should not validate a configuration with an invalid reqValidatorName property', () => {
const config = yaml.load(fs.readFileSync('./test/fixtures/invalidValidatorFunction.yaml', 'utf8'));
const valid = validateConfig(config.functions.hello.events[0].http);
expect(valid).toBeFalsy();
});
});

0 comments on commit aeb151c

Please sign in to comment.