NodeJS Mongoose Validation

Validation

  • defined in SchemaType
  • middleware

Built in validators

  • required attr
  • Numbers have min and max
  • String have enum and match

required

var s = new Schema({ n: { type: Number, max: 10 });

Numbers

var s = new Schema({ n: { type: Number, max: 10 });

match

var s = new Schema({ name: { type: String, match: /^a/ }});

If failed, err has come

Validation Errors

ValidationError class
Properties are following

  • err.errors.color.message
  • err.errors.color.message
  • err.errors.color.path
  • err.name
  • err.message