update classValidator nestJS

89 Views Asked by At

When I used an older version of the validator it was like this:

{
    "success": false,
    "error": [
        {
            "value": "mclarenlo.cards",
            "property": "email",
            "children": [],
            "constraints": {
                "isEmail": "email must be an email"
            }
        }
    ],
    "time": 1701174874
}

But after the update, the application's response changed:

{
    "success": false,
    "error": [
        "email must be an email"
    ],
    "time": 1701174816
}

How can I keep the new validator but change the response as before?

1

There are 1 best solutions below

0
On

Super simple, just map your error like this:

validate(yourObject).then(async errors => {
        if (errors.length > 0) {
            res.status(409).send(errors.map(() => {
                return {
                    success: false, // to change 
                    error: [
                        {
                            "value": "mclarenlo.cards", // to change
                            "property": "email", // to change
                            "children": [], // to change
                            "constraints": { // to change
                                "isEmail": "email must be an email"
                            }
                        }
                    ],
                    "time": 1701174874 // to change
                }
            }))
        }
    });

All you have to do is modulate as you see fit.