Skip to content

Commit

Permalink
validate regex too
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed May 5, 2024
1 parent 3f6c837 commit 9bc85f9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
12 changes: 10 additions & 2 deletions lib/config/validation-helpers/regex-glob-matchers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { check } from './regex-glob-matchers';

describe('config/validation-helpers/regex-glob-matchers', () => {
it('should have errors', () => {
it('should error for multiple match alls', () => {
const res = check({
val: ['*', '**'],
currentPath: 'hostRules[0].allowedHeaders',
});
expect(res).toHaveLength(1);
});

it('should have errors - 2', () => {
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',
Expand Down
34 changes: 23 additions & 11 deletions lib/config/validation-helpers/regex-glob-matchers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import is from '@sindresorhus/is';
import { getRegexPredicate, isRegexMatch } from '../../util/string-match';
import type { ValidationMessage } from '../types';
import type { CheckMatcherArgs } from './types';

Expand All @@ -9,22 +10,33 @@ export function check({
val,
currentPath,
}: CheckMatcherArgs): ValidationMessage[] {
let errMessage: string | undefined;
const res: ValidationMessage[] = [];

if (is.array(val, is.string)) {
if ((val.includes('*') || val.includes('**')) && val.length > 1) {
errMessage = `${currentPath}: Your input contains * or ** along with other patterns. Please remove them, as * or ** matches all patterns.`;
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 pattern of val) {
// Validate regex pattern
if (isRegexMatch(pattern)) {
const autodiscoveryPred = getRegexPredicate(pattern);
if (!autodiscoveryPred) {
res.push({
topic: 'Configuration Error',
message: `Failed to parse regex pattern "${pattern}"`,
});
}
}
}
} else {
errMessage = `${currentPath}: should be an array of strings. You have included ${typeof val}.`;
res.push({
topic: 'Configuration Error',
message: `${currentPath}: should be an array of strings. You have included ${typeof val}.`,
});
}

return errMessage
? [
{
topic: 'Configuration Error',
message: errMessage,
},
]
: [];
return res;
}

0 comments on commit 9bc85f9

Please sign in to comment.