문제

I am trying to build a MEAN project, so I need to validate some of my model's dynamic key...

I want to create a Schema like this

var exampleSchema = new Schema({
    x: {
        type: String,
        default: '',
        required: true,
        trim: true
    },
    y: {}
});

as you see I have mixed type object, but actually it is a Language Map and it should be something like this,

{
   "en-US": "answered"
}

can I validate my key with mongoose? (I think it has no function like that)

if no, how and where can I validate it (in model or controller)?

도움이 되었습니까?

해결책

You may want to look into this: http://mongoosejs.com/docs/middleware.html

Specifically pre-save events. Mongoose gives you control over this and you can perform validation, mapping as needed before the actual model gets saved.

Also works nice for pre-init event if you need defaults such as "current date" for an audit trail such as "createdOn: date".

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top