-
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 1 commit
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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| 'use strict'; | ||
|
|
||
| const assert = require('assert'); | ||
| const BSON = require('bson'); | ||
|
|
||
| /** | ||
| * Given a value, cast it to a Int32, or throw an `Error` if the value | ||
|
|
@@ -20,16 +21,23 @@ module.exports = function castInt32(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; | ||
| let coercedVal; | ||
| if (val instanceof BSON.Int32 || val instanceof BSON.Double) { | ||
|
||
| coercedVal = val.value; | ||
| } else if (val instanceof BSON.Long) { | ||
| coercedVal = val.toNumber(); | ||
| } else { | ||
| coercedVal = Number(val); | ||
| } | ||
nbbeeken marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const _val = Number(val); | ||
| const INT32_MAX = 0x7FFFFFFF; | ||
| const INT32_MIN = -0x80000000; | ||
|
|
||
| if (_val === (_val | 0) && _val > INT32_MIN && _val < INT32_MAX) { | ||
| return _val; | ||
| } | ||
| assert.ok(false); | ||
| if (coercedVal === (coercedVal | 0) && | ||
| coercedVal >= INT32_MIN && | ||
| coercedVal <= INT32_MAX | ||
| ) { | ||
| return coercedVal; | ||
| } | ||
| assert.ok(false); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,6 @@ | |
| const CastError = require('../error/cast'); | ||
| const SchemaType = require('../schemaType'); | ||
| const castInt32 = require('../cast/int32'); | ||
| const isBsonType = require('../helpers/isBsonType'); | ||
|
|
||
| /** | ||
| * Int32 SchemaType constructor. | ||
|
|
@@ -81,23 +80,44 @@ SchemaInt32.setters = []; | |
|
|
||
| SchemaInt32.get = SchemaType.get; | ||
|
|
||
| /*! | ||
| * ignore | ||
| */ | ||
|
|
||
| SchemaInt32._defaultCaster = v => { | ||
| const INT32_MAX = 0x7FFFFFFF; | ||
| const INT32_MIN = -0x80000000; | ||
|
|
||
| if (v != null) { | ||
| if (typeof v !== 'number') { | ||
|
||
| throw new Error(); | ||
| } | ||
| if (v < INT32_MIN || v > INT32_MAX) { | ||
| throw new Error(); | ||
| } | ||
| } | ||
|
|
||
| return v; | ||
| }; | ||
|
|
||
| /** | ||
| * Get/set the function used to cast arbitrary values to 32-bit integers | ||
| * | ||
| * #### Example: | ||
| * | ||
| * // Make Mongoose cast NaN to 0 | ||
| * const defaultCast = mongoose.Schema.Int32.cast(); | ||
| * mongoose.Schema.Int32.cast(v => { | ||
| * const defaultCast = mongoose.Schema.Types.Int32.cast(); | ||
| * mongoose.Schema.Types.Int32.cast(v => { | ||
| * if (isNaN(v)) { | ||
| * return 0; | ||
| * } | ||
| * return defaultCast(v); | ||
| * }); | ||
| * | ||
| * // Or disable casting for Int32s entirely | ||
| * // Or disable casting for Int32s entirely (only JS numbers within 32-bit integer bounds and null-ish values are permitted) | ||
| * mongoose.Schema.Int32.cast(false); | ||
| * | ||
| * | ||
| * @param {Function} caster | ||
| * @return {Function} | ||
| * @function get | ||
|
|
@@ -112,16 +132,18 @@ SchemaInt32.cast = function cast(caster) { | |
| if (caster === false) { | ||
| caster = this._defaultCaster; | ||
| } | ||
|
|
||
| this._cast = caster; | ||
|
|
||
| return this._cast; | ||
| }; | ||
|
|
||
|
|
||
| /*! | ||
| * ignore | ||
| */ | ||
|
|
||
| SchemaInt32._checkRequired = v => v != null && isBsonType(v, 'Int32'); | ||
| SchemaInt32._checkRequired = v => v != null; | ||
| /** | ||
| * Override the function the required validator uses to check whether a value | ||
| * passes the `required` check. | ||
|
|
@@ -157,9 +179,6 @@ SchemaInt32.prototype.checkRequired = function(value) { | |
|
|
||
| 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') { | ||
|
|
@@ -225,24 +244,6 @@ SchemaInt32.prototype.castForQuery = function($conditional, val, context) { | |
| } | ||
| }; | ||
|
|
||
| /** | ||
| * | ||
| * @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. | ||
| */ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.