컴퓨터언어/Database
[Mongoose] Validation 유효성검사
bbanpro
2020. 5. 13. 14:28
728x90
반응형
스키마를 정의할 때 단순히 자료형만 주는 것이 아니라, 다시 {} 객체를 열어서 값의 범위나 필수여부를 지정할 수 있다.
const guitarSchema = new mongoose.Schema({
name: String,
company: String,
price: Number,
});
▼
const guitarSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
company: {
type: String
},
price: {
type: Number,
min: 1000,
max: 10000
},
});
*required를 배열로 처리하여 아래와 같이 입력하면 에러메시지를 커스텀화 할 수 있다.
const guitarSchema = new mongoose.Schema({
name: {
type: String,
required: [true, "how could it be noname?"]
},
company: {
type: String
},
price: {
type: Number,
min: 1000,
max: 10000
},
});
728x90
반응형