33* Takes in the **express()** function
44* @param {Object } app - The **express()** function
55* @param {Object } [opt] - The options
6- * @param {String } [opt.path='/x' ] - The redirect path
6+ * @param {String } [opt.path] - The redirect path
77* @example //For the option
88{ path: '/PnF' }
99*
@@ -76,7 +76,81 @@ const routeCheck = (app, opt) => {
7676
7777} ;
7878
79+ //Route Validation Function
80+ /**
81+ *
82+ * @param {Object } [opt] - The options
83+ * @param {Boolean } [opt.checkGet]
84+ * @returns {function():void }
85+ */
86+ const inputValidation = ( opt ) => {
87+
88+
89+ return ( req , res , next ) => {
90+
91+ //Solve cors issue || Browser will send an OPTIONS request
92+ //that expects a HTTP staus code of 200
93+ if ( req . method === 'OPTIONS' ) {
94+ return res . sendStatus ( 200 ) ;
95+ }
96+
97+ //Let the get method pass
98+ if ( req . method === 'GET' ) {
99+ return next ( ) ;
100+ }
101+
102+ //Deconstruct the request to get the body out
103+ const { body, } = req ;
104+
105+ //Check if the input is any flase value
106+ //(when nothing is pass into the middleware)
107+ if ( ! body ) {
108+
109+ res . status ( 400 ) . json ( { msg : 'Some fields are missing!' , } ) ;
110+ return ;
111+ }
112+
113+ //Array for storing non-0 values
114+ let checkZ = [ ] ;
115+
116+ for ( const key in body ) {
117+
118+ const element = body [ key ] ;
119+
120+ if ( element !== 0 ) {
121+ checkZ . push ( element ) ;
122+ }
123+
124+ }
125+
126+ //Make sure the array is not empty
127+ if ( checkZ . length === 0 ) {
128+
129+ res . status ( 400 ) . json ( { msg : 'Some fields are missing!' , } ) ;
130+ return ;
131+ }
132+
133+ //Check for falsy values in the array without 0s
134+ for ( let index = 0 ; index < checkZ . length ; index ++ ) {
135+ const nonZinput = checkZ [ index ] ;
136+
137+ //Return false if there is any falsy value
138+ if ( ! nonZinput ) {
139+
140+ res . status ( 400 ) . json ( { msg : 'Some fields are missing!' , } ) ;
141+ return ;
142+ }
143+
144+ }
145+
146+ return next ( ) ;
147+
148+ } ;
149+ } ;
150+
151+
79152
80153module . exports = {
81154 routeCheck,
155+ inputValidation,
82156} ;
0 commit comments