Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

363
Views
¿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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!