-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into bump-cfnspec-main/v79.0.0
- Loading branch information
Showing
3 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Tag } from '../cdk-toolkit'; | ||
|
||
/** | ||
* Throws an error if tags is neither undefined nor an array of Tags, | ||
* as defined in cdk-toolkit.ts. | ||
* | ||
* It does not attempt to validate the tags themselves. It only validates | ||
* that the objects in the array conform to the Tags interface. | ||
*/ | ||
export function validateTags(tags: any): Tag[] | undefined { | ||
const valid = tags === undefined || ( | ||
Array.isArray(tags) && | ||
tags.every(t => typeof(t.Tag) === 'string' && typeof(t.Value) === 'string') | ||
); | ||
if (valid) { | ||
return tags; | ||
} else { | ||
throw new Error('tags must be an array of { Tag: string, Value: string } objects'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { validateTags } from '../../lib/util/tags'; | ||
|
||
test('validateTags does not throw when given undefined', () => { | ||
expect(validateTags(undefined)).toBeUndefined(); | ||
}); | ||
|
||
test('validateTags does not throw when given an empty array', () => { | ||
const tags: any = []; | ||
expect(validateTags(tags)).toBe(tags); | ||
}); | ||
|
||
test('validateTags does not throw when given array of Tag objects', () => { | ||
const tags: any = [{ Tag: 'a', Value: 'b' }]; | ||
expect(validateTags(tags)).toBe(tags); | ||
}); | ||
|
||
test('validateTags throws when given an object', () => { | ||
expect(() => validateTags({ a: 'b' })).toThrow('tags must be'); | ||
}); | ||
|
||
test('validateTags throws when given an array of non-Tag objects', () => { | ||
expect(() => validateTags([{ a: 'b' }])).toThrow('tags must be'); | ||
}); | ||
|
||
test('validateTags throws when Tag is not a string', () => { | ||
expect(() => validateTags([{ Tag: null, Value: 'b' }])).toThrow(); | ||
}); | ||
|
||
test('validateTags throws when Value is not a string', () => { | ||
expect(() => validateTags([{ Tag: 'a', Value: null }])).toThrow(); | ||
}); |