Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin validating unique on a non unique field #62

Open
ValentinGB opened this issue Feb 7, 2019 · 0 comments
Open

Plugin validating unique on a non unique field #62

ValentinGB opened this issue Feb 7, 2019 · 0 comments

Comments

@ValentinGB
Copy link

ValentinGB commented Feb 7, 2019

I have the following schema

const   mongoose        = require('mongoose'),
        uniqueValidator  = require('mongoose-beautiful-unique-validation'),
        Schema          = mongoose.Schema;

const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

const ClientSchema = new Schema({
    name: {
        type: String,
        required: [true, 'Campo nombre es requerido'],
    },
    email: {
        type: String,
        unique: 'Ya existe un cliente registrado con el correo: \'{VALUE}\'',
        required: [true, 'Campo email es requerido'],
        validate:  {
            validator: v => re.test(v),
            message: props => `Ingrese un correo valido`
        }
    },
    address: {
        type: String,
        required: [true, 'Campo dirección es requerido'],
        validate:  {
            validator: v => v.length >= 5,
            message: props => `Ingrese una dirección valida`
        }
    },
    phone : {
        type: String,
        unique: 'Ya existe un cliente registrado con el telefono: \'{VALUE}\'',
        required: [true, 'Campo telefono es requerido'],
        validate:  {
            validator: v => !isNaN(v) && v.length >= 7,
            message: props => `'${props.value}' no es un número telefónico válido`
        }
    }
});
ClientSchema.plugin(uniqueValidator);

The problem is, that mongoose-beautiful-unique-validation is validating address as an unique field when I never set the unique property on that field.

@ValentinGB ValentinGB changed the title Plugin validating unique on all fields Plugin validating unique on a non unique field Feb 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant