feature-wildcards.md 1.1 KB


id: wildcards

title: Wildcards

Sometimes you will want to apply the same rules to all items of an array or all keys of some object.
That's what the * character - also known as a wildcard -- is for.

For example, imagine you want to validate that all addresses have a valid postal code, and that the number of each address is sanitized as an integer number.

We can do this with the following code:

const express = require('express');
const { check } = require('express-validator/check');
const { sanitize } = require('express-validator/filter');

const app = express();
app.use(express.json());

app.post('/addresses', [
  check('addresses.*.postalCode').isPostalCode(),
  sanitize('addresses.*.number').toInt()
], (req, res) => {
  // Handle the request
});

This will handle cases where you send an array of addresses:

{
  "addresses": [
    { "postalCode": "2010", "number": "500" },
    { "postalCode": "", "number": "501" },
  ]
}

...or even cases where you want a predefined set of addresses:

{
  "addresses": {
    "home": { "postalCode": "", "number": "501" },
    "work": { "postalCode": "2010", "number": "500" }
  }
}