• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

panates / valgen / 10865412103

14 Sep 2024 08:48PM UTC coverage: 83.514% (-0.9%) from 84.424%
10865412103

push

github

erayhanoglu
fixed: Circular imports

431 of 589 branches covered (73.17%)

Branch coverage included in aggregate %.

35 of 35 new or added lines in 35 files covered. (100.0%)

61 existing lines in 27 files now uncovered.

881 of 982 relevant lines covered (89.71%)

184.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.87
/src/core/context.ts
1
import { omitUndefined } from '../helpers/omit-undefined.js';
90✔
2
import type { ErrorIssue, ExecutionOptions, OnFailFunction } from './types.js';
3
import { ValidationError } from './validation-error.js';
90✔
4
import type { Validator } from './validator.js';
5

6
const VARIABLE_REPLACE_PATTERN = /{{([^}]*)}}/g;
90✔
7
const OPTIONAL_VAR_PATTERN = /^([^?]+)(?:\||(.*))?$/;
90✔
8

9
export class Context implements ExecutionOptions {
90✔
10
  errors: ErrorIssue[] = [];
1,290✔
11
  maxErrors?: number;
12
  onFail?: OnFailFunction;
13
  coerce?: boolean;
14
  root?: string;
15
  location?: string;
16
  scope?: object;
17
  context?: string;
18
  property?: string;
19
  index?: number;
20
  label?: string;
21

22
  [key: string]: any;
23

24
  constructor(options?: ExecutionOptions) {
25
    Object.assign(this, options);
1,290✔
26
  }
27

28
  fail(
29
    rule: Validator,
30
    message: string | Error,
31
    value: any,
32
    details?: Record<string, any>,
33
  ): void {
34
    const issue = omitUndefined<ErrorIssue>({
606✔
35
      message: message instanceof Error ? message.message : String(message),
606!
36
      rule: rule.id,
37
      root: this.root,
38
      location: this.location,
39
      context: this.context,
40
      property: this.property,
41
      index: this.index,
42
      label: this.label,
43
      value,
44
      ...details,
45
    });
46
    issue.value = value;
606✔
47

48
    if (this.onFail) {
606✔
49
      const x = this.onFail(issue, this);
12✔
50
      if (!x) return;
12!
51
      if (typeof x === 'object') Object.assign(issue, x);
12!
52
      else issue.message = String(x);
12✔
53
    }
54
    issue.message = ('' + issue.message).replace(
606✔
55
      VARIABLE_REPLACE_PATTERN,
56
      (x, g: string) => {
57
        const m = OPTIONAL_VAR_PATTERN.exec(g);
504✔
58
        if (!m) return x;
504!
59
        const k = m[1];
504✔
60
        let v = issue[k];
504✔
61
        if (k === 'value') {
504✔
62
          const s = String(issue.value);
207✔
63
          return s.length < 30 ? s : s.substring(0, 30) + '..';
207!
64
        }
65
        if (!v && k === 'label' && (this.location || this.property)) {
297✔
66
          v = '`' + (this.location || this.property) + '`';
3!
67
        }
68
        if (v != null) return v;
297✔
69
        if (m[2]) return m[2];
276!
70
        return m[1] === 'label' ? 'Value' : x;
276!
71
      },
72
    );
73

74
    this.errors.push(issue);
606✔
75
    if (this.errors.length >= (this.maxErrors ?? Infinity)) {
606!
UNCOV
76
      throw new ValidationError(this.errors);
×
77
    }
78
  }
79

80
  extend(options?: ExecutionOptions): Context {
81
    const extended = {} as Context;
651✔
82
    if (options) {
651✔
83
      for (const [k, v] of Object.entries(options)) {
480✔
84
        if (v !== undefined) extended[k] = v;
48✔
85
      }
86
    }
87
    Object.setPrototypeOf(extended, this);
651✔
88
    return extended;
651✔
89
  }
90
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc