summaryrefslogtreecommitdiff
path: root/node_modules/mongoose/docs/errors.md
blob: a1852efc8fcd7f285a7d8c57062f218422320258 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Error handling
==============

Errors returned after failed validation contain an `errors` object
holding the actual ValidatorErrors. Each ValidatorError has a `type` and `path` property
providing us with a little more error handling flexibility.

    var ToySchema = new Schema({
        color: String
      , name: String
    });

    var Toy = db.model('Toy', ToySchema);

    Toy.schema.path('name').validate(function (value) {
      return /blue|green|white|red|orange|periwinkel/i.test(value);
    }, 'Invalid color');

    var toy = new Toy({ color: 'grease'});

    toy.save(function (err) {
      // previous behavior (v1x):

      console.log(err.errors.color)
      // prints 'Validator "Invalid color" failed for path color'

      // new v2x behavior - err.errors.color is a ValidatorError object

      console.log(err.errors.color.message)
      // prints 'Validator "Invalid color" failed for path color'

      // you can get v1 behavior back by casting error.color toString

      console.log(String(err.errors.color))
      // prints 'Validator "Invalid color" failed for path color'

      console.log(err.errors.color.type);
      // prints "Invalid color"

      console.log(err.errors.color.path)
      // prints "color"

      console.log(err.name)
      // prints "ValidationError"

      console.log(err.message)
      // prints "Validation failed"
    });

BTW, the `err.errors` object is also available on the model instance.

    toy.errors.color.message === err.errors.color.message