Skip to content

Commit 67babb8

Browse files
committed
docs(error): add more detail about the ValidatorError class, including properties
Fix #8346
1 parent 40dbc06 commit 67babb8

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

lib/error/index.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,32 @@ MongooseError.CastError = require('./cast');
102102
MongooseError.ValidationError = require('./validation');
103103

104104
/**
105-
* A `ValidationError` has a hash of `errors` that contain individual `ValidatorError` instances
105+
* A `ValidationError` has a hash of `errors` that contain individual
106+
* `ValidatorError` instances.
107+
*
108+
* ####Example:
109+
*
110+
* const schema = Schema({ name: { type: String, required: true } });
111+
* const Model = mongoose.model('Test', schema);
112+
* const doc = new Model({});
113+
*
114+
* // Top-level error is a ValidationError, **not** a ValidatorError
115+
* const err = doc.validateSync();
116+
* err instanceof mongoose.Error.ValidationError; // true
117+
*
118+
* // A ValidationError `err` has 0 or more ValidatorErrors keyed by the
119+
* // path in the `err.errors` property.
120+
* err.errors['name'] instanceof mongoose.Error.ValidatorError;
121+
*
122+
* err.errors['name'].kind; // 'required'
123+
* err.errors['name'].path; // 'name'
124+
* err.errors['name'].value; // undefined
125+
*
126+
* Instances of `ValidatorError` have the following properties:
127+
*
128+
* - `kind`: The validator's `type`, like `'required'` or `'regexp'`
129+
* - `path`: The path that failed validation
130+
* - `value`: The value that failed validation
106131
*
107132
* @api public
108133
* @memberOf Error

0 commit comments

Comments
 (0)