Schema validation dùng JSON Schema để enforce shape/rules ở database level. Nó hữu ích khi nhiều services ghi cùng collection hoặc cần bảo vệ khỏi dữ liệu bẩn ngoài app validation.
Ví dụ:
javascript
db.createCollection("users", {
validator: { $jsonSchema: { required: ["email"], properties: { email: { bsonType: "string" } } } }
})Validation không thay thế app-level DTO, nhưng là lớp phòng thủ tốt cho dữ liệu quan trọng.