1.2.5 • Published 5 years ago

validator-v2 v1.2.5

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

data-validator v2

Javascript module that can be very efficiently and conveniently used with forms validation.
Can be used either in html file script tags or as a node.js module

Install

npm i install validator-v2

Usage Example

const Validator = require('validator-v2')
const songValidation = new Validator({
    id: {
        required: true,
        type: Number,
        onError: {
            required: "ID is necessary",
            type: "ID is not a number"
        }
    },
    title: {
        required: true,
        type: String,
        minLen: 4,
        maxLen: 25,
        regexMatch: /^[a-zA-Z\s]+$/,
        onError: {
            any: "Title must be of 4-25 chars and contain only english letters"
        }
    },
    uploaderUsername: {
        type: String,
        minLen: 4,
        maxLen: 20,
        regexMatch: /^[a-zA-Z_]+/,
        onError: {
            any: "Username must be of 4-20 chars",
            regexMatch: "Username must contain only english letters and underscore"
        }
    },
    duration: {
        required: true,
        type: String,
        regexMatch: /^(\d+):(\d+)$/,
        onError: {
            regexMatch: "Duration must be of m:s structure. example: 4:25"
        }
    }
})
songValidation.check({id: "string id", title: "One Day", duration: "3:35"})
	.then(() => {
		console.log("Song is valid")
	})
	.catch(err => {
		console.log(err.msg)
	})

Options

  • required: boolean (default is false)
  • minLen: number
  • maxLen: number
  • length: number(exact length) or arraymin, max
  • validate: function (view below)
  • type: string (Number, String, etc.)
  • regexMatch: regex (on unmatch is invalid)
  • regexFail: regex (on match is invalid)
  • onError: object (view below)
  • object: another validator-v2 instance
  • each: another validator-v2 instance

object

let personValidation = new Validator({
	name: {
		required: true,
		type: String
	},
	contact: {
		required: true,
		object: new Validator({
			tel: {
				required: false,
				regexMatch: /^\d+$/,
				onError: "Only numbers are allowed in 'tel' field. Remove last 'E' to make it work."
			},
			email: {
				required: true,
				email: true
			},
			address: {
				required: false,
				type: String
			}
		})
	}
 })

each

let authorValidation = new Validator({
	name: {
		required: true,
		type: String
	},
	books: {
		each: new Validator({
			title: {
				required: true,
				type: String
			},
			pagesAmount: {
				required: true,
				type: Number
			}
		})
	}
 })

onError

An object that decides which message to return on rejection. Every failed field may have its own message.
'any' property is the default error message (for all cases).
not more then one (option) message will be returned for a field

validate

Use:

validate: (value, cb) => { ... cb(boolean) }

Parameters

value: The value which was inserted in the check method
cb: The function to invoke when a result whether the validation has succeeded or not was concluded

Details:

Best used for: 1. Async validations (such as database querying, etc.)
2. Customized validations

Example:

    validate: (value, cb) => {
        setTimeout(() => { 
            cb(value > 5)
        }, 1000
    });
1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.0.3

6 years ago

2.0.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago