Search code examples
javaspringsoftware-design

Revalidate data on backend side?


I'm creating a schedule app. Teachers, students and cars.

Spring + DB + React

The way it's designed right know:

Adding new ride looks like this:

  • 1 Select student (autofill provided from database)
  • 2 Select time range
  • 3.1 Select teacher from list who doesn't have any ride in that time range (2)
  • 3.2 Select car from list which doesn't have any ride in that time range (2)

Till this step everting works by Queries (e.g. provide teachers that are available in that time range) through Repository in Spring. Should I validate received Ride once again on backend before saving it to repository/database?


Solution

  • Yes, you should re-validate on the server, because you cannot control whether clients send bad information or not. Clients can send bad information either because they are evil (and/or their computer is compromised) or because they received stale information from the DB -- or the information became stale between the moment the page was generated and the moment when they made their choice.

    Depending on your constraints, the DB layer may detect an invalid insert - but it is better to check beforehand, within the Spring transaction on the server that would do the important insert, that no conflicts exist.