tianji/src/client/utils/validator.ts
2024-01-24 13:26:42 +00:00

37 lines
935 B
TypeScript

import { RuleObject } from 'antd/es/form';
import { z } from 'zod';
import { hostnameRegex, slugRegex } from '@tianji/shared';
type Validator = (
rule: RuleObject,
value: any,
callback: (error?: string) => void
) => Promise<void | any> | void;
export const hostnameValidator: Validator = (rule, value, callback) => {
try {
z.union([z.string().ip(), z.string().regex(hostnameRegex)]).parse(value);
callback();
} catch (err) {
callback('Not valid host, it should be ip or hostname');
}
};
export const urlSlugValidator: Validator = (rule, value, callback) => {
try {
z.string().regex(slugRegex).parse(value);
callback();
} catch (err) {
callback('Not valid slug');
}
};
export const portValidator: Validator = (rule, value, callback) => {
try {
z.number().min(1).max(65535).parse(value);
callback();
} catch (err) {
callback('Not valid port, it should be 1 ~ 65535');
}
};