1.0.4 • Published 8 months ago

express-mongo-schema-validator v1.0.4

Weekly downloads
-
License
-
Repository
-
Last release
8 months ago

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.

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago