Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aditi-khare-mongoDB committed Nov 7, 2024
1 parent 10a3aed commit 05ce588
Show file tree
Hide file tree
Showing 6 changed files with 475 additions and 2 deletions.
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;

let _val = Number(val);

Check warning on line 28 in lib/cast/int32.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

'_val' is never reassigned. Use 'const' instead

Check warning on line 28 in lib/cast/int32.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

'_val' is never reassigned. Use 'const' instead

if (!isNaN(_val) && _val === Math.round(_val) && _val > INT32_MIN && _val < INT32_MAX) {
return _val;
}
assert.ok(false);
}
};

Check warning on line 35 in lib/cast/int32.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Newline required at end of file but not found

Check warning on line 35 in lib/cast/int32.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Newline required at end of file but not found
2 changes: 1 addition & 1 deletion lib/schema/bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function SchemaBigInt(path, options) {

/**
* This schema type's name, to defend against minifiers that mangle
* function names.
* fun
*
* @api public
*/
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
229 changes: 229 additions & 0 deletions lib/schema/int32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
'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;

Check warning on line 229 in lib/schema/int32.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Newline required at end of file but not found

Check warning on line 229 in lib/schema/int32.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Newline required at end of file but not found
2 changes: 1 addition & 1 deletion test/bigint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('BigInt', function() {
assert.strictEqual(doc.bigint2, 997n);
});

it('handles cast errors', async function() {
it.only('handles cast errors', async function() {

Check failure on line 42 in test/bigint.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected "only" method called on a mocha test keyword

Check failure on line 42 in test/bigint.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected "only" method called on a mocha test keyword
const schema = new Schema({
bigint: 'BigInt'
});
Expand Down
Loading

0 comments on commit 05ce588

Please sign in to comment.