0.2.7 • Published 5 years ago

validate-form-7 v0.2.7

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

Validate Form 7 provides a comprehensive data validation that helps minimize the amount of code you’ll write.

Example

import validation from 'validate-form-7';

const data = {
      _id: '5bf56a5fc384b83ef6e11071',
      _idx: '5bf56a5fc384b83ef6e11071',
    };
const config = {
      _id: { rules: 'required', title: 'ID' },
      _idx: { rules: 'required|matches:_id', title: 'IDX' },
    };
const resp = validation(data, config);
To validate a single entity
import { validateEntity } from 'validate-form-7';

const data = '27';
const rules = 'minLength:33|maxLength:100';
const title = 'Marks';
const resp = validation(data, rules, title);

Config Reference

AttributeRequiredDescriptionExample
rulesYesYou can set as many validation rules as you need for a given field in rules attribute. Validation rules, as a string list separated by a pipe \"|"rules: 'requiredminLength:4'
titleNoA “human” name for this field, which will be inserted into the error message. For example, if your field is named “user” you might give it a human name of “Username”.title: 'FIELD NAME'

Validation Response

errors

Default Value: Empty Object {} errors attribute provides the object of invalid field(s) and each field contains the error message(s).

errorsList

Default Value: Empty Array [] errorsList attribute provides the array of error messages of all invalid field(s).

Example
import validation from 'validate-form-7';

const data = {
      ryan: 'this is ryan',
      markhor: 'this is markhor',
      markus: 'this is markus',
    };
    const config = {
      ryan: { rules: 'required|minLength:4', title: 'Ryan' },
      markhor: { rules: 'required|minLength:40', title: 'Markhor' },
      markus: { rules: 'required|maxLength:4', title: 'Markus' },
    };
    const resp = validation(data, config);
    console.log(resp);
Response
{
  errorsList:
   [
    'The Markhor field must be at least 40 characters in length.',
    'The Markus field cannot exceed 4 characters in length.'
    ],
  errors:
   {
     markhor: [ 'The Markhor field must be at least 40 characters in length.' ],
     markus: [ 'The Markus field cannot exceed 4 characters in length.' ],
   }
}

Rule Reference

The following is a list of all the native rules that are available to use:

RuleParameterDescriptionExample
requiredNoReturns FALSE if the form element is empty.
matchesYesReturns FALSE if the form element does not match the one in the parameter.matches:form_item
differsYesReturns FALSE if the form element does not differ from the one in the parameter.differs:form_item
minLengthYesReturns FALSE if the form element is shorter than the parameter value.minLength:3
maxLengthYesReturns FALSE if the form element is longer than the parameter value.maxLength:12
exactLengthYesReturns FALSE if the form element is not exactly the parameter value.exactLength:8
greaterThanYesReturns FALSE if the form element is less than or equal to the parameter value or not numeric.greaterThan:8
greaterThanEqualToYesReturns FALSE if the form element is less than the parameter value, or not numeric.greaterThanEqualTo:8
lessThanYesReturns FALSE if the form element is greater than or equal to the parameter value or not numeric.lessThan:8
lessThanEqualToYesReturns FALSE if the form element is greater than the parameter value, or not numeric.lessThanEqualTo:8
inListYesReturns FALSE if the form element is not within a predetermined list.inList:red,blue,green
alphaNoReturns FALSE if the form element contains anything other than alphabetical characters.
alphaNumericNoReturns FALSE if the form element contains anything other than alpha-numeric characters.
alphaNumericSpacesNoReturns FALSE if the form element contains anything other than alpha-numeric characters or spaces. Should be used after trim to avoid spaces at the beginning or end.
alphaDashNoReturns FALSE if the form element contains anything other than alpha-numeric characters, underscores or dashes.
numericNoReturns FALSE if the form element contains anything other than numeric characters.
integerNoReturns FALSE if the form element contains anything other than an integer.
decimalNoReturns FALSE if the form element contains anything other than a decimal number.
validUrlNoReturns FALSE if the form element does not contain a valid URL.
validEmailNoReturns FALSE if the form element does not contain a valid email address.
validIPYesReturns FALSE if the supplied IP address is not valid. Accepts an optional parameter of ‘ipv4’ to specify an IP format.

Click here to see the examples

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago