Skip to content

Commit 5495eaa

Browse files
authored
Merge pull request #32 from alexdrans/master
adding support for existing validator id
2 parents aeb151c + e2af776 commit 5495eaa

4 files changed

Lines changed: 58 additions & 0 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,23 @@ functions:
103103
Fn::ImportValue: 'myReqValidator'
104104
```
105105

106+
### Use an external validator by ID
107+
If you have an existing request validator defined outside of CloudFormation e.g. Terraform, you can reference it by id.
108+
109+
```
110+
plugins:
111+
- serverless-reqvalidator-plugin
112+
service: my-service-a
113+
functions:
114+
hello:
115+
handler: handler.myHandler
116+
events:
117+
- http:
118+
path: hello
119+
reqValidatorName:
120+
id: 'g5ch0h'
121+
```
122+
106123
### Full example
107124
```
108125
service:

src/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@
2929
* reqValidatorName:
3030
* Fn::ImportValue: 'my-import-value'
3131
*
32+
* Use request validator by Id:
33+
*
34+
* myFuncGetItem:
35+
* handler: myFunc.get
36+
* name: ${self:provider.stage}-myFunc-get-item
37+
* events:
38+
* - http:
39+
* method: GET
40+
* path: mypath
41+
* cors: true
42+
* reqValidatorName:
43+
* id: 'g5ch0h'
44+
*
45+
*
46+
*
3247
* Resources used:
3348
* - https://www.snip2code.com/Snippet/1467589/adds-the-posibility-to-configure-AWS_IAM/
3449
*/
@@ -44,6 +59,12 @@ const ServerlessReqValidatorPluginConfigSchema = {
4459
},
4560
required: ['Fn::ImportValue'],
4661
},
62+
{ type: 'object',
63+
properties: {
64+
id: { type: 'string' },
65+
},
66+
required: ['id'],
67+
},
4768
]
4869
},
4970
},
@@ -106,6 +127,8 @@ class ServerlessReqValidatorPlugin {
106127
case 'object':
107128
if (reqValidatorName['Fn::ImportValue']) {
108129
resources[methodName].Properties.RequestValidatorId = reqValidatorName;
130+
} else if (reqValidatorName['id']) {
131+
resources[methodName].Properties.RequestValidatorId = reqValidatorName['id']
109132
} else { // other use cases should be added here
110133
resources[methodName].Properties.RequestValidatorId = reqValidatorName;
111134
}
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+
id: 'abc123'

test/schema.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,11 @@ describe('serverless-reqvalidator-plugin schema', () => {
3838
const valid = validateConfig(config.functions.hello.events[0].http);
3939
expect(valid).toBeFalsy();
4040
});
41+
42+
it('should validate a configuration with an external reqValidatorName.id property', () => {
43+
const config = yaml.load(fs.readFileSync('./test/fixtures/externalValidatorId.yaml', 'utf8'));
44+
45+
const valid = validateConfig(config.functions.hello.events[0].http);
46+
expect(valid).toBeTruthy();
47+
})
4148
});

0 commit comments

Comments
 (0)