From bd5099e15019352ab5ae9b2cd5519eaff50c520e Mon Sep 17 00:00:00 2001 From: Oz Weiss Date: Thu, 24 Sep 2020 17:10:02 +0300 Subject: [PATCH] feat(Config Schema): Schema for AWS `alexaSmartHome` event (#8255) --- .../compile/events/alexaSmartHome/index.js | 30 ++++++++----------- .../events/alexaSmartHome/index.test.js | 30 ------------------- lib/plugins/aws/provider/awsProvider.js | 6 ++++ 3 files changed, 18 insertions(+), 48 deletions(-) diff --git a/lib/plugins/aws/package/compile/events/alexaSmartHome/index.js b/lib/plugins/aws/package/compile/events/alexaSmartHome/index.js index 57e8c3c0455..625b33a835e 100644 --- a/lib/plugins/aws/package/compile/events/alexaSmartHome/index.js +++ b/lib/plugins/aws/package/compile/events/alexaSmartHome/index.js @@ -11,9 +11,19 @@ class AwsCompileAlexaSmartHomeEvents { 'package:compileEvents': this.compileAlexaSmartHomeEvents.bind(this), }; - // TODO: Complete schema, see https://github.com/serverless/serverless/issues/8024 this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'alexaSmartHome', { - anyOf: [{ type: 'string' }, { type: 'object' }], + anyOf: [ + { $ref: '#/definitions/awsAlexaEventToken' }, + { + type: 'object', + properties: { + appId: { $ref: '#/definitions/awsAlexaEventToken' }, + enabled: { type: 'boolean' }, + }, + required: ['appId'], + additionalProperties: false, + }, + ], }); } @@ -30,15 +40,6 @@ class AwsCompileAlexaSmartHomeEvents { let Action; if (typeof event.alexaSmartHome === 'object') { - if (!event.alexaSmartHome.appId) { - const errorMessage = [ - `Missing "appId" property for alexaSmartHome event in function ${functionName}`, - ' The correct syntax is: appId: amzn1.ask.skill.xxxx-xxxx', - ' OR an object with "appId" property.', - ' Please check the docs for more info.', - ].join(''); - throw new this.serverless.classes.Error(errorMessage); - } EventSourceToken = event.alexaSmartHome.appId; Action = event.alexaSmartHome.enabled !== false @@ -47,13 +48,6 @@ class AwsCompileAlexaSmartHomeEvents { } else if (typeof event.alexaSmartHome === 'string') { EventSourceToken = event.alexaSmartHome; Action = 'lambda:InvokeFunction'; - } else { - const errorMessage = [ - `Alexa Smart Home event of function "${functionName}" is not an object or string.`, - ' The correct syntax is: alexaSmartHome.', - ' Please check the docs for more info.', - ].join(''); - throw new this.serverless.classes.Error(errorMessage); } const lambdaLogicalId = this.provider.naming.getLambdaLogicalId(functionName); diff --git a/lib/plugins/aws/package/compile/events/alexaSmartHome/index.test.js b/lib/plugins/aws/package/compile/events/alexaSmartHome/index.test.js index 2ec877993d8..fbbae877a60 100644 --- a/lib/plugins/aws/package/compile/events/alexaSmartHome/index.test.js +++ b/lib/plugins/aws/package/compile/events/alexaSmartHome/index.test.js @@ -23,36 +23,6 @@ describe('AwsCompileAlexaSmartHomeEvents', () => { }); describe('#compileAlexaSmartHomeEvents()', () => { - it('should throw an error if alexaSmartHome event type is not a string or an object', () => { - awsCompileAlexaSmartHomeEvents.serverless.service.functions = { - first: { - events: [ - { - alexaSmartHome: 42, - }, - ], - }, - }; - - expect(() => awsCompileAlexaSmartHomeEvents.compileAlexaSmartHomeEvents()).to.throw(Error); - }); - - it('should throw an error if the "appId" property is not given', () => { - awsCompileAlexaSmartHomeEvents.serverless.service.functions = { - first: { - events: [ - { - alexaSmartHome: { - appId: null, - }, - }, - ], - }, - }; - - expect(() => awsCompileAlexaSmartHomeEvents.compileAlexaSmartHomeEvents()).to.throw(Error); - }); - it('should create corresponding resources when alexaSmartHome events are given', () => { awsCompileAlexaSmartHomeEvents.serverless.service.functions = { first: { diff --git a/lib/plugins/aws/provider/awsProvider.js b/lib/plugins/aws/provider/awsProvider.js index d5a4126d15d..47aaf08c04f 100644 --- a/lib/plugins/aws/provider/awsProvider.js +++ b/lib/plugins/aws/provider/awsProvider.js @@ -142,6 +142,12 @@ class AwsProvider { // TODO: Complete schema, see https://github.com/serverless/serverless/issues/8016 serverless.configSchemaHandler.defineProvider('aws', { definitions: { + awsAlexaEventToken: { + type: 'string', + minLength: 0, + maxLength: 256, + pattern: '^[a-zA-Z0-9._\\-]+$', + }, awsArn: { oneOf: [ { $ref: '#/definitions/awsArnString' },