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

Haixing-Hu / js-common-decorator / 1970a515-3d5d-49ef-bafd-31c9a52cf4e9

10 Nov 2024 05:00PM UTC coverage: 80.83% (-1.5%) from 82.353%
1970a515-3d5d-49ef-bafd-31c9a52cf4e9

push

circleci

Haixing-Hu
feat: make @Normalizable support normalization of enum class field

481 of 608 branches covered (79.11%)

Branch coverage included in aggregate %.

90 of 116 new or added lines in 19 files covered. (77.59%)

20 existing lines in 4 files now uncovered.

649 of 790 relevant lines covered (82.15%)

227.51 hits per line

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

21.43
/src/impl/model/validate-set-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 getElementValidationContext from '../utils/get-element-validation-context';
11
import validateEmptyField from './validate-empty-field';
12

13
/**
14
 * Validates the specified set field of the specified object.
15
 *
16
 * If the field value is a set, this function recursively validates each element
17
 * in the set, and returns the combination of the validation results;
18
 * otherwise, this function does nothing and returns `null`.
19
 *
20
 * This function assumes that the field exists, and is validatable, not nullish
21
 * nor empty.
22
 *
23
 * @param {function} Class
24
 *     The constructor of the class of the object to be validated.
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, which must be an instance of the `Class` class.
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, and is non-nullish.
32
 * @param {any} value
33
 *     The value of the specified field of the specified object.
34
 * @param {function} validator
35
 *     The validator function
36
 * @param {object} context
37
 *     The validation context.
38
 * @returns {ValidationResult|null}
39
 *     The validation result if the specified field is a set; `null` otherwise.
40
 * @author Haixing Hu
41
 * @private
42
 */
43
function validateSetField(Class, metadata, obj, field, value, validator, context) {
44
  if (value instanceof Set) {
251!
45
    // check the empty set
46
    const result = validateEmptyField(metadata, obj, field, value, context);
×
47
    if (result) {
×
48
      return result;
×
49
    }
50
    // get the validation options
NEW
51
    const ctx = getElementValidationContext(Class, metadata, obj, field, context);
×
52
    // validate each element of the set
53
    const results = Array.from(value, (e, i) => {
×
54
      ctx.index = i;
×
55
      return validator(e, ctx);
×
56
    });
57
    return ValidationResult.merge(results);
×
58
  }
59
  return null;
251✔
60
}
61

62
export default validateSetField;
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