0.13.2 • Published 4 years ago

@dock365/validator v0.13.2

Weekly downloads
12
License
MIT
Repository
github
Last release
4 years ago

Validator

Build Status npm Version

A general purpose validation library to validate strings or numbers.

Install

# Using yarn package manager
$ yarn add @dock365/validator

# Using npm package manager
$ npm install --save @dock365/validator

Usage

Initialize new validator instance with default configurations

import Validator from "@dock365/validator";

const validator = new Validator();

Initialize new validator instance with custom fail messages.

  • $field will be replaced with field name.

  • $maxLength, $include etc will be replaced with defined validation criteria values.

  • $value will be replaced with value to be validated.
import Validator from "@dock365/validator";

const validator = new Validator({
  failMessages: {
    include: "Custom validation message for $field to validate the value contain '$include'",
    maxLength: "custom validation message to validate $field max length",
    maxValue: "custom validation message to validate $field max value",
    minLength: "custom validation message to check $field min length",
    minValue: "custom validation message to check $field min value",
    noTrailingSpace: "custom validation message to check $field has trailing spaces",
    required: "custom validation message to check $field is required",
    type: "custom validation message to check $field type",
    regx: "custom validation message to check $regx type",
    /* All values are optional, if not defined default will be used */
  }
});

Validate a value with initially defined/default fail message

// String
validator.string(
  "Title", // Field Name
  "Some Value", // Value to be validated
  {
    required: true,
    /* validation criterias*/
  },
)

// Number
validator.number(
  "Title", // Field Name
  25, // Value to be validated
  {
    required: true,
    /* validation criterias*/
  },
)

Validate a value with custom fail message

// String
validator.string(
  "Title", // Field Name
  "Some Value", // Value to be validated
  {
    required: true,
    /* validation criterias*/
  },
  {
    required: "If the $field doesnt have a value this message will be displayed",
    type: "If the $field is not a string this message will be displayed",
    /* All values are optional, if not defined default will be used */
  }
)

// Number
validator.number(
  "Title", // Field Name
  25, // Value to be validated
  {
    required: true,
    /* validation criterias*/
  },
  {
    required: "If the $field doesnt have a value this message will be displayed",
    type: "If the $field is not a number this message will be displayed",
    /* All values are optional, if not defined default will be used */
  }
)

Sample response

{
  success: true,
  messages: [ /*...validation messages if fail*/ ],
}

Options

Initialization configurations

NameTypeDefaultDescription
failMessagesObjectDefault fail messagesHere define custome valiadtion messages and all other configurations
failMessages
NameTypeDefaultDescription
requiredstring$field is required!validation fail message if field is required and it's not present
regxstring$field must match $regx!validation fail message if field does not match regx
typestring$field must be a $type!validation fail message if type check fails
minLengthstring$field can't be less than $minLength charectors!validation fail message if field length is less than specified value
maxLengthstring$field can't be greater than $maxLength charectors!validation fail message if field length is greater than specified value
minValuestring$field can't be less than $minValue!validation fail message if field value is less than specified value
maxValuestring$field can't be greater than $maxValue!validation fail message if field value is greater than specified value
includestring\$field doesn't contain '$include'!validation fail message if value doesn't contain specified value
noTrailingSpacesstring\$field must not contain any trailing spaces!validation fail message if value contain trailing space

Validation criterias

Base

NameTypeDefaultDescription
requiredBooleanfalsereturn success as false if the field is required and value is not present

String

NameTypeDefaultDescription
minLengthnumberundefinedreturn success as false if the field value length is less than specified value
maxLengthnumberundefinedreturn success as false if the field value length is greater than specified value
regxregxundefinedreturn success as false if the field doesn't match specified regx
includestringundefinedreturn success as false if the field doesn't contain specified include string
noTrailingSpacesbooleanundefinedreturn success as false if the field contains trailing space

Number

NameTypeDefaultDescription
minValuenumberundefinedreturn success as false if the field value is less than specified value
maxValuenumberundefinedreturn success as false if the field value is greater than specified value

Contributing!

All contributions are super welcome!

License

Validator is MIT licensed.

0.13.2

4 years ago

0.13.1

4 years ago

0.13.0

4 years ago

0.12.5

4 years ago

0.12.4

4 years ago

0.12.2

4 years ago

0.12.1

5 years ago

0.12.0

5 years ago

0.11.6

5 years ago

0.11.4

5 years ago

0.11.3

5 years ago

0.11.1

5 years ago

0.11.0

5 years ago

0.10.5

5 years ago

0.10.4

5 years ago

0.10.3

5 years ago

0.10.2

5 years ago

0.10.1

5 years ago

0.10.0

5 years ago

0.8.1

5 years ago

0.8.0

5 years ago

0.7.1

5 years ago

0.7.0

5 years ago

0.6.0

5 years ago

0.2.4

6 years ago