All files / tr8-script/validators/rules single-book-source-per-tx-for-notif-rule.ts

92.3% Statements 12/13
75% Branches 6/8
100% Functions 1/1
91.66% Lines 11/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            1x   4x 4x 4x 5x 5x 10x     10x     5x 2x               2x                
import { autoInjectable } from "tsyringe";
 
import { Tr8AST } from "../../domain/ast.js";
import { SemanticValidationRule, ValidationResult } from "../validator.js";
 
@autoInjectable()
class SingleBookSourcePerTxForNotifRule implements SemanticValidationRule {
  validate(ast: Tr8AST): ValidationResult {
    Eif (ast.type === "NOTIF") {
      const { txs } = ast;
      for (const tx of txs) {
        const set: Set<string> = new Set();
        for (const source of tx.sources) {
          Iif (source.book.type === "world" || source.extern) {
            continue;
          }
          set.add(source.book.book);
        }
 
        if (set.size > 1) {
          return {
            isValid: false,
            errors: ["Notif can only come from a single book source per tx."],
          };
        }
      }
    }
 
    return {
      isValid: true,
      errors: [],
    };
  }
}
 
export { SingleBookSourcePerTxForNotifRule };