Skip to content

Commit 05ce588

Browse files
initial commit
1 parent 10a3aed commit 05ce588

File tree

6 files changed

+475
-2
lines changed

6 files changed

+475
-2
lines changed

lib/cast/int32.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
5+
/**
6+
* Given a value, cast it to a Int32, or throw an `Error` if the value
7+
* cannot be casted. `null` and `undefined` are considered valid.
8+
*
9+
* @param {Any} value
10+
* @return {Number}
11+
* @throws {Error} if `value` does not represent an integer, or is beyond the bounds of an 32-bit integer.
12+
* @api private
13+
*/
14+
15+
module.exports = function castInt32(val) {
16+
if (val == null) {
17+
return val;
18+
}
19+
if (val === '') {
20+
return null;
21+
}
22+
23+
if (typeof val === 'string' || typeof val === 'number') {
24+
25+
const INT32_MAX = 0x7FFFFFFF;
26+
const INT32_MIN = -0x80000000;
27+
28+
let _val = Number(val);
29+
30+
if (!isNaN(_val) && _val === Math.round(_val) && _val > INT32_MIN && _val < INT32_MAX) {
31+
return _val;
32+
}
33+
assert.ok(false);
34+
}
35+
};

lib/schema/bigint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function SchemaBigInt(path, options) {
2323

2424
/**
2525
* This schema type's name, to defend against minifiers that mangle
26-
* function names.
26+
* fun
2727
*
2828
* @api public
2929
*/

lib/schema/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ exports.ObjectId = require('./objectId');
1919
exports.String = require('./string');
2020
exports.Subdocument = require('./subdocument');
2121
exports.UUID = require('./uuid');
22+
exports.Int32 = require('./int32');
2223

2324
// alias
2425

lib/schema/int32.js

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
'use strict';
2+
3+
/*!
4+
* Module dependencies.
5+
*/
6+
7+
const CastError = require('../error/cast');
8+
const SchemaType = require('../schemaType');
9+
const castInt32 = require('../cast/int32');
10+
const isBsonType = require('../helpers/isBsonType');
11+
12+
/**
13+
* Int32 SchemaType constructor.
14+
*
15+
* @param {String} path
16+
* @param {Object} options
17+
* @inherits SchemaType
18+
* @api public
19+
*/
20+
21+
function SchemaInt32(path, options) {
22+
SchemaType.call(this, path, options, 'Int32');
23+
}
24+
25+
/**
26+
* This schema type's name, to defend against minifiers that mangle
27+
* function names.
28+
*
29+
* @api public
30+
*/
31+
SchemaInt32.schemaName = 'Int32';
32+
33+
SchemaInt32.defaultOptions = {};
34+
35+
/*!
36+
* Inherits from SchemaType.
37+
*/
38+
SchemaInt32.prototype = Object.create(SchemaType.prototype);
39+
SchemaInt32.prototype.constructor = SchemaInt32;
40+
41+
/*!
42+
* ignore
43+
*/
44+
45+
SchemaInt32._cast = castInt32;
46+
47+
/**
48+
* Sets a default option for all Int32 instances.
49+
*
50+
*
51+
* @param {String} option The option you'd like to set the value for
52+
* @param {Any} value value for option
53+
* @return {undefined}
54+
* @function set
55+
* @static
56+
* @api public
57+
*/
58+
59+
SchemaInt32.set = SchemaType.set;
60+
61+
SchemaInt32.setters = [];
62+
63+
/**
64+
* Attaches a getter for all Int32 instances
65+
*
66+
*
67+
* @param {Function} getter
68+
* @return {this}
69+
* @function get
70+
* @static
71+
* @api public
72+
*/
73+
74+
SchemaInt32.get = SchemaType.get;
75+
76+
/**
77+
* Get/set the function used to cast arbitrary values to booleans.
78+
*
79+
*
80+
* @param {Function} caster
81+
* @return {Function}
82+
* @function get
83+
* @static
84+
* @api public
85+
*/
86+
87+
SchemaInt32.cast = function cast(caster) {
88+
if (arguments.length === 0) {
89+
return this._cast;
90+
}
91+
if (caster === false) {
92+
caster = this._defaultCaster;
93+
}
94+
this._cast = caster;
95+
96+
return this._cast;
97+
};
98+
99+
/*!
100+
* ignore
101+
*/
102+
103+
SchemaInt32._checkRequired = v => v != null && isBsonType(v, 'Int32');
104+
/**
105+
* Override the function the required validator uses to check whether a value
106+
* passes the `required` check.
107+
*
108+
* @param {Function} fn
109+
* @return {Function}
110+
* @function checkRequired
111+
* @static
112+
* @api public
113+
*/
114+
115+
SchemaInt32.checkRequired = SchemaType.checkRequired;
116+
117+
/**
118+
* Check if the given value satisfies a required validator.
119+
*
120+
* @param {Any} value
121+
* @return {Boolean}
122+
* @api public
123+
*/
124+
125+
SchemaInt32.prototype.checkRequired = function(value) {
126+
return this.constructor._checkRequired(value);
127+
};
128+
129+
/**
130+
* Casts to Int32
131+
*
132+
* @param {Object} value
133+
* @param {Object} model this value is optional
134+
* @api private
135+
*/
136+
137+
SchemaInt32.prototype.cast = function(value) {
138+
let castInt32;
139+
if (isBsonType(value, 'Int32')) {
140+
return value;
141+
}
142+
if (typeof this._castFunction === 'function') {
143+
castInt32 = this._castFunction;
144+
} else if (typeof this.constructor.cast === 'function') {
145+
castInt32 = this.constructor.cast();
146+
} else {
147+
castInt32 = SchemaInt32.cast();
148+
}
149+
150+
try {
151+
return castInt32(value);
152+
} catch (error) {
153+
throw new CastError('Int32', value, this.path, error, this);
154+
}
155+
};
156+
157+
/*!
158+
* ignore
159+
*/
160+
161+
SchemaInt32.$conditionalHandlers = {
162+
...SchemaType.prototype.$conditionalHandlers,
163+
$gt: handleSingle,
164+
$gte: handleSingle,
165+
$lt: handleSingle,
166+
$lte: handleSingle
167+
};
168+
169+
/*!
170+
* ignore
171+
*/
172+
173+
function handleSingle(val, context) {
174+
return this.castForQuery(null, val, context);
175+
}
176+
177+
/**
178+
* Casts contents for queries.
179+
*
180+
* @param {String} $conditional
181+
* @param {any} val
182+
* @api private
183+
*/
184+
185+
SchemaInt32.prototype.castForQuery = function($conditional, val, context) {
186+
let handler;
187+
if ($conditional != null) {
188+
handler = SchemaInt32.$conditionalHandlers[$conditional];
189+
190+
if (handler) {
191+
return handler.call(this, val);
192+
}
193+
194+
return this.applySetters(null, val, context);
195+
}
196+
197+
try {
198+
return this.applySetters(val, context);
199+
} catch (err) {
200+
if (err instanceof CastError && err.path === this.path && this.$fullPath != null) {
201+
err.path = this.$fullPath;
202+
}
203+
throw err;
204+
}
205+
};
206+
207+
/**
208+
*
209+
* @api private
210+
*/
211+
212+
SchemaInt32.prototype._castNullish = function _castNullish(v) {
213+
if (typeof v === 'undefined') {
214+
return v;
215+
}
216+
const castInt32 = typeof this.constructor.cast === 'function' ?
217+
this.constructor.cast() :
218+
SchemaInt32.cast();
219+
if (castInt32 == null) {
220+
return v;
221+
}
222+
return v;
223+
};
224+
225+
/*!
226+
* Module exports.
227+
*/
228+
229+
module.exports = SchemaInt32;

test/bigint.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('BigInt', function() {
3939
assert.strictEqual(doc.bigint2, 997n);
4040
});
4141

42-
it('handles cast errors', async function() {
42+
it.only('handles cast errors', async function() {
4343
const schema = new Schema({
4444
bigint: 'BigInt'
4545
});

0 commit comments

Comments
 (0)