0
我有這樣的模式:貓鼬嵌套文檔驗證
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var restaurantSchema = new Schema({
working_hours: {
weekday: {
start: String,
end: String
},
weekend: {
start: String,
end: String
}
}
});
,我想驗證start
和end
領域的每一個weekday
和weekend
。 我目前這樣做非常明確使用如下正則表達式:
restaurantSchema.path('working_hours.weekday.start').validate(function(time) {
var timeRegex = /^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
return timeRegex.test(time);
}, 'Time must be in the format `hh:mm` and be a valid time of the day.');
restaurantSchema.path('working_hours.weekday.end').validate(function(time) {
var timeRegex = /^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
return timeRegex.test(time);
}, 'Time must be in the format `hh:mm` and be a valid time of the day.');
restaurantSchema.path('working_hours.weekend.start').validate(function(time) {
var timeRegex = /^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
return timeRegex.test(time);
}, 'Time must be in the format `hh:mm` and be a valid time of the day.');
restaurantSchema.path('working_hours.weekend.end').validate(function(time) {
var timeRegex = /^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
return timeRegex.test(time);
}, 'Time must be in the format `hh:mm` and be a valid time of the day.');
一定有什麼比這更好的辦法。有任何想法嗎?
這是一個更好的選擇,非常感謝! – SalmaFG