PageSourceSearch

https://express-validator.github.io/assets/js/81c85956.4c8bd9f5.js

js express-validator.github.io collected 2026-10-03 09:12:02 UTC 3,299 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunk=self.webpackChunk||[]).push([[2296],{85491(e,s,n){n.r(s),n.d(s,{assets:()=>i,contentTitle:()=>d,default:()=>p,frontMatter:()=>o,metadata:()=>r,toc:()=>c});const r=JSON.parse('{"id":"wildcards","title":"Wildcards","description":"Sometimes you will want to apply the same rules to all items of an array or all keys of some object.","source":"@site/versioned_docs/version-5.3.0/feature-wildcards.md","sourceDirName":".","slug":"/wildcards","permalink":"/docs/5.3.0/wildcards","draft":false,"unlisted":false,"editUrl":"https://github.com/express-validator/express-validator/edit/master/docs/versioned_docs/version-5.3.0/feature-wildcards.md","tags":[],"version":"5.3.0","frontMatter":{"id":"wildcards","title":"Wildcards"},"sidebar":"docs","previous":{"title":"Custom Error Messages","permalink":"/docs/5.3.0/custom-error-messages"},"next":{"title":"Schema Validation","permalink":"/docs/5.3.0/schema-validation"}}');var a=n(62540),t=n(43023);const o={id:"wildcards",title:"Wildcards"},d=void 0,i={},c=[];function l(e){const s={br:"br",code:"code",p:"p",pre:"pre",...(0,t.R)(),...e.components};return(0,a.jsxs)(a.Fragment,{children:[(0,a.jsxs)(s.p,{children:["Sometimes you will want to apply the same rules to all items of an array or all keys of some object.",(0,a.jsx)(s.br,{}),"\n","That's what the ",(0,a.jsx)(s.code,{children:"*"})," character - also known as a wildcard -- is for."]}),"\n",(0,a.jsx)(s.p,{children:"For example, imagine you want to validate that all addresses have a valid postal code,\nand that the number of each address is sanitized as an integer number."}),"\n",(0,a.jsx)(s.p,{children:"We can do this with the following code:"}),"\n",(0,a.jsx)(s.pre,{children:(0,a.jsx)(s.code,{className:"language-js",children:"const express = require('express');\nconst { check } = require('express-validator/check');\nconst { sanitize } = require('express-validator/filter');\n\nconst app = express();\napp.use(express.json());\n\napp.post('/addresses', [\n  check('addresses.*.postalCode').isPostalCode(),\n  sanitize('addresses.*.number').toInt()\n], (req, res) => {\n  // Handle the request\n});\n"})}),"\n",(0,a.jsx)(s.p,{children:"This will handle cases where you send an array of addresses:"}),"\n",(0,a.jsx)(s.pre,{children:(0,a.jsx)(s.code,{className:"language-json",children:'{\n  "addresses": [\n    { "postalCode": "2010", "number": "500" },\n    { "postalCode": "", "number": "501" },\n  ]\n}\n'})}),"\n",(0,a.jsx)(s.p,{children:"...or even cases where you want a predefined set of addresses:"}),"\n",(0,a.jsx)(s.pre,{children:(0,a.jsx)(s.code,{className:"language-json",children:'{\n  "addresses": {\n    "home": { "postalCode": "", "number": "501" },\n    "work": { "postalCode": "2010", "number": "500" }\n  }\n}\n'})})]})}function p(e={}){const{wrapper:s}={...(0,t.R)(),...e.components};return s?(0,a.jsx)(s,{...e,children:(0,a.jsx)(l,{...e})}):l(e)}},43023(e,s,n){n.d(s,{R:()=>o,x:()=>d});var r=n(63696);const a={},t=r.createContext(a);function o(e){const s=r.useContext(t);return r.useMemo(function(){return"function"==typeof e?e(s):{...s,...e}},[s,e])}function d(e){let s;return s=e.disableParentContext?"function"==typeof e.components?e.components(a):e.components||a:o(e.components),r.createElement(t.Provider,{value:s},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.