Wondering what’s next for npm?Check out our public roadmap! »

ve-form-validation

1.0.0 • Public • Published

VE-Form-Validate

form-validate is a library that allows you to validate inputs and return streams of data that detect the validation.

INSTALATION

npm i ve-form-validate

API

Schema

With Validate Object user can handle all stuff validation

Validator Description
BooleanValidator Validate.boolean
NumberValidator Validate.number
validate number with minLength & maxLength
Example: Validate.number.minLength(3)
ObjectValidator Validate.object
object validator has keys function
that wrap all validation
StringValidator Validate.string
validate string with min & max
validate any type of string with type method
color - email - date - text - url - image
for special pattern you can use pattern method
Example: Validate.string.type('email')
Example: Validate.string.pattern(/^([^.]+)/)
ArrayValidator validate input array with contains method
Example: Validate.array.contains(Validate.number.minLength(5))

Special Methods

required() is a shared method for all validators

Events

after you handle schema, you can access these events to handle more stuff when user input or on submit event

Event Description
onValues got a stream of form values
onMessages this event will emit success - error messages depend on the validation
onSubmit will send object that contains isValidating - isSubmiting - errors if found - Form Values

Access Events

const formEvents = FormValidate("validate_form_api", Validation);

formEvents.onSubmit(function(submit) {
  console.log(submit, 'onSubmit');
});
formEvents.onValues(function(values) {
  console.log(values, 'onValues');
});
formEvents.onMessages(function(messages) {
  console.log(messages, 'onMessages');
});

USAGE

you will send validation object to FormValidate Method with form className

import { FormValidate, Validate } from 've-form-validate';
const Validation = {
  schema: Validate.object.keys({
    email: Validate.string.type('email'),
    phone: Validate.number.required()
  }),
  messages: {
    email: {
      success: 'email is valid',
      error: 'email not valid'
    },
    phone: {
      error: 'phone number not correct',
      success: 'phone is correct'
    }
  }
};
const formEvents = FormValidate('validate_form_api', Validation);

EXAMPLES

Simple Filed

 
<input name="phone" placeholder="phone" />
<input name="email" placeholder="email" />
 
 Validate.object.keys({
     email: Validate.string.type("email"),
     phone: Validate.number.required()
});

Object Field

<fieldset name="person">
  <input type="text" name="firstName" value="ayman" />
  <input type="text" name="lastName" value="shafik" />
  <input type="text" name="age" />
</fieldset>;
 
Validate.object.keys({
  person: Validate.object.keys({
    firstName: Validate.string,
    lastName: Validate.string,
    age: Validate.number
  })
});

Validate array of boolean/number/string fields

<select name="cars" multiple>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>;
 
Validate.object.keys({
  cars: Validate.array.contains(Validate.string)
});

TODO

  • Localization
  • Validate arr of objects

LICENCE

MIT

Install

npm i ve-form-validation

DownloadsWeekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

38.2 kB

Total Files

34

Last publish

Collaborators

  • avatar