Node.js里有什么方便的方法验证一个模型值的值的合法性?
 发布于 11 年前  作者 sandheart  3825 次预览  最后一次回复是 11 年前  来自 问答 

Node.js里有什么方便的方法验证一个模型值的值的合法性,像下面的代码还能指定属性的类型与默认值,感觉好便捷。

下面是cnnode.js源码里验证json的合法性的代码: var mongoose = require(‘mongoose’); var Schema = mongoose.Schema; var ObjectId = Schema.ObjectId;

var ReplySchema = new Schema({ content: { type: String }, topic_id: { type: ObjectId}, author_id: { type: ObjectId }, reply_id: { type: ObjectId }, create_at: { type: Date, default: Date.now }, update_at: { type: Date, default: Date.now }, content_is_html: { type: Boolean }, ups: [Schema.Types.ObjectId] });

ReplySchema.index({topic_id: 1}); ReplySchema.index({author_id: 1, create_at: -1});

mongoose.model(‘Reply’, ReplySchema);