diff --git a/lib/plugins/aws/package/compile/events/cloudFront/index.js b/lib/plugins/aws/package/compile/events/cloudFront/index.js index 78bf860b002..91d0e882706 100644 --- a/lib/plugins/aws/package/compile/events/cloudFront/index.js +++ b/lib/plugins/aws/package/compile/events/cloudFront/index.js @@ -85,13 +85,72 @@ class AwsCompileCloudFrontEvents { properties: { AllowedMethods: { oneOf: [ - { enum: ['GET', 'HEAD'] }, - { enum: ['GET', 'HEAD', 'OPTIONS'] }, - { enum: ['GET', 'HEAD', 'OPTIONS', 'PUT', 'PATCH', 'POST', 'DELETE'] }, + { + type: 'array', + uniqueItems: true, + minItems: 2, + items: { enum: ['GET', 'HEAD'] }, + }, + { + type: 'array', + uniqueItems: true, + minItems: 3, + items: { enum: ['GET', 'HEAD', 'OPTIONS'] }, + }, + { + type: 'array', + uniqueItems: true, + minItems: 7, + items: { enum: ['GET', 'HEAD', 'OPTIONS', 'PUT', 'PATCH', 'POST', 'DELETE'] }, + }, ], }, CachedMethods: { - oneOf: [{ enum: ['GET', 'HEAD'] }, { enum: ['GET', 'HEAD', 'OPTIONS'] }], + oneOf: [ + { + type: 'array', + uniqueItems: true, + minItems: 2, + items: { enum: ['GET', 'HEAD'] }, + }, + { + type: 'array', + uniqueItems: true, + minItems: 3, + items: { enum: ['GET', 'HEAD', 'OPTIONS'] }, + }, + ], + }, + ForwardedValues: { + type: 'object', + properties: { + Cookies: { + oneOf: [ + { + type: 'object', + properties: { + Forward: { enum: ['all', 'none'] }, + }, + additionalProperties: false, + required: ['Forward'], + }, + { + type: 'object', + properties: { + Forward: { const: 'whitelist' }, + WhitelistedNames: { type: 'array', items: { type: 'string' } }, + }, + additionalProperties: false, + required: ['Forward', 'WhitelistedNames'], + }, + ], + }, + Headers: { type: 'array', items: { type: 'string' } }, + QueryString: { type: 'boolean' }, + QueryStringCacheKeys: { type: 'array', items: { type: 'string' } }, + }, + additionalProperties: false, + required: ['QueryString'], }, CachePolicyId: { type: 'string' }, Compress: { type: 'boolean' }, diff --git a/lib/plugins/aws/package/compile/events/cloudFront/index.test.js b/lib/plugins/aws/package/compile/events/cloudFront/index.test.js index b231ebf5765..b089913596b 100644 --- a/lib/plugins/aws/package/compile/events/cloudFront/index.test.js +++ b/lib/plugins/aws/package/compile/events/cloudFront/index.test.js @@ -1662,7 +1662,7 @@ describe('AwsCompileCloudFrontEvents', () => { }); }); -describe('#AwsCompileCloudFrontEvents() Remove VPC & Env Vars', () => { +describe('#AwsCompileCloudFrontEvents() package', () => { const fixtureName = 'functionCloudFront'; const edgeFunctionName = 'foo'; const otherFunctionName = 'bar'; @@ -1670,6 +1670,43 @@ describe('#AwsCompileCloudFrontEvents() Remove VPC & Env Vars', () => { return runServerless({ fixture: fixtureName, configExt: extension, cliArgs: ['package'] }); } + it('Should validate configuration schema', () => { + return extendAndPackage({ + functions: { + [edgeFunctionName]: { + handler: 'myLambdaAtEdge.handler', + events: [ + { + cloudFront: { + origin: 's3://bucketname.s3.amazonaws.com/files', + eventType: 'viewer-response', + behavior: { + ViewerProtocolPolicy: 'https-only', + ForwardedValues: { + QueryString: true, + Cookies: { + Forward: 'all', + }, + }, + AllowedMethods: ['GET', 'HEAD'], + CachedMethods: ['HEAD', 'GET', 'OPTIONS'], + }, + }, + }, + ], + }, + }, + }).then(({ cfTemplate }) => { + const behavior = + cfTemplate.Resources.CloudFrontDistribution.Properties.DistributionConfig + .DefaultCacheBehavior; + expect(behavior.AllowedMethods).to.deep.eq(['GET', 'HEAD']); + expect(behavior.CachedMethods).to.deep.eq(['HEAD', 'GET', 'OPTIONS']); + expect(behavior.ForwardedValues.QueryString).to.eq(true); + expect(behavior.ForwardedValues.Cookies.Forward).to.eq('all'); + }); + }); + it('Should ignore Environment variables if provided in function properties', () => { return extendAndPackage({ functions: { [edgeFunctionName]: { environment: { HELLO: 'WORLD' } } },