Code Monkey home page Code Monkey logo

form-validator-js's Introduction

form-validator-js

Installation

npm install --save @gin0606/form-validator

or

yarn add @gin0606/form-validator

Usage

import { FormValidator, Errors } from '@gin0606/form-validator';

class MustNotBeEmptyError extends ValidationError {
}
class MaxLengthError extends ValidationError {
  max: number
  constructor(max: number) {
    super();
    this.max = max;
  }
}

const Rules = {
  cantBeEmpty: () => ({
    verify: (v: string): Promise<?ValidationError> => Promise.resolve((isNotEmpty(v) ? null : new MustNotBeEmptyError())),
  }),
  maxLength: (max: number) => ({
    verify: (v: string): Promise<?ValidationError> => {
      let error = null;
      if (v && v.length > max) {
        error = new MaxLengthError(max);
      }
      return Promise.resolve(error);
    },
  }),
  acceptEmail: () => ({
    verify: (v: string): Promise<?ValidationError> => {
      if (!v) { return Promise.resolve(null); }
      if (isEmail(v)) { return Promise.resolve(null); }
      return Promise.resolve(new AcceptEmailError());
    },
  }),
};

const validator = new FormValidator({
  name: [Rules.cantBeEmpty(), Rules.maxLength(8)],
  message: [Rules.maxLength(16)],
});

validator.validate({
  name: 'gin0606',
  message: 'hello',
}).then((result) => {
  const errors = result.errors();
  console.log(errors.name); // => []
  console.log(errors.email); // => []
  console.log(errors.message); // => []
});

validator.validate({
  name: 'gin060606',
  message: 'hello hello hello hello',
}).then((result) => {
  const errors = result.errors();
  console.log(errors.name[0] instanceof MaxLengthError); // => true
  console.log(errors.message[0] instanceof MaxLengthError); // => true
});

validator.validate({
}).then((result) => {
  const errors = result.errors();
  console.log(!!errors.name.find(e => e instanceof MustNotBeEmptyError)); // => true
  console.log(errors.message); // => []
});

form-validator-js's People

Contributors

gin0606 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.