How to prevent unwanted request in node js express?

182 Views Asked by At

expected request:

{
    "name": "Raju",
    "email": "[email protected]"
}

Actual:

{
    "name": "Raju",
    "email": "[email protected]",
    "xyz" : "xxxx"
}

I would like to throw an error or escape for "xyz" in a validation/router level. Am using the fastest-validator.

is there any other validator supports this feature?

Any help appreciated

1

There are 1 best solutions below

3
On

You can set $$strict: true like below example:

const schema = {
    name: { type: "string" }, // required
    $$strict: true // no additional properties allowed
}

v.validate({ name: "John" }, schema); // Valid
v.validate({ name: "John", age: 42 }, schema); // Fail