form-checker-ts
v2.1.1
Published
The FormChecker (form-checker-ts) is a TypeScript/JavaSctipt form validation library that supports custom validation rules like required fields, length checks, pattern matching, and custom functions. It provides asynchronous validation and flexible error
Downloads
47
Maintainers
Readme
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-tsOr with Yarn:
yarn add form-checker-tsExample 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: '[email protected]',
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.
