Skip to content

Commit 0a61b43

Browse files
ARSN-227: refining type and validation
1 parent c014e63 commit 0a61b43

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed

lib/models/BucketInfo.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export default class BucketInfo {
6060
_objectLockEnabled?: boolean;
6161
_objectLockConfiguration?: any;
6262
_notificationConfiguration?: any;
63-
_tags?: Array<BucketTag> | [];
63+
_tags?: Array<BucketTag>;
6464
_readLocationConstraint: string | null;
6565
_isNFS: boolean | null;
6666
_azureInfo: any | null;
@@ -244,7 +244,7 @@ export default class BucketInfo {
244244
};
245245

246246
if (tags === undefined) {
247-
tags = [];
247+
tags = [] as BucketTag[];
248248
}
249249
validateTags(tags);
250250

@@ -864,7 +864,7 @@ export default class BucketInfo {
864864
* Set bucket tags
865865
* @return - bucket info instance
866866
*/
867-
setTags(tags: Array<BucketTag> | []) {
867+
setTags(tags: Array<BucketTag>) {
868868
this._tags = tags;
869869
return this;
870870
}

lib/s3middleware/tagging.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ export const _validator = {
3737
&& tag.Key[0] !== undefined && tag.Value[0] !== undefined
3838
&& typeof tag.Key[0] === 'string' && typeof tag.Value[0] === 'string',
3939

40+
// Allowed characters are letters, whitespace, and numbers, plus
41+
// the following special characters: + - = . _ : /
42+
// Maximum key length: 128 Unicode characters
43+
// Maximum value length: 256 Unicode characters
4044
validateTagObjectStructure: (tag: BucketTag) => tag
4145
&& Object.keys(tag).length === 2
42-
&& tag.Key && tag.Value
43-
&& tag.Key.length >= 1 && tag.Value.length >= 1
44-
&& typeof tag.Key === 'string' && typeof tag.Value === 'string',
46+
&& typeof tag.Key === 'string' && typeof tag.Value === 'string'
47+
&& tag.Key.length >= 1 && tag.Value.length >= 1,
4548

4649
validateXMLStructure: (result: any) =>
4750
result && Object.keys(result).length === 1 &&
@@ -111,8 +114,9 @@ function _validateTags(tags: Array<{ Key: string[], Value: string[] }>) {
111114
}
112115
// not repeating keys
113116
if (tags.length > Object.keys(tagsResult).length) {
114-
return errors.InvalidTag.customizeDescription('Cannot provide ' +
115-
'multiple Tags with the same key');
117+
return errors.InvalidTag.customizeDescription(
118+
'Cannot provide multiple Tags with the same key'
119+
);
116120
}
117121
return tagsResult;
118122
}
@@ -138,18 +142,8 @@ export function validateTags(tags: Array<BucketTag>) {
138142
if (!_validator.validateTagObjectStructure(tag)) {
139143
throw errors.MalformedXML;
140144
}
141-
const key = tag.Key;
142-
const value = tag.Value;
143-
144-
if (!key) {
145-
throw errors.InvalidTag.customizeDescription('The TagKey you ' +
146-
'have provided is invalid');
147-
}
145+
const { Key: key, Value: value } = tag;
148146

149-
// Allowed characters are letters, whitespace, and numbers, plus
150-
// the following special characters: + - = . _ : /
151-
// Maximum key length: 128 Unicode characters
152-
// Maximum value length: 256 Unicode characters
153147
const result = _validator.validateKeyValue(key, value);
154148
if (result instanceof Error) {
155149
throw result;

0 commit comments

Comments
 (0)