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

65
export default validateMapField;
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