Skip to content

Commit

Permalink
feat(Config Schema): Schema for AWS websocket event (#8218)
Browse files Browse the repository at this point in the history
  • Loading branch information
rzaldana committed Sep 18, 2020
1 parent 0ced414 commit e1ca63c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 61 deletions.
4 changes: 4 additions & 0 deletions lib/configSchema.js
Expand Up @@ -129,6 +129,10 @@ const schema = {
type: 'string',
pattern: '^[A-Z0-9_]+$',
},
functionName: {
type: 'string',
pattern: functionNamePattern,
},
},
};

Expand Down
32 changes: 30 additions & 2 deletions lib/plugins/aws/package/compile/events/websockets/index.js
Expand Up @@ -51,9 +51,37 @@ class AwsCompileWebsockets {
},
};

// TODO: Complete schema, see https://github.com/serverless/serverless/issues/8019
this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'websocket', {
anyOf: [{ type: 'string' }, { type: 'object' }],
anyOf: [
{ type: 'string' },
{
type: 'object',
properties: {
route: { type: 'string' },
routeResponseSelectionExpression: {
const: '$default',
},
authorizer: {
anyOf: [
{ $ref: '#/definitions/awsArnString' },
{ $ref: '#/definitions/functionName' },
{
type: 'object',
properties: {
name: { $ref: '#/definitions/functionName' },
arn: { $ref: '#/definitions/awsArnString' },
identitySource: { type: 'array', items: { type: 'string' } },
},
oneOf: [{ required: ['name'] }, { required: ['arn'] }],
additionalProperties: false,
},
],
},
},
required: ['route'],
additionalProperties: false,
},
],
});
}
}
Expand Down
14 changes: 0 additions & 14 deletions lib/plugins/aws/package/compile/events/websockets/lib/validate.js
Expand Up @@ -13,19 +13,9 @@ module.exports = {

_.forEach(this.serverless.service.functions, (functionObject, functionName) => {
functionObject.events.forEach(event => {
// check if we have both, `http` and `websocket` events which is not supported
if (event.websocket && event.http) {
const errorMessage = 'The event type can either be "http" or "websocket" but not both.';
throw new this.serverless.classes.Error(errorMessage);
}
if (event.websocket) {
// dealing with the extended object definition
if (_.isObject(event.websocket)) {
if (!event.websocket.route) {
const errorMessage = 'You need to set the "route" when using the websocket event.';
throw new this.serverless.classes.Error(errorMessage);
}

const websocketObj = {
functionName,
route: event.websocket.route,
Expand Down Expand Up @@ -126,10 +116,6 @@ module.exports = {
],
};
websocketObj.authorizer.permission = lambdaLogicalId;
} else {
const errorMessage =
'You must specify name or arn properties when using a websocket authorizer';
throw new this.serverless.classes.Error(errorMessage);
}

if (!event.websocket.authorizer.identitySource) {
Expand Down
Expand Up @@ -267,49 +267,4 @@ describe('#validate()', () => {
.to.be.an('Array')
.with.length(0);
});

it('should reject a websocket event without a route', () => {
awsCompileWebsocketsEvents.serverless.service.functions = {
first: {
events: [
{
websocket: {},
},
],
},
};
expect(() => awsCompileWebsocketsEvents.validate()).to.throw(/set the "route"/);
});

it('should reject an authorizer definition without name nor arn', () => {
awsCompileWebsocketsEvents.serverless.service.functions = {
first: {
events: [
{
websocket: {
route: '$connect',
authorizer: {
identitySource: ['route.request.header.Auth', 'route.request.querystring.Auth'],
},
},
},
],
},
};
expect(() => awsCompileWebsocketsEvents.validate()).to.throw(/You must specify name or arn/);
});

it('should reject a usage of both, http and websocket event types', () => {
awsCompileWebsocketsEvents.serverless.service.functions = {
first: {
events: [
{
websocket: {},
http: {},
},
],
},
};
expect(() => awsCompileWebsocketsEvents.validate()).to.throw(/can either be/);
});
});
15 changes: 15 additions & 0 deletions lib/plugins/aws/provider/awsProvider.js
Expand Up @@ -314,6 +314,18 @@ class AwsProvider {
},
],
},
websocket: {
oneOf: [
{ type: 'boolean' },
{
type: 'object',
properties: {
level: { enum: ['INFO', 'ERROR'] },
},
additionalProperties: false,
},
],
},
},
},
resourcePolicy: {
Expand All @@ -322,6 +334,9 @@ class AwsProvider {
type: 'object',
},
},
websocketsApiName: { type: 'string' },
websocketsApiRouteSelectionExpression: { type: 'string' },
apiGateway: { type: 'object', properties: { websocketApiId: { type: 'string' } } },
},
},
function: {
Expand Down

0 comments on commit e1ca63c

Please sign in to comment.