New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

passable-react

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

passable-react

Wrapper Component for react that allows easy Passable integration

latest
npmnpm
Version
0.7.6
Version published
Maintainers
1
Created
Source

Passable-React

Greenkeeper badge

Passable-React is a package that allows easy integration with passable validations. It manages the form state itself, leaving you to only care about writing the validations and hooking it all together.

Philosophy

When using passable-react, you let its core component PassableProvider take control over the state of the form and supply you with different functions and values that allow you connect it to your state. PassableProvider uses the children as a function pattern both for familiarity and flexibility.

Installation

npm install --save passable-react

Usage

// ./App.js
import React, {Component} from 'react';
import PassableProvider from 'passable-react';
import passes from './passes.js';

class App extends Component {
    initiaFormState = {
        username: 'initial value',
        password: '',
        spam: {
            checked: true
        }
    }

    render() {
        return (
            <PassableProvider passes={passes} initialFormState={this.initialFormState}>
            {(setTouchedOnEvent, validateOnEvent, validateOne, validateAll, fields, errors, warnings) => {
                <form name="myform" onBlur={setTouchedOnEvent} onChange={validateOne} onSubmit={validateAll}>
                    <input type="text" name="username" value={fields.username.value}/>
                    {errors[username] && <span>{fields.username.errors}</span>}
                    <input type="password" name="password" value={fields.password.value}/>
                    {errors[password] && <span>{fields.password.errors}</span>}
                    <input type="checkbox" name="spam" value={fields.spam.value} checked={fields.spam.checked}/>
                    {errors[spam] && <span>{fields.spam.errors}</span>}
                    <input type="submit" value="Submit"/>
                </form>
            }}
            </PassableProvider>
        );
    }
}

// ./passes.js
import passable, { enforce } from 'passable';

export default function passes({ specific = [], data }) {
    return passable('myform', (test) => {
        test('username', 'should be a string between 3 and 10 chars', () => {
            enforce(data.username.value).allOf({
                largerThanOrEquals: 5,
                smallerThanOrEquals: 10
            });
        });

        test('password', 'can either be a number, or empty', () => {
            enforce(data.password.value).isNotEmpty();
        });

        test('spam', 'must be checked', () => !!data.password.checked);
    }, specific);
}

And that's all there is to it. Here is what we did:

  • We set our initial form state. It contains the default values for our form fields, including dirty and touched.
  • We created a passes file which holds our validation logic.
  • We attached both our initial form state and the passes to PassableProvider
  • We grabbed the functions supplied by provider and used them for our onChange and onBlur for the form.
  • We used the fields object to grab the current value and connect the inputs to the current state.
  • We added conditional rendering of errors next to each input. They will automatically get added once validation of a field fails.

What else happens here?

  • The validateOnEvent function both validates a field, and marks it as dirty.
  • setTouchedOnEvent marks the field as touched

The fields object:

Here is an example of a fields object

fields: {
    checkbox: {
        errors: []
        value: true
        warnings: []
    },
    radio_input: {
        checked: true,
        errors: [],
        value: "yo",
        warnings: []
    },
    username: {
        dirty: true,
        errors: Array["should be a string between 3 and 10 chars"],
        hasError: true,
        hasWarning: false,
        touched: true,
        value: "An",
        warnings: []
    }
}

The errors and warnings objects

The errors and warnings objects are simply counters of the amount of errors in each field, saving you the hassle of traversing down the fields object and counting each field manually:

errors: {
    phone: 1
    username: 1
}

FAQs

Package last updated on 16 Nov 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