Skip to content

Commit

Permalink
feat: add validation for matchers in config parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
HashCookie committed Sep 29, 2024
1 parent 1591055 commit 1ca4e96
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,43 @@ export class Config implements ConfigInterface {
if (equalIndex === -1) {
throw new Error(`parse the content error : line ${lineNum}`);
}
const key = line.substring(0, equalIndex);
const value = line.substring(equalIndex + 1);
this.addConfig(section, key.trim(), value.trim());
const key = line.substring(0, equalIndex).trim();
const value = line.substring(equalIndex + 1).trim();

if (section === 'matchers') {
this.validateMatcher(value, lineNum);
}

this.addConfig(section, key, value);
}

private validateMatcher(matcherStr: string, lineNumber: number): void {
const errors: string[] = [];

const validProps = ['r.sub', 'r.obj', 'r.act', 'p.sub', 'p.obj', 'p.act', 'p.eft'];
const usedProps = matcherStr.match(/[rp]\.\w+/g) || [];
const invalidProps = usedProps.filter((prop) => !validProps.includes(prop));
if (invalidProps.length > 0) {
errors.push(`Invalid properties: ${invalidProps.join(', ')}`);
}

if (matcherStr.includes('..')) {
errors.push('Found extra dots');
}

if (matcherStr.trim().endsWith(',')) {
errors.push('Unnecessary comma');
}

const openBrackets = (matcherStr.match(/\(/g) || []).length;
const closeBrackets = (matcherStr.match(/\)/g) || []).length;
if (openBrackets !== closeBrackets) {
errors.push('Mismatched parentheses');
}

if (errors.length > 0) {
throw new Error(`${errors.join(', ')}`);
}
}

public getBool(key: string): boolean {
Expand Down

0 comments on commit 1ca4e96

Please sign in to comment.