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

reactive-form-validator

v1.0.6

Published

reactive form input validator with rxjs 5

Downloads

8

Readme

Reactive form validator

Simple library to handle form input validation following a reactive approach (rxjs 5), built with typescript can be used with any client side framework like react or angular

The idea behind the library is to have an Observable validator coupled with a specific dom element and to subscribe to this observable to check if the element is is valid and modify the dom accordingly (eg. add some error classes etc.)

Install with npm or yarn

  • npm install --save reactive-form-validator
  • yarn add --save reactive-form-validator

API

there are two api , one to instantiate the library and one to register a validator for a specific dom element

let config = {
  'debounce': 300, //default debounce
  'evtType': 'blur' //default event handled
}
this.validator = new ReactiveFormValidator(config);

//returns an Observable
let validator$ = this.validator.registerValidator(
  'domEl', //dom element
  'validator': {
    'fn': evt => evt.target.value, //validator check if field is empty
    'msg': 'name cannot be empty'
  }, 
  'blur', // specific event handled for this element
  ['input_err'], //classes to be added in case of error
  100 //specific debounce time (override the default one)
);

React simple form example (client side validation)

import ReactiveFormValidator from 'reactive-form-validator';

class MyForm extends Component {
constructor() {
  super();
  this.inputs = []; // collection of inputs / validators
  this.validator = new ReactiveFormValidator({
    'debounce': 300,
    'evtType': 'blur'
   });
}
componentDidMount() {
  this.inputs.forEach(input => {
    let validator$ = this.validator.registerValidator(
      input.dom,
      input.validator,
      'blur', 
      ['input_err'],
      100
    );

    validator$
      .subscribe(el => {
        if (el.isValid) {
          //dom element is valid
        } else {
          //dom element is not valid, change dom accordingly
          //here you can access the input.validator.msg that you previously passed to the validator
        }
      });
  });
}

render() {
  return (
    <div>
      <form>
        Name <input 
              type="text" 
              ref={el => (this.inputs.push({
                'dom': el, 
                'validator': {
                  'fn': evt => evt.target.value, 
                  'msg': 'surname cannot be empty'
                }
                }))}
              /><br/>
              
        Surname <input 
                  type="text"
                  ref={el => (this.inputs.push({
                    'dom': el, 
                     'validator': {
                        'fn': evt => evt.target.value, 
                        'msg': 'name cannot be empty'
                      }
                     }))}
                /><br/>
      </form>
   </div>  

Advanced usage with server side validation

the validator function can return either a primitive value or a Promise/Observable, in this case it is possible to perform a server side validation, if for example the input is not valid then you should reject the promise or throw an error if you're using an Observable.

//Promise
<input
type="text"
ref={el => this.inputs.push({
  'dom': el,
  'validator': {
    'fn': evt => new Promise((resolve, reject) => evt.target.value ? resolve() : reject()),
    'msg': 'field cannot be empty'
  }
})}
/>

//Observable
<input
type="text"
ref={el => this.inputs.push({
  'dom': el,
  'validator': {
    'fn': evt => new Observable(observer => evt.target.value ? observer.complete() : observer.error()),
    'msg': 'field cannot be empty'
  }
})}
/>

Working Examples (with function, Promise and Observable)

  • React example