You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<!DOCTYPE html><htmllang="en"><head><metacharSet="utf-8"/><metahttp-equiv="X-UA-Compatible" content="IE=edge"/><title>Migration from v5 to v6 · express-validator</title><metaname="viewport" content="width=device-width"/><metaname="generator" content="Docusaurus"/><metaname="description" content="Starting with v6, the approach for doing validations is more declarative and, in turn, some of the APIs we had in v5 require some changes."/><metaname="docsearch:version" content="next"/><metaname="docsearch:language" content="en"/><metaproperty="og:title" content="Migration from v5 to v6 · express-validator"/><metaproperty="og:type" content="website"/><metaproperty="og:url" content="https://express-validator.github.io/index.html"/><metaproperty="og:description" content="Starting with v6, the approach for doing validations is more declarative and, in turn, some of the APIs we had in v5 require some changes."/><metaname="twitter:card" content="summary"/><linkrel="shortcut icon" href="/img/favicon.png"/><linkrel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css"/><scripttype="text/javascript" src="https://buttons.github.io/buttons.js"></script><scriptsrc="/js/scrollSpy.js"></script><linkrel="stylesheet" href="/css/main.css"/><scriptsrc="/js/codetabs.js"></script></head><bodyclass="sideNavVisible separateOnPageNav"><divclass="fixedHeaderContainer"><divclass="headerWrapper wrapper"><header><ahref="/"><imgclass="logo" src="/img/logo.svg" alt="express-validator"/><h2class="headerTitleWithLogo">express-validator</h2></a><ahref="/versions.html"><h3>next</h3></a><divclass="navigationWrapper navigationSlider"><navclass="slidingNav"><ulclass="nav-site nav-site-internal"><liclass=""><ahref="/docs/next/index.html" target="_self">Docs</a></li><liclass=""><ahref="/docs/next/check-api.html" target="_self">API</a></li><liclass=""><ahref="https://github.com/express-validator/express-validator" target="_self">GitHub</a></li></ul></nav></div></header></div></div><divclass="navPusher"><divclass="docMainWrapper wrapper"><divclass="container mainContainer docsContainer"><divclass="wrapper"><divclass="post"><headerclass="postHeader"><aclass="edit-page-link button" href="https://github.com/express-validator/express-validator/edit/master/docs/migration-v5-to-v6.md" target="_blank" rel="noreferrer noopener">Edit</a><h1id="__docusaurus" class="postHeaderTitle">Migration from v5 to v6</h1></header><article><div><span><p>Starting with v6, the approach for doing validations is more declarative and, in turn, some of the APIs we had in v5 require some changes.</p>
2
+
<p>The purpose of this documentation is to show how to migrate your existing express-validator code from v5 to v6 through sample codes. With the understanding that there is a more declarative way of doing the validations in v6+, the only requirement in the sample codes we set for is to keep the similar programmatical approach in both versions, so that we are comparing apples to apples.</p>
3
+
<blockquote>
4
+
<p><strong>For a complete list of breaking changes and new features in 6.0.0</strong>,
<p>Say we want to leverage this library in a more <em>programmatic</em> way: A POST request handler takes a JSON object in the request's body and responds based on if the <strong><em>greetings</em></strong> attribute is found in the JSON object.</p>
<spanclass="hljs-built_in">console</span>.info(<spanclass="hljs-string">'app listening at port 3000'</span>);
35
+
});
36
+
</code></pre>
37
+
<p>For example, we want to make our v5 code to use the <ahref="https://github.com/express-validator/express-validator/blob/master/docs/api-check.md"><code>check</code></a> and <ahref="https://github.com/express-validator/express-validator/blob/master/docs/api-validation-result.md"><code>validationResult</code></a> functions in v6</p>
38
+
<ol>
39
+
<li>Change from
40
+
<code>const expressValidator = require('express-validator')</code> to
<spanclass="hljs-built_in">console</span>.info(<spanclass="hljs-string">'app listening at port 3000'</span>);
83
+
});
84
+
</code></pre>
85
+
<blockquote>
86
+
<p>The complete diff between the two versions of the example: <ahref="https://github.com/shwei/express-validator-migrate-5-to-6/blob/main/v5_to_v6.diff">https://github.com/shwei/express-validator-migrate-5-to-6/blob/main/v5_to_v6.diff</a></p>
87
+
</blockquote>
88
+
<blockquote>
89
+
<p>To see the sample codes in action I created the project <ahref="https://github.com/shwei/express-validator-migrate-5-to-6">express-validator-migrate-5-to-6</a>.</p>
<li><code>options</code><em>(optional)</em>: an object which accepts the following options:
189
+
<ul>
190
+
<li><code>strict</code>: If set to <code>false</code> the validation passes also for <code>array</code> and <code>null</code> types (defaults to <code>true</code>).</li>
191
+
</ul></li>
192
+
</ul>
193
+
<blockquote>
194
+
<p><em>Returns:</em> the current validation chain instance</p>
195
+
</blockquote>
196
+
<p>Adds a validator to check if a value is an object.</p>
0 commit comments