11# Mobkit TypeScript
22
3+ ![ npm version] ( https://img.shields.io/npm/v/mobkit-typescript.svg )
4+ ![ License] ( https://img.shields.io/npm/l/mobkit-typescript.svg )
5+ ![ Downloads] ( https://img.shields.io/npm/dt/mobkit-typescript.svg )
6+ ![ TypeScript] ( https://img.shields.io/badge/TypeScript-Ready-blue.svg )
7+
38A lightweight, flexible validation library for TypeScript applications with customizable rules and multilingual support.
49
5- ## Installation
10+ ## 📦 Installation
611
712``` bash
813npm install mobkit-typescript
914```
1015
11- ## Basic Usage
16+ ## 🚀 Basic Usage
1217
1318``` typescript
1419import { Validator } from ' mobkit-typescript' ;
@@ -65,21 +70,53 @@ async function validateForm() {
6570validateForm ();
6671```
6772
68- ## Available Validation Rules
73+ ## 📋 Available Validation Rules
74+
75+ ### Basic Rules
76+
77+ | Rule | Description | Example |
78+ | ------| -------------| ---------|
79+ | ` required ` | Checks if the field is not empty | ` required: { value: true } ` |
80+ | ` nullable ` | Field may be empty | ` nullable: { value: true } ` |
81+ | ` min ` | Validates minimum numeric value | ` min: { value: 18 } ` |
82+ | ` max ` | Validates maximum numeric value | ` max: { value: 100 } ` |
83+ | ` minLen ` | Validates minimum string length | ` minLen: { value: 8 } ` |
84+ | ` maxLen ` | Validates maximum string length | ` maxLen: { value: 255 } ` |
85+ | ` email ` | Validates email format | ` email: { value: true } ` |
86+ | ` regex ` | Validates against a regular expression | ` regex: { value: /^[A-Z]+$/ } ` |
87+ | ` same ` | Checks if the field matches another field | ` same: { value: 'password' } ` |
88+ | ` numeric ` | Checks if the field is a number | ` numeric: { value: true } ` |
89+
90+ ### Conditional Rules
91+
92+ | Rule | Description | Example |
93+ | ------| -------------| ---------|
94+ | ` required_if ` | Required when another field equals any value | ` required_if: { value: 'payment_type,credit,paypal' } ` |
95+ | ` required_unless ` | Required unless another field equals any value | ` required_unless: { value: 'payment_type,cash' } ` |
96+ | ` required_with ` | Required when any of the other fields are present | ` required_with: { value: 'shipping_name,shipping_address' } ` |
97+ | ` required_with_all ` | Required when all of the other fields are present | ` required_with_all: { value: 'cc_name,cc_number,cc_expiry' } ` |
98+ | ` required_without ` | Required when any of the other fields are not present | ` required_without: { value: 'phone,mobile' } ` |
99+ | ` required_without_all ` | Required when all of the other fields are not present | ` required_without_all: { value: 'email,phone,mobile' } ` |
69100
70- - ` required ` : Checks if the field is not empty
71- - ` min ` : Validates minimum numeric value
72- - ` max ` : Validates maximum numeric value
73- - ` minLen ` : Validates minimum string length
74- - ` maxLen ` : Validates maximum string length
75- - ` email ` : Validates email format
76- - ` regex ` : Validates against a regular expression
77- - ` same ` : Checks if the field matches another field
78- - ` numeric ` : Checks if the field is a number
101+ ### Format Rules
79102
80- ## Multilingual Support
103+ | Rule | Description | Example |
104+ | ------| -------------| ---------|
105+ | ` alpha ` | Field must contain only alphabetic characters | ` alpha: { value: true } ` |
106+ | ` uppercase ` | Field must be uppercase | ` uppercase: { value: true } ` |
107+ | ` lowercase ` | Field must be lowercase | ` lowercase: { value: true } ` |
108+ | ` json ` | Field must be a valid JSON string | ` json: { value: true } ` |
81109
82- Rakit TypeScript supports multiple languages for error messages:
110+ ### List Rules
111+
112+ | Rule | Description | Example |
113+ | ------| -------------| ---------|
114+ | ` in ` | Field must be included in the given list | ` in: { value: 'admin,editor,user' } ` |
115+ | ` not_in ` | Field must not be included in the given list | ` not_in: { value: 'admin,root' } ` |
116+
117+ ## 🌐 Multilingual Support
118+
119+ mobkit TypeScript supports multiple languages for error messages:
83120
84121``` typescript
85122// Create a validator with a specific locale
@@ -89,7 +126,7 @@ const validator = new Validator('fr');
89126validator .setLocale (' es' );
90127```
91128
92- ## Custom Error Messages
129+ ## 💬 Custom Error Messages
93130
94131You can provide custom error messages for each rule:
95132
@@ -104,10 +141,124 @@ const rules = {
104141};
105142```
106143
107- ## Contributing
144+ ## 🔍 Detailed Examples
145+
146+ ### Basic Validation
147+
148+ ``` typescript
149+ const inputs = {
150+ name: ' John Doe' ,
151+ email: ' john@example.com' ,
152+ age: 25
153+ };
154+
155+ const rules = {
156+ name: {
157+ required: { value: true , message: ' Name is required' },
158+ minLen: { value: 3 , message: ' Name must be at least 3 characters' }
159+ },
160+ email: {
161+ required: { value: true , message: ' Email is required' },
162+ email: { value: true , message: ' Please enter a valid email address' }
163+ },
164+ age: {
165+ required: { value: true , message: ' Age is required' },
166+ numeric: { value: true , message: ' Age must be a number' },
167+ min: { value: 18 , message: ' You must be at least 18 years old' }
168+ }
169+ };
170+ ```
171+
172+ ### Conditional Validation
173+
174+ ``` typescript
175+ const inputs = {
176+ payment_type: ' credit' ,
177+ credit_card: ' 4111111111111111' ,
178+ paypal_email: ' '
179+ };
180+
181+ const rules = {
182+ credit_card: {
183+ required_if: {
184+ value: ' payment_type,credit' ,
185+ message: ' Credit card number is required when payment type is credit'
186+ }
187+ },
188+ paypal_email: {
189+ required_if: {
190+ value: ' payment_type,paypal' ,
191+ message: ' PayPal email is required when payment type is paypal'
192+ }
193+ }
194+ };
195+ ```
196+
197+ ### Format Validation
198+
199+ ``` typescript
200+ const inputs = {
201+ username: ' johndoe' ,
202+ country_code: ' US' ,
203+ config: ' {"theme":"dark","notifications":true}'
204+ };
205+
206+ const rules = {
207+ username: {
208+ required: { value: true , message: ' Username is required' },
209+ lowercase: { value: true , message: ' Username must be lowercase' }
210+ },
211+ country_code: {
212+ required: { value: true , message: ' Country code is required' },
213+ uppercase: { value: true , message: ' Country code must be uppercase' }
214+ },
215+ config: {
216+ json: { value: true , message: ' Configuration must be a valid JSON string' }
217+ }
218+ };
219+ ```
220+
221+ ### List Validation
222+
223+ ``` typescript
224+ const inputs = {
225+ role: ' editor' ,
226+ status: ' active'
227+ };
228+
229+ const rules = {
230+ role: {
231+ required: { value: true , message: ' Role is required' },
232+ in: { value: ' admin,editor,user' , message: ' Invalid role selected' }
233+ },
234+ status: {
235+ required: { value: true , message: ' Status is required' },
236+ not_in: { value: ' banned,suspended' , message: ' Status cannot be banned or suspended' }
237+ }
238+ };
239+ ```
240+
241+ ### Strict Type Checking with In/Not In Rules
242+
243+ ``` typescript
244+ const inputs = {
245+ value: ' 1' , // string "1", not number 1
246+ };
247+
248+ const rules = {
249+ value: {
250+ in: {
251+ value: ' 1,2,3,strict' ,
252+ message: ' Value must be string "1", "2", or "3" (strict type checking)'
253+ }
254+ }
255+ };
256+ ```
257+
258+ ## 🤝 Contributing
108259
109260Contributions are welcome! Please feel free to submit a Pull Request.
110261
111- ## License
262+ ## 📄 License
112263
113264This project is licensed under the MIT License.
0 commit comments