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 ;
0 commit comments