-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added regex validation #20
Conversation
export const validateRegularExpressions = ( | ||
config: ConfigurationFile, | ||
logger: ActionLogger, | ||
): [true] | [false, string] => { | ||
/** Regex evaluator */ | ||
const isRegexValid = (regex: string): boolean => { | ||
try { | ||
new RegExp(regex); | ||
return true; | ||
} catch (e) { | ||
logger.error(e as Error); | ||
return false; | ||
} | ||
}; | ||
|
||
for (const rule of config.rules) { | ||
for (const condition of rule.condition.include) { | ||
if (!isRegexValid(condition)) { | ||
return [false, `Include condition '${condition}' is not a valid regex`]; | ||
} | ||
} | ||
if (rule.condition.exclude) { | ||
for (const condition of rule.condition.exclude) { | ||
if (!isRegexValid(condition)) { | ||
return [false, `Exclude condition '${condition}' is not a valid regex`]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return [true]; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could maybe change this to be the following:
export const validateRegularExpressions = ( | |
config: ConfigurationFile, | |
logger: ActionLogger, | |
): [true] | [false, string] => { | |
/** Regex evaluator */ | |
const isRegexValid = (regex: string): boolean => { | |
try { | |
new RegExp(regex); | |
return true; | |
} catch (e) { | |
logger.error(e as Error); | |
return false; | |
} | |
}; | |
for (const rule of config.rules) { | |
for (const condition of rule.condition.include) { | |
if (!isRegexValid(condition)) { | |
return [false, `Include condition '${condition}' is not a valid regex`]; | |
} | |
} | |
if (rule.condition.exclude) { | |
for (const condition of rule.condition.exclude) { | |
if (!isRegexValid(condition)) { | |
return [false, `Exclude condition '${condition}' is not a valid regex`]; | |
} | |
} | |
} | |
} | |
return [true]; | |
}; | |
export const validateRegularExpressions = ( | |
config: ConfigurationFile, | |
logger: ActionLogger, | |
): boolean => { | |
/** Regex evaluator */ | |
const isRegexValid = (regex: string): boolean => { | |
try { | |
new RegExp(regex); | |
return true; | |
} catch (e) { | |
logger.error(e as Error); | |
return false; | |
} | |
}; | |
for (const rule of config.rules) { | |
for (const condition of rule.condition.include) { | |
if (!isRegexValid(condition)) { | |
logger.error(`Include condition '${condition}' is not a valid regex`); | |
return false; | |
} | |
} | |
if (rule.condition.exclude) { | |
for (const condition of rule.condition.exclude) { | |
if (!isRegexValid(condition)) { | |
logger.error(`Exclude condition '${condition}' is not a valid regex`); | |
return false; | |
} | |
} | |
} | |
} | |
return true; | |
}; |
what do you think?
This will now have it in a more ordered way
src/runner.ts
Outdated
const [result, error] = validateRegularExpressions(configFile, this.logger); | ||
if (!result) { | ||
this.logger.error(error); | ||
throw new Error("Regular expression is invalid. Check the logs"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe instead of Regular expression is invalid. Check the logs
, do
throw new Error(`Regular expression is invalid: ${error.message}`);
?
We wouldn't expect anything aside from descriptive error there, right? (because, there's nothing going on except compiling the regex).
Or, leave it like this, it's good enough already.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it makes sense. I don't need to capture the state, only validate that it works or fail.
It also makes it easier for testing
Created method which validates the regex expressions in a configuration.
Closes #16
I want to have a constant error type + a custom error log so I make it return either a passed status or a failed status + error message so it can be logged.