express-mongo-schema-validator
v1.0.4
Published
Add auto detected validation to your data . You only need the mongo db schema and the model. Our libary will automatically finds out every validation that you are gonna need
Downloads
3
Readme
Express Mongo Schema Validator
Add auto detected validation to your data . You only need the mongo db schema and the model. Our libary will automatically finds out every validation that you are gonna need
Demo
A Basic Validation of user data
const {validateSchema} = require("express-mongo-schema-validator")
const UserSchema = mongoose.Schema({
name:{
type:String,
required:true,
},
username:{
type:String,
required:true
}
})
const UserModel = mongoose.Model("users",UserSchema)
app.post("/user",validateSchema(UserSchema,UserModel),async (req,res) => {
const user = UserModel(req.body)
await user.save()
return res.status(200).send({'success':true,'user':user})
})
First import the validateSchema function.Then create your schema as usual.Then build your model.Now you can create a middleware and pass it before
the acutal saving you wanna do.validateSchema
will create middleware.
In this case both username
and name
will be checked.Because one of them have unique and other has required attribute.