Alireza
Alireza

Reputation: 45

request body validation with fastest validator

i want to change the error style of fastest validator.

copy from fastest validator document


    const Validator = require("fastest-validator");
    
    const v = new Validator();
    
    const schema = {
        id: { type: "number", positive: true, integer: true },
        name: { type: "string", min: 3, max: 255 },
        status: "boolean" // short-hand def
    };


    const check = v.compile(schema);
    
    console.log("Second:", check({ id: 2, name: "Adam" }));
    
    /* Returns an array with errors:
    [
        {
            type: 'required',
            field: 'status',
            message: 'The \'status\' field is required!'
        }
    ]
    */

in the top example the error have some key value but i want this syntax :


    [
        {
            status: 'The \'status\`enter code here`' field is required!',
        }
    ]

Upvotes: 1

Views: 339

Answers (1)

Smit Gajera
Smit Gajera

Reputation: 1039

For this, we need to prepare a response likewise Use try and catch then so we can grab that error in catch block and we can change the syntax of error accordingly.

    try {
        // Your code
    } catch (error) {
        console.log(error) // Look into where this message is located
        return res.json([
            {
                status: error.message
            }
        ])
    }

Upvotes: 0

Related Questions