Search code examples
mysqljsonnode.jsrestful-authentication

Which is the right approach to validate a mandatory field in front end or back end?


I am new in back end development. I have already write the API to update user info, whose request body like this -

{
   "id": 26,
   "email": "tom.richards@yahoo.com",
   "firstName": "Tommy",
   "lastName": "Richards",
   "photoUrl": null,
   "userAddress": [
        {
           "id": 8,
           "type": "home",
           "addressLine1": "DP Road",
           "addressLine2": "Main Street",
           "city": "Los Angel",
           "state": "CA",
           "country": "USA",
           "postalCode":915890
        },
        {
          "id": 25,
          "type": "office",
          "addressLine1": "Dr Red Road",
          "addressLine2": null,
          "city": "SA",
          "state": "CA",
          "country": "USA",
          "postalCode":918950
       }
    ]
}

Where should ideally validate the address type [in my case home or office] in front end[Web site or Phone] or back end [server side] or both side? Which is good approach to validate address type ? If we validate it on backend side, which will cause any performance issue ?

Note - If the developer pass any string, the address type of like pass string will create in DB.


Solution

  • Reasons Front End Validation : Validation on the server, User has to wait for the response in case of the invalid data.

    Backend Validation : To make sure that the data could not alter by the intruder.

    Validate @database end : Like Mongoose provide the built in and custom validation.

    As per the requirement we have to cross check the data before user profile update.