我想使用Ajv 驗證器來驗證我的應用程序的請求主體express。要使用驗證器,我們首先需要編譯要測試的模式,然后運行驗證,如下所示:const Ajv = require('Ajv');const ajv = new Ajv();const schema = { type: 'object', required: ['username', 'password'], properties: { username: { type: 'string', minLength: 3, }, password: { type: 'string', minLength: 8, }, },};/* What we want to validate */const body = { username: 'johndoe', password: 'secret'};/* We compile the validator */const validate = ajv.compile(schema.query)/* We test our data to see if it is valid */const valid = validate(body)我應該在調用端點時始終編譯驗證器,以便將其包含在我的路由中,還是可以將編譯過程留在路由主體之外?
我應該總是在 express 中編譯 Ajv 模式嗎?
12345678_0001
2022-12-02 16:46:45