下面回傳成功,即使obj包含不在架構中的元素。
問題
當它看到未在架構中指定的元素時,是否有可能驗證失敗?
開玩笑有這個expect(obj1).toEqual(obj2)。
如果驗證無法做到這一點,那么我必須使用哪些選項來檢測不需要的元素?
const Schema = require("validate");
const obj = {good: "1", bad: "2"};
const user = new Schema({
good: {
type: String,
required: true,
}
});
const r = user.validate(obj);
console.log(r);
uj5u.com熱心網友回復:
是的,有一strict對選項Schema,從檔案:
架構
Schema 定義了物件應該被驗證的結構。
引數
obj模式定義opts選項
opts.typecast
opts.strip
opts.strict當物件包含架構中未定義的屬性時驗證失敗 (可選,默認為 false)
所以你需要類似的東西:
const options = { strict: true };
const user = new Schema({
good: {
type: String,
required: true,
}
}, options);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/411268.html
標籤:
