Validation Made EZ

Amad27
3 min readNov 16, 2019

--

“That was easy” — Staple’s Easy Button
“It’s so small! Downloads faster than you can say ‘ez-validation’ ”
— Random bystander I didn’t pay to say this

ez-validation is an extremely small, robust validation library built with 💚 and typescript that allows on-the-fly customization

I despised doing validation, so I strived to make it more simple.

ez-validation provides basic validation methods using method chaining to make your validation more robust, semantic, tasty, and fun!

Simple Use Case:

import {EzValidation} from "ez-validation"EzValidation(123).required().isNumber().maxValue(100).errorMessage;// outputs: "value exceeds maximum value of 100"

Now, I know what some of you are thinking:

non-hater (aka cool guy):
“WOW THAT WAS TOO EZ! Sign me up.”
hater:
“Lame… Why would I want only one error message”

Multiple Error Messages:

import {EzValidation} from "ez-validation"EzValidation("A@S@D").required().isAlphanumeric().minLength(10).isObject().errorMessages;// outputs: [ 'Must be Alphanumeric', 'Value is below minimum length of 10', 'Not an object' ]

non-hater (aka cool guy):
“What?! So cool! Now I can create good UX by showing my users exactly what I want from them!”
hater:
“So what? I don’t want those lame error messages…”
random guy passing by:
🤔 Why would you ever check if it’s an object that is alphanumeric?”

Custom Error Message:

import {EzValidation} from "ez-validation"EzValidation(123).required().isNumber().maxValue(100, "custom error look at me").errorMessage;// outputs: "custom error look at me"

non-haters (aka really cool guy):
“WAIT, WHAT!? CUSTOM ERROR MESSAGES!!!
hater (aka hipster with IQ of 2000): “Meh. Still got lame functions, doesn’t fit my extremely unique needs.

Custom Validation Function:

import {EzValidation} from "ez-validation"const customValidationFunction = val => {
val === "unicorns" ? false : true
if(val === "unicorns") {
return "unicorns are not real. Please enter valid response"
}
if(val === "cactus") {
return "sorry a cactus is not a valid animal"
}
}
EzValidation('unicorns').required().minLength(3).customValidation(
customValidationFunction
).errorMessage
// outputs: "unicorns are not real. Please enter valid response"

non-haters (aka really cool guy that is notorious for fainting after seeing cool thing):
*faints* 😵
hater (aka hipster with IQ of 2012): “Ummmmmmmm…. What if I don’t use a lot of custom validations. I think seeing .customValidation(fn).customValidation(fn).customValidation(fn) is really ugly”

Well, the hater might have point here 🤷‍♀

Custom Validation Class:

import { EZValidationAPI } from "ez-validation";class CustomEzValidation extends EZValidationAPI {
disableHaters() {
if (this.validating === isHater)) {
this._returnError("Sorry Hater you cant submit anything");
}
return this;
}
}
new CustomEZValidation("I love ez-validation").disableHaters().isString().errorMessage;

non-haters (aka really cool guy that is notorious for fainting after seeing really cool thing):
*wakes up and looks* … *faints again* 😵😵😵😵
hater (aka hater about to turn into a non-hater): “Uhhhh cool I guess, but what about schema validation? What if I want to share some code with my backend or organize things better?”

😏 EZ

Schema Based Validation:

import { schemaValidation } from "ez-validations";const values = {
noValidation: "hi",
email: "fake-email",
name: "im a cow"
};
const schema = {
email: (val: string) => EzValidation(val).isEmail().required().errorMessage,
name: (val: string) => EzValidation(val).maxLength(2).errorMessage
};
schemaValidation(values, validationSchema);/* output: {
email: "not a valid email",
name: "name length too long"
}

non-haters (aka really cool guy that can’t faint more than 3 times a day):
“…….. What have the heavens bestowed upon us?” *faints* 😵
hater (aka hater turned into a non-hater): “WOW COOL! But can it do my taxes?”

😏 😼 😏 😼

Ez-Validation Doing You Taxes:

...
EzValidation(taxes).doMyTaxes().errorMessage
...

… EZ 😏 *Fades to Black* 😏 …

https://www.npmjs.com/package/ez-validation

disclaimers:
ez-validation can’t really do your taxes. This is why the package is able to stay so small.
Common side effects of using ez-validation may include but are not limited to: being less stressed, less tired, less depressed, more happy, Vasovagal syncope, sudden loss of breath, and a love for all things EZ.
Please consult with your local developer before downloading ez-validation.

--

--