我有一个用户模式,如下所示:
const Joi = require("joi");
const message = require("./types/string");
const username = Joi.string()
.regex(/^[A-Za-z0-9]+$/)
.required()
.min(5)
.max(20)
.messages(message);
const email = Joi.string()
.regex(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/)
.required()
.min(5)
.max(255)
.email()
.messages(message);
const password = Joi.string()
.regex(/^[A-Za-z0-9]+$/)
.required()
.min(5)
.max(255)
.messages(message);
const userSchema = Joi.object({
username,
password,
email,
});
exports.userSchema = { route: "/users", object: userSchema };以及我在https://www.digitalocean.com/community/tutorials/how-to-use-joi-for-node-api-schema-validation上找到的类似下面代码的模式验证
const _ = require("lodash");
const { StatusCodes } = require("http-status-codes");
const { Schemas } = require("../joi-schema-validation");
exports.schemaValidator = function (useJoiError = false) {
const _useJoiError = _.isBoolean(useJoiError) && useJoiError;
const _supportedMethods = ["post", "put", "patch"];
const _validationOptions = {
abortEarly: false,
allowUnknown: true,
stripUnknown: true,
};
return (req, res, next) => {
const route = req.route.path;
const method = req.method.toLowerCase();
const schema = Schemas.find(
(schema) => schema.route === route.replace("/:id", "")
);
if (_.includes(_supportedMethods, method) && schema) {
const { object } = schema;
const { error, value } = object.validate(req.body, _validationOptions);
if (error) {
const JoiError = {
status: "failed",
validationErrors: {
details: _.map(error.details, ({ message, context }) => ({
[context.label]: req.t(message.replace(/['"]/g, "")),
})),
},
};
const CustomError = {
status: "failed",
error: "Invalid request data. Please review request and try again.",
};
return res
.status(StatusCodes.BAD_REQUEST)
.json(_useJoiError ? JoiError : CustomError);
} else {
req.body = value;
return next();
}
}
return next();
};
};假设我有两条地点信息,一条是英语的,一条是西班牙语的。如果“接受-语言”标题是"es-es“,我如何切换到locale joi消息?
发布于 2022-03-22 20:16:22
在翻来覆去之后,我设法想出了一个简单的解决方案。在路由处理程序中间件的开头,我有一个"messages“变量,在这里我检查请求头接受语言。如果语言LCID为"en“,则返回英文错误消息或返回默认错误消息,在我的示例中是荷兰语言错误消息。在验证之前,我将返回错误消息传递到我的模式的messages方法中,然后将验证函数链接到它。
更新后的代码现在看起来像下面的代码:
const _ = require("lodash");
const { StatusCodes } = require("http-status-codes");
const { Schemas } = require("../joi-schema-validation");
exports.schemaValidator = function (useJoiError = false) {
const _useJoiError = _.isBoolean(useJoiError) && useJoiError;
const _supportedMethods = ["post", "put", "patch"];
const _validationOptions = {
abortEarly: false,
allowUnknown: true,
stripUnknown: true,
};
return (req, res, next) => {
const messages =
req.headers["accept-language"] === "en"
? require("../joi-schema-validation/types/string")
: require("../joi-schema-validation/types/string-nl");
const route = req.route.path;
const method = req.method.toLowerCase();
const schema = Schemas.find(
(schema) => schema.route === route.replace("/:id", "")
);
if (_.includes(_supportedMethods, method) && schema) {
const { object } = schema;
const { error, value } = object
.messages(messages)
.validate(req.body, _validationOptions);
if (error) {
const JoiError = {
status: "failed",
validationErrors: {
details: _.map(error.details, ({ message, context }) => ({
[context.label]: message.replace(/['"]/g, ""),
})),
},
};
const CustomError = {
status: "failed",
error: "Invalid request data. Please review request and try again.",
};
return res
.status(StatusCodes.BAD_REQUEST)
.json(_useJoiError ? JoiError : CustomError);
} else {
req.body = value;
return next();
}
}
return next();
};
};如果对此解决方案有任何评论或替代方案,我非常乐意阅读它们。
快乐编码
https://stackoverflow.com/questions/71564341
复制相似问题