@@ -27,8 +27,8 @@ function integerValidator(fieldLocation, fieldname, optional = true, lowerBound
2727
2828 if ( optional ) {
2929 return value . optional ( {
30- checkFalsy : true
31- } )
30+ checkFalsy : true
31+ } )
3232 . isInt ( ) . withMessage ( `${ fieldname } must be an integer.` )
3333 . custom ( ( value ) => {
3434 return value >= lowerBound && value <= upperBound ;
@@ -71,8 +71,8 @@ function mongoIdArrayValidator(fieldLocation, fieldname, optional = true) {
7171
7272 if ( optional ) {
7373 return arr . optional ( {
74- checkFalsy : true
75- } )
74+ checkFalsy : true
75+ } )
7676 . custom ( isMongoIdArray ) . withMessage ( "Value must be an array of mongoIDs" ) ;
7777 } else {
7878 return arr . exists ( )
@@ -137,8 +137,8 @@ function regexValidator(fieldLocation, fieldname, optional = true, desire = Cons
137137
138138 if ( optional ) {
139139 return match . optional ( {
140- checkFalsy : true
141- } )
140+ checkFalsy : true
141+ } )
142142 . matches ( desire )
143143 . withMessage ( "must be valid url" ) ;
144144 } else {
@@ -315,8 +315,8 @@ function jwtValidator(fieldLocation, fieldname, jwtSecret, optional = true) {
315315 const jwtValidationChain = setProperValidationChainBuilder ( fieldLocation , fieldname , "Must be vali jwt" ) ;
316316 if ( optional ) {
317317 return jwtValidationChain . optional ( {
318- checkFalsy : true
319- } )
318+ checkFalsy : true
319+ } )
320320 . custom ( value => {
321321 const token = jwt . verify ( value , jwtSecret ) ;
322322 if ( typeof token !== "undefined" ) {
@@ -406,8 +406,8 @@ function searchValidator(fieldLocation, fieldname) {
406406function searchSortValidator ( fieldLocation , fieldName ) {
407407 const searchSort = setProperValidationChainBuilder ( fieldLocation , fieldName , "Invalid sort criteria" )
408408 return searchSort . optional ( {
409- checkFalsy : true
410- } )
409+ checkFalsy : true
410+ } )
411411 . custom ( ( value , {
412412 req
413413 } ) => {
@@ -551,6 +551,38 @@ function enumValidator(fieldLocation, fieldname, enums, optional = true) {
551551 }
552552}
553553
554+ /**
555+ * Validates that action field is a valid action from constants passed, and checks if corresponding new status is valid.
556+ * @param {"query" | "body" | "header" | "param" } fieldLocation The location where the field should be found.
557+ * @param {string } actionFieldName The name of the action that needs to be performed.
558+ * @param {string } statusFieldName The name of the action that needs to be performed.
559+ */
560+ function actionValidator ( fieldLocation , actionFieldName ) {
561+ const actionValue = setProperValidationChainBuilder ( fieldLocation , actionFieldName , "Invalid action." ) ;
562+
563+ return actionValue . exists ( )
564+ . withMessage ( "The action must exist." )
565+ . custom ( actionValidatorHelper ) . withMessage ( "The value must be a valid action." ) ;
566+ }
567+
568+
569+ function statusValidator ( fieldLocation , statusFieldName ) {
570+ const statusValue = setProperValidationChainBuilder ( fieldLocation , statusFieldName , "Invalid status." ) ;
571+ return statusValue . exists ( ) . withMessage ( "The status must exist!" ) . custom ( ( val , {
572+ req
573+ } ) => {
574+ return Constants . CORRESPONDING_STATUSES [ req . query . action ] . includes ( val ) ;
575+ } ) . withMessage ( "The value must be a proper status." )
576+ }
577+
578+ function actionValidatorHelper ( action ) {
579+ if ( Constants . VALID_SEARCH_ACTIONS . includes ( action ) ) {
580+ return true ;
581+ }
582+ return false ;
583+ }
584+
585+
554586/**
555587 * Checks that 'value' is part of 'enums'. 'enums' should be an enum dict.
556588 * @param {* } value Should be of the same type as the values of the enum
@@ -614,4 +646,6 @@ module.exports = {
614646 dateValidator : dateValidator ,
615647 enumValidator : enumValidator ,
616648 routesValidator : routesValidator ,
649+ actionValidator : actionValidator ,
650+ statusValidator : statusValidator
617651} ;
0 commit comments