How efficient is to have validators instead of a conditional before calling a method?

tdd

#1

Question

How efficient is to have validators instead of a conditional before calling a method?

Answer

Preferably we will implement both, the validator helps us by not allowing the document to be created in our database collection, but if we were to leave it there, it will only give us an error which instead of helping our user know that they gave the program the wrong information (or no information at all), it merely breaks the code.
For example, a form that asks for a name and an email, once you press the submit button it gets saved in the database, but for the database collection, both name and email are required:

const usersSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  email: {
    type: String,
    required: true
  }
})

const usersModel = mongoose.model('users', usersSchema);

And so if a user tries to submit only an email, the page will do its part and make it look like the task was processed, but the user was never entered in the collection since both paths were required.

That is why we will also need to check that the information exists before we try to send it to the collection in the database, for example in our function that submits the data from the input we can check if it is received, if it is not there it will send and alert, and if it is, it will insert it in the collection:

//I will use regular javascript, for the sample, 'name' 
// and 'email' will be the ids of the input elements 
const nameElement = document.getElementById('name');
const emailElement = document.getElementById('email');

const onSubmit = () => {
  if(nameElement.value() ){  //we check if name exists
    if(emailElement.value()){ //if so, we check if email exists
      const user = { //if so we create our user object
        name: nameElement.value(),
        email: emailElement.value()
      }

      runWithDatabase(async () => {
        await usersModel.create(user) //and submited to the collection
      })

      return;
    } else { //if there is no email we send an alert to the user
      alert('Email is required');
      return;
    }
  } else { //the same is there is no name
    alert('Name is required');
    return;
  }
}

We could also wait and check if the database returns an error or a cancellation, but chances are we will not be too sure of what may be missing, it is usually safer and more efficient to check before submitting it to the database.

Why don’t we just leave the conditional without validators? Because, we are not perfect, and even though programs work, there are times when the input could give us wrong information that passes through our first condition if it isn’t caught the validators will help us by not allowing the data to be saved.

Ideally we will have checks on both sides, and after an attempt to create a document, only then, we could almost rest assure that our program will perform as we expect it to.