All files / mongoose/lib/schema map.js

52.27% Statements 23/44
44.44% Branches 8/18
66.66% Functions 4/6
52.27% Lines 23/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120            1x 1x 1x 1x               6x 6x               7x       7x   7x                                                       7x                               17x                       17x 17x   17x 17x   17x 6x       11x     17x                   1x   1x   1x   1x  
'use strict';
 
/*!
 * ignore
 */
 
const MongooseMap = require('../types/map');
const SchemaMapOptions = require('../options/schemaMapOptions');
const SchemaType = require('../schemaType');
const createJSONSchemaTypeDefinition = require('../helpers/createJSONSchemaTypeDefinition');
 
/*!
 * ignore
 */
 
class SchemaMap extends SchemaType {
  constructor(key, options) {
    super(key, options, 'Map');
    this.$isSchemaMap = true;
  }
 
  set(option, value) {
    return SchemaType.set(option, value);
  }
 
  cast(val, doc, init) {
    Iif (val instanceof MongooseMap) {
      return val;
    }
 
    const path = this.path;
 
    Iif (init) {
      const map = new MongooseMap({}, path, doc, this.$__schemaType);
 
      if (val instanceof global.Map) {
        for (const key of val.keys()) {
          let _val = val.get(key);
          if (_val == null) {
            _val = map.$__schemaType._castNullish(_val);
          } else {
            _val = map.$__schemaType.cast(_val, doc, true, null, { path: path + '.' + key });
          }
          map.$init(key, _val);
        }
      } else {
        for (const key of Object.keys(val)) {
          let _val = val[key];
          if (_val == null) {
            _val = map.$__schemaType._castNullish(_val);
          } else {
            _val = map.$__schemaType.cast(_val, doc, true, null, { path: path + '.' + key });
          }
          map.$init(key, _val);
        }
      }
 
      return map;
    }
 
    return new MongooseMap(val, path, doc, this.$__schemaType);
  }
 
  clone() {
    const schematype = super.clone();
 
    if (this.$__schemaType != null) {
      schematype.$__schemaType = this.$__schemaType.clone();
    }
    return schematype;
  }
 
  /**
   * Returns the embedded schema type (i.e. the `.$*` path)
   */
  getEmbeddedSchemaType() {
    return this.$__schemaType;
  }
 
  /**
   * Returns this schema type's representation in a JSON schema.
   *
   * @param [options]
   * @param [options.useBsonType=false] If true, return a representation with `bsonType` for use with MongoDB's `$jsonSchema`.
   * @returns {Object} JSON schema properties
   */
 
  toJSONSchema(options) {
    const useBsonType = options?.useBsonType;
    const embeddedSchemaType = this.getEmbeddedSchemaType();
 
    const isRequired = this.options.required && typeof this.options.required !== 'function';
    const result = createJSONSchemaTypeDefinition('object', 'object', useBsonType, isRequired);
 
    if (embeddedSchemaType.schema) {
      result.additionalProperties = useBsonType
        ? { ...embeddedSchemaType.toJSONSchema(options) }
        : { ...embeddedSchemaType.toJSONSchema(options) };
    } else {
      result.additionalProperties = embeddedSchemaType.toJSONSchema(options);
    }
 
    return result;
  }
}
 
/**
 * This schema type's name, to defend against minifiers that mangle
 * function names.
 *
 * @api public
 */
SchemaMap.schemaName = 'Map';
 
SchemaMap.prototype.OptionsConstructor = SchemaMapOptions;
 
SchemaMap.defaultOptions = {};
 
module.exports = SchemaMap;