私は 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();
ただし、内部スキーマの詳細が変更される可能性があるため、これは安全ではないと感じています。より良い方法はありますか?