Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

284 lines (236 sloc) 8.475 kB
<meta charset="utf-8">
<title>Mongoose ODM</title>
body {
background: url('/docs/2.7.x/images/bg.png');
padding: 0 30px 30px;
margin-top: 0;
#wrap {
background: url('/docs/2.7.x/images/pattern.png') no-repeat -134px -211px;
min-height: 600px;
padding-top: 30px;
#page {
width: 650px;
margin: auto;
position: relative;
#footer {
font: 13px Helvetica;
padding-top: 15px;
border-top: 1px solid #000;
color: #666;
margin-top: 25px;
#footer > div {
padding-top: 8px;
float: right;
text-align: right;
padding-bottom: 20px;
#content {
color: #111;
font: 15px Courier;
#content h1, #content h2, #content h3 {
text-shadow: 0 1px 0 #fff;
#announce {
background: #2A758A;
border: 10px solid black;
border-radius: 7px;
padding: 30px;
font-size: 125%;
color: white;
text-shadow: none;
font-family: Arial;
#announce a {
color: orange;
text-shadow: none;
pre {
background: rgba(255,255,255,.8);
border: 1px solid #c6c6c6;
padding: 10px;
border-radius: 3px;
box-shadow: 1px 3px 6px #ddd;
code {
background: rgba(255,255,255,.8);
border: 1px solid #c6c6c6;
color: #333;
border-radius: 3px
pre code {
border: 0 none;
form {
font: bold 12px Helvetica;
text-shadow: 0 1px 0 #fff;
float: left;
padding-bottom: 25px;
form img {
margin-right: 5px
#google-members-count {
vertical-align: top;
line-height: 32px;
h1 a {
background: url('/docs/2.7.x/images/logo.png');
width: 404px;
height: 47px;
margin-bottom: 40px;
text-indent: -500em;
display: block;
a {
color: #2a758a;
text-shadow: 0 1px 0 #fff;
text-decoration: none;
a:hover {
opacity: 0.8;
#menu {
position: fixed;
top: 0;
left: 0;
height: 100%;
background-color: rgba(0,0,0,0.3);
font: bold 15px Helvetica;
border-right: 1px solid rgba(0,0,0,0.2);
#menu ul {
margin: 0;
padding: 50px 0;
#menu ul li {
list-style-type: none;
padding-left: 15px;
padding-right: 20px;
#menu ul li a {
color: #fff;
text-shadow: none;
#menu ul li a:hover {
text-decoration: underline;
#wild { margin-top: 10px; }
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1122274-9']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
function getMembers(data){
if (!(data && data.query && data.query.results && data.query.results.p)) return;
var members = document.createElement('span'); = 'google-members-count';
members.innerHTML = '('+ data.query.results.p +' members)';
document.getElementsByTagName('FORM')[0].insertBefore(members, document.getElementById('google-subscribe-input'));
window.onload = function(){
// lame jsonp
var script = document.createElement('script');
script.src = "*'%2F%2Fdiv%5B%40class%3D%5C'maincontbox%5C'%5D%2Ftable%2Ftr%5B1%5D%2Ftd%2Fp%5B1%5D'%0A&format=json&callback=getMembers";
<a href=""><img style="position: absolute; top: 0; right: 0; border: 0;" src="" alt="Fork me on GitHub"></a>
<div id="wrap">
<div id="page">
<div id="menu">
<li><a href="../index.html">Home</a></li>
<li><a href="model-definition.html">Models/Schemas</a></li>
<li><a href="schematypes.html">Schema Types</a></li>
<li><a href="schema-options.html">Schema Options</a></li>
<li><a href="middleware.html">Middleware</a></li>
<li><a href="finding-documents.html">Finding Docs</a></li>
<li><a href="updating-documents.html">Updating Docs</a></li>
<li><a href="query.html">Queries</a></li>
<li><a href="querystream.html">Query Streams</a></li>
<li><a href="methods-statics.html">Methods &amp; Statics</a>
<li><a href="defaults.html">Defaults</a></li>
<li><a href="indexes.html">Indexes</a></li>
<li><a href="validation.html">Validation</a></li>
<li><a href="virtuals.html">Virtuals</a></li>
<li><a href="getters-setters.html">Getters / Setters</a></li>
<li><a href="embedded-documents.html">Embedded Docs</a></li>
<li><a href="populate.html">Populate (DBRef-like)</a></li>
<li><a href="plugins.html">Plugins</a></li>
<li><a href="errors.html">Errors</a></li>
<li><a href="migration-1x-2x.html">v1x - v2x Migration</a></li>
<li><a href="api.html">API</a></li>
<h1><a href="../index.html">Mongoose</a></h1>
<div id="content">
<!-- ANNOUNCE -->
<h1>Error handling</h1>
<p>Errors returned after failed validation contain an <code>errors</code> object holding the actual ValidatorErrors. Each ValidatorError has a <code>type</code> and <code>path</code> property providing us with a little more error handling flexibility.</p>
<pre><code>var ToySchema = new Schema({
color: String
, name: String
var Toy = db.model('Toy', ToySchema);
Toy.schema.path('color').validate(function (value) {
return /blue|green|white|red|orange|periwinkel/i.test(value);
}, 'Invalid color');
var toy = new Toy({ color: 'grease'}); (err) {
// previous behavior (v1x):
// prints 'Validator "Invalid color" failed for path color'
// new v2x behavior - err.errors.color is a ValidatorError object
// prints 'Validator "Invalid color" failed for path color'
// you can get v1 behavior back by casting error.color toString
// prints 'Validator "Invalid color" failed for path color'
// prints "Invalid color"
// prints "color"
// prints "ValidationError"
// prints "Validation failed"
<p>BTW, the <code>err.errors</code> object is also available on the model instance.</p>
<pre><code>toy.errors.color.message === err.errors.color.message
<div id="footer">
<form action="">
<a href="" id="google-subscribe-link"><img src="/docs/2.7.x/images/groups.png" border="0"></a>
<span id="google-members-count">&nbsp;</span>
<div id="google-subscribe-input">
Email: <input type="text" name="email" id="google-subscribe-email">
<input type="submit" name="go" value="Subscribe">
Mongoose by <a href="">LearnBoost Labs</a>.<br>
Released under the MIT license.<br>
Copyright <a href="">LearnBoost 2011</a>.
Jump to Line
Something went wrong with that request. Please try again.