@kashi-ui/schemas 0.0.0-stage → 2.28.0
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 +57 -2
- package/dist/core.d.ts +103 -0
- package/dist/core.js +134 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +8 -0
- package/dist/yup.d.ts +60 -0
- package/dist/yup.js +108 -0
- package/dist/zod.d.ts +67 -0
- package/dist/zod.js +88 -0
- package/package.json +42 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kashi contributors
|
|
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,3 +1,58 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kashi-ui/schemas
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
اسکیماهای آمادهٔ **Zod** و **Yup** برای دادههای ایرانی، از [کاشی](https://github.com/Sepantapouya/Kashi): کد ملی، موبایل، شماره کارت، شبا، کد پستی، تاریخ شمسی، مبلغ، قبض و پلاک، با پیام خطای فارسی (یا انگلیسی) و مقدار یکدستشده. منطق اعتبارسنجی از [`@kashi-ui/persian`](https://www.npmjs.com/package/@kashi-ui/persian) میآید.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm i @kashi-ui/schemas zod # یا yup
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
import { nationalId, iranMobile, iban, jalaliDate, price } from "@kashi-ui/schemas/zod";
|
|
12
|
+
|
|
13
|
+
const Order = z.object({
|
|
14
|
+
nationalId: nationalId(),
|
|
15
|
+
mobile: iranMobile(),
|
|
16
|
+
iban: iban(),
|
|
17
|
+
birth: jalaliDate(), // یا jalaliDate({ output: "iso" }) ← "1991-09-22"
|
|
18
|
+
amount: price({ min: 10_000 }),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
Order.parse({ nationalId: "۰۰۱۳۵۴۲۴۱۹", mobile: "+98 912 345 6789", iban: "ir11 0170 0000 0012 3456 7890 01", birth: "۱۳۷۰/۶/۳۱", amount: "۱٬۲۵۰٬۰۰۰" });
|
|
22
|
+
// { nationalId: "0013542419", mobile: "09123456789", iban: "IR110170000000123456789001", birth: Date, amount: 1250000 }
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import * as yup from "yup";
|
|
27
|
+
import { nationalId, iranMobile, postalCode } from "@kashi-ui/schemas/yup";
|
|
28
|
+
|
|
29
|
+
const Profile = yup.object({ nationalId: nationalId(), mobile: iranMobile(), postal: postalCode().optional() });
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## اسکیماها
|
|
33
|
+
|
|
34
|
+
`nationalId` · `legalId` · `iranMobile` · `landline` · `cardNumber` · `iban` · `postalCode` · `sayad` · `economicCode` · `taxId` · `billId` · `bill` (شناسهٔ قبض + پرداخت) · `jalaliDate` · `time` · `price` · `plate` · `persianText`
|
|
35
|
+
|
|
36
|
+
- ورودی با ارقام فارسی/عربی، فاصله و خط تیره پذیرفته میشود؛ حرف و نشانهٔ بیربط نه.
|
|
37
|
+
- خروجی یکدست است: ارقام لاتین، موبایل همیشه `09…`، شبا `IR` + ۲۴ رقم، تاریخ `Date` (یا ISO یا شمسی با `output`)، مبلغ عدد.
|
|
38
|
+
- همه بهطور پیشفرض اجباریاند. فیلد اختیاری: در Zod `optional(landline())`، در Yup `landline().optional()`.
|
|
39
|
+
|
|
40
|
+
## پیامها
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { createSchemas } from "@kashi-ui/schemas/zod";
|
|
44
|
+
|
|
45
|
+
const en = createSchemas({ locale: "en" });
|
|
46
|
+
const custom = createSchemas({ messages: { required: "لازم است", mobile: "شمارهٔ همراه درست نیست." } });
|
|
47
|
+
nationalId({ message: "کد ملی را دوباره بررسی کنید.", requiredMessage: "کد ملی لازم است." });
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## سازگاری
|
|
51
|
+
|
|
52
|
+
- `@kashi-ui/schemas/zod`: Zod 4 و Zod 3 (از 3.23)
|
|
53
|
+
- `@kashi-ui/schemas/yup`: Yup 1
|
|
54
|
+
- ESM با تایپ کامل TypeScript؛ `z.infer` و `yup.InferType` نوع خروجی یکدستشده را میدهند.
|
|
55
|
+
|
|
56
|
+
راهنمای کامل با نمونهٔ react-hook-form و Formik: https://kashi.sepantapouya.com/docs/schemas/
|
|
57
|
+
|
|
58
|
+
مجوز MIT
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* هستهٔ مشترک اسکیماهای کاشی: یکدستسازی، اعتبارسنجی و پیامها.
|
|
3
|
+
* هیچ منطق اعتبارسنجی اینجا نوشته نشده؛ همه از @kashi-ui/persian میآید و این فایل فقط آنها را کنار هم میگذارد
|
|
4
|
+
* تا آداپتور Zod و Yup رفتار یکسانی داشته باشند.
|
|
5
|
+
*/
|
|
6
|
+
import { toEnDigits, PLATE_LETTERS } from "@kashi-ui/persian";
|
|
7
|
+
import type { KashiLocale, BillInfo, Plate } from "@kashi-ui/persian";
|
|
8
|
+
export type { KashiLocale, BillInfo, Plate };
|
|
9
|
+
/** همهٔ پیامهای خطای اسکیماها؛ پیشفرضها همان متنهای @kashi-ui/persian (فارسی یا انگلیسی) هستند */
|
|
10
|
+
export interface SchemaMessages {
|
|
11
|
+
required: string;
|
|
12
|
+
nationalId: string;
|
|
13
|
+
legalId: string;
|
|
14
|
+
mobile: string;
|
|
15
|
+
landline: string;
|
|
16
|
+
card: string;
|
|
17
|
+
iban: string;
|
|
18
|
+
postal: string;
|
|
19
|
+
sayad: string;
|
|
20
|
+
economicCode: string;
|
|
21
|
+
taxId: string;
|
|
22
|
+
billId: string;
|
|
23
|
+
paymentId: string;
|
|
24
|
+
plate: string;
|
|
25
|
+
time: string;
|
|
26
|
+
date: string;
|
|
27
|
+
/** مقدار عددی/مبلغ با نویسههای نامعتبر */
|
|
28
|
+
number: string;
|
|
29
|
+
priceMin: (min: string) => string;
|
|
30
|
+
priceMax: (max: string) => string;
|
|
31
|
+
minLength: (n: string) => string;
|
|
32
|
+
maxLength: (n: string) => string;
|
|
33
|
+
}
|
|
34
|
+
export interface SchemaConfig {
|
|
35
|
+
/** زبان پیامها @default "fa" */
|
|
36
|
+
locale?: KashiLocale;
|
|
37
|
+
/** عوض کردن هر پیام دلخواه */
|
|
38
|
+
messages?: Partial<SchemaMessages>;
|
|
39
|
+
}
|
|
40
|
+
/** پیامهای کامل برای یک زبان، بهاضافهٔ پیامهایی که خودتان عوض کردهاید */
|
|
41
|
+
export declare function schemaMessages(locale?: KashiLocale, overrides?: Partial<SchemaMessages>): SchemaMessages;
|
|
42
|
+
/** گزینههای مشترک هر اسکیما */
|
|
43
|
+
export interface FieldOptions extends SchemaConfig {
|
|
44
|
+
/** پیام خطای «نامعتبر» برای همین فیلد (بر messages مقدم است) */
|
|
45
|
+
message?: string;
|
|
46
|
+
/** پیام خطای «خالی» برای همین فیلد */
|
|
47
|
+
requiredMessage?: string;
|
|
48
|
+
}
|
|
49
|
+
/** پیامهای نهایی یک فیلد: پیشفرضهای سازنده (createSchemas) زیر گزینههای خود فیلد */
|
|
50
|
+
export declare function resolve(defaults: SchemaConfig | undefined, opts: FieldOptions | undefined): SchemaMessages;
|
|
51
|
+
export type StringFieldKey = "nationalId" | "legalId" | "mobile" | "landline" | "card" | "iban" | "postal" | "sayad" | "economicCode" | "taxId" | "billId";
|
|
52
|
+
export interface StringField {
|
|
53
|
+
/** کدام پیام برای خطای «نامعتبر» */
|
|
54
|
+
message: keyof SchemaMessages;
|
|
55
|
+
/** نویسههای مجاز ورودی؛ حروف و نشانههای بیربط رد میشوند حتی اگر persian آنها را نادیده بگیرد */
|
|
56
|
+
allowed: RegExp;
|
|
57
|
+
valid: (raw: string) => boolean;
|
|
58
|
+
normalize: (raw: string) => string;
|
|
59
|
+
}
|
|
60
|
+
export declare const STRING_FIELDS: Record<StringFieldKey, StringField>;
|
|
61
|
+
export type Check<T> = {
|
|
62
|
+
ok: true;
|
|
63
|
+
value: T;
|
|
64
|
+
} | {
|
|
65
|
+
ok: false;
|
|
66
|
+
message: string;
|
|
67
|
+
};
|
|
68
|
+
export declare function checkString(key: StringFieldKey, input: unknown, msgs: SchemaMessages, message?: string): Check<string>;
|
|
69
|
+
export type JalaliOutput = "date" | "iso" | "jalali";
|
|
70
|
+
/** خروجی jalaliDate بسته به گزینهٔ output */
|
|
71
|
+
export type JalaliValue<O extends JalaliOutput> = O extends "date" ? Date : string;
|
|
72
|
+
export interface JalaliDateOptions<O extends JalaliOutput = "date"> extends FieldOptions {
|
|
73
|
+
/**
|
|
74
|
+
* شکل مقدار برگشتی:
|
|
75
|
+
* "date" یک Date (نیمهشب به وقت محلی)، "iso" رشتهٔ میلادی YYYY-MM-DD (با toISODate، بدون خطای منطقهٔ زمانی)،
|
|
76
|
+
* "jalali" رشتهٔ شمسی یکدست YYYY/MM/DD با ارقام لاتین. @default "date"
|
|
77
|
+
*/
|
|
78
|
+
output?: O;
|
|
79
|
+
}
|
|
80
|
+
export declare function checkJalali(input: unknown, output: JalaliOutput, msgs: SchemaMessages, message?: string): Check<Date | string>;
|
|
81
|
+
export declare function checkTime(input: unknown, msgs: SchemaMessages, message?: string): Check<string>;
|
|
82
|
+
export interface PriceOptions extends FieldOptions {
|
|
83
|
+
min?: number;
|
|
84
|
+
max?: number;
|
|
85
|
+
/** واحدی که در پیام حداقل/حداکثر نوشته میشود؛ false یعنی بدون واحد @default "toman" */
|
|
86
|
+
unit?: "toman" | "rial" | false;
|
|
87
|
+
}
|
|
88
|
+
export declare function checkPrice(input: unknown, opts: PriceOptions | undefined, msgs: SchemaMessages): Check<number>;
|
|
89
|
+
export interface PersianTextOptions extends FieldOptions {
|
|
90
|
+
min?: number;
|
|
91
|
+
max?: number;
|
|
92
|
+
/** ارقام خروجی؛ پیشفرض بدون تغییر */
|
|
93
|
+
digits?: "fa" | "latin";
|
|
94
|
+
}
|
|
95
|
+
export declare function checkText(input: unknown, opts: PersianTextOptions | undefined, msgs: SchemaMessages): Check<string>;
|
|
96
|
+
export declare function checkPlate(input: unknown, msgs: SchemaMessages, message?: string): Check<Plate>;
|
|
97
|
+
/** خروجی bill(): دو شناسهٔ یکدستشده بهاضافهٔ اطلاعات خواندهشده از قبض */
|
|
98
|
+
export interface BillValue extends BillInfo {
|
|
99
|
+
billId: string;
|
|
100
|
+
paymentId: string;
|
|
101
|
+
}
|
|
102
|
+
export declare function checkPaymentId(billId: string, paymentInput: unknown, msgs: SchemaMessages, message?: string): Check<BillValue>;
|
|
103
|
+
export { PLATE_LETTERS, toEnDigits };
|
package/dist/core.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* هستهٔ مشترک اسکیماهای کاشی: یکدستسازی، اعتبارسنجی و پیامها.
|
|
3
|
+
* هیچ منطق اعتبارسنجی اینجا نوشته نشده؛ همه از @kashi-ui/persian میآید و این فایل فقط آنها را کنار هم میگذارد
|
|
4
|
+
* تا آداپتور Zod و Yup رفتار یکسانی داشته باشند.
|
|
5
|
+
*/
|
|
6
|
+
import { getMessages, onlyDigits, toEnDigits, formatMoney, formatNumber, formatTime, parseTime, parseJalali, toISODate, formatJalali, normalizePersian, isValidNationalId, isValidLegalId, isValidMobile, normalizeMobile, isValidLandline, normalizeLandline, isValidCardNumber, isValidIban, ibanDigits, isValidPostalCode, isValidSayad, isValidEconomicCode, isValidTaxId, normalizeTaxId, isValidBillId, parseBill, isValidPlate, PLATE_LETTERS, } from "@kashi-ui/persian";
|
|
7
|
+
/** پیامهای کامل برای یک زبان، بهاضافهٔ پیامهایی که خودتان عوض کردهاید */
|
|
8
|
+
export function schemaMessages(locale = "fa", overrides) {
|
|
9
|
+
const m = getMessages(locale);
|
|
10
|
+
const i = m.invalid;
|
|
11
|
+
const base = {
|
|
12
|
+
required: m.form.required,
|
|
13
|
+
nationalId: i.nationalId, legalId: i.legalId, mobile: i.mobile, landline: i.landline, card: i.card, iban: i.iban,
|
|
14
|
+
postal: i.postal, sayad: i.sayad, economicCode: i.economicCode, taxId: i.taxId, billId: i.billId, paymentId: i.paymentId,
|
|
15
|
+
plate: i.plate, time: i.time, date: m.datePicker.invalid, number: m.form.pattern,
|
|
16
|
+
priceMin: i.priceMin, priceMax: i.priceMax, minLength: m.form.minLength, maxLength: m.form.maxLength,
|
|
17
|
+
};
|
|
18
|
+
if (!overrides)
|
|
19
|
+
return base;
|
|
20
|
+
const out = { ...base };
|
|
21
|
+
for (const [k, v] of Object.entries(overrides))
|
|
22
|
+
if (v !== undefined)
|
|
23
|
+
out[k] = v;
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
/** پیامهای نهایی یک فیلد: پیشفرضهای سازنده (createSchemas) زیر گزینههای خود فیلد */
|
|
27
|
+
export function resolve(defaults, opts) {
|
|
28
|
+
const locale = opts?.locale ?? defaults?.locale ?? "fa";
|
|
29
|
+
const msgs = schemaMessages(locale, { ...defaults?.messages, ...opts?.messages });
|
|
30
|
+
return opts?.requiredMessage ? { ...msgs, required: opts.requiredMessage } : msgs;
|
|
31
|
+
}
|
|
32
|
+
// ── رشتههای ساده: «پذیرفتن نویسهها ← اعتبارسنجی با persian ← یکدستسازی»
|
|
33
|
+
const D = "0-9۰-۹٠-٩";
|
|
34
|
+
const SEP = "\\s\\-\\u2010-\\u2014_.\\u200c";
|
|
35
|
+
const DIGITS = new RegExp(`^[${D}${SEP}]+$`);
|
|
36
|
+
const PHONE = new RegExp(`^\\+?[${D}${SEP}()]+$`);
|
|
37
|
+
export const STRING_FIELDS = {
|
|
38
|
+
nationalId: { message: "nationalId", allowed: DIGITS, valid: isValidNationalId, normalize: onlyDigits },
|
|
39
|
+
legalId: { message: "legalId", allowed: DIGITS, valid: isValidLegalId, normalize: onlyDigits },
|
|
40
|
+
mobile: { message: "mobile", allowed: PHONE, valid: isValidMobile, normalize: normalizeMobile },
|
|
41
|
+
landline: { message: "landline", allowed: PHONE, valid: isValidLandline, normalize: normalizeLandline },
|
|
42
|
+
card: { message: "card", allowed: DIGITS, valid: isValidCardNumber, normalize: onlyDigits },
|
|
43
|
+
iban: { message: "iban", allowed: new RegExp(`^\\s*(IR)?[${D}${SEP}]+$`, "i"), valid: isValidIban, normalize: (v) => `IR${ibanDigits(v)}` },
|
|
44
|
+
postal: { message: "postal", allowed: DIGITS, valid: isValidPostalCode, normalize: onlyDigits },
|
|
45
|
+
sayad: { message: "sayad", allowed: DIGITS, valid: isValidSayad, normalize: onlyDigits },
|
|
46
|
+
economicCode: { message: "economicCode", allowed: DIGITS, valid: isValidEconomicCode, normalize: onlyDigits },
|
|
47
|
+
taxId: { message: "taxId", allowed: new RegExp(`^[A-Za-z${D}${SEP}]+$`), valid: isValidTaxId, normalize: normalizeTaxId },
|
|
48
|
+
billId: { message: "billId", allowed: DIGITS, valid: isValidBillId, normalize: (v) => onlyDigits(v).replace(/^0+/, "") },
|
|
49
|
+
};
|
|
50
|
+
const isEmpty = (v) => v === undefined || v === null || (typeof v === "string" && v.trim() === "");
|
|
51
|
+
export function checkString(key, input, msgs, message) {
|
|
52
|
+
if (isEmpty(input))
|
|
53
|
+
return { ok: false, message: msgs.required };
|
|
54
|
+
const f = STRING_FIELDS[key];
|
|
55
|
+
const raw = typeof input === "number" && Number.isSafeInteger(input) ? String(input) : input;
|
|
56
|
+
if (typeof raw !== "string" || !f.allowed.test(raw) || !f.valid(raw))
|
|
57
|
+
return { ok: false, message: message ?? msgs[f.message] };
|
|
58
|
+
return { ok: true, value: f.normalize(raw) };
|
|
59
|
+
}
|
|
60
|
+
export function checkJalali(input, output, msgs, message) {
|
|
61
|
+
if (isEmpty(input))
|
|
62
|
+
return { ok: false, message: msgs.required };
|
|
63
|
+
let d = null;
|
|
64
|
+
if (input instanceof Date)
|
|
65
|
+
d = Number.isNaN(input.getTime()) ? null : input;
|
|
66
|
+
else if (typeof input === "string")
|
|
67
|
+
d = parseJalali(input);
|
|
68
|
+
if (!d)
|
|
69
|
+
return { ok: false, message: message ?? msgs.date };
|
|
70
|
+
return { ok: true, value: output === "iso" ? toISODate(d) : output === "jalali" ? formatJalali(d, { digits: "latin" }) : d };
|
|
71
|
+
}
|
|
72
|
+
// ── ساعت
|
|
73
|
+
export function checkTime(input, msgs, message) {
|
|
74
|
+
if (isEmpty(input))
|
|
75
|
+
return { ok: false, message: msgs.required };
|
|
76
|
+
const t = typeof input === "string" && new RegExp(`^[${D}:٫.،,\\s]+$`).test(input) ? parseTime(input) : null;
|
|
77
|
+
return t ? { ok: true, value: formatTime(t) } : { ok: false, message: message ?? msgs.time };
|
|
78
|
+
}
|
|
79
|
+
export function checkPrice(input, opts, msgs) {
|
|
80
|
+
if (isEmpty(input))
|
|
81
|
+
return { ok: false, message: msgs.required };
|
|
82
|
+
let n;
|
|
83
|
+
if (typeof input === "number")
|
|
84
|
+
n = input;
|
|
85
|
+
else if (typeof input === "string" && new RegExp(`^\\s*[${D}][${D},٬،'\\s]*$`).test(input))
|
|
86
|
+
n = Number(onlyDigits(input));
|
|
87
|
+
else
|
|
88
|
+
n = NaN;
|
|
89
|
+
if (!Number.isSafeInteger(n) || n < 0)
|
|
90
|
+
return { ok: false, message: opts?.message ?? msgs.number };
|
|
91
|
+
const locale = opts?.locale ?? "fa";
|
|
92
|
+
const unit = opts?.unit ?? "toman";
|
|
93
|
+
const fmt = (v) => formatMoney(v, { locale, unit: unit || "toman", from: unit || "toman", showUnit: unit !== false });
|
|
94
|
+
if (opts?.min !== undefined && n < opts.min)
|
|
95
|
+
return { ok: false, message: msgs.priceMin(fmt(opts.min)) };
|
|
96
|
+
if (opts?.max !== undefined && n > opts.max)
|
|
97
|
+
return { ok: false, message: msgs.priceMax(fmt(opts.max)) };
|
|
98
|
+
return { ok: true, value: n };
|
|
99
|
+
}
|
|
100
|
+
export function checkText(input, opts, msgs) {
|
|
101
|
+
if (isEmpty(input))
|
|
102
|
+
return { ok: false, message: msgs.required };
|
|
103
|
+
if (typeof input !== "string")
|
|
104
|
+
return { ok: false, message: opts?.message ?? msgs.required };
|
|
105
|
+
const text = normalizePersian(input, { digits: opts?.digits });
|
|
106
|
+
const n = (v) => formatNumber(v, { digits: (opts?.locale ?? "fa") === "fa" ? "fa" : "latin" });
|
|
107
|
+
if (opts?.min !== undefined && [...text].length < opts.min)
|
|
108
|
+
return { ok: false, message: msgs.minLength(n(opts.min)) };
|
|
109
|
+
if (opts?.max !== undefined && [...text].length > opts.max)
|
|
110
|
+
return { ok: false, message: msgs.maxLength(n(opts.max)) };
|
|
111
|
+
return { ok: true, value: text };
|
|
112
|
+
}
|
|
113
|
+
// ── پلاک
|
|
114
|
+
export function checkPlate(input, msgs, message) {
|
|
115
|
+
if (input === undefined || input === null)
|
|
116
|
+
return { ok: false, message: msgs.required };
|
|
117
|
+
const p = input;
|
|
118
|
+
const parts = [p.first, p.letter, p.middle, p.region];
|
|
119
|
+
if (parts.every(isEmpty))
|
|
120
|
+
return { ok: false, message: msgs.required };
|
|
121
|
+
if (!parts.every((x) => typeof x === "string") || !isValidPlate(p))
|
|
122
|
+
return { ok: false, message: message ?? msgs.plate };
|
|
123
|
+
return { ok: true, value: { first: onlyDigits(p.first), letter: p.letter, middle: onlyDigits(p.middle), region: onlyDigits(p.region) } };
|
|
124
|
+
}
|
|
125
|
+
export function checkPaymentId(billId, paymentInput, msgs, message) {
|
|
126
|
+
if (isEmpty(paymentInput))
|
|
127
|
+
return { ok: false, message: msgs.required };
|
|
128
|
+
const raw = typeof paymentInput === "string" ? paymentInput : "";
|
|
129
|
+
const info = DIGITS.test(raw) ? parseBill(billId, raw) : null;
|
|
130
|
+
if (!info)
|
|
131
|
+
return { ok: false, message: message ?? msgs.paymentId };
|
|
132
|
+
return { ok: true, value: { billId, paymentId: onlyDigits(raw).replace(/^0+/, ""), ...info } };
|
|
133
|
+
}
|
|
134
|
+
export { PLATE_LETTERS, toEnDigits };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @kashi-ui/schemas — اسکیماهای آمادهٔ Zod و Yup برای دادههای ایرانی.
|
|
3
|
+
* اسکیماها را از زیرمسیرها بگیرید:
|
|
4
|
+
* import { nationalId } from "@kashi-ui/schemas/zod";
|
|
5
|
+
* import { nationalId } from "@kashi-ui/schemas/yup";
|
|
6
|
+
* این ورودی فقط پیامها و تایپهای مشترک را دارد و به Zod یا Yup وابسته نیست.
|
|
7
|
+
*/
|
|
8
|
+
export { schemaMessages } from "./core.js";
|
|
9
|
+
export type { SchemaMessages, SchemaConfig, FieldOptions, JalaliOutput, JalaliValue, JalaliDateOptions, PriceOptions, PersianTextOptions, BillValue, Plate, KashiLocale, } from "./core.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @kashi-ui/schemas — اسکیماهای آمادهٔ Zod و Yup برای دادههای ایرانی.
|
|
3
|
+
* اسکیماها را از زیرمسیرها بگیرید:
|
|
4
|
+
* import { nationalId } from "@kashi-ui/schemas/zod";
|
|
5
|
+
* import { nationalId } from "@kashi-ui/schemas/yup";
|
|
6
|
+
* این ورودی فقط پیامها و تایپهای مشترک را دارد و به Zod یا Yup وابسته نیست.
|
|
7
|
+
*/
|
|
8
|
+
export { schemaMessages } from "./core.js";
|
package/dist/yup.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* اسکیماهای آمادهٔ Yup (نسخهٔ ۱) برای دادههای ایرانی.
|
|
3
|
+
*
|
|
4
|
+
* import * as yup from "yup";
|
|
5
|
+
* import { nationalId, iranMobile, jalaliDate } from "@kashi-ui/schemas/yup";
|
|
6
|
+
* const Form = yup.object({ nationalId: nationalId(), mobile: iranMobile(), birth: jalaliDate() });
|
|
7
|
+
*
|
|
8
|
+
* هر اسکیما بهطور پیشفرض اجباری است (با پیام کاشی)؛ برای فیلد اختیاری .optional() بزنید،
|
|
9
|
+
* رشتهٔ خالی خودش undefined حساب میشود.
|
|
10
|
+
*/
|
|
11
|
+
import * as yup from "yup";
|
|
12
|
+
import { schemaMessages } from "./core.js";
|
|
13
|
+
import type { FieldOptions, SchemaConfig, JalaliOutput, JalaliValue, JalaliDateOptions, PriceOptions, PersianTextOptions, BillValue, Plate } from "./core.js";
|
|
14
|
+
export { schemaMessages };
|
|
15
|
+
export type { SchemaMessages, SchemaConfig, FieldOptions, JalaliOutput, JalaliValue, JalaliDateOptions, PriceOptions, PersianTextOptions, BillValue, Plate } from "./core.js";
|
|
16
|
+
/** اسکیمای اجباری Yup با مقدار یکدستشدهٔ T؛ .optional() آن را T | undefined میکند */
|
|
17
|
+
export type KashiYupSchema<T extends NonNullable<unknown>> = yup.MixedSchema<T>;
|
|
18
|
+
export interface KashiYupSchemas {
|
|
19
|
+
/** کد ملی ۱۰ رقمی → «0013542419» */
|
|
20
|
+
nationalId(opts?: FieldOptions): KashiYupSchema<string>;
|
|
21
|
+
/** شناسهٔ ملی اشخاص حقوقی، ۱۱ رقم */
|
|
22
|
+
legalId(opts?: FieldOptions): KashiYupSchema<string>;
|
|
23
|
+
/** موبایل ایران → همیشه «09…» */
|
|
24
|
+
iranMobile(opts?: FieldOptions): KashiYupSchema<string>;
|
|
25
|
+
/** تلفن ثابت → «021…» */
|
|
26
|
+
landline(opts?: FieldOptions): KashiYupSchema<string>;
|
|
27
|
+
/** شماره کارت ۱۶ رقمی → فقط ارقام */
|
|
28
|
+
cardNumber(opts?: FieldOptions): KashiYupSchema<string>;
|
|
29
|
+
/** شبا → «IR» + ۲۴ رقم */
|
|
30
|
+
iban(opts?: FieldOptions): KashiYupSchema<string>;
|
|
31
|
+
/** کد پستی ۱۰ رقمی */
|
|
32
|
+
postalCode(opts?: FieldOptions): KashiYupSchema<string>;
|
|
33
|
+
/** شناسهٔ صیادی چک */
|
|
34
|
+
sayad(opts?: FieldOptions): KashiYupSchema<string>;
|
|
35
|
+
/** کد اقتصادی */
|
|
36
|
+
economicCode(opts?: FieldOptions): KashiYupSchema<string>;
|
|
37
|
+
/** شمارهٔ مالیاتی فاکتور */
|
|
38
|
+
taxId(opts?: FieldOptions): KashiYupSchema<string>;
|
|
39
|
+
/** شناسهٔ قبض */
|
|
40
|
+
billId(opts?: FieldOptions): KashiYupSchema<string>;
|
|
41
|
+
/** شیء { billId, paymentId } با خطای جدا برای هر کدام و اطلاعات قبض در خروجی */
|
|
42
|
+
bill(opts?: FieldOptions & {
|
|
43
|
+
paymentMessage?: string;
|
|
44
|
+
}): KashiYupSchema<BillValue>;
|
|
45
|
+
/** تاریخ شمسی → Date (یا رشتهٔ ISO / شمسی با output) */
|
|
46
|
+
jalaliDate<O extends JalaliOutput = "date">(opts?: JalaliDateOptions<O>): KashiYupSchema<JalaliValue<O>>;
|
|
47
|
+
/** ساعت → «09:30» */
|
|
48
|
+
time(opts?: FieldOptions): KashiYupSchema<string>;
|
|
49
|
+
/** مبلغ → عدد، با min و max */
|
|
50
|
+
price(opts?: PriceOptions): KashiYupSchema<number>;
|
|
51
|
+
/** پلاک خودرو */
|
|
52
|
+
plate(opts?: FieldOptions): KashiYupSchema<Plate>;
|
|
53
|
+
/** متن فارسی یکدست */
|
|
54
|
+
persianText(opts?: PersianTextOptions): KashiYupSchema<string>;
|
|
55
|
+
}
|
|
56
|
+
/** سازندهها با زبان و پیامهای پیشفرض خودتان: createSchemas({ locale: "en" }) */
|
|
57
|
+
export declare function createSchemas(config?: SchemaConfig): KashiYupSchemas;
|
|
58
|
+
export declare const nationalId: (opts?: FieldOptions) => KashiYupSchema<string>, legalId: (opts?: FieldOptions) => KashiYupSchema<string>, iranMobile: (opts?: FieldOptions) => KashiYupSchema<string>, landline: (opts?: FieldOptions) => KashiYupSchema<string>, cardNumber: (opts?: FieldOptions) => KashiYupSchema<string>, iban: (opts?: FieldOptions) => KashiYupSchema<string>, postalCode: (opts?: FieldOptions) => KashiYupSchema<string>, sayad: (opts?: FieldOptions) => KashiYupSchema<string>, economicCode: (opts?: FieldOptions) => KashiYupSchema<string>, taxId: (opts?: FieldOptions) => KashiYupSchema<string>, billId: (opts?: FieldOptions) => KashiYupSchema<string>, bill: (opts?: FieldOptions & {
|
|
59
|
+
paymentMessage?: string;
|
|
60
|
+
}) => KashiYupSchema<BillValue>, jalaliDate: <O extends JalaliOutput = "date">(opts?: JalaliDateOptions<O>) => KashiYupSchema<JalaliValue<O>>, time: (opts?: FieldOptions) => KashiYupSchema<string>, price: (opts?: PriceOptions) => KashiYupSchema<number>, plate: (opts?: FieldOptions) => KashiYupSchema<Plate>, persianText: (opts?: PersianTextOptions) => KashiYupSchema<string>;
|
package/dist/yup.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* اسکیماهای آمادهٔ Yup (نسخهٔ ۱) برای دادههای ایرانی.
|
|
3
|
+
*
|
|
4
|
+
* import * as yup from "yup";
|
|
5
|
+
* import { nationalId, iranMobile, jalaliDate } from "@kashi-ui/schemas/yup";
|
|
6
|
+
* const Form = yup.object({ nationalId: nationalId(), mobile: iranMobile(), birth: jalaliDate() });
|
|
7
|
+
*
|
|
8
|
+
* هر اسکیما بهطور پیشفرض اجباری است (با پیام کاشی)؛ برای فیلد اختیاری .optional() بزنید،
|
|
9
|
+
* رشتهٔ خالی خودش undefined حساب میشود.
|
|
10
|
+
*/
|
|
11
|
+
import * as yup from "yup";
|
|
12
|
+
import { resolve, checkString, checkJalali, checkTime, checkPrice, checkText, checkPlate, checkPaymentId, schemaMessages, } from "./core.js";
|
|
13
|
+
export { schemaMessages };
|
|
14
|
+
const blank = (v) => v === null || (typeof v === "string" && v.trim() === "") || v === undefined;
|
|
15
|
+
function fromCheck(check, msgs, requiredMessage) {
|
|
16
|
+
return yup
|
|
17
|
+
.mixed()
|
|
18
|
+
// تبدیل پیش از اعتبارسنجی: خالی ← undefined (تا required/optional کار کند)، معتبر ← مقدار یکدستشده
|
|
19
|
+
.transform((_value, original) => {
|
|
20
|
+
if (blank(original))
|
|
21
|
+
return undefined;
|
|
22
|
+
const r = check(original);
|
|
23
|
+
return r.ok ? r.value : original;
|
|
24
|
+
})
|
|
25
|
+
.test({
|
|
26
|
+
name: "kashi",
|
|
27
|
+
skipAbsent: true,
|
|
28
|
+
test(_value, ctx) {
|
|
29
|
+
// بررسی روی مقدار خام انجام میشود، چون خروجی یکدستشده (مثلاً ISO) ممکن است شکل ورودی را نداشته باشد
|
|
30
|
+
const original = ctx.originalValue;
|
|
31
|
+
const r = check(original);
|
|
32
|
+
return r.ok || ctx.createError({ message: r.message });
|
|
33
|
+
},
|
|
34
|
+
})
|
|
35
|
+
.required(requiredMessage ?? msgs.required);
|
|
36
|
+
}
|
|
37
|
+
/** سازندهها با زبان و پیامهای پیشفرض خودتان: createSchemas({ locale: "en" }) */
|
|
38
|
+
export function createSchemas(config = {}) {
|
|
39
|
+
const str = (key) => (opts) => {
|
|
40
|
+
const msgs = resolve(config, opts);
|
|
41
|
+
return fromCheck((v) => checkString(key, v, msgs, opts?.message), msgs, opts?.requiredMessage);
|
|
42
|
+
};
|
|
43
|
+
return {
|
|
44
|
+
nationalId: str("nationalId"),
|
|
45
|
+
legalId: str("legalId"),
|
|
46
|
+
iranMobile: str("mobile"),
|
|
47
|
+
landline: str("landline"),
|
|
48
|
+
cardNumber: str("card"),
|
|
49
|
+
iban: str("iban"),
|
|
50
|
+
postalCode: str("postal"),
|
|
51
|
+
sayad: str("sayad"),
|
|
52
|
+
economicCode: str("economicCode"),
|
|
53
|
+
taxId: str("taxId"),
|
|
54
|
+
billId: str("billId"),
|
|
55
|
+
bill(opts) {
|
|
56
|
+
const msgs = resolve(config, opts);
|
|
57
|
+
const check = (input) => {
|
|
58
|
+
const o = (input ?? {});
|
|
59
|
+
const b = checkString("billId", o.billId, msgs, opts?.message);
|
|
60
|
+
if (!b.ok)
|
|
61
|
+
return { ...b, field: "billId" };
|
|
62
|
+
const p = checkPaymentId(b.value, o.paymentId, msgs, opts?.paymentMessage);
|
|
63
|
+
return p.ok ? p : { ...p, field: "paymentId" };
|
|
64
|
+
};
|
|
65
|
+
return yup
|
|
66
|
+
.mixed()
|
|
67
|
+
.transform((_v, original) => {
|
|
68
|
+
const r = check(original);
|
|
69
|
+
return r.ok ? r.value : original;
|
|
70
|
+
})
|
|
71
|
+
.test({
|
|
72
|
+
name: "kashi-bill",
|
|
73
|
+
skipAbsent: true,
|
|
74
|
+
test(_v, ctx) {
|
|
75
|
+
const r = check(ctx.originalValue);
|
|
76
|
+
if (r.ok)
|
|
77
|
+
return true;
|
|
78
|
+
return ctx.createError({ message: r.message, path: ctx.path ? `${ctx.path}.${r.field}` : r.field });
|
|
79
|
+
},
|
|
80
|
+
})
|
|
81
|
+
.required(opts?.requiredMessage ?? msgs.required);
|
|
82
|
+
},
|
|
83
|
+
jalaliDate(opts) {
|
|
84
|
+
const msgs = resolve(config, opts);
|
|
85
|
+
return fromCheck((v) => checkJalali(v, opts?.output ?? "date", msgs, opts?.message), msgs, opts?.requiredMessage);
|
|
86
|
+
},
|
|
87
|
+
time(opts) {
|
|
88
|
+
const msgs = resolve(config, opts);
|
|
89
|
+
return fromCheck((v) => checkTime(v, msgs, opts?.message), msgs, opts?.requiredMessage);
|
|
90
|
+
},
|
|
91
|
+
price(opts) {
|
|
92
|
+
const msgs = resolve(config, opts);
|
|
93
|
+
const o = { ...opts, locale: opts?.locale ?? config.locale };
|
|
94
|
+
return fromCheck((v) => checkPrice(v, o, msgs), msgs, opts?.requiredMessage);
|
|
95
|
+
},
|
|
96
|
+
plate(opts) {
|
|
97
|
+
const msgs = resolve(config, opts);
|
|
98
|
+
return fromCheck((v) => checkPlate(v, msgs, opts?.message), msgs, opts?.requiredMessage);
|
|
99
|
+
},
|
|
100
|
+
persianText(opts) {
|
|
101
|
+
const msgs = resolve(config, opts);
|
|
102
|
+
const o = { ...opts, locale: opts?.locale ?? config.locale };
|
|
103
|
+
return fromCheck((v) => checkText(v, o, msgs), msgs, opts?.requiredMessage);
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
const fa = createSchemas();
|
|
108
|
+
export const { nationalId, legalId, iranMobile, landline, cardNumber, iban, postalCode, sayad, economicCode, taxId, billId, bill, jalaliDate, time, price, plate, persianText, } = fa;
|
package/dist/zod.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* اسکیماهای آمادهٔ Zod برای دادههای ایرانی. با Zod 4 و Zod 3 (از 3.23) کار میکند.
|
|
3
|
+
*
|
|
4
|
+
* import { z } from "zod";
|
|
5
|
+
* import { nationalId, iranMobile, jalaliDate } from "@kashi-ui/schemas/zod";
|
|
6
|
+
* const Form = z.object({ nationalId: nationalId(), mobile: iranMobile(), birth: jalaliDate() });
|
|
7
|
+
*
|
|
8
|
+
* هر اسکیما ورودی با ارقام فارسی/عربی، فاصله و خط تیره را میپذیرد و مقدار یکدستشده برمیگرداند.
|
|
9
|
+
*/
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
import { schemaMessages } from "./core.js";
|
|
12
|
+
import type { FieldOptions, SchemaConfig, JalaliOutput, JalaliValue, JalaliDateOptions, PriceOptions, PersianTextOptions, BillValue, Plate } from "./core.js";
|
|
13
|
+
export { schemaMessages };
|
|
14
|
+
export type { SchemaMessages, SchemaConfig, FieldOptions, JalaliOutput, JalaliValue, JalaliDateOptions, PriceOptions, PersianTextOptions, BillValue, Plate } from "./core.js";
|
|
15
|
+
/** همهٔ سازندهها؛ از createSchemas یا export های پیشفرض (فارسی) بگیرید */
|
|
16
|
+
export interface KashiZodSchemas {
|
|
17
|
+
/** کد ملی ۱۰ رقمی → «0013542419» */
|
|
18
|
+
nationalId(opts?: FieldOptions): z.ZodType<string>;
|
|
19
|
+
/** شناسهٔ ملی اشخاص حقوقی، ۱۱ رقم */
|
|
20
|
+
legalId(opts?: FieldOptions): z.ZodType<string>;
|
|
21
|
+
/** موبایل ایران؛ +98، 0098، 98 و 9… پذیرفته و همیشه «09…» برمیگردد */
|
|
22
|
+
iranMobile(opts?: FieldOptions): z.ZodType<string>;
|
|
23
|
+
/** تلفن ثابت با پیششمارهٔ استان → «021…» */
|
|
24
|
+
landline(opts?: FieldOptions): z.ZodType<string>;
|
|
25
|
+
/** شماره کارت ۱۶ رقمی (Luhn) → فقط ارقام */
|
|
26
|
+
cardNumber(opts?: FieldOptions): z.ZodType<string>;
|
|
27
|
+
/** شبا → «IR» + ۲۴ رقم */
|
|
28
|
+
iban(opts?: FieldOptions): z.ZodType<string>;
|
|
29
|
+
/** کد پستی ۱۰ رقمی → بدون خط تیره */
|
|
30
|
+
postalCode(opts?: FieldOptions): z.ZodType<string>;
|
|
31
|
+
/** شناسهٔ صیادی چک، ۱۶ رقم */
|
|
32
|
+
sayad(opts?: FieldOptions): z.ZodType<string>;
|
|
33
|
+
/** کد اقتصادی (۱۰، ۱۱ یا ۱۲ رقم) */
|
|
34
|
+
economicCode(opts?: FieldOptions): z.ZodType<string>;
|
|
35
|
+
/** شمارهٔ مالیاتی ۲۲ نویسهای فاکتور → حروف بزرگ لاتین */
|
|
36
|
+
taxId(opts?: FieldOptions): z.ZodType<string>;
|
|
37
|
+
/** شناسهٔ قبض (بدون صفرهای ابتدایی) */
|
|
38
|
+
billId(opts?: FieldOptions): z.ZodType<string>;
|
|
39
|
+
/** شیء { billId, paymentId }؛ شناسهٔ پرداخت با شناسهٔ قبض خودش سنجیده میشود و نوع، مبلغ و دوره هم برمیگردد */
|
|
40
|
+
bill(opts?: FieldOptions & {
|
|
41
|
+
paymentMessage?: string;
|
|
42
|
+
}): z.ZodType<BillValue>;
|
|
43
|
+
/** تاریخ شمسی «۱۴۰۳/۵/۱۲» یا Date → Date (یا رشتهٔ ISO / شمسی با output) */
|
|
44
|
+
jalaliDate<O extends JalaliOutput = "date">(opts?: JalaliDateOptions<O>): z.ZodType<JalaliValue<O>>;
|
|
45
|
+
/** ساعت «۹:۳۰» یا «0930» → «09:30» */
|
|
46
|
+
time(opts?: FieldOptions): z.ZodType<string>;
|
|
47
|
+
/** مبلغ «۱٬۲۵۰٬۰۰۰» یا 1250000 → عدد، با min و max و پیامهای مبلغ */
|
|
48
|
+
price(opts?: PriceOptions): z.ZodType<number>;
|
|
49
|
+
/** پلاک { first, letter, middle, region } با ارقام یکدستشده */
|
|
50
|
+
plate(opts?: FieldOptions): z.ZodType<Plate>;
|
|
51
|
+
/** متن فارسی یکدست (ی و ک عربی، اعراب، کشیده و فاصلههای اضافه) با min و max نویسه */
|
|
52
|
+
persianText(opts?: PersianTextOptions): z.ZodType<string>;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* سازندهها با زبان و پیامهای پیشفرض خودتان:
|
|
56
|
+
* const s = createSchemas({ locale: "en", messages: { required: "Required" } });
|
|
57
|
+
* z.object({ id: s.nationalId() })
|
|
58
|
+
*/
|
|
59
|
+
export declare function createSchemas(config?: SchemaConfig): KashiZodSchemas;
|
|
60
|
+
export declare const nationalId: (opts?: FieldOptions) => z.ZodType<string>, legalId: (opts?: FieldOptions) => z.ZodType<string>, iranMobile: (opts?: FieldOptions) => z.ZodType<string>, landline: (opts?: FieldOptions) => z.ZodType<string>, cardNumber: (opts?: FieldOptions) => z.ZodType<string>, iban: (opts?: FieldOptions) => z.ZodType<string>, postalCode: (opts?: FieldOptions) => z.ZodType<string>, sayad: (opts?: FieldOptions) => z.ZodType<string>, economicCode: (opts?: FieldOptions) => z.ZodType<string>, taxId: (opts?: FieldOptions) => z.ZodType<string>, billId: (opts?: FieldOptions) => z.ZodType<string>, bill: (opts?: FieldOptions & {
|
|
61
|
+
paymentMessage?: string;
|
|
62
|
+
}) => z.ZodType<BillValue>, jalaliDate: <O extends JalaliOutput = "date">(opts?: JalaliDateOptions<O>) => z.ZodType<JalaliValue<O>>, time: (opts?: FieldOptions) => z.ZodType<string>, price: (opts?: PriceOptions) => z.ZodType<number>, plate: (opts?: FieldOptions) => z.ZodType<Plate>, persianText: (opts?: PersianTextOptions) => z.ZodType<string>;
|
|
63
|
+
/**
|
|
64
|
+
* فیلد اختیاری فرم: رشتهٔ خالی (که فرمها بهجای undefined میفرستند) undefined میشود و بقیه با همان اسکیما سنجیده میشود.
|
|
65
|
+
* z.object({ landline: optional(landline()) })
|
|
66
|
+
*/
|
|
67
|
+
export declare function optional<T>(schema: z.ZodType<T>): z.ZodType<T | undefined>;
|
package/dist/zod.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* اسکیماهای آمادهٔ Zod برای دادههای ایرانی. با Zod 4 و Zod 3 (از 3.23) کار میکند.
|
|
3
|
+
*
|
|
4
|
+
* import { z } from "zod";
|
|
5
|
+
* import { nationalId, iranMobile, jalaliDate } from "@kashi-ui/schemas/zod";
|
|
6
|
+
* const Form = z.object({ nationalId: nationalId(), mobile: iranMobile(), birth: jalaliDate() });
|
|
7
|
+
*
|
|
8
|
+
* هر اسکیما ورودی با ارقام فارسی/عربی، فاصله و خط تیره را میپذیرد و مقدار یکدستشده برمیگرداند.
|
|
9
|
+
*/
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
import { resolve, checkString, checkJalali, checkTime, checkPrice, checkText, checkPlate, checkPaymentId, schemaMessages, } from "./core.js";
|
|
12
|
+
export { schemaMessages };
|
|
13
|
+
/** یک بررسی هسته را به اسکیمای Zod تبدیل میکند: خطا با پیام کاشی، موفقیت با مقدار یکدستشده */
|
|
14
|
+
function fromCheck(check) {
|
|
15
|
+
return z.unknown().transform((input, ctx) => {
|
|
16
|
+
const r = check(input);
|
|
17
|
+
if (r.ok)
|
|
18
|
+
return r.value;
|
|
19
|
+
ctx.addIssue({ code: "custom", message: r.message });
|
|
20
|
+
return z.NEVER;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* سازندهها با زبان و پیامهای پیشفرض خودتان:
|
|
25
|
+
* const s = createSchemas({ locale: "en", messages: { required: "Required" } });
|
|
26
|
+
* z.object({ id: s.nationalId() })
|
|
27
|
+
*/
|
|
28
|
+
export function createSchemas(config = {}) {
|
|
29
|
+
const str = (key) => (opts) => {
|
|
30
|
+
const msgs = resolve(config, opts);
|
|
31
|
+
return fromCheck((v) => checkString(key, v, msgs, opts?.message));
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
nationalId: str("nationalId"),
|
|
35
|
+
legalId: str("legalId"),
|
|
36
|
+
iranMobile: str("mobile"),
|
|
37
|
+
landline: str("landline"),
|
|
38
|
+
cardNumber: str("card"),
|
|
39
|
+
iban: str("iban"),
|
|
40
|
+
postalCode: str("postal"),
|
|
41
|
+
sayad: str("sayad"),
|
|
42
|
+
economicCode: str("economicCode"),
|
|
43
|
+
taxId: str("taxId"),
|
|
44
|
+
billId: str("billId"),
|
|
45
|
+
bill(opts) {
|
|
46
|
+
const msgs = resolve(config, opts);
|
|
47
|
+
const billSchema = fromCheck((v) => checkString("billId", v, msgs, opts?.message));
|
|
48
|
+
return z.object({ billId: billSchema, paymentId: z.unknown() }).transform((v, ctx) => {
|
|
49
|
+
const r = checkPaymentId(v.billId, v.paymentId, msgs, opts?.paymentMessage);
|
|
50
|
+
if (r.ok)
|
|
51
|
+
return r.value;
|
|
52
|
+
ctx.addIssue({ code: "custom", message: r.message, path: ["paymentId"] });
|
|
53
|
+
return z.NEVER;
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
jalaliDate(opts) {
|
|
57
|
+
const msgs = resolve(config, opts);
|
|
58
|
+
return fromCheck((v) => checkJalali(v, opts?.output ?? "date", msgs, opts?.message));
|
|
59
|
+
},
|
|
60
|
+
time(opts) {
|
|
61
|
+
const msgs = resolve(config, opts);
|
|
62
|
+
return fromCheck((v) => checkTime(v, msgs, opts?.message));
|
|
63
|
+
},
|
|
64
|
+
price(opts) {
|
|
65
|
+
const msgs = resolve(config, opts);
|
|
66
|
+
const o = { ...opts, locale: opts?.locale ?? config.locale };
|
|
67
|
+
return fromCheck((v) => checkPrice(v, o, msgs));
|
|
68
|
+
},
|
|
69
|
+
plate(opts) {
|
|
70
|
+
const msgs = resolve(config, opts);
|
|
71
|
+
return fromCheck((v) => checkPlate(v, msgs, opts?.message));
|
|
72
|
+
},
|
|
73
|
+
persianText(opts) {
|
|
74
|
+
const msgs = resolve(config, opts);
|
|
75
|
+
const o = { ...opts, locale: opts?.locale ?? config.locale };
|
|
76
|
+
return fromCheck((v) => checkText(v, o, msgs));
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
const fa = createSchemas();
|
|
81
|
+
export const { nationalId, legalId, iranMobile, landline, cardNumber, iban, postalCode, sayad, economicCode, taxId, billId, bill, jalaliDate, time, price, plate, persianText, } = fa;
|
|
82
|
+
/**
|
|
83
|
+
* فیلد اختیاری فرم: رشتهٔ خالی (که فرمها بهجای undefined میفرستند) undefined میشود و بقیه با همان اسکیما سنجیده میشود.
|
|
84
|
+
* z.object({ landline: optional(landline()) })
|
|
85
|
+
*/
|
|
86
|
+
export function optional(schema) {
|
|
87
|
+
return z.preprocess((v) => (v === null || (typeof v === "string" && v.trim() === "") ? undefined : v), schema.optional());
|
|
88
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kashi-ui/schemas",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "2.28.0",
|
|
4
|
+
"description": "اسکیماهای آمادهٔ Zod و Yup کاشی: کد ملی، موبایل، شماره کارت، شبا، کد پستی، تاریخ شمسی، مبلغ، قبض و پلاک با پیام خطای فارسی و مقدار یکدستشده",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": { "types": "./dist/index.d.ts", "default": "./dist/index.js" },
|
|
10
|
+
"./zod": { "types": "./dist/zod.d.ts", "default": "./dist/zod.js" },
|
|
11
|
+
"./yup": { "types": "./dist/yup.d.ts", "default": "./dist/yup.js" },
|
|
12
|
+
"./package.json": "./package.json"
|
|
13
|
+
},
|
|
14
|
+
"files": ["dist", "README.md"],
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsc -p tsconfig.json",
|
|
18
|
+
"test": "tsc -p test/tsconfig.json && tsc -p test/tsconfig.zod3.json && node --test test/*.test.mjs"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@kashi-ui/persian": "^2.28.0"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"yup": "^1.0.0",
|
|
25
|
+
"zod": "^3.23.0 || ^4.0.0"
|
|
26
|
+
},
|
|
27
|
+
"peerDependenciesMeta": {
|
|
28
|
+
"yup": { "optional": true },
|
|
29
|
+
"zod": { "optional": true }
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"typescript": "^5.6.0",
|
|
33
|
+
"yup": "^1.7.1",
|
|
34
|
+
"zod": "^4.1.0",
|
|
35
|
+
"zod-v3": "npm:zod@^3.25.76"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": { "access": "public" },
|
|
38
|
+
"keywords": ["zod", "yup", "schema", "validation", "persian", "farsi", "iran", "national-id", "jalali", "iban", "kashi"],
|
|
39
|
+
"repository": { "type": "git", "url": "git+https://github.com/Sepantapouya/Kashi.git", "directory": "packages/schemas" },
|
|
40
|
+
"homepage": "https://kashi.sepantapouya.com/docs/schemas/",
|
|
41
|
+
"bugs": "https://github.com/Sepantapouya/Kashi/issues",
|
|
42
|
+
"author": "Sepanta Pouya (https://sepantapouya.com)",
|
|
43
|
+
"license": "MIT"
|
|
44
|
+
}
|