How do you restrict inputting negative numbers when calculating equations for REST API using Node JS/Express?

Issue

I am creating a REST API using Node JS for a picture framing calculator. The user inputs the appropriate POST request, for example:

{
    "FrameWidth": "16",
    "FrameWidthFraction": "0",
    "FrameHeight": "20",
    "FrameHeightFraction": "0",
    "PictureWidth": "11",
    "PictureWidthFraction": "0",
    "PictureHeight": "17",
    "PictureHeightFraction": "0",
    "MiddleMat": "1",
    "MiddleMatFraction": "0",
    "BottomMat": "0",
    "BottomMatFraction": "1/4"
}

The JSON output is as such:

{
    "messages": [
        {
            "text": "Place the parallel mat guide at the following inch mark, then make the respective width and height cut starting with the Top Mat, Middle Mat, then Bottom Mat:"
        },
        {
            "text": "Top Mat Width Cut = 1/2 inches"
        },
        {
            "text": "Top Mat Height Cut = 1 1/2 inches"
        },
        {
            "text": "Middle Mat Width Cut = 1 1/2 inches"
        },
        {
            "text": "Middle Mat Height Cut = 2 1/2 inches"
        },
        {
            "text": "Bottom Mat Width Cut = 1 1/2 inches"
        },
        {
            "text": "Bottom Mat Height Cut = 2 1/2 inches"
        },
        {
            "buttons": [
                {
                    "title": "Go to I Was Framed!",
                    "type": "web_url",
                    "url": "https://iwasframed.com/"
                }
            ]
        }
    ]
}

The JavaScript I use to process the input, calculate the equation, and display the output is here:

app.post("/api/chatfuel/calculator/triplematapi", (req,res) => {
    
    let FrameWidth = 1 * req.body.FrameWidth.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
    let FrameHeight = 1 * req.body.FrameHeight.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
    let PictureWidth = 1 * req.body.PictureWidth.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
    let PictureHeight = 1 * req.body.PictureHeight.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
    let MiddleMat = 1 * req.body.MiddleMat.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
    let BottomMat = 1 * req.body.BottomMat.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
    let TopMatWidth = ((FrameHeight)-(PictureHeight))/2-(MiddleMat);
    let TopMatHeight = ((FrameWidth)-(PictureWidth))/2-(MiddleMat);
    let MiddleMatWidth = ((FrameHeight)-(PictureHeight))/2;
    let MiddleMatHeight = ((FrameWidth)-(PictureWidth))/2;
    let BottomMatWidth = ((FrameHeight)-(PictureHeight))/(2)+(BottomMat);
    let BottomMatHeight = ((FrameWidth)-(PictureWidth))/(2)+(BottomMat);
    res.json({"messages": [{"text": "Place the parallel mat guide at the following inch mark, then make the respective width and height cut starting with the Top Mat, Middle Mat, then Bottom Mat:"},{"text": `Top Mat Width Cut = ${new Fraction(TopMatWidth).toString()} inches`},{"text": `Top Mat Height Cut = ${new Fraction(TopMatHeight).toString()} inches`},{"text": `Middle Mat Width Cut = ${new Fraction(MiddleMatWidth).toString()} inches`},{"text": `Middle Mat Height Cut = ${new Fraction(MiddleMatHeight).toString()} inches`},{"text": `Bottom Mat Width Cut = ${new Fraction(BottomMatWidth).toString()} inches`},{"text": `Bottom Mat Height Cut = ${new Fraction(BottomMatHeight).toString()} inches`},{"buttons": [{"title": "Go to I Was Framed!","type": "web_url","url": "https://iwasframed.com/"}]}]});
});

The problem I’m running into is that the user is able to input negative numbers even though those are invalid entries.

I’ve attempted to review the information on this site:JavaScript if…else statement and declaration, and use this:

function testNum(a) {
  let result;
  if (a > 0) {
    result = 'positive';
  } else {
    result = 'NOT positive';
  }
  return result;
}
console.log(testNum(-5));

I was also reading about a possible truthy or falsy conditions from the same site, but I’m just unsure if I’m heading in the right direction. What I’m trying to accomplish is that I need to restrict the user input to just positive numbers from the equation that’s solved based on their JSON input.

Can anyone provide me some guidance on how to restrict inputting negative numbers when calculating equations using Node JS/Express using my equations above?

Solution

As far as the backend is concerned, a simple solution is to return an error message in case the input value is not a positive numeric one. For example

app.post("/api/chatfuel/calculator/triplematapi", (req,res) => {
// add this code before your calculations.
// do the same for the fields you need validation.
    if (!(req.body.FrameWidth > 0)) {
        res.status(422).json({ messages: "Please insert a positive value" });
    }

    ...
});

On frontend what you can do, is to restrict the inputs options

<input type="number" name="FrameWidth" min="0">

If this is what you need, I recommend you to use a validation library like express-validator

Answered By – dimitris tseggenes

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published