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

rsuite / schema-typed / 24773888898

22 Apr 2026 10:40AM UTC coverage: 94.47% (-0.7%) from 95.177%
24773888898

Pull #87

github

web-flow
Merge 81ff780d8 into 3776457b1
Pull Request #87: feat: schema-typed API improvements — nullable, transform, meta, composition, LiteralType, zero deps

446 of 500 branches covered (89.2%)

Branch coverage included in aggregate %.

139 of 142 new or added lines in 9 files covered. (97.89%)

1 existing line in 1 file now uncovered.

596 of 603 relevant lines covered (98.84%)

240.75 hits per line

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

95.08
/src/utils/createValidator.ts
1
import { CheckResult, RuleType } from '../types';
2
import formatErrorMessage from './formatErrorMessage';
3✔
3
function isObj(o: unknown): o is Record<PropertyKey, unknown> {
4
  return o != null && (typeof o === 'object' || typeof o == 'function');
1,509✔
5
}
6
function isPromiseLike(v: unknown): v is Promise<unknown> {
7
  return v instanceof Promise || (isObj(v) && typeof v.then === 'function');
1,512✔
8
}
9
/**
10
 * Create a data validator
11
 * @param data
12
 * @param name
13
 * @param label
14
 * @param abortEarly  When `false`, all rules are evaluated and all errors are collected into
15
 *                    `errorMessages`. Defaults to `true` (stop at first error).
16
 */
17
export function createValidator<V, D, E>(
3✔
18
  data?: D,
19
  name?: string | string[],
20
  label?: string,
21
  abortEarly = true
180✔
22
) {
23
  return (value: V, rules: RuleType<V, D, E>[]): CheckResult<E> | null => {
1,275✔
24
    const errors: (E | string)[] = [];
2,418✔
25

26
    for (let i = 0; i < rules.length; i += 1) {
2,418✔
27
      const { onValid, errorMessage, params, isAsync } = rules[i];
2,082✔
28
      if (isAsync) continue;
2,082✔
29
      const checkResult = onValid(value, data, name);
2,079✔
30
      const errorMsg = typeof errorMessage === 'function' ? errorMessage() : errorMessage;
2,079✔
31

32
      if (checkResult === false) {
2,079✔
33
        const formatted = formatErrorMessage<E>(errorMsg, {
567✔
34
          ...params,
35
          name: label || (Array.isArray(name) ? name.join('.') : name)
1,677✔
36
        });
37

38
        if (abortEarly) {
567✔
39
          return { hasError: true, errorMessage: formatted };
561✔
40
        }
41
        if (formatted != null) {
6!
42
          errors.push(formatted as E | string);
6✔
43
        }
44
      } else if (isPromiseLike(checkResult)) {
1,512✔
45
        throw new Error(
3✔
46
          'synchronous validator had an async result, you should probably call "checkAsync()"'
47
        );
48
      } else if (typeof checkResult === 'object' && (checkResult.hasError || checkResult.array)) {
1,509✔
49
        if (abortEarly) {
78!
50
          return checkResult;
78✔
51
        }
52
        // For structured results (nested object / array), return immediately even in non-abortEarly mode
UNCOV
53
        return checkResult;
×
54
      }
55
    }
56

57
    if (!abortEarly && errors.length > 0) {
1,776✔
58
      return { hasError: true, errorMessage: errors[0], errorMessages: errors };
3✔
59
    }
60

61
    return null;
1,773✔
62
  };
63
}
64

65
export default createValidator;
3✔
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