0.0.4 • Published 3 years ago
typescript-validation v0.0.4
typescript-validation
A simple typescript library to validate strings
Installing
Using npm
npm install typescript-validation
Using yarn
yarn add typescript-validation
Usage
Importing package
import validation from "typescript-validation";
Validating
validation.validate("example@exmail.com", {
isEmail: {
need: true,
message: "Invalid e-mail",
},
});
Checking if the validations were successfull
Get error functions:
getErrors() - Get all errors (Return String[])
getLastError() - Get last error (Return String)
if (validation.success()) {
// Success methods here
} else {
// Unsuccess methods here
console.log(validation.getLastError());
}
Full code example
import validation from "typescript-validation";
validation.validate("example@exmail.com", {
isEmail: {
need: true,
message: "Invalid e-mail",
},
});
if (validation.success()) {
// Success methods here
} else {
// Unsuccess methods here
console.log(validation.getLastError());
}
Validations
need (Boolean): The final expected value (Default value: false)
message (String): Message if the result is not as expected
isAlpha: { need: false, message: "These are not alphabetic characters"}
isEmail: { need: false, message: "This is not an e-mail"}
isEmpty: { need: true, message: "This is empty"}
isNumeric: { need: false, message: "These are not numeric characters"}
min (Number): Minimum characters value
max (Number): Maximum characters value
isLength: { need: true, min: 5, max: 10, message: "This must be between 5 and 10 characters"}