From 05103896934fe942e8d0b86f87e5585e55047eef Mon Sep 17 00:00:00 2001 From: ozwi Date: Wed, 16 Sep 2020 11:05:50 +0300 Subject: [PATCH 1/4] alex smart home event schema --- .../compile/events/alexaSmartHome/index.js | 36 +++++++++---------- .../events/alexaSmartHome/index.test.js | 30 ---------------- 2 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..eea4f066375 100644 --- a/lib/plugins/aws/package/compile/events/alexaSmartHome/index.js +++ b/lib/plugins/aws/package/compile/events/alexaSmartHome/index.js @@ -11,9 +11,25 @@ class AwsCompileAlexaSmartHomeEvents { 'package:compileEvents': this.compileAlexaSmartHomeEvents.bind(this), }; - // TODO: Complete schema, see https://github.com/serverless/serverless/issues/8024 + const appIdSchema = { + type: 'string', + minLength: 0, + maxLength: 256, + pattern: '^[a-zA-Z0-9._\\-]+$', + }; + this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'alexaSmartHome', { - anyOf: [{ type: 'string' }, { type: 'object' }], + anyOf: [ + appIdSchema, + { + type: 'object', + properties: { + appId: appIdSchema, + enabled: { type: 'boolean' }, + }, + required: ['appId'], + }, + ], }); } @@ -30,15 +46,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 +54,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: { From c8e65638bb306655c063bcf371bb74fa93590510 Mon Sep 17 00:00:00 2001 From: ozwi Date: Wed, 16 Sep 2020 16:45:41 +0300 Subject: [PATCH 2/4] move awsAlexaEventToken schema to awsProvider make it reusable for alexaSkill --- .../package/compile/events/alexaSmartHome/index.js | 11 ++--------- lib/plugins/aws/provider/awsProvider.js | 6 ++++++ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/plugins/aws/package/compile/events/alexaSmartHome/index.js b/lib/plugins/aws/package/compile/events/alexaSmartHome/index.js index eea4f066375..adb107119a3 100644 --- a/lib/plugins/aws/package/compile/events/alexaSmartHome/index.js +++ b/lib/plugins/aws/package/compile/events/alexaSmartHome/index.js @@ -11,20 +11,13 @@ class AwsCompileAlexaSmartHomeEvents { 'package:compileEvents': this.compileAlexaSmartHomeEvents.bind(this), }; - const appIdSchema = { - type: 'string', - minLength: 0, - maxLength: 256, - pattern: '^[a-zA-Z0-9._\\-]+$', - }; - this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'alexaSmartHome', { anyOf: [ - appIdSchema, + { $ref: '#/definitions/awsAlexaEventToken' }, { type: 'object', properties: { - appId: appIdSchema, + appId: { $ref: '#/definitions/awsAlexaEventToken' }, enabled: { type: 'boolean' }, }, required: ['appId'], diff --git a/lib/plugins/aws/provider/awsProvider.js b/lib/plugins/aws/provider/awsProvider.js index 7afb978bd81..59bdd33fbd8 100644 --- a/lib/plugins/aws/provider/awsProvider.js +++ b/lib/plugins/aws/provider/awsProvider.js @@ -234,6 +234,12 @@ class AwsProvider { UpdateReplacePolicy: { type: 'string' }, Condition: { type: 'string' }, }, + awsAlexaEventToken: { + type: 'string', + minLength: 0, + maxLength: 256, + pattern: '^[a-zA-Z0-9._\\-]+$', + }, }, provider: { properties: { From f7759aa45d4c24bd42bb9cc4b8652d9d9e9a1578 Mon Sep 17 00:00:00 2001 From: ozwi Date: Tue, 22 Sep 2020 11:10:49 +0300 Subject: [PATCH 3/4] add additionalProperties: false --- lib/plugins/aws/package/compile/events/alexaSmartHome/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/plugins/aws/package/compile/events/alexaSmartHome/index.js b/lib/plugins/aws/package/compile/events/alexaSmartHome/index.js index adb107119a3..625b33a835e 100644 --- a/lib/plugins/aws/package/compile/events/alexaSmartHome/index.js +++ b/lib/plugins/aws/package/compile/events/alexaSmartHome/index.js @@ -21,6 +21,7 @@ class AwsCompileAlexaSmartHomeEvents { enabled: { type: 'boolean' }, }, required: ['appId'], + additionalProperties: false, }, ], }); From 9ccb0afe8142d250f6110f9d1b6630ec40efd854 Mon Sep 17 00:00:00 2001 From: ozwi Date: Thu, 24 Sep 2020 16:44:56 +0300 Subject: [PATCH 4/4] alphabetical order --- lib/plugins/aws/provider/awsProvider.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/plugins/aws/provider/awsProvider.js b/lib/plugins/aws/provider/awsProvider.js index 59bdd33fbd8..4cfb0d716ac 100644 --- a/lib/plugins/aws/provider/awsProvider.js +++ b/lib/plugins/aws/provider/awsProvider.js @@ -141,6 +141,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' }, @@ -234,12 +240,6 @@ class AwsProvider { UpdateReplacePolicy: { type: 'string' }, Condition: { type: 'string' }, }, - awsAlexaEventToken: { - type: 'string', - minLength: 0, - maxLength: 256, - pattern: '^[a-zA-Z0-9._\\-]+$', - }, }, provider: { properties: {