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

Haixing-Hu / js-common-decorator / 214d62ca-32aa-44ba-af6c-f0078b9541b4

29 Oct 2024 06:30PM UTC coverage: 82.353% (-0.7%) from 83.06%
214d62ca-32aa-44ba-af6c-f0078b9541b4

push

circleci

Haixing-Hu
build: upgrade dependencies and rebuild the library

473 of 581 branches covered (81.41%)

Branch coverage included in aggregate %.

633 of 762 relevant lines covered (83.07%)

174.02 hits per line

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

0.0
/src/impl/model/validate-nullish-field.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 '@haixing_hu/common-validation-rule';
10
import isFieldNullable from '../utils/is-field-nullable';
11
import getInstanceName from '../utils/get-instance-name';
12
import getFieldLabel from '../utils/get-field-label';
13

14
/**
15
 * Validates the specified nullish field of the specified object.
16
 *
17
 * If the field value is nullish, i.e., is `null` or `undefined`, this function
18
 * check whether the field is decorated with `@Nullable`. If it is not, a
19
 * validation error is returned; otherwise a success validation result is
20
 * returned. If the field value is not nullish, this function does nothing and
21
 * returns `null`.
22
 *
23
 * This function assumes that the field exists and is validatable.
24
 *
25
 * @param {object} metadata
26
 *     The metadata of the class of the object to be validated.
27
 * @param {object} obj
28
 *     The object to be validated.
29
 * @param {string} field
30
 *     The name of the specified field to be validated. This function assumes
31
 *     that the field exists and is validatable.
32
 * @param {any} value
33
 *     The value of the specified field of the specified object.
34
 * @returns {ValidationResult|null}
35
 *     The validation result if the specified field is nullish; `null` otherwise.
36
 * @author Haixing Hu
37
 * @private
38
 */
39
function validateNullishField(metadata, obj, field, value) {
40
  if (value === undefined || value === null) {
×
41
    if (context.nullable || isFieldNullable(metadata, field)) {
×
42
      return new ValidationResult(true);
×
43
    } else {
44
      const label = getFieldLabel(metadata, field);
×
45
      const name = getInstanceName(metadata, obj);
×
46
      // TODO: make the message i18n
47
      const message = name
×
48
        ? `The ${label} of ${name} must be specified.`
49
        : `The ${label} must be specified.`;
50
      return new ValidationResult(false, message);
×
51
    }
52
  }
53
  return null;
×
54
}
55

56
export default validateNullishField;
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