#StackBounty: #node.js #mongoose how can I customize the output of validation errors in a mongoose schema?

Bounty: 100

I get this syntax from errors when I do an insert like this:

enter image description here

I get this error syntax in the err property. In this case the documento attribute and thecorreo attribute are not unique.
in the same err.message a text string is built where it includes the fields and the errors for which in this case the registry insertion could not be done. How can I do something like this, with the previous example so that the output is like this:

     {
err: {
.
.
.
          “message”: “documento debe ser unicon correo debe ser unicon”
     }
}

this is my full code:

    const mongoose = require('mongoose');
    const uniqueValidator = require('mongoose-unique-validator');

    let Schema = mongoose.Schema;

    let usuarioSchema = new Schema({
        nombres: {
            type: String,
        },
        documento: {
            type: String,
            unique: true,
            required: [true, 'documento debe ser único']
        },
        correo: {
            type: String,
            unique: true,
            required: [true, 'Correo  debe ser único']
        },
        ultimo_inicio_sesion: {
            type: Date,
            default: null
        },
        contrasena: {
            type: String,
            required: [true, 'Contraseña es necesario']
        },
        rol:
        {
            type: String
        }

    })


    usuarioSchema.methods.toJSON = function () {

        let user = this;
        let userObject = user.toObject();

        return userObject;
    }


    usuarioSchema.plugin(uniqueValidator, { message: '{PATH} debe de ser único' });

    module.exports = mongoose.model('usuarios_', usuarioSchema);

How can do it?


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.