All files / src/utils/validation/validators Validator.ts

71.43% Statements 5/7
100% Branches 0/0
66.67% Functions 2/3
71.43% Lines 5/7

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 447x               7x             7x                       3x   3x                            
import template from "../../../utils/template";
import { ValidationContext } from "../interfaces";
 
export interface ValidatorOptions {
 
    message?: string;
}
 
export default abstract class Validator {
 
    /** The message to add to the validation context */
    message?: string;
 
    constructor(options: ValidatorOptions) {
 
        this.message = options?.message;
    }
 
    abstract validate(context: ValidationContext): boolean;
 
    /**
     * Adds an error message to the arrays of errors of the context
     * @param context The context containing the array of errors
     * @param data The data to replace the template placeholders with
     */
    emitErrors(context: ValidationContext, data: any) {
 
        const result = template(this.message!, data);
 
        context.errors.push(result.text!);
    }
 
    /**
     * Adds an warning message to the arrays of warnings of the context
     * @param context The context containing the array of warnings
     * @param data The data to replace the template placeholders with
     */
    emitWarnings(context: ValidationContext, data: any) {
 
        const result = template(this.message!, data);
 
        context.warnings.push(result.text!);
    }
}