All files / tr8-script/validators/rules single-book-source-dest-for-request-rule.ts

100% Statements 13/13
100% Branches 4/4
100% Functions 1/1
100% Lines 12/12

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            1x   5x 4x 4x 4x 6x 6x   6x 6x     6x 2x                   3x                
import { autoInjectable } from "tsyringe";
 
import { Tr8AST } from "../../domain/ast.js";
import { SemanticValidationRule, ValidationResult } from "../validator.js";
 
@autoInjectable()
class SingleBookSourceDestForRequestRule implements SemanticValidationRule {
  validate(ast: Tr8AST): ValidationResult {
    if (ast.type === "REQ") {
      const { txs } = ast;
      const set: Set<string> = new Set();
      for (const tx of txs) {
        for (const source of tx.sources) {
          set.add(source.book.book);
        }
        for (const dest of tx.destinations) {
          set.add(dest.book.book);
        }
 
        if (set.size > 1) {
          return {
            isValid: false,
            errors: [
              "Request can only have one book in both sources and destinations across all txs.",
            ],
          };
        }
      }
    }
 
    return {
      isValid: true,
      errors: [],
    };
  }
}
 
export { SingleBookSourceDestForRequestRule };