mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-06-06 05:04:22 +00:00
Apply the schema + safeParse-on-submit pattern (introduced for
ClientFormModal / ClientBulkAddModal) to five more forms:
- ClientBulkAdjustModal: ClientBulkAdjustFormSchema enforces 'at least
one of addDays / addGB is non-zero' via .refine(), replacing the
ad-hoc days+gb check.
- BalancerFormModal: BalancerFormSchema covers tag and selector
required-ness; the duplicate-tag check stays inline since it needs
the otherTags prop. Per-field validateStatus now reads from the
parsed issues map.
- RuleFormModal: RuleFormSchema captures the form shape (no required
fields - every property is optional by design). safeParse short-
circuits if anything is structurally wrong.
- CustomGeoFormModal: CustomGeoFormSchema folds the regex alias rule
and the http(s) URL validation (including URL parse) into the
schema, replacing a 20-line validate() function.
- TwoFactorModal: TotpCodeSchema (z.string().regex(/^\d{6}$/)) drives
both the disabled-state of the OK button and the safeParse gate
before the TOTP comparison.
Schemas live alongside the matching API schemas:
- ClientBulkAdjustFormSchema in schemas/client.ts
- BalancerFormSchema / RuleFormSchema / CustomGeoFormSchema in schemas/xray.ts
- TotpCodeSchema in schemas/login.ts (next to LoginFormSchema)
No UX change for valid inputs.
142 lines
4.5 KiB
TypeScript
142 lines
4.5 KiB
TypeScript
import { z } from 'zod';
|
|
|
|
const nullableStringArray = z.array(z.string()).nullable().transform((v) => v ?? []);
|
|
const nullableNumberArray = z.array(z.number()).nullable().transform((v) => v ?? []);
|
|
|
|
export const ClientTrafficSchema = z.object({
|
|
up: z.number().optional(),
|
|
down: z.number().optional(),
|
|
total: z.number().optional(),
|
|
expiryTime: z.number().optional(),
|
|
enable: z.boolean().optional(),
|
|
lastOnline: z.number().optional(),
|
|
});
|
|
|
|
export const ClientRecordSchema = z.object({
|
|
id: z.number().optional(),
|
|
email: z.string(),
|
|
subId: z.string().optional(),
|
|
uuid: z.string().optional(),
|
|
password: z.string().optional(),
|
|
auth: z.string().optional(),
|
|
flow: z.string().optional(),
|
|
security: z.string().optional(),
|
|
totalGB: z.number().optional(),
|
|
expiryTime: z.number().optional(),
|
|
limitIp: z.number().optional(),
|
|
tgId: z.union([z.number(), z.string()]).optional(),
|
|
comment: z.string().optional(),
|
|
enable: z.boolean().optional(),
|
|
reset: z.number().optional(),
|
|
inboundIds: nullableNumberArray.optional(),
|
|
traffic: ClientTrafficSchema.nullable().optional(),
|
|
reverse: z.object({ tag: z.string().optional() }).loose().nullable().optional(),
|
|
createdAt: z.number().optional(),
|
|
updatedAt: z.number().optional(),
|
|
}).loose();
|
|
|
|
export const InboundOptionSchema = z.object({
|
|
id: z.number(),
|
|
remark: z.string().optional(),
|
|
protocol: z.string().optional(),
|
|
port: z.number().optional(),
|
|
tlsFlowCapable: z.boolean().optional(),
|
|
}).loose();
|
|
|
|
export const InboundOptionsSchema = z.array(InboundOptionSchema);
|
|
|
|
export const ClientsSummarySchema = z.object({
|
|
total: z.number(),
|
|
active: z.number(),
|
|
online: nullableStringArray,
|
|
depleted: nullableStringArray,
|
|
expiring: nullableStringArray,
|
|
deactive: nullableStringArray,
|
|
});
|
|
|
|
const nullableClientArray = z.array(ClientRecordSchema).nullable().transform((v) => v ?? []);
|
|
|
|
export const ClientPageResponseSchema = z.object({
|
|
items: nullableClientArray,
|
|
total: z.number(),
|
|
filtered: z.number(),
|
|
page: z.number(),
|
|
pageSize: z.number(),
|
|
summary: ClientsSummarySchema.nullable().optional(),
|
|
});
|
|
|
|
export const ClientHydrateSchema = z.object({
|
|
client: ClientRecordSchema,
|
|
inboundIds: nullableNumberArray,
|
|
});
|
|
|
|
export const BulkAdjustResultSchema = z.object({
|
|
adjusted: z.number(),
|
|
skipped: z
|
|
.array(z.object({ email: z.string(), reason: z.string() }))
|
|
.optional(),
|
|
});
|
|
|
|
export const DelDepletedResultSchema = z.object({
|
|
deleted: z.number().optional(),
|
|
});
|
|
|
|
export const OnlinesSchema = nullableStringArray;
|
|
|
|
export const ClientFormSchema = z.object({
|
|
email: z.string().trim().min(1, 'pages.clients.email'),
|
|
subId: z.string(),
|
|
uuid: z.string(),
|
|
password: z.string(),
|
|
auth: z.string(),
|
|
flow: z.string(),
|
|
reverseTag: z.string(),
|
|
totalGB: z.number().min(0),
|
|
delayedStart: z.boolean(),
|
|
delayedDays: z.number().int().min(0),
|
|
limitIp: z.number().int().min(0),
|
|
tgId: z.number().int().min(0),
|
|
comment: z.string(),
|
|
enable: z.boolean(),
|
|
inboundIds: z.array(z.number()),
|
|
});
|
|
|
|
export const ClientCreateFormSchema = ClientFormSchema.extend({
|
|
inboundIds: z.array(z.number()).min(1, 'pages.clients.selectInbound'),
|
|
});
|
|
|
|
export const ClientBulkAdjustFormSchema = z
|
|
.object({
|
|
addDays: z.number().int(),
|
|
addGB: z.number(),
|
|
})
|
|
.refine((v) => v.addDays !== 0 || v.addGB !== 0, {
|
|
message: 'pages.clients.bulkAdjustNothing',
|
|
});
|
|
|
|
export const ClientBulkAddFormSchema = z.object({
|
|
emailMethod: z.number().int().min(0).max(4),
|
|
firstNum: z.number().int().min(1),
|
|
lastNum: z.number().int().min(1),
|
|
emailPrefix: z.string(),
|
|
emailPostfix: z.string(),
|
|
quantity: z.number().int().min(1).max(100),
|
|
subId: z.string(),
|
|
comment: z.string(),
|
|
flow: z.string(),
|
|
limitIp: z.number().int().min(0),
|
|
totalGB: z.number().min(0),
|
|
expiryTime: z.number(),
|
|
inboundIds: z.array(z.number()).min(1, 'pages.clients.selectInbound'),
|
|
});
|
|
|
|
export type ClientRecord = z.infer<typeof ClientRecordSchema>;
|
|
export type ClientTraffic = z.infer<typeof ClientTrafficSchema>;
|
|
export type InboundOption = z.infer<typeof InboundOptionSchema>;
|
|
export type ClientsSummary = z.infer<typeof ClientsSummarySchema>;
|
|
export type ClientPageResponse = z.infer<typeof ClientPageResponseSchema>;
|
|
export type ClientHydrate = z.infer<typeof ClientHydrateSchema>;
|
|
export type BulkAdjustResult = z.infer<typeof BulkAdjustResultSchema>;
|
|
export type ClientBulkAddFormValues = z.infer<typeof ClientBulkAddFormSchema>;
|
|
export type ClientBulkAdjustFormValues = z.infer<typeof ClientBulkAdjustFormSchema>;
|
|
export type ClientFormValues = z.infer<typeof ClientFormSchema>;
|