Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket event schema #8218

Merged
merged 14 commits into from Sep 18, 2020
Merged
4 changes: 4 additions & 0 deletions lib/configSchema.js
Expand Up @@ -116,6 +116,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,
},
rzaldana marked this conversation as resolved.
Show resolved Hide resolved
],
},
},
},
resourcePolicy: {
Expand All @@ -322,6 +334,9 @@ class AwsProvider {
type: 'object',
},
},
websocketsApiName: { type: 'string' },
websocketsApiRouteSelectionExpression: { type: 'string' },
medikoo marked this conversation as resolved.
Show resolved Hide resolved
apiGateway: { type: 'object', properties: { websocketApiId: { type: 'string' } } },
},
},
function: {
Expand Down