zod-ir 1.1.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +139 -18
- package/package.json +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 [Reza Kheradmandi]
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,26 +1,38 @@
|
|
|
1
|
-
# Zod Iranian Utils 🇮🇷
|
|
2
|
-
|
|
3
1
|
<div align="center">
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
2
|
+
<h1>zod-ir</h1>
|
|
3
|
+
<p>
|
|
4
|
+
<strong>Comprehensive Zod validations for Iranian data structures</strong>
|
|
5
|
+
</p>
|
|
6
|
+
<p>
|
|
7
|
+
A lightweight, TypeScript-first extension for Zod.
|
|
8
|
+
<br />
|
|
9
|
+
Compatible with React Hook Form, Next.js, and Node.js.
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
<p>
|
|
13
|
+
<a href="https://www.npmjs.com/package/zod-ir">
|
|
14
|
+
<img src="https://img.shields.io/npm/v/zod-ir?style=flat-square&color=blue" alt="npm version" />
|
|
15
|
+
</a>
|
|
16
|
+
<a href="https://bundlephobia.com/result?p=zod-ir">
|
|
17
|
+
<img src="https://img.shields.io/bundlephobia/minzip/zod-ir?style=flat-square&color=green" alt="bundle size" />
|
|
18
|
+
</a>
|
|
19
|
+
<a href="https://github.com/Reza-kh80/zod-ir/blob/main/LICENSE">
|
|
20
|
+
<img src="https://img.shields.io/npm/l/zod-ir?style=flat-square&color=orange" alt="license" />
|
|
21
|
+
</a>
|
|
22
|
+
</p>
|
|
12
23
|
</div>
|
|
13
24
|
|
|
25
|
+
<hr />
|
|
26
|
+
|
|
14
27
|
## Features ✨
|
|
15
28
|
|
|
16
|
-
- ✅ **National Code:**
|
|
17
|
-
- 💳 **Bank Card:** 16-digit Luhn algorithm
|
|
18
|
-
- 📱 **Mobile Number:**
|
|
19
|
-
- 🏦 **Sheba (IBAN):** ISO 7064
|
|
20
|
-
- 📮 **Postal Code:**
|
|
21
|
-
- ☎️ **Landline:**
|
|
22
|
-
- 🌍 **Bilingual:** Built-in Persian
|
|
23
|
-
- 🌲 **Tree-shakable:** Minimal bundle size.
|
|
29
|
+
- ✅ **National Code:** Validates using the official checksum algorithm.
|
|
30
|
+
- 💳 **Bank Card:** Validates 16-digit card numbers (Luhn algorithm).
|
|
31
|
+
- 📱 **Mobile Number:** Validates `09xx`, `+989xx`, `9xx`.
|
|
32
|
+
- 🏦 **Sheba (IBAN):** Validates structure and checksum (ISO 7064).
|
|
33
|
+
- 📮 **Postal Code:** Validates 10-digit Iranian postal codes.
|
|
34
|
+
- ☎️ **Landline:** Validates fixed line numbers with area codes.
|
|
35
|
+
- 🌍 **Bilingual:** Built-in error messages in **Persian** and **English**.
|
|
24
36
|
|
|
25
37
|
---
|
|
26
38
|
|
|
@@ -28,4 +40,113 @@ Compatible with **React Hook Form**, **Next.js**, and Node.js backends.
|
|
|
28
40
|
|
|
29
41
|
```bash
|
|
30
42
|
npm install zod zod-ir
|
|
43
|
+
# or
|
|
44
|
+
yarn add zod zod-ir
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Usage 🚀
|
|
48
|
+
|
|
49
|
+
1. Basic Validation
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { z } from "zod";
|
|
53
|
+
import {
|
|
54
|
+
zMelliCode,
|
|
55
|
+
zSheba,
|
|
56
|
+
zIranianMobile,
|
|
57
|
+
zCardNumber,
|
|
58
|
+
zPostalCode,
|
|
59
|
+
zLandline,
|
|
60
|
+
} from "zod-ir";
|
|
61
|
+
|
|
62
|
+
const UserSchema = z.object({
|
|
63
|
+
// Default Persian error message
|
|
64
|
+
nationalCode: zMelliCode(),
|
|
65
|
+
|
|
66
|
+
// Custom error message & Strict mode (must start with 0)
|
|
67
|
+
mobile: zIranianMobile({
|
|
68
|
+
strictZero: true,
|
|
69
|
+
message: "شماره موبایل اشتباه است",
|
|
70
|
+
}),
|
|
71
|
+
|
|
72
|
+
// English error message for Sheba
|
|
73
|
+
iban: zSheba({ locale: "en" }),
|
|
74
|
+
|
|
75
|
+
// Bank Card
|
|
76
|
+
card: zCardNumber(),
|
|
77
|
+
|
|
78
|
+
// Postal Code
|
|
79
|
+
postal: zPostalCode(),
|
|
80
|
+
|
|
81
|
+
// Landline (Phone)
|
|
82
|
+
phone: zLandline(),
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
UserSchema.parse({
|
|
87
|
+
nationalCode: "1234567891",
|
|
88
|
+
mobile: "09121234567",
|
|
89
|
+
iban: "IR120770000000000000000001",
|
|
90
|
+
card: "6037991155667788",
|
|
91
|
+
postal: "1234567890",
|
|
92
|
+
phone: "02122334455",
|
|
93
|
+
});
|
|
94
|
+
} catch (err) {
|
|
95
|
+
console.log(err);
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
2. Usage with React Hook Form 📋
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
import { useForm } from "react-hook-form";
|
|
103
|
+
import { zodResolver } from "@hookform/resolvers/zod";
|
|
104
|
+
import { z } from "zod";
|
|
105
|
+
import { zMelliCode } from "zod-ir";
|
|
106
|
+
|
|
107
|
+
const schema = z.object({
|
|
108
|
+
nationalId: zMelliCode({ message: "کد ملی صحیح نیست" }),
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
export default function MyForm() {
|
|
112
|
+
const {
|
|
113
|
+
register,
|
|
114
|
+
handleSubmit,
|
|
115
|
+
formState: { errors },
|
|
116
|
+
} = useForm({
|
|
117
|
+
resolver: zodResolver(schema),
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
return (
|
|
121
|
+
<form onSubmit={handleSubmit((d) => console.log(d))}>
|
|
122
|
+
<input {...register("nationalId")} placeholder="Code Melli" />
|
|
123
|
+
<p>{errors.nationalId?.message}</p>
|
|
124
|
+
<button type="submit">Submit</button>
|
|
125
|
+
</form>
|
|
126
|
+
);
|
|
127
|
+
}
|
|
31
128
|
```
|
|
129
|
+
|
|
130
|
+
## API Reference 📚
|
|
131
|
+
|
|
132
|
+
| Validator | Description | Options |
|
|
133
|
+
| :--------------- | :---------------------------------------------------- | :-------------------------------- |
|
|
134
|
+
| `zMelliCode` | Validates Iranian National Code (Melli Code) | `message`, `locale` |
|
|
135
|
+
| `zCardNumber` | Validates 16-digit bank card numbers (Luhn algorithm) | `message`, `locale` |
|
|
136
|
+
| `zIranianMobile` | Validates Iranian mobile numbers | `strictZero`, `message`, `locale` |
|
|
137
|
+
| `zSheba` | Validates Sheba (IBAN) structure and checksum | `message`, `locale` |
|
|
138
|
+
| `zPostalCode` | Validates 10-digit Iranian postal codes | `message`, `locale` |
|
|
139
|
+
| `zLandline` | Validates landline phone numbers with area codes | `message`, `locale` |
|
|
140
|
+
|
|
141
|
+
#### Options Interface
|
|
142
|
+
All validators accept an optional configuration object to customize behavior.
|
|
143
|
+
|
|
144
|
+
| Name | Type | Description |
|
|
145
|
+
| :----------- | :-------------------- | :--------------------------------------------------------- |
|
|
146
|
+
| `message` | `string` | Custom error message to display when validation fails. |
|
|
147
|
+
| `locale` | `"fa"`, `"en"` | Language for the default error message (defaults to "fa"). |
|
|
148
|
+
| `strictZero` | `boolean`, `optional` | (Mobile Only) Indicates strictness of the leading zero. |
|
|
149
|
+
|
|
150
|
+
## License
|
|
151
|
+
|
|
152
|
+
MIT
|