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

Haixing-Hu / js-common-validator / d68634be-aa47-48ea-adda-8c76cd3e8d2b

21 Dec 2023 07:32AM UTC coverage: 84.435% (+8.0%) from 76.419%
d68634be-aa47-48ea-adda-8c76cd3e8d2b

push

circleci

Haixing-Hu
refactor: merge old codes from other projects and refactor

194 of 237 branches covered (0.0%)

Branch coverage included in aggregate %.

112 of 117 new or added lines in 13 files covered. (95.73%)

202 of 232 relevant lines covered (87.07%)

22.54 hits per line

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

90.91
/src/validators/impl/validate-string-field-by-rule.js
1
////////////////////////////////////////////////////////////////////////////////
2
//
3
//    Copyright (c) 2022 - 2023.
4
//    Haixing Hu, Qubit Co. Ltd.
5
//
6
//    All rights reserved.
7
//
8
////////////////////////////////////////////////////////////////////////////////
9
import ValidationResult from '../../validation-result';
10

11
/**
12
 * Use the specified validation rule to validate a string field.
13
 *
14
 * @param {string} value
15
 *     The field value to be verified.
16
 * @param {object} rule
17
 *     The rule object used for verification. The object must provide a
18
 *     `isValid()` function to verify whether a string value conforms to the
19
 *     rule.
20
 * @param {Object} context
21
 *     The validation context, which is the context object provided by the
22
 *     second argument of the validator function. It may have the following
23
 *     properties:
24
 *     - `instance: object`: the object to which the field belongs.
25
 *     - `owner: string|undefined`: the name of the owner (a person) of the field.
26
 *     - `field: string`: the name of the field to be validated.
27
 *     - `type: function`: the constructor of the field to be validated. If the
28
 *        field is decorated by the `@Type` decorator, this property is the
29
 *        argument of the decorator, otherwise it is the constructor of the
30
 *        default value of the field. If the default value of the field is
31
 *        `null` or `undefined`, this property is set to `undefined`.
32
 *     - `label: string`: the display label of the field to be validated.
33
 *     - `nullable: boolean`: whether the field to be validated is nullable.
34
 *     - `nonEmpty: boolean`: whether the field to be validated is non-empty.
35
 *     - `extraMessage: string`: extra error message.
36
 * @return {ValidationResult}
37
 *     The validation result.
38
 * @author Haixing Hu
39
 */
40
function validateStringFieldByRule(value, rule, context) {
41
  if (typeof rule?.isValid !== 'function') {
92!
NEW
42
    throw new Error('The rule object must provide a isValid() function');
×
43
  }
44
  const { owner, nullable, label, extraMessage } = context;
92✔
45
  const whose = (owner ? `${owner}的` : '');
92✔
46
  if (value === undefined || value === null || value === '') {
92✔
47
    if (nullable) {
32✔
48
      return new ValidationResult(true);
7✔
49
    } else {
50
      return new ValidationResult(false, `请填写${whose}${label}`);
25✔
51
    }
52
  }
53
  if ((typeof value !== 'string') && (!(value instanceof String))) {
60✔
54
    return new ValidationResult(false, `${whose}${label}必须以字符串形式表示`);
1✔
55
  }
56
  if (rule.isValid(value.valueOf())) {  // call rule.isValid() with a primitive string
59✔
57
    return new ValidationResult(true);
24✔
58
  } else {
59
    const message = (extraMessage ? `: ${extraMessage}` : '');
35!
60
    return new ValidationResult(false, `${whose}${label}格式不正确${message}`);
35✔
61
  }
62
}
63

64
export default validateStringFieldByRule;
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