nope-validator icon indicating copy to clipboard operation
nope-validator copied to clipboard

🛠 nope-validator v2.0.0

Open ftonato opened this issue 2 years ago • 1 comments

This is a proposal for the new version of the library: v2.0.0 which implies a complete change (refactoring) of how the data is returned by the library.

The current behaviour is quite simple:

const Nope = require("nope-validator")

const schema = Nope.string().email().required()

schema.validate('[email protected]') // 🟢 returns undefined since there are no errors

schema.validate('invalid-email') // 🔴 returns a string message with the error: "Input is not a valid email"

There is absolutely nothing wrong with this pattern, however as we are thinking of implementing transformers (handlers for input data), we assumed that it would make more sense to return something different that could benefit users.

So the API return was defined to meet all possible needs, returning not only the error but also the input data and the data that was transformed, as follows:

Key Description
data returns always exactly the input data
transformed returns the transformed data (if any transformation has taken place), otherwise it returns the input data
errors returns an empty array (if there are no errors), otherwise it returns an array of errors

Let's see what the return should look like for both cases (success/error):

🟢 Success scenario (no errors will be returned)

const Nope = require("nope-validator")

const schema = Nope.string().trim().toLowerCase();
const { data, transformed, errors } = schema.validate('  NOPE-validation  ');

/*
  {
    data: '  NOPE-validation  ',
    transformed : 'nope-validation',
    errors: [ ]
  }
*/

🔴 Failure scenario (a errors list will be returned)

const Nope = require("nope-validator")

const schema = Nope.string().url('url-error-message');
const { data, transformed, errors } = schema.validate('http://');

/*
  {
    data: 'http://',
    transformed : 'http://',
    errors: ['url-error-message']
  }
*/

You may be wondering, but what are the transformers? And the truth is that currently we don't have many defined, only one that was introduced last week (trim #824), however another one has already been requested, it is the case of (default #641), and along with the new version, we plan to release others, like: (toLowerCase, toUpperCase, round.floor**, round.ceil, round.trunc and round.round).

** By the way, round a.k.a. Math['type'].


If you have any ideas/suggestions or if you'd like to contribute to the release of the new version, feel free to reply in this thread and I'll explain how you can. If you want to keep up with the work in progress, keep an eye on the branch where the temp work is being done: https://github.com/ftonato/nope-validator/tree/feat/nope-2.0.0.

Thanks for your attention 🎈

ftonato avatar Jun 02 '22 09:06 ftonato

It would be nice to change the behaviour of min. Currently it's just an alias to greaterThan but it's more intuitive that it's actually equal or greater than. For example, I would expect this to be valid:

Nope.string().min(8).validate("password");

But it actually should 9 characters to be valid.

The current behaviour also differs from Yup min.

The same applies to max.

lesha1201 avatar Jun 07 '22 12:06 lesha1201