Skip to content

Commit

Permalink
fix(schema): support setting schema path to an instance of SchemaType…
Browse files Browse the repository at this point in the history
…Options

Fix #8292
  • Loading branch information
vkarpov15 committed Nov 6, 2019
1 parent 081f2ec commit d02ebd2
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 44 deletions.
7 changes: 1 addition & 6 deletions lib/options/SchemaArrayOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ const SchemaTypeOptions = require('./SchemaTypeOptions');

class SchemaArrayOptions extends SchemaTypeOptions {}

const opts = {
enumerable: true,
configurable: true,
writable: true,
value: null
};
const opts = require('./propertyOptions');

/**
* If this is an array of strings, an array of allowed values for this path.
Expand Down
7 changes: 1 addition & 6 deletions lib/options/SchemaBufferOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ const SchemaTypeOptions = require('./SchemaTypeOptions');

class SchemaBufferOptions extends SchemaTypeOptions {}

const opts = {
enumerable: true,
configurable: true,
writable: true,
value: null
};
const opts = require('./propertyOptions');

/**
* Set the default subtype for this buffer.
Expand Down
7 changes: 1 addition & 6 deletions lib/options/SchemaDateOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ const SchemaTypeOptions = require('./SchemaTypeOptions');

class SchemaDateOptions extends SchemaTypeOptions {}

const opts = {
enumerable: true,
configurable: true,
writable: true,
value: null
};
const opts = require('./propertyOptions');

/**
* If set, Mongoose adds a validator that checks that this path is after the
Expand Down
7 changes: 1 addition & 6 deletions lib/options/SchemaNumberOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ const SchemaTypeOptions = require('./SchemaTypeOptions');

class SchemaNumberOptions extends SchemaTypeOptions {}

const opts = {
enumerable: true,
configurable: true,
writable: true,
value: null
};
const opts = require('./propertyOptions');

/**
* If set, Mongoose adds a validator that checks that this path is at least the
Expand Down
7 changes: 1 addition & 6 deletions lib/options/SchemaObjectIdOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ const SchemaTypeOptions = require('./SchemaTypeOptions');

class SchemaObjectIdOptions extends SchemaTypeOptions {}

const opts = {
enumerable: true,
configurable: true,
writable: true,
value: null
};
const opts = require('./propertyOptions');

/**
* If truthy, uses Mongoose's default built-in ObjectId path.
Expand Down
7 changes: 1 addition & 6 deletions lib/options/SchemaStringOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ const SchemaTypeOptions = require('./SchemaTypeOptions');

class SchemaStringOptions extends SchemaTypeOptions {}

const opts = {
enumerable: true,
configurable: true,
writable: true,
value: null
};
const opts = require('./propertyOptions');

/**
* Array of allowed values for this path
Expand Down
7 changes: 1 addition & 6 deletions lib/options/SchemaTypeOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ class SchemaTypeOptions {
}
}

const opts = {
enumerable: true,
configurable: true,
writable: true,
value: null
};
const opts = require('./propertyOptions');

/**
* The type to cast this path to.
Expand Down
8 changes: 8 additions & 0 deletions lib/options/propertyOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = Object.freeze({
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
5 changes: 3 additions & 2 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const EventEmitter = require('events').EventEmitter;
const Kareem = require('kareem');
const SchemaType = require('./schematype');
const SchemaTypeOptions = require('./options/SchemaTypeOptions');
const VirtualType = require('./virtualtype');
const applyTimestampsToChildren = require('./helpers/update/applyTimestampsToChildren');
const applyTimestampsToUpdate = require('./helpers/update/applyTimestampsToUpdate');
Expand Down Expand Up @@ -426,7 +427,7 @@ Schema.prototype.add = function add(obj, prefix) {
'`, got value "' + obj[key][0] + '"');
}

if (utils.isPOJO(obj[key]) &&
if ((utils.isPOJO(obj[key]) || obj[key] instanceof SchemaTypeOptions) &&
(!obj[key][this.options.typeKey] || (this.options.typeKey === 'type' && obj[key].type.type))) {
if (Object.keys(obj[key]).length) {
// nested object { last: { name: String }}
Expand Down Expand Up @@ -795,7 +796,7 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
// copy of SchemaTypes re: gh-7158 gh-6933
const MongooseTypes = this.base != null ? this.base.Schema.Types : Schema.Types;

if (obj.constructor) {
if (!utils.isPOJO(obj) && !(obj instanceof SchemaTypeOptions)) {
const constructorName = utils.getFunctionName(obj.constructor);
if (constructorName !== 'Object') {
const oldObj = obj;
Expand Down

0 comments on commit d02ebd2

Please sign in to comment.