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

valle

v0.4.1

Published

Help to validate react component easily, functionally and extendable

Downloads

2

Readme

valle

npm Build Status Dependency Status Coverage Status

Help to validate react component easily, functionally and extendable. Powered by Higher-Order Components.

Inspire by

Feature

  • Extendable: You can defined validator through jQuery-validation-like addMethod by yourself
  • Easily: Just pass the properties to component to control which validator you want to use
  • Functionally: valle use HOC component to wrap component which want to be valided, so you can wrap any component easily.
  • Asynchronous: It support asynchronous validator.

Full Example

You can see the simple example on example

import React from 'react';
import ReactDOM from 'react-dom';

import valle from 'valle';

// valle would pass three properties to component
// valid(Boolean): Present component is valid or not
// message(String): Error message if not valid
// validate(Function): Trigger validate. Just pass string value
const Input = ({ validate, message }) => (
  <div>
    <input onChange={e => validate(e.target.value)} />
    <div>{message}</div>
  </div>
);

// User defined validator
valle.addMethod('required', value => value !== '', 'It should have value');

// Use HOC to connect component
const ValidInput = valle.connect(Input);

ReactDOM.render(
  <ValidInput
    required  // Just pass properties to tell valle which validator you want to use
    onValid={value => console.log(`This is valid value: ${value}`)}  // onValid would be called when validate success
    onInvalid={value => console.log(`This is invalid value: ${value}`)}  // onValid would be called when validate falied
  />,
	document.getElementById('content')
);

API

addMethod(name, method, message)

Could define the custom validator

ex.

valle.addMethod('required', value => value !== '', 'It should have value');

Support asynchronous method. ex. ex.

valle.addMethod('required', async value => new Promise(resolve => setTimeout(() => resolve(value !== ''), 500)), 'It should have value');

Arguments

  • [name](String): Validator name
  • [method(value, property value)](Function): Validator method. it would be called when validate be called. It would catch value(validate function argument) and property value. It could be the asynchronous method and return promise
  • [message](String | Function): Validate failed message

addMethods(validators)

Could define the many custom validators at once

ex.

valle.addMethods({
  required: {
    method: value => value !== '',
    message: 'It should have value',
  },
});

Arguments

  • [validators](Object): Validators' object, each validator has two key: method and message

Validator object format:

  • [method(value, property value)](Function): Validator method. it would be called when validate be called. It would catch value(validate function argument) and property value. It could be the asynchronous method and return promise
  • [message](String | Function): Validate failed message

setMessages(validators)

Could override the validators' default message

ex.

valle.addMethods({
  required: 'It would override the required validator\'s message',
});

Arguments

  • [Messages](Object): The message object. key is validator's name, value is message which want to override

connect(Component)

Use Higher-Order component to wrap component.

Arguments

  • [Component](Component): The Component which want to validate.

templateTemplate Strings

It could use ES6 Template Strings to custom message

ex.

valle.addMethods({
  empty: {
    method: value => value === '',
    message: valle.template`It has value: ${0}`,
  },
});

Template Strings Arguments

  • ${0}: The validate value
  • ${1}: The property value
  • ${key}: If property value is the object, it could use key to read it.

Message method

You could also set message method instead of template string.

ex.

valle.addMethods({
  empty: {
    method: value => value === '',
    message: value => `It has value: ${value}`,
  },
});

Arguments

  • [value](Function): Current validate value
  • [property value](String): The property value

Todo

  • [x] Add default validator. Like required, isEmail, isInt
  • [x] Support addMethods to add many validators at onece
  • [x] Filter properties key to prevent pass unneccessay properties to child component
  • etc...

Contribute

devDependency Status

  1. Fork it.
  2. Create your feature-branch git checkout -b your-new-feature-branch
  3. Commit your change git commit -am 'Add new feature'
  4. Push to the branch git push origin your-new-feature-branch
  5. Create new Pull Request with master branch

License

The MIT License (MIT)

Copyright (c) 2017 Lee < [email protected] >

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.