Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

364
Vistas
¿Cómo hacer que el requisito sea verdadero, solo si el usuario tiene un valor seguro?

hola, estoy haciendo modelos para mi mongodb, y al validar quiero que se requiera el cumpleaños solo si el usuario que se agrega es un estudiante, así que si el usuario es un maestro, está bien si no se proporciona un cumpleaños

 const userSchema = mongoose.Schema({ 20 21 _id: mongoose.Schema.Types.ObjectId, 22 23 firstName: { 24 type: String, 25 required: [true, 'the firstName is missing'], 26 validate: [(val) => validator.isAlpha(val, ['fr-FR']), 'not valid first name'], 27 }, 28 lastName: { 29 type: String, 30 required: [true, 'the lastName is missing'], 31 validate: [(val) => validator.isAlpha(val, ['fr-FR']), 'not valid last name'], 32 }, 33 birthday: { 34 type: Number, 35 required: [true, 'birthday is missing'] 36 validate: [(val) => 37 phoneNumber: { 38 type: String, 39 required: [true, 'the phoneNumber is missing'], 40 unique: [true, 'phoneNumber already in use'], 41 validate: [(val) => validator.isMobilePhone(val,['ar-DZ']), 'not valid phone number'], 42 }, 43 email : { 44 type: String, 45 required: [true, 'the email is missing'], 46 unique: [true, 'email already in use'], 47 validate: [validator.isEmail, 'not valid email'], 48 }, 49 password: { 50 type: String, 51 required: [true, 'the password is missing'], 52 minlength: [10, 'error when generating a password for the user'], 53 }, 54 role: { 55 type : String, 56 "enum" : ['teacher', 'student'], 57 required : [true, 'the user `s role is missing'], 58 },

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

La propiedad required también puede aceptar una función . Por ejemplo:

 required: [ function () { return this.role === 'student'; }, 'birthday is missing' ]
about 4 years ago · Santiago Gelvez Denunciar

0

Puede crear una función para validar el valor del role y utilizarlo como un valor booleano en la propiedad required .

 const mongoose = require('mongoose'); const Schema = mongoose.Schema; var userSchema = new Schema({ _id: mongoose.Schema.Types.ObjectId, firstName: { type: String, required: [true, 'the firstName is missing'], validate: [(val) => validator.isAlpha(val, ['fr-FR']), 'not valid first name'], }, role: { type: String, enum: ['teacher', 'student'], required: [true, 'the user `s role is missing'], }, birthday: { type: Number, required: [isBirthdayRequired, 'the birthday is missing'] }, }); function isBirthdayRequired() { if (this.role === 'student') { return true; } return false; }
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda