ts-form-validator
v1.0.3
Published
The TypeScript Form Validator is a utility that simplifies the validation of form data in TypeScript applications.
Downloads
6
Readme
Typescript Form Validator
The TypeScript Form Validator is a utility that simplifies the validation of form data in TypeScript applications.
Installation
Install the module via npm:
npm i ts-form-validator
How to use
Import from package
import {Validator, TypeInteger, TypeString, TypeEmail, TypeBoolean} from 'ts-form-validator';
Creating a Validator
const validator: Validator = new Validator({
'name': new TypeString({'required': true, min: 1, max: 30}),
'last_name': new TypeString({'required': false, min: 1, max: 50}),
'email': new TypeEmail({'required': true, max: 40}),
'is_investor': new TypeBoolean(),
});
Validator Methods:
validate
- Description: Validates the form data based on the rules defined in the validator instance.
- Parameters:
form
: Form data represented as an object {key: value}.
- Return: Void.
getError
- Description: Retrieves the error associated with the specified rule.
- Parameters:
key
: key of the rule. Has to be a string
- Return: The error message if it exists, otherwise null.
hasError
- Description: Checks if there is an error associated with the specified rule.
- Parameters:
key
: Key of the rule (string).
- Return: True if an error exists, otherwise false.
getErrors
- Description: Retrieves all errors found.
- Return: An object containing all errors.
hasErrors
- Description: Checks if the validator has any errors.
- Return: True if there are errors, otherwise false.
isValid
- Description: Checks if the validator is valid.
- Return: True if the validator is valid, otherwise false.
Validator types
Default attributes:
- required: Boolean. The default value is false.
- min: null|number|string.
- max: null|number|string.
Default Responses:
- ERROR_REQUIRED: The field is required.
TypeBoolean
- Responses:
- ERROR_BOOLEAN_FORMAT_INCORRECT: The boolean format is incorrect.
TypeDate
- Responses:
- ERROR_DATE_FORMAT_INCORRECT: The date format is incorrect.
- ERROR_DATE_MIN: The date is too early.
- ERROR_DATE_MAX: The date is too late.
TypeEmail
- Responses:
- ERROR_EMAIL_FORMAT_INCORRECT: The email format is incorrect.
- ERROR_EMAIL_MIN: The email is too short.
- ERROR_EMAIL_MAX: The email is to long.
TypeNumber
- Attributes:
- only_integer: boolean.
- Responses:
- ERROR_NUMBER_FORMAT_INCORRECT: The number format is incorrect.
- ERROR_NUMBER_FORMAT_INTEGER_INCORRECT: The integer number format is incorrect.
- ERROR_NUMBER_MIN: The number is too small.
- ERROR_NUMBER_MAX: The number is too big.
TypePhone
- Responses:
- ERROR_PHONE_FORMAT_INCORRECT: The phone format is incorrect.
- ERROR_PHONE_MIN: The phone is too short.
- ERROR_PHONE_MAX: The phone is too long.
TypeString
- Responses:
- ERROR_STRING_FORMAT_INCORRECT: The string format is incorrect.
- ERROR_STRING_MIN: The string is too short.
- ERROR_STRING_MAX: The string is too long.
TypeUrl
- Responses:
- ERROR_URL_FORMAT_INCORRECT: The url format is incorrect.
- ERROR_URL_MIN: The url is too short.
- ERROR_URL_MAX: The url is too long.
TypeZip
- Responses:
- ERROR_DATE_FORMAT_INCORRECT: The ZIP code format is incorrect.
Custom Validator Types
If you need a different type, you can create your own validator type. Here's an example:
class TestType extends TypeBase implements Type{
constructor(options = {}) {
super(options);
}
validate(value:string):void{
if(value !== 'test'){
this.error = 'ERROR_TEST';
}
}
}
const validator:Validator = new Validator({
'field_1': new TestType()
});