Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/source/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const files = [
'lib/types/subdocument.js',
'lib/types/arraySubdocument.js',
'lib/types/buffer.js',
'lib/types/decimal128.js',
'lib/types/.js',
'lib/types/map.js',
'lib/types/array/methods/index.js'
];
Expand Down
1 change: 1 addition & 0 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ exports.Schema = require('./schema');
* - [ObjectId](https://mongoosejs.com/docs/schematypes.html#objectids)
* - [Map](https://mongoosejs.com/docs/schematypes.html#maps)
* - [Subdocument](https://mongoosejs.com/docs/schematypes.html#schemas)
* - Int32
*
* Using this exposed access to the `ObjectId` type, we can construct ids on demand.
*
Expand Down
35 changes: 35 additions & 0 deletions lib/cast/int32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const assert = require('assert');

/**
* Given a value, cast it to a Int32, or throw an `Error` if the value
* cannot be casted. `null` and `undefined` are considered valid.
*
* @param {Any} value
* @return {Number}
* @throws {Error} if `value` does not represent an integer, or is beyond the bounds of an 32-bit integer.
* @api private
*/

module.exports = function castInt32(val) {
if (val == null) {
return val;
}
if (val === '') {
return null;
}

if (typeof val === 'string' || typeof val === 'number') {

const INT32_MAX = 0x7FFFFFFF;
const INT32_MIN = -0x80000000;

const _val = Number(val);

if (!isNaN(_val) && _val === Math.round(_val) && _val > INT32_MIN && _val < INT32_MAX) {
return _val;
}
assert.ok(false);
}
};
17 changes: 17 additions & 0 deletions lib/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,7 @@ Mongoose.prototype.VirtualType = VirtualType;
* - [ObjectId](https://mongoosejs.com/docs/schematypes.html#objectids)
* - [Map](https://mongoosejs.com/docs/schematypes.html#maps)
* - [Subdocument](https://mongoosejs.com/docs/schematypes.html#schemas)
* - Int32
*
* Using this exposed access to the `ObjectId` type, we can construct ids on demand.
*
Expand Down Expand Up @@ -1138,6 +1139,22 @@ Mongoose.prototype.syncIndexes = function(options) {

Mongoose.prototype.Decimal128 = SchemaTypes.Decimal128;


/**
* The Mongoose Int32 [SchemaType](https://mongoosejs.com/docs/schematypes.html). Used for
* declaring paths in your schema that should be 32-bit integers floating points.
* Do not use this to create a new Int32 instance, use `mongoose.Types.Int32`
* instead.
*
* #### Example:
*
* const vehicleSchema = new Schema({ numberOfCows: mongoose.Int32 });
*
* @property Decimal128
* @api public
*/
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to store a reference to our schema on the Mongoose object? My thought is no becase the majority of schema types do not. Can we remove this? (and the TS changes you made as well)?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed them - will add back in depending on discussion with Val

Mongoose.prototype.Int32 = SchemaTypes.Int32;

/**
* The Mongoose Mixed [SchemaType](https://mongoosejs.com/docs/schematypes.html). Used for
* declaring paths in your schema that Mongoose's change tracking, casting,
Expand Down
1 change: 1 addition & 0 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,7 @@ Query.prototype.getOptions = function() {
* - [projection](https://mongoosejs.com/docs/api/query.html#Query.prototype.projection())
* - sanitizeProjection
* - useBigInt64
* - promoteValues
*
* The following options are only for all operations **except** `updateOne()`, `updateMany()`, `deleteOne()`, and `deleteMany()`:
*
Expand Down
1 change: 1 addition & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2848,6 +2848,7 @@ module.exports = exports = Schema;
* - [Mixed](https://mongoosejs.com/docs/schematypes.html#mixed)
* - [UUID](https://mongoosejs.com/docs/schematypes.html#uuid)
* - [BigInt](https://mongoosejs.com/docs/schematypes.html#bigint)
* - Int32
*
* Using this exposed access to the `Mixed` SchemaType, we can use them in our schema.
*
Expand Down
1 change: 1 addition & 0 deletions lib/schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ exports.ObjectId = require('./objectId');
exports.String = require('./string');
exports.Subdocument = require('./subdocument');
exports.UUID = require('./uuid');
exports.Int32 = require('./int32');

// alias

Expand Down
227 changes: 227 additions & 0 deletions lib/schema/int32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
'use strict';

/*!
* Module dependencies.
*/

const CastError = require('../error/cast');
const SchemaType = require('../schemaType');
const castInt32 = require('../cast/int32');
const isBsonType = require('../helpers/isBsonType');

/**
* Int32 SchemaType constructor.
*
* @param {String} path
* @param {Object} options
* @inherits SchemaType
* @api public
*/

function SchemaInt32(path, options) {
SchemaType.call(this, path, options, 'Int32');
}

/**
* This schema type's name, to defend against minifiers that mangle
* function names.
*
* @api public
*/
SchemaInt32.schemaName = 'Int32';

SchemaInt32.defaultOptions = {};

/*!
* Inherits from SchemaType.
*/
SchemaInt32.prototype = Object.create(SchemaType.prototype);
SchemaInt32.prototype.constructor = SchemaInt32;

/*!
* ignore
*/

SchemaInt32._cast = castInt32;

/**
* Sets a default option for all Int32 instances.
*
*
* @param {String} option The option you'd like to set the value for
* @param {Any} value value for option
* @return {undefined}
* @function set
* @static
* @api public
*/

SchemaInt32.set = SchemaType.set;

SchemaInt32.setters = [];

/**
* Attaches a getter for all Int32 instances
*
* @param {Function} getter
* @return {this}
* @function get
* @static
* @api public
*/

SchemaInt32.get = SchemaType.get;

/**
* Get/set the function used to cast arbitrary values to booleans.
*
* @param {Function} caster
* @return {Function}
* @function get
* @static
* @api public
*/

SchemaInt32.cast = function cast(caster) {
if (arguments.length === 0) {
return this._cast;
}
if (caster === false) {
caster = this._defaultCaster;
}
this._cast = caster;

return this._cast;
};

/*!
* ignore
*/

SchemaInt32._checkRequired = v => v != null && isBsonType(v, 'Int32');
/**
* Override the function the required validator uses to check whether a value
* passes the `required` check.
*
* @param {Function} fn
* @return {Function}
* @function checkRequired
* @static
* @api public
*/

SchemaInt32.checkRequired = SchemaType.checkRequired;

/**
* Check if the given value satisfies a required validator.
*
* @param {Any} value
* @return {Boolean}
* @api public
*/

SchemaInt32.prototype.checkRequired = function(value) {
return this.constructor._checkRequired(value);
};

/**
* Casts to Int32
*
* @param {Object} value
* @param {Object} model this value is optional
* @api private
*/

SchemaInt32.prototype.cast = function(value) {
let castInt32;
if (isBsonType(value, 'Int32')) {
return value;
}
if (typeof this._castFunction === 'function') {
castInt32 = this._castFunction;
} else if (typeof this.constructor.cast === 'function') {
castInt32 = this.constructor.cast();
} else {
castInt32 = SchemaInt32.cast();
}

try {
return castInt32(value);
} catch (error) {
throw new CastError('Int32', value, this.path, error, this);
}
};

/*!
* ignore
*/

SchemaInt32.$conditionalHandlers = {
...SchemaType.prototype.$conditionalHandlers,
$gt: handleSingle,
$gte: handleSingle,
$lt: handleSingle,
$lte: handleSingle
};

/*!
* ignore
*/

function handleSingle(val, context) {
return this.castForQuery(null, val, context);
}

/**
* Casts contents for queries.
*
* @param {String} $conditional
* @param {any} val
* @api private
*/

SchemaInt32.prototype.castForQuery = function($conditional, val, context) {
let handler;
if ($conditional != null) {
handler = SchemaInt32.$conditionalHandlers[$conditional];

if (handler) {
return handler.call(this, val);
}

return this.applySetters(null, val, context);
}

try {
return this.applySetters(val, context);
} catch (err) {
if (err instanceof CastError && err.path === this.path && this.$fullPath != null) {
err.path = this.$fullPath;
}
throw err;
}
};

/**
*
* @api private
*/

SchemaInt32.prototype._castNullish = function _castNullish(v) {
if (typeof v === 'undefined') {
return v;
}
const castInt32 = typeof this.constructor.cast === 'function' ?
this.constructor.cast() :
SchemaInt32.cast();
if (castInt32 == null) {
return v;
}
return v;
};

/*!
* Module exports.
*/

module.exports = SchemaInt32;
Loading
Loading