Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

calidators

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

calidators

Red hot JavaScript validators

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
318
decreased by-18.04%
Maintainers
1
Weekly downloads
 
Created
Source

calidators

Red hot JavaScript validators 🌶

Build Status codecov

yarn add calidators

What's this?

This is a set of validators implemented in JavaScript. They are the validators backing the calidation React form validation library. You can, however, use them whichever way you want!

How do you even?

You can import validators like this:

import * as allOfTheValidators from 'calidators';

However, I suggest you import only the ones you need, so that it's easier for your bundler to tree-shake dead code:

import { isRequired, isEqual } from 'calidators';

Each validator is a curried function that first accepts a configuration, and then the value. The configuration is an object with a message field and an optional extra field that depends on the validator.

If the validator passes, it returns the message field. If the validator fails, it returns null.

Here's an example where I - very step by step - check the content of a field:

import { isRequired } from 'calidators';

const fieldValue = document.querySelector('#first-name').value;
const config = { message: 'You have to give us your first name!' };
const configuredValidator = isRequired(config);
const errorMessage = configuredValidator(fieldValue);
if (errorMessage) console.error(errorMessage);

Here's another example where I validate that the input is at least 5 (in a bit terser way):

import { isGreaterThan } from 'calidators';

const ageFieldValue = document.querySelector('#age').value;
const errorMessage = isGreaterThan({
    message: 'Your age is important to us',
    value: 5,
})(ageFieldValue);
if (errorMessage) console.error(errorMessage);

Does this look pretty straight forward? If not, please submit an issue with your question, and I'll both answer your question and improve this README for future users.

Validators

Here's the available validators. If you need a new one, feel free to open a pull request!

isRequired

Validates that a value is not the empty string

import { isRequired } from 'calidators';

const message = 'Field is required';
isRequired({ message })('') === message;
isRequired({ message })('Some input') === null;
isNumber

Validates that a field only contains numeric characters

import { isNumber } from 'calidators';

const message = 'Field must be a number';
isNumber({ message })('') === null;
isNumber({ message })('123') === null;
isNumber({ message })('123.321') === null;
isNumber({ message })('Not a number') === message;
isEqual

Validates that a value equals a given value. The value is cast to a String, and then checked for equality with the === operator.

import { isEqual } from 'calidators';

const message = "Value must be 'true'";
const value = true;
isEqual({ message, value })('') === null;
isEqual({ message, value })('true') === null;
isEqual({ message, value })('false') === message;
isGreaterThan / isLessThan

Validates that a value is greater or less than a given number.

import { isGreaterThan, isLessThan } from 'calidators';

{
    const message = 'Value must be greater than 5';
    const value = 5;
    isGreaterThan({ message, value })('') === null;
    isGreaterThan({ message, value })('6') === null;
    isGreaterThan({ message, value })('5') === message;
}
{
    const message = 'Value must be less than 5';
    const value = 5;
    isLessThan({ message, value })('') === null;
    isLessThan({ message, value })('4') === null;
    isLessThan({ message, value })('5') === message;
}
isEmail

Validates that a value is a potentially valid email address. This is weak on purpose to save bytes - but it will validate most email address errors.

import { isEmail } from 'calidators';

const message = 'Value must be a valid email';
isEmail({ message })('') === null;
isEmail({ message })('valid@email.com') === null;
isEmail({ message })('not a valid @ email') === message;
isRegexMatch

Validates that a value matches a given regular expression.

import { isRegexMatch } from 'calidators';

const message = 'Filename must end with either .js or .jsx';
const regex = /.jsx?$/;
isRegexMatch({ message, regex })('') === null;
isRegexMatch({ message, regex })('userReducer.js') === null;
isRegexMatch({ message, regex })('App.jsx') === null;
isRegexMatch({ message, regex })('.jsx-files') === message;
isRegexMatch({ message, regex })('just baloney') === message;
isWhitelisted

Validates that a value is present in a provided whitelist. The whitelist must be an array.

import { isWhitelisted } from 'calidators';

const message = "You're not a bro, bro";
const whitelist = ['Chad', 'Bret'];
isWhitelisted({ message, whitelist })('') === null;
isWhitelisted({ message, whitelist })('Chad') === null;
isWhitelisted({ message, whitelist })('Bret') === null;
isWhitelisted({ message, whitelist })('Ping') === message;
isBlacklisted

Validates that a value is not present in a provided blacklist. The blacklist must be an array.

import { isBlacklisted } from 'calidators';

const message = "You're too bro-ey, bro";
const blacklist = ['Chad', 'Bret'];

isBlacklisted({ message, blacklist })('') === null;
isBlacklisted({ message, blacklist })('Ping') === null;
isBlacklisted({ message, blacklist })('Chad') === message;
isBlacklisted({ message, blacklist })('Bret') === message;

Want to contribute?

I'd love some help! Report bugs, help me document stuff, create new validators and add new features!

Keywords

FAQs

Package last updated on 11 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc