-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config/validation): validate options which support regex/glob ma…
…tching (#28693) Co-authored-by: Sebastian Poxhofer <secustor@users.noreply.github.com> Co-authored-by: Rhys Arkins <rhys@arkins.net>
- Loading branch information
1 parent
2910185
commit 265e628
Showing
8 changed files
with
218 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { check } from './regex-glob-matchers'; | ||
|
||
describe('config/validation-helpers/regex-glob-matchers', () => { | ||
it('should error for multiple match alls', () => { | ||
const res = check({ | ||
val: ['*', '**'], | ||
currentPath: 'hostRules[0].allowedHeaders', | ||
}); | ||
expect(res).toHaveLength(1); | ||
}); | ||
|
||
it('should error for invalid regex', () => { | ||
const res = check({ | ||
val: ['[', '/[/', '/.*[/'], | ||
currentPath: 'hostRules[0].allowedHeaders', | ||
}); | ||
expect(res).toHaveLength(2); | ||
}); | ||
|
||
it('should error for non-strings', () => { | ||
const res = check({ | ||
val: ['*', 2], | ||
currentPath: 'hostRules[0].allowedHeaders', | ||
}); | ||
expect(res).toMatchObject([ | ||
{ | ||
message: | ||
'hostRules[0].allowedHeaders: should be an array of strings. You have included object.', | ||
topic: 'Configuration Error', | ||
}, | ||
]); | ||
}); | ||
}); |
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,44 @@ | ||
import is from '@sindresorhus/is'; | ||
import { getRegexPredicate, isRegexMatch } from '../../util/string-match'; | ||
import type { ValidationMessage } from '../types'; | ||
import type { CheckMatcherArgs } from './types'; | ||
|
||
/** | ||
* Only if type condition or context condition violated then errors array will be mutated to store metadata | ||
*/ | ||
export function check({ | ||
val: matchers, | ||
currentPath, | ||
}: CheckMatcherArgs): ValidationMessage[] { | ||
const res: ValidationMessage[] = []; | ||
|
||
if (is.array(matchers, is.string)) { | ||
if ( | ||
(matchers.includes('*') || matchers.includes('**')) && | ||
matchers.length > 1 | ||
) { | ||
res.push({ | ||
topic: 'Configuration Error', | ||
message: `${currentPath}: Your input contains * or ** along with other patterns. Please remove them, as * or ** matches all patterns.`, | ||
}); | ||
} | ||
for (const matcher of matchers) { | ||
// Validate regex pattern | ||
if (isRegexMatch(matcher)) { | ||
if (!getRegexPredicate(matcher)) { | ||
res.push({ | ||
topic: 'Configuration Error', | ||
message: `Failed to parse regex pattern "${matcher}"`, | ||
}); | ||
} | ||
} | ||
} | ||
} else { | ||
res.push({ | ||
topic: 'Configuration Error', | ||
message: `${currentPath}: should be an array of strings. You have included ${typeof matchers}.`, | ||
}); | ||
} | ||
|
||
return res; | ||
} |
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