diff --git a/CHANGELOG.md b/CHANGELOG.md index b1338e3..85e7cfd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- Merge PR #223, allowing Ref usage inside SAM Global ## [1.9.5] - 2019-02-05 ### Fixed diff --git a/src/test/validatorTest.ts b/src/test/validatorTest.ts index 7460618..b97b82c 100644 --- a/src/test/validatorTest.ts +++ b/src/test/validatorTest.ts @@ -1924,5 +1924,12 @@ describe('validator', () => { expect(result).to.have.deep.property('templateValid', false); expect(result['errors']['crit']).to.have.lengthOf(2); }); + + it('a valid template with Ref in Globals (sam_20161031_environment_ref.yaml) should validate successfully', () => { + const input = 'testData/valid/yaml/sam_20161031_environment_ref.yaml'; + let result = validator.validateFile(input); + expect(result).to.have.deep.property('templateValid', true); + expect(result['errors']['crit']).to.have.lengthOf(0); + }); }); }); diff --git a/src/validator.ts b/src/validator.ts index 2171685..9b9284e 100644 --- a/src/validator.ts +++ b/src/validator.ts @@ -852,7 +852,7 @@ function inferParameterValue(parameterName: string, parameter: any, okToGuess: b normalizedType = 'string'; } - const parameterDefault = parameterDefaultsByType[parameterTypesSpec[parameterType]!]! + const parameterDefault = parameterDefaultsByType[parameterTypesSpec[parameterType]!]! if (isList) { return [parameterDefault]; } else { @@ -1067,11 +1067,12 @@ function recursiveDecent(ref: any){ // Check if an Intrinsic function is allowed here let inResourceProperty = (placeInTemplate[0] == "Resources" || placeInTemplate[2] == "Properties"); let inResourceMetadata = (placeInTemplate[0] == "Resources" || placeInTemplate[2] == "Metadata"); + let inGlobalEnvironment = (placeInTemplate[0] == "Globals" && placeInTemplate[2] == "Environment"); let inOutputs = (placeInTemplate[0] == "Outputs"); let inConditions = (placeInTemplate[0] == "Conditions"); // TODO Check for usage inside update policy - if(!(inResourceProperty || inResourceMetadata || inOutputs || inConditions)){ + if(!(inGlobalEnvironment || inResourceProperty || inResourceMetadata || inOutputs || inConditions)){ addError("crit", `Intrinsic function ${key} is not supported here`, placeInTemplate, key); }else { // Resolve the function @@ -1382,7 +1383,7 @@ function doIntrinsicSelect(ref: any, key: string){ } } else if (list.indexOf(null) > -1) { addError('crit', "Fn::Select requires that the list be free of null values", placeInTemplate, "Fn::Select"); - + } if (index >= 0 && index < list.length) { return list[index]; @@ -1909,7 +1910,7 @@ export interface PrimitiveType { resourceType: string, primitiveType: string } - + export type ObjectType = ResourceType | NamedProperty | PropertyType | PrimitiveType; /** @@ -1929,7 +1930,7 @@ function getTypeName(objectType: ResourceType | NamedProperty | PropertyType ): } /** - * + * */ function getItemType(objectType: NamedProperty): PrimitiveType | PropertyType { const maybePrimitiveType = resourcesSpec.getPrimitiveItemType(objectType.parentType, objectType.propertyName); @@ -2064,7 +2065,7 @@ function check(objectType: ObjectType, objectToCheck: any) { verify(isList, objectToCheck); checkList(objectType as NamedProperty, objectToCheck); break; - case KnownTypes.Arn: + case KnownTypes.Arn: verify(isArn, objectToCheck); break; case KnownTypes.String: @@ -2338,7 +2339,7 @@ function checkComplexObject(objectType: ResourceType | NamedProperty | PropertyT parentType: objectTypeName, propertyName: subPropertyName } as NamedProperty; - + check(subPropertyObjectType, propertyValue) } finally { diff --git a/testData/valid/yaml/sam_20161031_environment_ref.yaml b/testData/valid/yaml/sam_20161031_environment_ref.yaml new file mode 100644 index 0000000..5728467 --- /dev/null +++ b/testData/valid/yaml/sam_20161031_environment_ref.yaml @@ -0,0 +1,17 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: Tests if Ref is allowed in Globals + +Parameters: + LocalVar: + Type: String + +Globals: + Function: + Environment: + Variables: + GLOB_ENV_VAR: !Ref LocalVar + +Resources: + Bucket: + Type: AWS::S3::Bucket