In this node js model, all the field appear required. I don’t want all of them to be ‘required’, i did the joi component of it but it is still not working
I removed the required() from the Joi validation but still i am getting the messages that the field is required
<code>const { Schema, model } = require("mongoose");
const Joi = require("joi");
// Schema Definition
const AppointmentSchema = new Schema({
name: String,
age: Number,
gender: String,
email: String,
phone: String,
date: Date,
time: String,
disease: String,
operation: String,
comorbids: String,
specialNotes: String,
socialConnect: String,
plan: String,
created: {
type: Date,
default: Date.now,
},
});
const Appointment = model("Appointment", AppointmentSchema);
// Joi Validation
function validateAppointment(appointment) {
const schema = Joi.object({
name: Joi.string().min(3).required(),
age: Joi.number().min(1).required(),
gender: Joi.string().min(4).valid("male", "female", "other").required(),
disease: Joi.string(),
operation: Joi.string().min(3).required(),
comorbids: Joi.string(),
specialNotes: Joi.string(),
socialConnect: Joi.string(),
email: Joi.string().email(),
phone: Joi.string().min(5),
date: Joi.date().required(),
time: Joi.string(),
});
return schema.validate(appointment);
}
exports.Appointment = Appointment;
exports.validate = validateAppointment;
</code>
<code>const { Schema, model } = require("mongoose");
const Joi = require("joi");
// Schema Definition
const AppointmentSchema = new Schema({
name: String,
age: Number,
gender: String,
email: String,
phone: String,
date: Date,
time: String,
disease: String,
operation: String,
comorbids: String,
specialNotes: String,
socialConnect: String,
plan: String,
created: {
type: Date,
default: Date.now,
},
});
const Appointment = model("Appointment", AppointmentSchema);
// Joi Validation
function validateAppointment(appointment) {
const schema = Joi.object({
name: Joi.string().min(3).required(),
age: Joi.number().min(1).required(),
gender: Joi.string().min(4).valid("male", "female", "other").required(),
disease: Joi.string(),
operation: Joi.string().min(3).required(),
comorbids: Joi.string(),
specialNotes: Joi.string(),
socialConnect: Joi.string(),
email: Joi.string().email(),
phone: Joi.string().min(5),
date: Joi.date().required(),
time: Joi.string(),
});
return schema.validate(appointment);
}
exports.Appointment = Appointment;
exports.validate = validateAppointment;
</code>
const { Schema, model } = require("mongoose");
const Joi = require("joi");
// Schema Definition
const AppointmentSchema = new Schema({
name: String,
age: Number,
gender: String,
email: String,
phone: String,
date: Date,
time: String,
disease: String,
operation: String,
comorbids: String,
specialNotes: String,
socialConnect: String,
plan: String,
created: {
type: Date,
default: Date.now,
},
});
const Appointment = model("Appointment", AppointmentSchema);
// Joi Validation
function validateAppointment(appointment) {
const schema = Joi.object({
name: Joi.string().min(3).required(),
age: Joi.number().min(1).required(),
gender: Joi.string().min(4).valid("male", "female", "other").required(),
disease: Joi.string(),
operation: Joi.string().min(3).required(),
comorbids: Joi.string(),
specialNotes: Joi.string(),
socialConnect: Joi.string(),
email: Joi.string().email(),
phone: Joi.string().min(5),
date: Joi.date().required(),
time: Joi.string(),
});
return schema.validate(appointment);
}
exports.Appointment = Appointment;
exports.validate = validateAppointment;
New contributor
Nisar Ahmed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.