Skip to content

Commit 88519db

Browse files
committed
Deploy website
Deploy website version based on 30d28734426a0912811a76654b420201491801bf
1 parent cfb8be6 commit 88519db

3 files changed

Lines changed: 104 additions & 2 deletions

File tree

docs/next/migration-v5-to-v6.html

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<!DOCTYPE html><html lang="en"><head><meta charSet="utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><title>Migration from v5 to v6 · express-validator</title><meta name="viewport" content="width=device-width"/><meta name="generator" content="Docusaurus"/><meta name="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."/><meta name="docsearch:version" content="next"/><meta name="docsearch:language" content="en"/><meta property="og:title" content="Migration from v5 to v6 · express-validator"/><meta property="og:type" content="website"/><meta property="og:url" content="https://express-validator.github.io/index.html"/><meta property="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."/><meta name="twitter:card" content="summary"/><link rel="shortcut icon" href="/img/favicon.png"/><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css"/><script type="text/javascript" src="https://buttons.github.io/buttons.js"></script><script src="/js/scrollSpy.js"></script><link rel="stylesheet" href="/css/main.css"/><script src="/js/codetabs.js"></script></head><body class="sideNavVisible separateOnPageNav"><div class="fixedHeaderContainer"><div class="headerWrapper wrapper"><header><a href="/"><img class="logo" src="/img/logo.svg" alt="express-validator"/><h2 class="headerTitleWithLogo">express-validator</h2></a><a href="/versions.html"><h3>next</h3></a><div class="navigationWrapper navigationSlider"><nav class="slidingNav"><ul class="nav-site nav-site-internal"><li class=""><a href="/docs/next/index.html" target="_self">Docs</a></li><li class=""><a href="/docs/next/check-api.html" target="_self">API</a></li><li class=""><a href="https://github.com/express-validator/express-validator" target="_self">GitHub</a></li></ul></nav></div></header></div></div><div class="navPusher"><div class="docMainWrapper wrapper"><div class="container mainContainer docsContainer"><div class="wrapper"><div class="post"><header class="postHeader"><a class="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><h1 id="__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>,
5+
please check <a href="https://github.com/express-validator/express-validator/releases/tag/v6.0.0">its release notes</a>.</p>
6+
</blockquote>
7+
<h2><a class="anchor" aria-hidden="true" id="sample-code-using-v531"></a><a href="#sample-code-using-v531" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Sample code using v5.3.1</h2>
8+
<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>
9+
<pre><code class="hljs css language-js"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">'express'</span>);
10+
<span class="hljs-keyword">const</span> bodyParser = <span class="hljs-built_in">require</span>(<span class="hljs-string">'body-parser'</span>);
11+
<span class="hljs-keyword">const</span> expressValidator = <span class="hljs-built_in">require</span>(<span class="hljs-string">'express-validator'</span>);
12+
13+
<span class="hljs-keyword">const</span> app = express();
14+
app.use(bodyParser.json());
15+
app.use(bodyParser.urlencoded({ <span class="hljs-attr">extended</span>: <span class="hljs-literal">false</span> }));
16+
app.use(expressValidator());
17+
18+
app.post(<span class="hljs-string">'/'</span>, (req, res) =&gt; {
19+
<span class="hljs-built_in">console</span>.info(<span class="hljs-string">`checking request body: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(req.body)}</span>`</span>);
20+
req.checkBody(<span class="hljs-string">'greetings'</span>).isLength({ <span class="hljs-attr">min</span>: <span class="hljs-number">1</span> });
21+
22+
<span class="hljs-keyword">const</span> validationResults = req.validationErrors();
23+
<span class="hljs-built_in">console</span>.info(<span class="hljs-string">`validation results: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(validationResults)}</span>`</span>);
24+
25+
<span class="hljs-keyword">if</span> (!validationResults || validationResults.length &lt; <span class="hljs-number">1</span>) {
26+
req.sanitizeBody(<span class="hljs-string">'greetings'</span>).escape().trim();
27+
res.send(<span class="hljs-string">`checking done, hello: <span class="hljs-subst">${req.body[<span class="hljs-string">'greetings'</span>]}</span>`</span>);
28+
} <span class="hljs-keyword">else</span> {
29+
res.send(<span class="hljs-string">`checking done, error: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(validationResults)}</span>`</span>);
30+
}
31+
});
32+
33+
app.listen(<span class="hljs-number">3000</span>, () =&gt; {
34+
<span class="hljs-built_in">console</span>.info(<span class="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 <a href="https://github.com/express-validator/express-validator/blob/master/docs/api-check.md"><code>check</code></a> and <a href="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
41+
<code>const {check, validationResult} = require('express-validator')</code></li>
42+
<li>Remove <code>app.use(expressValidator())</code></li>
43+
<li>Change from
44+
<code>req.checkBody('greetings').isLength({min: 1})</code> to
45+
<code>await check('greetings').notEmpty().run(req)</code></li>
46+
<li>Change from
47+
<code>const validationResults = req.validationErrors()</code> to
48+
<code>const validationResults = validationResult(req)</code></li>
49+
<li>Change from
50+
<code>req.sanitizeBody('greetings').escape().trim()</code> to
51+
<code>await check('greetings').trim().escape().run(req)</code></li>
52+
</ol>
53+
<h2><a class="anchor" aria-hidden="true" id="sample-code-using-v6"></a><a href="#sample-code-using-v6" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Sample code using v6+</h2>
54+
<pre><code class="hljs css language-js"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">'express'</span>);
55+
<span class="hljs-keyword">const</span> bodyParser = <span class="hljs-built_in">require</span>(<span class="hljs-string">'body-parser'</span>);
56+
<span class="hljs-keyword">const</span> { check, validationResult } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'express-validator'</span>);
57+
58+
<span class="hljs-keyword">const</span> app = express();
59+
app.use(bodyParser.json());
60+
app.use(bodyParser.urlencoded({ <span class="hljs-attr">extended</span>: <span class="hljs-literal">false</span> }));
61+
62+
app.post(<span class="hljs-string">'/'</span>, <span class="hljs-keyword">async</span> (req, res) =&gt; {
63+
<span class="hljs-built_in">console</span>.info(<span class="hljs-string">`checking request body: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(req.body)}</span>`</span>);
64+
<span class="hljs-keyword">await</span> check(<span class="hljs-string">'greetings'</span>).notEmpty().run(req);
65+
66+
<span class="hljs-keyword">const</span> validationResults = validationResult(req);
67+
<span class="hljs-built_in">console</span>.info(
68+
<span class="hljs-string">`validation results: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(
69+
validationResults,
70+
)}</span>, is empty? <span class="hljs-subst">${validationResults.isEmpty()}</span>`</span>,
71+
);
72+
73+
<span class="hljs-keyword">if</span> (validationResults.isEmpty()) {
74+
<span class="hljs-keyword">await</span> check(<span class="hljs-string">'greetings'</span>).trim().escape().run(req);
75+
res.send(<span class="hljs-string">`checking done, hello: <span class="hljs-subst">${req.body[<span class="hljs-string">'greetings'</span>]}</span>`</span>);
76+
} <span class="hljs-keyword">else</span> {
77+
res.send(<span class="hljs-string">`checking done, error: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(validationResults.array())}</span>`</span>);
78+
}
79+
});
80+
81+
app.listen(<span class="hljs-number">3000</span>, () =&gt; {
82+
<span class="hljs-built_in">console</span>.info(<span class="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: <a href="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 <a href="https://github.com/shwei/express-validator-migrate-5-to-6">express-validator-migrate-5-to-6</a>.</p>
90+
</blockquote>
91+
</span></div></article></div><div class="docs-prevnext"></div></div></div><nav class="onPageNav"><ul class="toc-headings"><li><a href="#sample-code-using-v531">Sample code using v5.3.1</a></li><li><a href="#sample-code-using-v6">Sample code using v6+</a></li></ul></nav></div><footer class="nav-footer" id="footer"><section class="sitemap"><a href="/" class="nav-home"><img src="/img/logo.svg" alt="express-validator" width="66" height="58"/></a><div><h5>Docs</h5><a href="/docs/index.html">Getting Started</a><a href="/docs/check-api.html">API Reference</a></div><div><h5>Community</h5><a href="http://stackoverflow.com/questions/tagged/express-validator" target="_blank" rel="noreferrer noopener">Stack Overflow</a></div><div><h5>More</h5><a href="https://github.com/express-validator/express-validator">GitHub</a><a class="github-button" href="https://github.com/express-validator/express-validator" data-icon="octicon-star" data-count-href="/express-validator/express-validator/stargazers" data-show-count="true" data-count-aria-label="# stargazers on GitHub" aria-label="Star this project on GitHub">Star</a></div></section><section class="copyright">Copyright © 2021 express-validator</section></footer></div></body></html>

docs/next/validation-chain-api.html

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,17 @@ <h3><a class="anchor" aria-hidden="true" id="isarrayoptions"></a><a href="#isarr
183183
<p><em>Returns:</em> the current validation chain instance</p>
184184
</blockquote>
185185
<p>Adds a validator to check if a value is an array.</p>
186+
<h3><a class="anchor" aria-hidden="true" id="isobjectoptions"></a><a href="#isobjectoptions" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a><code>.isObject(options)</code></h3>
187+
<ul>
188+
<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>
186197
<h3><a class="anchor" aria-hidden="true" id="isstring"></a><a href="#isstring" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a><code>.isString()</code></h3>
187198
<blockquote>
188199
<p><em>Returns:</em> the current validation chain instance</p>
@@ -277,4 +288,4 @@ <h3><a class="anchor" aria-hidden="true" id="withmessagemessage"></a><a href="#w
277288
<p>Sets the error message for the previous validator.<br>
278289
This will have precedence over errors thrown by a custom validator.</p>
279290
<p>To build dynamic messages, see also <a href="/docs/next/custom-error-messages.html#dynamic-messages">Dynamic Messages</a>.</p>
280-
</span></div></article></div><div class="docs-prevnext"><a class="docs-prev button" href="/docs/next/check-api.html"><span class="arrow-prev"></span><span>Validation middlewares</span></a><a class="docs-next button" href="/docs/next/filter-api.html"><span>Sanitization middlewares</span><span class="arrow-next"></span></a></div></div></div><nav class="onPageNav"><ul class="toc-headings"><li><a href="#standard-validators">Standard validators</a></li><li><a href="#sanitization-chain-api">Sanitization Chain API</a></li><li><a href="#additional-methods">Additional methods</a><ul class="toc-headings"><li><a href="#bail"><code>.bail()</code></a></li><li><a href="#customvalidator"><code>.custom(validator)</code></a></li><li><a href="#existsoptions"><code>.exists(options)</code></a></li><li><a href="#ifcondition"><code>.if(condition)</code></a></li><li><a href="#isarrayoptions"><code>.isArray(options)</code></a></li><li><a href="#isstring"><code>.isString()</code></a></li><li><a href="#not"><code>.not()</code></a></li><li><a href="#notempty"><code>.notEmpty()</code></a></li><li><a href="#optionaloptions"><code>.optional(options)</code></a></li><li><a href="#runreq-options"><code>.run(req[, options])</code></a></li><li><a href="#withmessagemessage"><code>.withMessage(message)</code></a></li></ul></li></ul></nav></div><footer class="nav-footer" id="footer"><section class="sitemap"><a href="/" class="nav-home"><img src="/img/logo.svg" alt="express-validator" width="66" height="58"/></a><div><h5>Docs</h5><a href="/docs/index.html">Getting Started</a><a href="/docs/check-api.html">API Reference</a></div><div><h5>Community</h5><a href="http://stackoverflow.com/questions/tagged/express-validator" target="_blank" rel="noreferrer noopener">Stack Overflow</a></div><div><h5>More</h5><a href="https://github.com/express-validator/express-validator">GitHub</a><a class="github-button" href="https://github.com/express-validator/express-validator" data-icon="octicon-star" data-count-href="/express-validator/express-validator/stargazers" data-show-count="true" data-count-aria-label="# stargazers on GitHub" aria-label="Star this project on GitHub">Star</a></div></section><section class="copyright">Copyright © 2021 express-validator</section></footer></div></body></html>
291+
</span></div></article></div><div class="docs-prevnext"><a class="docs-prev button" href="/docs/next/check-api.html"><span class="arrow-prev"></span><span>Validation middlewares</span></a><a class="docs-next button" href="/docs/next/filter-api.html"><span>Sanitization middlewares</span><span class="arrow-next"></span></a></div></div></div><nav class="onPageNav"><ul class="toc-headings"><li><a href="#standard-validators">Standard validators</a></li><li><a href="#sanitization-chain-api">Sanitization Chain API</a></li><li><a href="#additional-methods">Additional methods</a><ul class="toc-headings"><li><a href="#bail"><code>.bail()</code></a></li><li><a href="#customvalidator"><code>.custom(validator)</code></a></li><li><a href="#existsoptions"><code>.exists(options)</code></a></li><li><a href="#ifcondition"><code>.if(condition)</code></a></li><li><a href="#isarrayoptions"><code>.isArray(options)</code></a></li><li><a href="#isobjectoptions"><code>.isObject(options)</code></a></li><li><a href="#isstring"><code>.isString()</code></a></li><li><a href="#not"><code>.not()</code></a></li><li><a href="#notempty"><code>.notEmpty()</code></a></li><li><a href="#optionaloptions"><code>.optional(options)</code></a></li><li><a href="#runreq-options"><code>.run(req[, options])</code></a></li><li><a href="#withmessagemessage"><code>.withMessage(message)</code></a></li></ul></li></ul></nav></div><footer class="nav-footer" id="footer"><section class="sitemap"><a href="/" class="nav-home"><img src="/img/logo.svg" alt="express-validator" width="66" height="58"/></a><div><h5>Docs</h5><a href="/docs/index.html">Getting Started</a><a href="/docs/check-api.html">API Reference</a></div><div><h5>Community</h5><a href="http://stackoverflow.com/questions/tagged/express-validator" target="_blank" rel="noreferrer noopener">Stack Overflow</a></div><div><h5>More</h5><a href="https://github.com/express-validator/express-validator">GitHub</a><a class="github-button" href="https://github.com/express-validator/express-validator" data-icon="octicon-star" data-count-href="/express-validator/express-validator/stargazers" data-show-count="true" data-count-aria-label="# stargazers on GitHub" aria-label="Star this project on GitHub">Star</a></div></section><section class="copyright">Copyright © 2021 express-validator</section></footer></div></body></html>

sitemap.xml

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)