1

I have a controller that when i insert data into the database it always inserted. Now i wanna check that if the data i create is null is must report an error. Here is my code:

// create new car
export async function createCar (req, res) {
    const car = new Car({
      car_id: id,
      name: req.body.name,
      color: req.body.color,
      brand: req.body.brand,
    });
    
    return car
      .save()
      .then((newCar) => {
        return res.status(201).json({
          success: true,
          message: 'New car created successfully',
          Car: newCar,
        });
      })
      .catch((error) => {
          console.log(error);
        res.status(500).json({
          success: false,
          message: 'Server error. Please try again.',
          error: error.message,
        });
      });
  }

And i check on postman even i let Name is NULL is still inserted. Furthermore, how can i check that COLOR, BRAND if it's null must also report an error. Please help me.

enter image description here

7
  • For this some package their he first one is JOI and the other one is express-validator this is called Request Validation Commented Aug 25, 2022 at 4:38
  • You can use your own middleware function for validating the API. This link contains information about middle functions using the express framework: https://expressjs.com/en/guide/using-middleware.html You can use middleware for POST requests for your URL or simply for POST requests. Commented Aug 25, 2022 at 4:40
  • Still have an issue let me know I'll explain further more Commented Aug 25, 2022 at 4:41
  • Could you post the code and steps to help me with this, i really appreciate it. Commented Aug 25, 2022 at 4:50
  • 1
    if you are using mongoose you can define required inputs in the schema model ----> mongoosejs.com/docs/validation.html Commented Aug 25, 2022 at 4:51

2 Answers 2

1

Kato, please follow my boilerplate. I created it with the best practices, and it also provides high-end JOI request validation.

A boilerplate for building production-ready RESTful APIs using Node.js, ExpressJs, Mongoose and Joi (Request Validation)

Node-Express-Mongoose-Joi

Sign up to request clarification or add additional context in comments.

2 Comments

i check you github and i wonder where you can check Name, description of Book if it's null or not null.
Using joi, we can check whether our entire request/json is a number or a string. For a special case like email, there is a methods email() we can use to validate that it's a valid email address.
0

For validate before insert

// Validate request
if (!req.body.name) 
{
  res.status(400).send
  ({
      message: "Name can not be empty!"
  });
  return;
} else if (!req.body.color)
{
   res.status(400).send
   ({
       message: "Color can not be empty!"
   });
    return;
} else if (!req.body.brand) 
{
    res.status(400).send({
       message: "Brand can not be empty!"
    });
    return;
}

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.