diegohaz / bodymen

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

How to make validations? #17

Closed hackerart closed 6 years ago

hackerart commented 6 years ago

I want to create validation for phoneNumber phoneNumber:{ type: String, validate: /\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/ } or to choose validate that value equals only one of provided productType: { type: String, validate: ['bread', 'milk'] }

diegohaz commented 6 years ago

This project follows the same rules as Mongoose (check out their docs).

In that case, it will be:

{
  phoneNumber: {
    type: String,
    match: /\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/,
  },
  productType: {
    type: String,
    enum: ['bread', 'milk']
  }
}

validate accepts a function. You can use it to create custom validations.