FormChecker
A TypeScript library for flexible and customizable form validation with support for synchronous and asynchronous rules.
Features
- Define validation rules for each form field.
- Supports built-in validation rules such as
required
,checked
,min
,max
,minLength
,maxLength
,equal
, andregexp
. - Supports custom asynchronous validation rules (
test
). - Handles data transformation before and after validation (
onBefore
,onAfter
). - Returns detailed validation results including errors and custom or default messages.
- Contains standard validation error messages in several languages such as: English, Portuguese, Spanish, Chinese, etc.
Installation
With NPM:
npm install form-checker-ts
Or with Yarn:
yarn add form-checker-ts
Example Usage
import { formChecker, type FormCheckerSchema } from 'form-checker-ts';
type Data = {
name: string;
email: string;
password: string;
password_confirm: string;
};
const schema : FormCheckerSchema<Data> = {
name: { required: true, minLength: 3, maxLength: 30 },
email: { required: true, minLength: 5, maxLength: 50 },
password: { required: true, minLength: 6, maxLength: 20, regexp: [/[a-z]/, /[A-Z]/, /[0-9]/] },
password_confirm: { required: { ifFilled: 'password' }, equal: 'password' }
};
const data : Data = {
name: 'Guilherme',
email: 'contato@gn.dev.br',
password: 'Q1w2E3r4',
password_confirm: 'Q1w2E3r4'
};
formChecker(schema, data, 'pt').then(result => {
console.log(result);
});
Author
License
This project is under the MIT license - see the LICENSE file for details.