const baseHandler: APIGatewayProxyHandlerV2 = async (event) => {
return service.create(event.body);
}
const inputSchema = {
type: "object",
properties: {
body: {
type: "object",
properties: {
year: { type: "number" },
questionId: { type: "string" },
propSeq: { type: "number" },
questionTitle: { type: "string" },
propContent: { type: "string" },
isTrue: { type: "boolean" },
chapter: { type: "number" }
},
required: ["year", "questionId", "propSeq", "questionTitle", "propContent", "isTrue", "chapter"],
},
},
};
export const handler = middy(baseHandler)
.use(jsonBodyParser())
.use(validator({inputSchema}))
.use(httpErrorHandler())我正在无服务器框架上编写AWS代码。我想要像express-validator这样的请求体验证器,所以我找到了middy。但似乎不可能验证某物的长度。我想将year的长度强制为4,例如,2023(o),23(x)
properties: {
year: { type: "number", length: 4 }
}正如您猜测的那样,无法理解length属性。我不想在baseHandler函数中添加一些代码来验证长度。提前谢谢你。
发布于 2022-11-19 19:26:05
Middy使用JSONSchema,所以您可以使用与JSONSchema兼容的任何东西。您可以使用length,但是需要将类型从number切换到string,因为number不支持length (在我看来,这是正确的)。如果您想将其保留为number,那么最好使用基于范围的验证:https://json-schema.org/understanding-json-schema/reference/numeric.html#range。
https://stackoverflow.com/questions/74499839
复制相似问题