All files / tr8-script/validators/rules no-empty-source-and-destination-rule.ts

100% Statements 18/18
100% Branches 4/4
100% Functions 2/2
100% Lines 17/17

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            1x   3x 3x   3x 3x 3x 3x     3x             3x 3x   3x 1x 1x     3x 1x 1x     3x                
import { autoInjectable } from "tsyringe";
 
import { Tr8AST, Tx } from "../../domain/ast.js";
import { SemanticValidationRule, ValidationResult } from "../validator.js";
 
@autoInjectable()
class NoEmptySourceAndDestinationInTxRule implements SemanticValidationRule {
  validate(ast: Tr8AST): ValidationResult {
    let isValid = true;
    const errors: string[] = [];
 
    for (const tx of ast.txs) {
      const { isValid: txIsValid, errors: txErrors } = this.validateTx(tx);
      isValid &&= txIsValid;
      errors.push(...txErrors);
    }
 
    return {
      isValid,
      errors,
    };
  }
 
  validateTx(tx: Tx): ValidationResult {
    let isValid = true;
    const errors: string[] = [];
 
    if (tx.sources.length === 0) {
      isValid &&= false;
      errors.push("Tx must have at least one source.");
    }
 
    if (tx.destinations.length === 0) {
      isValid &&= false;
      errors.push("Tx must have at least one destination.");
    }
 
    return {
      isValid,
      errors,
    };
  }
}
 
export { NoEmptySourceAndDestinationInTxRule };