Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

327
Visualizações
¿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 },

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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

 required: [ function () { return this.role === 'student'; }, 'birthday is missing' ]
over 4 years ago · Santiago Trujillo Relatório

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; }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda