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

passable

Package Overview
Dependencies
Maintainers
2
Versions
660
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

passable

Isomorphic Data Model Validations

  • 7.5.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9.7K
increased by3.58%
Maintainers
2
Weekly downloads
 
Created
Source

Passable

Declarative data validations.

npm version Build Status

What is Passable?

Passable is a library for JS applications for writing validations in a way that's structured and declarative.

Inspired by the syntax of modern unit testing framework, passable validations are written as a spec or a contract, that reflects your form structure. Your validations run in production code, and you can use them in any framework (or without any framework at all).

The idea behind passable is that you can easily adopt its very familiar syntax, and transfer your knowledge from the world of testing to your form validations.

Much like most testing frameworks, Passable comes with its own assertion function, enforce, all error based assertion libraries are supported.

Key features

  1. Non failing tests.
  2. Conditionally running tests.
  3. Async validations.
  4. Test callbacks.

Syntactic differences from testing frameworks

Since Passable is running in production environment, and accommodates different needs, some changes to the basic unit test syntax have been made, to cover the main ones quickly:

  • Your test function is not available globally, it is an argument passed to your suite's callback.
  • Each test has two string values before its callback, one for the field name, and one for the error returned to the user.
  • Your suite accepts another argument after the callback - name (or array of names) of a field. This is so you can run tests selectively only for changed fields.
// validation.js
import passable, { enforce } from 'passable';

const validation = (data) => passable('NewUserForm', (test) => {

    test('username', 'Must be at least 3 chars', () => {
        enforce(data.username).longerThanOrEquals(3);
    });

    test('email', 'Is not a valid email address', () => {
        enforce(data.email)
            .isNotEmpty()
            .matches(/[^@]+@[^\.]+\..+/g);
    });
});

export default validation;
// myFeature.js
import validation from './validation.js';

const res = validation({
    username: 'example',
    email: 'email@example.com'
});

res.hasErrors() // returns whether the form has errors
res.hasErrors('username') // returns whether the 'username' field has errors
res.getErrors() // returns an object with an array of errors per field
res.getErrors('username') // returns an array of errors for the `username` field

"BUT HEY! I ALREADY USE X VALIDATION LIBRARY! CAN IT WORK WITH PASSABLE?"

As a general rule, Passable works similarly to unit tests in term that if your test throws an exception, it is considered to be failing. Otherwise, it is considered to be passing.

There are a few more ways to handle failures in order to ease migration, and in most cases, you can move your validation logic directly to into Passable with only a few adjustments.

For example, if you use a different assertion libraries such as chai (expect) or v8n, you can simply use it instead of enforce, and it should work straight out of the box.

FAQs

Package last updated on 21 May 2020

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