All files / tr8-script/validators/rules no-fixed-amount-allocation-policy-in-relative-ref-value-rule.ts

100% Statements 23/23
90% Branches 9/10
100% Functions 6/6
100% Lines 22/22

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            1x       4x 4x   4x 4x 4x 4x     4x             4x 4x   4x 4x   4x 4x   2x 2x         4x   4x 4x   2x 2x           4x                
import { autoInjectable } from "tsyringe";
 
import { Tr8AST, Tx } from "../../domain/ast.js";
import { SemanticValidationRule, ValidationResult } from "../validator.js";
 
@autoInjectable()
class NoFixedAmountAllocationPolicyInRelativeRefValueRule
  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[] = [];
 
    Eif (tx.refValue.type === "relative") {
      if (
        !tx.sources
          .map((source) => source.allocationPolicy.amount)
          .every((amount) => amount === undefined || Array.isArray(amount))
      ) {
        isValid &&= false;
        errors.push(
          "[sources] Relative refValue cannot use fixed amount allocationPolicy.",
        );
      }
 
      if (
        !tx.destinations
          .map((dest) => dest.allocationPolicy.amount)
          .every((amount) => amount === undefined || Array.isArray(amount))
      ) {
        isValid &&= false;
        errors.push(
          "[destinations] Relative refValue cannot use fixed amount allocationPolicy.",
        );
      }
    }
 
    return {
      isValid,
      errors,
    };
  }
}
 
export { NoFixedAmountAllocationPolicyInRelativeRefValueRule };