easy-validation
v1.2.0
Published
TypeScript validation library
Downloads
29
Maintainers
Readme
Easy Validation
A TypeScript validation library. The lib use a subset of validator.js validators, but it can use any function that validates string and returns boolean or boolean promise.
Installation
npm install easy-validation
Usage
import * as Promise from "bluebird";
import validate, {
V,
ValidationError,
ValidationObject,
Validations,
Validator
} from "easy-validation";
const invalidUser: ValidationObject = {
username: "uname",
email: "[email protected]",
phone: "1234-567"
};
const validUser: ValidationObject = {
username: "uname",
email: "[email protected]",
phone: "123456789"
};
const isUnique: Validator = email => {
return Promise.resolve(email !== "[email protected]");
};
const validations: Validations = {
username: [{
validate: V.isNotBlank(),
message: "username cannot be blank"
}],
email: [{
validate: V.isNotNull(),
message: "email cannot be null"
}, {
validate: isUnique,
message: "email has been taken"
}, {
validate: V.isEmail(),
message: "invalid email"
}],
phone: [{
validate: V.hasLengthOf({ min: 9, max: 9 }),
message: "phone must have nine digits"
}, {
validate: V.isNumeric(),
message: "phone must contain only digits"
}]
};
validate(invalidUser, validations)
.catch(ValidationError, err => console.log(err.errors));
// { username: { errorMessages: [], hasError: false },
// email:
// { errorMessages: [ 'email has been taken' ],
// hasError: true },
// phone:
// { errorMessages:
// [ 'phone must have nine digits',
// 'phone must contain only digits' ],
// hasError: true } }
validate(validUser, validations)
.then(console.log);
// { username: 'uname',
// email: '[email protected]',
// phone: '123456789' }
The extended example that includes nested properties and array of objects can be found in tests
Validators
All validators except isNotNull
treat properties as optional. That is they validate null
and undefined
.
contains
contains(str)
Fails if property does not contain given str
.
equals
equals(str)
Fails if property is not equal to the given str
.
hasLengthOf
interface LengthOption {
min?: number;
max?: number;
}
hasLengthOf(option)
Fails if property does not have length between min and max.
Valid options are { min: m }
and { max: m }
as well.
If both values are omitted string is considered valid.
isAfterDate
isAfterDate(datetime)
Fails if property is not valid date or not after datetime
param.
If date param is not valid date or datetime string validation fails with ValidatorError
.
isAfterNow
isAfterNow()
Fails if property is not valid date or not after current datetime.
isBeforeDate
isBeforeDate(datetime)
Fails if property is not valid date or not before datetime
param.
If date param is not valid date or datetime string validation fails with ValidatorError
.
isBeforeNow
isBeforeNow()
Fails if property is not valid date or not before current datetime.
isDate
isDate()
Fails if property is not parsable date string.
isDateOnly
isDateOnly()
Fails if property is not parasble date or if it contains any component but mm
dd
and yyyy
.
isEmail
isEmail()
Fails if property is not valid email.
IsFloat
isFloat()
Fails if property is not number.
isIn
isIn(arr)
Fails if property is not memeber of the given arr
.
isInt
isInt()
Fails if property is not an integer
isMax
isMax(num)
Fails if property is not number or is greater than given num
.
isMin
isMin(num)
Fails if property is not number or is less than the given num
.
isNotBlank
isNotBlank()
Fails if property is empty string.
isNotNull
isNotNull()
Fails if property is null
or undefined
.
isNotNumeric
isNotNumeric()
Fails if property is numeric.
isNumeric
isNumeric()
Fails if property is not numeric.