【发布时间】:2012-10-17 21:50:44
【问题描述】:
我有一个 Mongoose 架构(摘录如下):
var PersonSchema = new Schema({
name : {
first: { type: String, required: true }
, last: { type: String, required: true }
}
...
我想检查架构以确定哪些字段是必需的,然后验证这些字段是否存在于用户输入中。我可以测试 name.first 的“必需”属性,如下所示:
var person_schema = require('../models/person');
if (person_schema.schema.paths['name.first'].isRequired) {
req.assert('first', messages.form_messages.msg_required).notEmpty();
但感觉这是不安全的,因为内部架构细节可能会发生变化。有没有更好的办法?
【问题讨论】: