diegohaz / bodymen

Body parser middleware for MongoDB, Express and Nodejs (MEN)
Other
47 stars 15 forks source link

how to make conditional validation using bodymen #32

Open tamilvjm opened 4 years ago

tamilvjm commented 4 years ago

how to make conditional validation using bodymen? for example only validate if some flag is enabled.

Is there any such feature in it?

tguelcan commented 4 years ago

you can also import the simple validation like minlenght, required... also from the model schema of mongoose For custom validation, you can use this way:

router.post(
    '/',
    body({
        content: {
            ...content,
            required: true,
            minlength: 2,
            validate: (value) => ({ valid: value === 'flag', message: 'Content value is not flag'  }),
        }
    }),
    create
)