-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from splitio/sdks-7670
[SDKS-7670] flag set input validation improvements
- Loading branch information
Showing
8 changed files
with
116 additions
and
106 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
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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
const TRIMMABLE_SPACES_REGEX = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/; | ||
|
||
const EMPTY_FLAG_SETS = 'you passed an empty flag-sets, flag-sets must be a non-empty array.'; | ||
const NULL_FLAG_SETS = 'you passed a null or undefined flag-sets, flag-sets must be a non-empty array.'; | ||
|
||
module.exports = { | ||
TRIMMABLE_SPACES_REGEX, | ||
EMPTY_FLAG_SETS, | ||
NULL_FLAG_SETS, | ||
}; |
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 |
---|---|---|
@@ -1,19 +1,18 @@ | ||
const { NULL_FLAG_SETS, EMPTY_FLAG_SETS } = require('../constants'); | ||
const errorWrapper = require('./wrapper/error'); | ||
const okWrapper = require('./wrapper/ok'); | ||
const TRIMMABLE_SPACES_REGEX = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/; | ||
const { NULL_FLAG_SET, EMPTY_FLAG_SET, TRIMMABLE_SPACES_REGEX } = require('../constants'); | ||
|
||
const validateFlagSet = (maybeFlagSet) => { | ||
// eslint-disable-next-line | ||
if (maybeFlagSet == undefined) return errorWrapper(NULL_FLAG_SETS); | ||
if (maybeFlagSet == undefined) return errorWrapper(NULL_FLAG_SET); | ||
|
||
if (TRIMMABLE_SPACES_REGEX.test(maybeFlagSet)) { | ||
console.log(`flag-sets "${maybeFlagSet}" has extra whitespace, trimming.`); | ||
maybeFlagSet = maybeFlagSet.trim(); | ||
} | ||
if (maybeFlagSet.length === 0) return errorWrapper(EMPTY_FLAG_SETS); | ||
if (maybeFlagSet.length === 0) return errorWrapper(EMPTY_FLAG_SET); | ||
|
||
return okWrapper(maybeFlagSet.split(',')); | ||
return okWrapper(maybeFlagSet); | ||
}; | ||
|
||
module.exports = validateFlagSet; |
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,28 @@ | ||
const errorWrapper = require('./wrapper/error'); | ||
const okWrapper = require('./wrapper/ok'); | ||
const lang = require('../lang'); | ||
const validateFlagSet = require('./flagSet'); | ||
const { EMPTY_FLAG_SETS, NULL_FLAG_SETS } = require('../constants'); | ||
|
||
const validateFlagSets = (maybeFlagSets) => { | ||
// eslint-disable-next-line eqeqeq | ||
if (maybeFlagSets == undefined) return errorWrapper(NULL_FLAG_SETS); | ||
|
||
maybeFlagSets = maybeFlagSets.split(','); | ||
|
||
if (maybeFlagSets.length > 0) { | ||
let validatedArray = []; | ||
// Remove invalid values | ||
maybeFlagSets.forEach(maybeFlagSet => { | ||
const flagSetValidation = validateFlagSet(maybeFlagSet); | ||
if (flagSetValidation.valid) validatedArray.push(flagSetValidation.value); | ||
}); | ||
|
||
// Strip off duplicated values if we have valid flag sets then return | ||
if (validatedArray.length) return okWrapper(lang.uniq(validatedArray)); | ||
} | ||
|
||
return errorWrapper(EMPTY_FLAG_SETS); | ||
}; | ||
|
||
module.exports = validateFlagSets; |
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