DEV Community

Discussion on: Better TypeScript... With JavaScript

Collapse
 
ecyrbe profile image
ecyrbe

Hi Adam,

I skipped this article of yours last year. i just wanted to point one library that might also do the job and that i use everywhere. It's called Joi.

Joi is a really powerfull validation library that covers everything you listed and a lot more.

Here are some of your article examples converted :

import Joi,{assert} from 'joi';

const statuses = ['open', 'closed', 'hold'];

const myFunction = (status = '') => {
  assert(statuses,Joi.string().values(...statuses));
}

const colors = {
  red: '#ff0000',
  green: '#00ff00',
  blue: '#0000ff',
}
const myFunction = (color = '') => {
  assert(color, Joi.string().values(Object.values(colors)));
}
Enter fullscreen mode Exit fullscreen mode
import Joi,{assert} from 'joi';

const myFunction = (balance = 0) => {
  assert(balance,Joi.number());
}

const myFunction = (age = 0) => {
  assert(age,Joi.number().min(1).max(25));
}

const myFunction = (id = 0) => {
  assert(age,Joi.number().min(1));
}
Enter fullscreen mode Exit fullscreen mode

it's really powerfull, it can check anything with any complex schema. you can check emails, patterns, etc...

might be worth giving it a shot.

Collapse
 
bytebodger profile image
Adam Nathaniel Davis

Also, FWIW, the NPM package for my version of this is now published here:

npmjs.com/package/@toolz/allow

Collapse
 
bytebodger profile image
Adam Nathaniel Davis

Very cool - thanks for pointing this out!