express-validator icon indicating copy to clipboard operation
express-validator copied to clipboard

Is checkExact() with wildcards supported?

Open pano9000 opened this issue 1 year ago • 4 comments

Hi all,

quick question: Is the checkExact() middleware supported for wildcards?

E.g. consider the following scenario:

I need to validate a body that looks something like this:

const body = [
  {
    id: 1,
    qty: 100
  },
  {
    id: 2,
    qty: 100
  }
]

-> So essentially an array of objects with an id and qty property.

I can nicely check for validity with the following chain

  body()
    .isArray(),

  body("*")
    .isObject(),

  body("*.id")
    .isInt(),

  body("*.qty")
    .isInt(),

Adding checkExact() to the end of the chain above however does not seem to cause the following example to fail validation, despite it having one additional unwanted property (wrong) in the second object:

const body = [
  {
    id: 1,
    qty: 100
  },
  {
    id: 2,
    wrong: 1,
    qty: 100
  }
]

So I wonder: is this scenario even supported currently, or am I running into a bug (or maybe I am doing something wrong?)

thanks in advance

Kind regards,

Pano

pano9000 avatar Sep 07 '23 15:09 pano9000

Facing same challenge. checkExact does not work when request is validated for array level in checkSchema.

vardana-bh avatar Jan 04 '24 09:01 vardana-bh

Facing same problem here. I'm doing this:

app.post(
  "/signup",

  checkExact([
    body("addresses").isObject(),
    body("addresses.*.number").isInt(),
    body("siblings.*.name").notEmpty(),
  ]),
  (req, res) => {
    validationResult(req).throw()
    res.json({
      msg: "Sucess",
    })
  }
)

but there is no throwed error when I send this:

{
  "addresses": {
    "home": {
      "number": 35
    },
    "work": {
      "number": 501,
      "wrong": "yes"
    }
  },
  "siblings": [
    {
      "name": "Maria von Validator"
    },
    {
      "name": "Checky McCheckFace"
    }
  ]
}

CerealeZ avatar Jan 07 '24 22:01 CerealeZ

I'm investigating

fedeci avatar Feb 21 '24 16:02 fedeci