Skip to content

Commit

Permalink
fix: alterId shoud accept a number
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Jun 19, 2023
1 parent 0bee669 commit 866b6cf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
36 changes: 24 additions & 12 deletions src/validators/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,54 @@ import { z } from 'zod'

import { NodeTypeEnum } from '../types'

export const PortValidator = z
.union([z.string(), z.number()])
.transform((v, ctx): string | number => {
const port = Number(v)
export const getPositiveIntegersNumberValidatior = (
validator: (n: number) => boolean,
message: string,
) =>
z.union([z.string(), z.number()]).transform((v, ctx): string | number => {
const num = Number(v)
const isInputString = typeof v === 'string'

if (port > 0 && port < 65536 && Number.isInteger(port)) {
return isInputString ? `${port}` : port
if (validator(num) && Number.isInteger(num)) {
return isInputString ? `${num}` : num
} else {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: '端口号必须为 0 ~ 65535 之间的整数',
message,
})

return z.NEVER
}
})

export const IntegersVersionValidator = z
export const PortValidator = z
.union([z.string(), z.number()])
.transform((v, ctx): string | number => {
const version = Number(v)
const port = Number(v)
const isInputString = typeof v === 'string'

if (version > 0 && Number.isInteger(version)) {
return isInputString ? `${version}` : version
if (port > 0 && port < 65536 && Number.isInteger(port)) {
return isInputString ? `${port}` : port
} else {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: '版本号必须为大于 0 的整数',
message: '端口号必须为 0 ~ 65535 之间的整数',
})

return z.NEVER
}
})

export const AlterIdValiator = getPositiveIntegersNumberValidatior(
(n) => n >= 0,
'alterId 必须为大于等于 0 的整数',
)

export const IntegersVersionValidator = getPositiveIntegersNumberValidatior(
(n) => n > 0,
'版本号必须为大于 0 的整数',
)

export const SimpleNodeConfigValidator = z.object({
type: z.nativeEnum(NodeTypeEnum),
nodeName: z.string(),
Expand Down
8 changes: 6 additions & 2 deletions src/validators/vmess.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { z } from 'zod'

import { NodeTypeEnum } from '../types'
import { PortValidator, SimpleNodeConfigValidator } from './common'
import {
PortValidator,
SimpleNodeConfigValidator,
AlterIdValiator,
} from './common'

export const VmessNodeConfigValidator = SimpleNodeConfigValidator.extend({
type: z.literal(NodeTypeEnum.Vmess),
Expand All @@ -14,7 +18,7 @@ export const VmessNodeConfigValidator = SimpleNodeConfigValidator.extend({
z.literal('auto'),
]),
uuid: z.string().uuid(),
alterId: z.string(),
alterId: AlterIdValiator,
network: z.union([z.literal('tcp'), z.literal('ws')]),
tls: z.boolean(),
host: z.ostring(),
Expand Down

0 comments on commit 866b6cf

Please sign in to comment.