TypeScript validation library
npm install 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.
```
npm install easy-validation
`ts
import * as Promise from "bluebird";
import validate, {
V,
ValidationError,
ValidationObject,
Validations,
Validator
} from "easy-validation";
const invalidUser: ValidationObject = {
username: "uname",
email: "u@example.com",
phone: "1234-567"
};
const validUser: ValidationObject = {
username: "uname",
email: "user@example.com",
phone: "123456789"
};
const isUnique: Validator = email => {
return Promise.resolve(email !== "u@example.com");
};
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: 'user@example.com',
// phone: '123456789' }
`
The extended example that includes nested properties and array of objects can be found in tests
All validators except isNotNull treat properties as optional. That is they validate null and undefined.
`ts`
contains(str)
Fails if property does not contain given str.
`ts`
equals(str)
Fails if property is not equal to the given str.
`ts
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.
`ts`
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.
`ts`
isAfterNow()
Fails if property is not valid date or not after current datetime.
`ts`
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.
`ts`
isBeforeNow()
Fails if property is not valid date or not before current datetime.
`ts`
isDate()
Fails if property is not parsable date string.
`ts`
isDateOnly()mm
Fails if property is not parasble date or if it contains any component but dd and yyyy.
`ts`
isEmail()
Fails if property is not valid email.
`ts`
isFloat()
Fails if property is not number.
`ts`
isIn(arr)
Fails if property is not memeber of the given arr.
`ts`
isInt()
Fails if property is not an integer
`ts`
isMax(num)
Fails if property is not number or is greater than given num.
`ts`
isMin(num)
Fails if property is not number or is less than the given num.
`ts`
isNotBlank()
Fails if property is empty string.
`ts`
isNotNull()
Fails if property is null or undefined.
`ts`
isNotNumeric()
Fails if property is numeric.
`ts``
isNumeric()
Fails if property is not numeric.