summaryrefslogtreecommitdiff
path: root/node_modules/mongoose/docs/errors.md
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2012-09-24 16:22:07 -0400
committerJules Laplace <jules@okfoc.us>2012-09-24 16:22:07 -0400
commit686106d544ecc3b6ffd4db2b665d3bc879a58d8c (patch)
treea5b5e50237cef70e12f0745371896e96f5f6d578 /node_modules/mongoose/docs/errors.md
ok
Diffstat (limited to 'node_modules/mongoose/docs/errors.md')
-rw-r--r--node_modules/mongoose/docs/errors.md54
1 files changed, 54 insertions, 0 deletions
diff --git a/node_modules/mongoose/docs/errors.md b/node_modules/mongoose/docs/errors.md
new file mode 100644
index 0000000..a1852ef
--- /dev/null
+++ b/node_modules/mongoose/docs/errors.md
@@ -0,0 +1,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
+