Sign in
Log inSign up

"E-mail address already been taken" is responsibility of validator library or UI?

Riderman de Sousa Barbosa's photo
Riderman de Sousa Barbosa
·Oct 10, 2018

Le'ts suppouse we have the follow architrecture..

Lib (npm library) that validate some data:

So for instance.. the RegisterForm have the fields (Name, email and password) The RegisterForm imports the library (validator) that validate the data (name, email and password)

import registerValidator from 'validator'

onSubmit(values) {
    if (registerValidator.validate(values) {
        // Is valid, we can register new user...
    }
}

One of the rules that should be follow before the user can be registered, is that the e-mail address should be unique on our database.

The question is:

  1. should I made this validation (fetch my API to check if the e-mail was already taken) on validator library ?
  2. should I validade this on onSubmit after the data is valid.. something like this.

This...

import registerValidator from 'validator'

onSubmit(values) {
    if (registerValidator.validate(values) { // perform fetch operation to check on our database if the email was already taken
        // Is valid, we can register new user...
    }
}

Or this ...

import registerValidator from 'validator'
import isUniqueEmail from 'api/user';

async onSubmit(values) {
    if (registerValidator.validate(values) { // Is valid e-mail
        const isUniqueEmail = await isUniqueEmail(values.email); // is not taken yet
        if (isUniqueEmail) {
            // Is valid, we can register new user...
        }
    }
}

I know that the post method for the user register should check this .. but this is only an example to ask where the rule `e-mail must be uniqueà should placed