我正在尝试为我的Express后端编写一个“注册”验证器。我使用AJV来做这件事。显然,我希望将验证器模式作为模块导出,因此我遵循AJV文档中的this页面。然而,我得到的是:TypeError: standaloneCode is not a function
下面是我的代码:
const Ajv = require("ajv")
const ajv = new Ajv({ code: { source: true } })
const standaloneCode = require("ajv/dist/standalone")
const signupSchema = {
type: "object",
properties: {
username: { type: "string" },
email: { type: "string" },
password: { type: "string" },
confirmPassword: { type: "string" },
},
required: ["username", "email", "password", "confirmPassword"],
}
const validate = ajv.compile(signupSchema)
const moduleCode = standaloneCode(ajv, validate)
module.exports = moduleCode提前谢谢你。
发布于 2021-05-15 02:13:52
这似乎是AJV文档(https://ajv.js.org/standalone.html)中的一个错误。捆绑的standalone模块具有ESM样式导出,因此在使用require时需要显式引用.default。
const standaloneCode = require('ajv/dist/standalone').defaulthttps://stackoverflow.com/questions/67538994
复制相似问题