npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@omarzion/validation

v2.3.2

Published

simple react validation library

Downloads

6

Readme

@omarzion/validation

A simple validation library that uses React's context library.

It can be used to wrap around basic inputs as it is, but it is recommended to integrate it into stateless components.

Upon invoking the validate function the error state is remembered for each invalid input and retested on each change for that input until a valid state is reached in which case it the error state is removed.

Requires React ^16.4.2

for old documentation see version 2 docs

For basic usage

import withValidation, { Wrapper } from '@omarzion/validation';


const submit = async controller => {
  // this is where the magic happens
  // if validation fails it fires up all the error messages
  // and adds listeners to all the bad inputs and revalidates them
  // on each change until they pass
  if (await controller.validate()) {
    // if validate() returns true, everything is valid

    // getValues() returns an object with all your form inputs in key value form
    const formData = controller.getValues();

    // do whatever with your clean form data here
  }
}

// withValidation provides controller prop
const ValidatedForm = ({ controller, Wrap, Long }) => (
  <div>
    <Wrap name='name' controller={controller} validate={v => v.length > 0}>
      ({ onChange, value, error, message }) => (
        <input
          style={{ borderColor: error ? 'red' : 'initial' }}
          onChange={onChange}
          value={value}
        />
      )
    </Wrap>
    <button onClick={() => submit(controller)} />
  </div>
)

export default withValidation()(ValidatedForm);

You can also map the controller (and optionably a validation function) and provide it to the withValidation function

import withValidation, { Wrapper } from '@omarzion/validation';


const submit = async controller => {
  if (await controller.validate()) {
    const formData = controller.getValues();
    // do whatever with your clean form data here
  }
}

// withValidation provides controller prop, and all registered validators
const ValidatedForm = ({ controller, Wrap, Long }) => (
  <div>
    <Wrap name='name' validate={v => v.length > 0}>
      ({ onChange, value, error, message }) => (
        <input
          style={{ borderColor: error ? 'red' : 'initial' }}
          onChange={onChange}
          value={value}
        />
      )
    </Wrap>
    <button onClick={() => submit(controller)} />
  </div>
)

const mapControllerToFields = register => {
  return {
    Wrap: register(Wrapper),

    // make a validator that requires a string at least 10 characters long
    Long: register(Wrapper, input => input.length > 9)
  }
}

export default withValidation(mapControllerToFields)(ValidatedForm);

Wrapper

primary primitive for wrapping components that need validated

interface Wrapper {
  name: string; // this will be the key returned by controller
  // value of current input
  // formValues = object of all inputs in current form
  validate: (value, formValues) => {
    boolean // validate should return whether input is valid
    || { valid: boolean, message: string} // can return message for errors
  }
  children: (ChildrenArguments) => React Element
}

interface ChildrenArguments {
   onChange
   value
   error: boolean; // is there currently an error?
   message: string; //error message or null
}

Controller

This is where all the magic happens, it handles:

  • when validation should be ran
  • when error messages should be shown
  • getting your valid data back in a nice object

The recommended way of getting a controller object is by wrapping your component in withValidation()() and a controller object will be passed in as a prop

most use cases can be handled with this concise snippet

if (async controller.validate()) {
  const formData = controller.getValues();
}

Api

async .validate()

the primary validation function, returns true if the form is valid. It also deals with setting the error states on anything that isn't.

getValues()

returns an object of key value pairs where the key is the name of the field and value is the user input

.set(name, { valid: boolean, message: string });

used to manually set an error on a field by name, good for a failed login

.get(name)

simply exists to accompany set

returns { valid, value, message }

.clear()

resets the controller, this is good if validated form fields are going to be dynamically added and removed