Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: b24b1d1d06
Fetching contributors…

Cannot retrieve contributors at this time

36 lines (28 sloc) 7.026 kb
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Mongoose Validation v3.0.2</title><link href="http://fonts.googleapis.com/css?family=Anonymous+Pro:400,700|Droid+Sans+Mono|Open+Sans:400,700|Linden+Hill|Quattrocento:400,700|News+Cycle:400,700|Antic+Slab|Cabin+Condensed:400,700" rel="stylesheet" type="text/css"><link href="/docs/css/default.css" rel="stylesheet" type="text/css"><link href="/docs/css/guide.css" rel="stylesheet" type="text/css"></head><body><a id="forkbanner" href="http://github.com/learnboost/mongoose"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png" alt="Fork me on GitHub"></a><div id="links"><div id="header"><h1><a href="../index.html"><div class="mongoose">Mongoose</div></a></h1></div><ul><li class="home"><a href="../index.html">home</a></li><li class="faq"><a href="./faq.html">FAQ</a></li><li class="plugins"><a href="http://plugins.mongoosejs.com">plugins</a></li><li class="changelog"><a href="http://github.com/learnboost/mongoose/tree/master/History.md">change log</a></li><li class="support"><a href="../index.html#support">support</a></li><li class="fork"><a href="http://github.com/learnboost/mongoose">fork</a></li><li class="guide"><a href="./guide.html">guide</a><ul><li class="double"><a href="./guide.html">schemas</a><ul><li class="schematypes"><a href="./schematypes.html"><span>schema</span>types</a></li></ul></li><li class="double"><a href="./documents.html">documents</a><ul><li class="subdocs"><a href="./subdocs.html">sub docs</a></li></ul></li><li><a href="./models.html">models</a></li><li><a href="./queries.html">queries</a></li><li><a href="./plugins.html">plugins</a></li><li><a href="./middleware.html">middleware</a></li><li><a href="./validation.html">validation</a></li><li><a href="./populate.html">populate</a></li><li><a href="./migration.html">migrating from 2.x</a></li><li><a href="./contributing.html">contributing</a></li></ul></li><li class="api"><a href="./api.html">api docs</a></li><li class="quickstart"><a href="./index.html">quick start</a></li><li class="contrib"><a href="http://github.com/learnboost/mongoose/contributors">contributors</a></li><li class="prior"><a href="./prior.html">prior releases</a></li></ul></div><div id="content"><div class="module"><h2>Validation</h2><p>Before we get into the specifics of validation syntax, please keep the following rules in mind:</p>
<ul><li>Validation is defined in the <a href="./schematypes.html">SchemaType</a></li><li>Validation is an internal piece of <a href="./middleware.html">middleware</a></li><li>Validation occurs when a document attempts to be <a href="./api.html#model_Model-save">saved</a>, after defaults have been applied</li><li>Validation is asynchronously recursive: when you call <a href="./api.html#model_Model-save">Model#save</a>, sub-document validation is executed. If an error happens, your Model#save callback receives it</li><li>Mongoose doesn&#39;t care about complex error message construction. Errors have type identifiers. For example, <code>&quot;min&quot;</code> is the identifier for the error triggered when a number doesn&#39;t meet the <a href="./api.html#schema_number_SchemaNumber-min">minimum value</a>. The path and value that triggered the error can be accessed in the <code>ValidationError</code> object</li></ul><h3>Built in validators</h3><p>Mongoose has several built in validators.</p>
<ul><li>All <a href="./schematypes.html">SchemaTypes</a> have the built in <a href="./api.html#schematype_SchemaType-required">required</a> validator.</li><li><a href="./api.html#schema-number-js">Numbers</a> have <a href="./api.html#schema_number_SchemaNumber-min">min</a> and <a href="./api.html#schema_number_SchemaNumber-max">max</a> validators.</li><li><a href="./api.html#schema-string-js">Strings</a> have <a href="./api.html#schema_string_SchemaString-enum">enum</a> and <a href="./api.html#schema_string_SchemaString-match">match</a> validators.</li></ul><h3>Custom validators</h3><p>Custom validation is declared by passing a validation <code>function</code> and an error type to your <code>SchemaType</code>s validate method. Read the <a href="./api.html#schematype_SchemaType-validate">API</a> docs for details on custom validators, async validators, and more.</p><h3>Validation errors</h3><p>Errors returned after failed validation contain an <code>errors</code> object holding the actual <code>ValidatorErrors</code>. Each <a href="./api.html#errors-validation-js">ValidatorError</a> has a <code>type</code> and <code>path</code> property providing us with a little more error handling flexibility.</p><pre><code class="javascript"><span class="keyword">var</span> toySchema = <span class="keyword">new</span> Schema({
color: String,
name: String
});
<span class="keyword">var</span> Toy = db.model(<span class="string">'Toy'</span>, toySchema);
Toy.schema.path(<span class="string">'color'</span>).validate(<span class="function"><span class="keyword">function</span> <span class="params">(value)</span> {</span>
<span class="keyword">return</span> <span class="regexp">/blue|green|white|red|orange|periwinkel/i</span>.test(value);
}, <span class="string">'Invalid color'</span>);
<span class="keyword">var</span> toy = <span class="keyword">new</span> Toy({ color: <span class="string">'grease'</span>});
toy.save(<span class="function"><span class="keyword">function</span> <span class="params">(err)</span> {</span>
<span class="comment">// err.errors.color is a ValidatorError object</span>
console.log(err.errors.color.message) <span class="comment">// prints 'Validator "Invalid color" failed for path color'</span>
console.log(String(err.errors.color)) <span class="comment">// prints 'Validator "Invalid color" failed for path color'</span>
console.log(err.errors.color.type) <span class="comment">// prints "Invalid color"</span>
console.log(err.errors.color.path) <span class="comment">// prints "color"</span>
console.log(err.name) <span class="comment">// prints "ValidationError"</span>
console.log(err.message) <span class="comment">// prints "Validation failed"</span>
});
</code></pre><p>After a validation error, the document will also have the same <code>errors</code> property available:</p><pre><code class="javascript">toy.errors.color.message === err.errors.color.message</code></pre></div></div><script>document.body.className = 'load';</script><script>var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1122274-9']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();</script></body></html>
Jump to Line
Something went wrong with that request. Please try again.