All files / Scripts/rules/types SimpleValidationRule.ts

100% Statements 20/20
100% Branches 16/16
100% Functions 2/2
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71                  8x   8x                               138x 138x 138x     138x 35x   103x 102x   1x       138x 138x 138x                   1259x       1259x   1259x 105x   104x 87x       1171x    
// A validation rule is a single, simple rule.  The rule is of the format:
//
//  IF regularExpression exists in section
//    then display error message on UI for section
//
 
import { HeaderSection, ISimpleValidationRule } from "./interfaces";
 
// rule counter to assign unique rule numbers to each rule (internal number only)
let uniqueRuleNumber = 0;
 
export class SimpleValidationRule implements ISimpleValidationRule {
    public checkSection: string;
    public errorPattern: string;
    public errorMessage: string;
    public errorReportingSection: string[];
    public ruleNumber: number;
    public primaryRule: boolean;
    public severity: "error" | "warning" | "info";
 
    constructor(
        checkSection: string,
        errorPattern: string,
        errorMessage: string,
        reportSection: string | string[],
        severity: "error" | "warning" | "info"
    ) {
        this.checkSection = checkSection;
        this.errorPattern = errorPattern;
        this.errorMessage = errorMessage || "";
 
        // Make sure reporting section is an array
        if (Array.isArray(reportSection)) {
            this.errorReportingSection = reportSection;
        } else {
            if (reportSection) {
                this.errorReportingSection = [reportSection];
            } else {
                this.errorReportingSection = [];
            }
        }
 
        this.ruleNumber = ++uniqueRuleNumber;
        this.primaryRule = true;
        this.severity = severity;
    }
 
    /**
     * Test this rule to see if it 'matches'
     * @param section - Section object containing header, value, and headerName
     * @returns null if no match, otherwise the text that matched the errorPattern Regular Expression
     */
    public violatesRule(section: HeaderSection): string | null {
        // Check if the header directly matches
        const headerMatches = section.header === this.checkSection;
 
        // OR check if this is a broken-out row from the header we're looking for
        // AND it's the "source" row which contains the full original value
        const isSourceRow = section.headerName === this.checkSection && section.header === "source";
 
        if (headerMatches || isSourceRow) {
            const matches = section.value.match(this.errorPattern);
 
            if (matches && matches.length > 0) {
                return matches[0];
            }
        }
 
        return null;
    }
}