我有以下幾乎相似的規則,除了一個路由引數是可選的,另一個是強制性的。需要將它們組合起來,以便我可以將單個代碼交替用于強制性條件和其他可選條件
有沒有辦法將它們組合起來,這樣我就沒有多余的代碼?
const pathParamValidation = check('convertedUrlId')
.isLength({ min: 5, max: 6 })
.withMessage({
error: 'Invalid length',
detail: {
convertedUrlId:
'Invalid Length! Character length >=5 and <7 characters are allowed',
},
})
.matches(/^[~A-Za-z0-9/./_/-]*$/)
.withMessage({
error: 'Invalid characters',
detail: {
convertedUrlId:
'Invalid characters! Only [A-Z],[a-z],[0-9], _ , - , . , ~ are allowed',
},
});
#選修的
const optionalConvertedUrlIdValidation = check('convertedUrlId')
.optional()
.matches(/^[~A-Za-z0-9/./_/-]*$/)
.withMessage({
error: 'Invalid characters',
detail: {
convertedUrlId:
'Invalid characters! Only [A-Z],[a-z],[0-9], _ , - , . , ~ are allowed',
},
})
.isLength({ min: 5, max: 6 })
.withMessage({
error: 'Invalid length',
detail: {
convertedUrlId:
'Invalid Length! Character length >=5 and <7 characters are allowed',
},
});
我嘗試以這種方式組合,但沒有運氣
const checkConvertedUrlSchema = check('convertedUrlId')
.matches(/^[~A-Za-z0-9/./_/-]*$/)
.withMessage({
error: 'Invalid characters',
detail: {
convertedUrlId:
'Invalid characters! Only [A-Z],[a-z],[0-9], _ , - , . , ~ are allowed',
},
});
const checkConvertedUrlLength = check('convertedUrlId')
.isLength({ min: 5, max: 6 })
.withMessage({
error: 'Invalid length',
detail: {
convertedUrlId:
'Invalid Length! Character length >=5 and <7 characters are allowed',
},
});
const convertedUrlIdValidation =
check('convertedUrlId').checkConvertedUrlLength.checkConvertedUrlSchema;
const optionalConvertedUrlIdValidation =
check('convertedUrlId').optional().checkConvertedUrlSchema
.checkConvertedUrlLength;
uj5u.com熱心網友回復:
您可以創建驗證器工廠來創建通用驗證規則。
import { body, validationResult } from 'express-validator';
import express from 'express';
const app = express();
const convertedUrlIdValidationFactory = () =>
body('convertedUrlId')
.isLength({ min: 5, max: 6 })
.withMessage({
error: 'Invalid length',
detail: {
convertedUrlId: 'Invalid Length! Character length >=5 and <7 characters are allowed',
},
})
.matches(/^[~A-Za-z0-9/./_/-]*$/)
.withMessage({
error: 'Invalid characters',
detail: {
convertedUrlId: 'Invalid characters! Only [A-Z],[a-z],[0-9], _ , - , . , ~ are allowed',
},
});
app.use(express.json());
app.post('/optional', convertedUrlIdValidationFactory().optional(), (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
console.log(req.body);
res.sendStatus(200);
});
app.post('/required', convertedUrlIdValidationFactory(), (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
console.log(req.body);
res.sendStatus(200);
});
app.listen(3000, () => console.log('server started at port 3000'));
測驗案例1:沒有convertedUrlId資料。
? curl -X POST http://localhost:3000/required
{"errors":[{"msg":{"error":"Invalid length","detail":{"convertedUrlId":"Invalid Length! Character length >=5 and <7 characters are allowed"}},"param":"convertedUrlId","location":"body"}]}%
? curl -X POST http://localhost:3000/optional
OK%
測驗用例2:有convertedUrlId資料。
? curl -X POST -H "Content-Type: application/json" --data '{"convertedUrlId": "12345"}' http://localhost:3000/required
OK%
? curl -X POST -H "Content-Type: application/json" --data '{"convertedUrlId": "12345"}' http://localhost:3000/optional
OK%
uj5u.com熱心網友回復:
這對我有用
const checkConvertedUrlSchema = (chain) =>
chain.matches(/^[~A-Za-z0-9/./_/-]*$/).withMessage({
error: 'Invalid characters',
detail: {
convertedUrlId:
'Invalid characters! Only [A-Z],[a-z],[0-9], _ , - , . , ~ are allowed',
},
});
const checkConvertedUrlLength = (chain) =>
chain.isLength({ min: 5, max: 6 }).withMessage({
error: 'Invalid length',
detail: {
convertedUrlId:
'Invalid Length! Character length >=5 and <7 characters are allowed',
},
});
const checkConvertedUrlIdBodyValidation = (chain) =>
chain
.not()
.isEmpty()
.withMessage({
error: 'Invalid Request Body',
detail: {
convertedUrlId:
'Request Syntax Error! convertedUrlId parameter is required',
},
});
const convertedUrlIdBodyValidation = checkConvertedUrlLength(
checkConvertedUrlSchema(
checkConvertedUrlIdBodyValidation(check('convertedUrlId'))
)
);
const convertedUrlIdValidation = checkConvertedUrlLength(
checkConvertedUrlSchema(check('convertedUrlId'))
);
const optionalConvertedUrlIdValidation = convertedUrlIdValidation.optional();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/390892.html
標籤:javascript 节点.js 表达 验证 快速验证器
