-
Notifications
You must be signed in to change notification settings - Fork 0
feat(NODE-6503): Add Int32 as a SchemaType #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
05ce588
df7c715
68f2ebd
021f84b
538629c
4492a3e
8226905
7c6ff5c
c5dc226
9537974
49804ed
c6bbafb
18d18a7
0e4b257
010d1e2
0f78bf1
9dcada3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| } | ||
|
|
||
baileympearson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (typeof val === 'string' || typeof val === 'number') { | ||
|
|
||
| const INT32_MAX = 0x7FFFFFFF; | ||
| const INT32_MIN = -0x80000000; | ||
|
|
||
| const _val = Number(val); | ||
baileympearson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (!isNaN(_val) && _val === Math.round(_val) && _val > INT32_MIN && _val < INT32_MAX) { | ||
| return _val; | ||
| } | ||
| assert.ok(false); | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
| * | ||
|
|
@@ -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 | ||
| */ | ||
|
||
| 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, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| 'use strict'; | ||
aditi-khare-mongoDB marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /*! | ||
| * 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; | ||
| }; | ||
baileympearson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /*! | ||
| * ignore | ||
| */ | ||
|
|
||
| SchemaInt32._checkRequired = v => v != null && isBsonType(v, 'Int32'); | ||
baileympearson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /** | ||
| * 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')) { | ||
baileympearson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 = { | ||
baileympearson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ...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) { | ||
baileympearson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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; | ||
Uh oh!
There was an error while loading. Please reload this page.