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

CBIIT / crdc-datahub-ui / 11074479132

27 Sep 2024 04:44PM UTC coverage: 44.982% (+26.5%) from 18.435%
11074479132

Pull #479

github

web-flow
Merge a0867d25a into 3d8b55818
Pull Request #479: 3.0.0 Release

1727 of 4418 branches covered (39.09%)

Branch coverage included in aggregate %.

2612 of 5228 relevant lines covered (49.96%)

128.96 hits per line

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

22.0
/src/utils/validationUtils.ts
1
/**
2
 * Updates the validity of an HTMLInputElement by setting a custom validation message.
3
 *
4
 * @param inputRef - The reference to the HTMLInputElement to be validated
5
 * @param message - The custom validation message to be set. Defaults to an empty string if not provided.
6
 */
7
export const updateInputValidity = (
62✔
8
  inputRef: React.RefObject<HTMLInputElement>,
9
  message = ""
32✔
10
): void => {
11
  if (!inputRef?.current) {
64✔
12
    return; // Invalid ref
×
13
  }
14
  if (typeof inputRef.current.setCustomValidity !== "function") {
64✔
15
    return; // Input element doesn't support custom validity
×
16
  }
17

18
  inputRef.current.setCustomValidity(message);
64✔
19
};
20

21
/**
22
 *  Updates the validity of a MUI select component by setting a custom validation message.
23
 *
24
 * NOTE: This interfaces with the MUI Select ref which returns { node, value, focus }
25
 *
26
 * @param selectRef - the reference to the MUI select component to be validated
27
 * @param message - The custom validation message to be set. Defaults to an empty string if not provided.
28
 */
29
export const updateSelectValidity = (selectRef, message = ""): void => {
62!
30
  if (!selectRef?.current?.node) {
×
31
    return; // Invalid ref
×
32
  }
33
  if (typeof selectRef.current.node?.setCustomValidity !== "function") {
×
34
    return; // Input element doesn't support custom validity
×
35
  }
36

37
  selectRef.current.node.setCustomValidity(message);
×
38
};
39

40
/**
41
 * Validates whether a given value (string or number) lies within a specified range.
42
 *
43
 * @param value - The value to be validated. It can be of type string or number.
44
 *                If it's a string, the function will attempt to parse a number from it.
45
 * @param min - The minimum allowed value. Defaults to 0 if not provided.
46
 * @param max - The maximum allowed value. Optional.
47
 * @param allowFloat - A boolean indicating whether floating point numbers are considered
48
 *                     valid. Defaults to false.
49
 * @returns A boolean indicating whether the value passed the validation or not.
50
 *          Returns false if the value is NaN after being parsed or if it doesn't
51
 *          fall within the min and max parameters.
52
 */
53
export const isValidInRange = (
62✔
54
  value: string | number,
55
  min = 0,
×
56
  max?: number,
57
  allowFloat = false
×
58
): boolean => {
59
  const numValue = typeof value === "string" ? parseFloat(value) : value;
×
60

61
  if (Number.isNaN(numValue)) {
×
62
    return false;
×
63
  }
64
  if (!allowFloat && !Number.isInteger(numValue)) {
×
65
    return false;
×
66
  }
67
  if (numValue < min || (max !== undefined && numValue > max)) {
×
68
    return false;
×
69
  }
70

71
  return true;
×
72
};
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