feat(utils): 优化validatorEndTime和validatorTimeGTCurrentDay,新增validatorTimeGECurrentDay

master
LiuJiaNan 2026-04-15 16:13:58 +08:00
parent 392631f9e6
commit 85cb7bf0de
2 changed files with 40 additions and 5 deletions

View File

@ -350,12 +350,19 @@ export function validatorEndTime(options: { startTime: string; message?: string;
};
/**
*
*
*/
export function validatorTimeGTCurrentDay(options?: { message?: string; type?: "date" | "datetime" }): {
validator: (_: any, value: any) => Promise<void | string>;
};
/**
*
*/
export function validatorTimeGECurrentDay(options?: { message?: string; type?: "date" | "datetime" }): {
validator: (_: any, value: any) => Promise<void | string>;
};
/**
* js
*/

View File

@ -487,7 +487,7 @@ export function processTreeDataForOnlyLastLevel(options) {
* 验证结束时间是否大于开始时间
*/
export function validatorEndTime(options) {
let { startTime, message = "结束时间不能早于开始时间", type = "" } = options;
let { startTime, message = "结束时间不能早于开始时间", type = "" } = options || {};
return {
validator: (_, value) => {
@ -514,17 +514,45 @@ export function validatorEndTime(options) {
}
/**
* 验证时间是否大于等于当前时间
* 验证时间是否大于当前时间
*/
export function validatorTimeGTCurrentDay(options) {
const { message = "需要大于当前时间", type = "date" } = options;
const { message = "需要大于当前时间", type = "date" } = options || {};
return {
validator: (_, value) => {
if (!value)
return Promise.resolve();
const selectedDate = dayjs.isDayjs(value) ? value : dayjs(value);
if (selectedDate.isBefore(dayjs(), type === "date" ? "day" : "seconds"))
const now = dayjs();
const isAfter = type === "date"
? selectedDate.isAfter(now, "day")
: selectedDate.isAfter(now, "seconds");
if (!isAfter)
return Promise.reject(message);
else
return Promise.resolve();
},
};
}
/**
* 验证时间是否大于等于当前时间
*/
export function validatorTimeGECurrentDay(options) {
const { message = "需要大于等于当前时间", type = "date" } = options || {};
return {
validator: (_, value) => {
if (!value)
return Promise.resolve();
const selectedDate = dayjs.isDayjs(value) ? value : dayjs(value);
const now = dayjs();
const isBefore = type === "date"
? selectedDate.isBefore(now, "day")
: selectedDate.isBefore(now, "seconds");
if (isBefore)
return Promise.reject(message);
else
return Promise.resolve();