Skip to content

Commit 5128978

Browse files
committed
refactor: uses built-in express validator functions instead of custom
1 parent b62793a commit 5128978

1 file changed

Lines changed: 25 additions & 44 deletions

File tree

src/routes/admin/proxy/proxyValidator.ts

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,67 +5,48 @@
55

66
import { check } from 'express-validator'
77

8-
const ipv4 =
9-
'^([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$'
10-
const ipv6 =
11-
'^((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4}))*::((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4}))*|((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4})){7}$'
12-
const fqdn = '^(?=.{1,254}$)((?=[a-z0-9-]{1,63}\\.)(xn--+)?[a-z0-9]+(-[a-z0-9]+)*\\.)+[a-z]{2,63}$'
13-
148
export const proxyValidator = (): any => [
15-
check('accessInfo')
16-
.not()
17-
.isEmpty()
18-
.withMessage('Server address is required')
19-
.custom((value, { req }) => {
20-
if (accessInfoFormatValidator(value, req)) {
21-
return true
22-
}
23-
return false
24-
}),
9+
// Validate and normalize infoFormat first so conditional checks can rely on a number
2510
check('infoFormat')
2611
.not()
2712
.isEmpty()
2813
.withMessage('Server address format is required')
14+
.isInt()
15+
.toInt()
2916
.isIn([
3017
3,
3118
4,
3219
201
3320
])
3421
.withMessage('Server address format should be either 3(IPV4), 4(IPV6) or 201(FQDN)'),
22+
23+
// accessInfo presence
24+
check('accessInfo').not().isEmpty().withMessage('Server address is required'),
25+
26+
// accessInfo format based on infoFormat
27+
check('accessInfo')
28+
.if((_, { req }) => req.body.infoFormat === 3)
29+
.isIP(4)
30+
.withMessage('infoFormat 3 requires IPV4 server address'),
31+
check('accessInfo')
32+
.if((_, { req }) => req.body.infoFormat === 4)
33+
.isIP(6)
34+
.withMessage('infoFormat 4 requires IPV6 server address'),
35+
check('accessInfo')
36+
.if((_, { req }) => req.body.infoFormat === 201)
37+
.isFQDN({ require_tld: true, allow_underscores: false, allow_numeric_tld: false })
38+
.withMessage('infoFormat 201 requires FQDN server address'),
39+
3540
check('port')
36-
.not()
37-
.isEmpty()
38-
.withMessage('Port is required')
39-
.isInt({ min: 0, max: 65535 })
40-
.withMessage('Port value should range between 0 and 65535'),
41+
.exists()
42+
.isPort()
43+
.withMessage('Port value should range between 1 and 65535'),
4144
check('networkDnsSuffix')
4245
.not()
4346
.isEmpty()
4447
.withMessage('Domain name of the network is required')
45-
.matches('^(?=.{1,192}$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z]{2,})+$')
48+
.isFQDN({ require_tld: true, allow_underscores: false })
4649
.withMessage('Domain name of the network should contain alphanumeric and hyphens in the middle')
4750
.isLength({ max: 192 })
4851
.withMessage('Domain name of the network maximum length is 192')
4952
]
50-
51-
function accessInfoFormatValidator(value, req): boolean {
52-
if (req.body.infoFormat == null) {
53-
throw new Error('accessInfo is required')
54-
}
55-
if (value != null) {
56-
if (req.body.infoFormat === 3) {
57-
if (!value.match(ipv4)) {
58-
throw new Error('infoFormat 3 requires IPV4 server address')
59-
}
60-
} else if (req.body.infoFormat === 4) {
61-
if (!value.match(ipv6)) {
62-
throw new Error('infoFormat 4 requires IPV6 server address')
63-
}
64-
} else if (req.body.infoFormat === 201) {
65-
if (!value.match(fqdn)) {
66-
throw new Error('infoFormat 201 requires FQDN server address')
67-
}
68-
}
69-
}
70-
return true
71-
}

0 commit comments

Comments
 (0)