0.2.2 • Published 8 years ago

@gin0606/form-validator v0.2.2

Weekly downloads
10
License
MIT
Repository
github
Last release
8 years ago

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); // => []
});
0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.7

8 years ago

0.1.6

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago