All files / tr8-script/validators/rules no-limit-and-keep-with-overdraft-constraint-rule.ts

100% Statements 21/21
100% Branches 8/8
100% Functions 3/3
100% Lines 19/19

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            1x       4x 4x   4x 4x 4x 4x     4x             4x 4x   4x 8x   6x 1x 1x         6x 1x 1x           4x                
import { autoInjectable } from "tsyringe";
 
import { Tr8AST, Tx } from "../../domain/ast.js";
import { SemanticValidationRule, ValidationResult } from "../validator.js";
 
@autoInjectable()
class NoLimitAndKeepWithOverdraftConstraintRule
  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[] = [];
 
    for (const source of tx.sources) {
      const constraints = source.constraints.map((c) => c.constraint);
 
      if (constraints.includes("overdraft") && constraints.includes("limit")) {
        isValid &&= false;
        errors.push(
          "Cannot use limit and overdraft constraints together in a tx source.",
        );
      }
 
      if (constraints.includes("overdraft") && constraints.includes("keep")) {
        isValid &&= false;
        errors.push(
          "Cannot use keep and overdraft constraints together in a tx source.",
        );
      }
    }
 
    return {
      isValid,
      errors,
    };
  }
}
 
export { NoLimitAndKeepWithOverdraftConstraintRule };