42 lines
2.6 KiB
TypeScript
42 lines
2.6 KiB
TypeScript
import { t } from 'i18next'
|
|
import { ZodString, z, ZodNumber } from 'zod'
|
|
|
|
export const hostname = (string?: ZodString) =>
|
|
(string ?? z.string()).regex(
|
|
/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/,
|
|
{
|
|
message: t('errors.invalid_string.hostname', {
|
|
ns: 'zod',
|
|
validation: t('hostname', { ns: 'strings' }).toLowerCase(),
|
|
})!,
|
|
}
|
|
)
|
|
|
|
export const englishKeyboardCharacters = (string?: ZodString) =>
|
|
(string ?? z.string()).regex(/^[A-Za-z0-9!@#$%^&*()_+\-=[\]{}|;':",.\/<>?\\ ]*$/, {
|
|
message: t('errors.invalid_string.english_keyboard_characters', {
|
|
ns: 'zod',
|
|
validation: t('english_keyboard_characters', { ns: 'strings' }).toLowerCase(),
|
|
})!,
|
|
})
|
|
|
|
export const password = (string?: ZodString) =>
|
|
(string ?? z.string()).regex(/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,})/, {
|
|
message: t('errors.invalid_string.password', {
|
|
ns: 'zod',
|
|
})!,
|
|
})
|
|
|
|
export const ipAddress = (string?: ZodString) =>
|
|
(string ?? z.string()).regex(
|
|
/((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))/,
|
|
{
|
|
message: t('errors.invalid_string.ip_address', {
|
|
ns: 'zod',
|
|
})!,
|
|
}
|
|
)
|
|
|
|
export const port = (number?: ZodNumber) => (number ?? z.number()).int().min(1).max(65535)
|
|
|
|
export const vmid = (number?: ZodNumber) => (number ?? z.number()).int().min(100).max(999999999)
|