Start learning free

LESSON 98 OF 173 · BACKEND: USERS AND SECURITY

Validate Everything in JavaScript

Practice

A client can send anything: an email without an @, a password of one letter, a price of -5, a price written as text. Today the shop stores all of it. A safe backend validates every value before it stores it.

When a value is wrong, the shop answers 400 Bad Request with a message and the name of the field that is wrong, so an app can show the message next to that box. Three JavaScript tools help: typeof tells a value's type, Number.isInteger tells whether a value is a whole number, and split cuts a text into parts.

typeof "bo@shop.io"        // "string"
typeof undefined           // "undefined": that's what a missing field gives
Number.isInteger(120)      // true
Number.isInteger("120")    // false: a text, even if it looks like a number
"bo@shop.io".split("@")    // ["bo", "shop.io"]

A field the client left out is undefined. Check the type first, and the shop answers 400 instead of crashing with 500. And check every route that takes a value: a rule that one route forgets protects nothing.

POST /register  {"email": "ana.example.com", "password": "secret123"}  →  400  {"error": "Enter a valid email, like ana@example.com", "field": "email"}
POST /register  {"password": "secret123"}  →  400  {"error": "Enter a valid email, like ana@example.com", "field": "email"}
POST /register  {"email": "ana@example.com", "password": "123"}  →  400  {"error": "The password needs at least 8 characters", "field": "password"}
POST /register  {"email": "ana@example.com", "password": "secret123"}  →  201  {"email": "ana@example.com", "id": 2}
PUT /me/password  {"old_password": "secret123", "new_password": "abc"}  (Ana's token)  →  400  {"error": "The password needs at least 8 characters", "field": "new_password"}
POST /products  {"name": "Desk", "price": -5}  (admin's token)  →  400  {"error": "The price must be a whole number from 1 to 10000", "field": "price"}
POST /products  {"name": "Desk", "price": "120"}  (admin's token)  →  400  {"error": "The price must be a whole number from 1 to 10000", "field": "price"}
POST /products  {"name": "Desk", "price": 120}  (admin's token)  →  201  {"id": 9, "name": "Desk", "price": 120}

Your task: write two helpers, invalid(res, field, message) for the 400 answer and validEmail(email), then check the email and the password in the /register route, the new password in the PUT /me/password route, and the price in addProduct.


Assignment
Follow the Coding Tutorial to write the two helpers and add the four checks.


Hint
Put the typeof test first in each if: || stops at the first true part, so split or length never runs on undefined. An email is valid when it is a text with exactly one @, no space, a name before the @ and a dot after it.


STEP 1 OF 11
    Ctrl + Enter to check

    Sec Validate in JavaScript

    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free