幾個月以來,我一直在使用 AJV 進行驗證。在 v6.x 上使用了一段時間,現在需要升級才能使格式設定和自定義錯誤訊息起作用。不幸的是,它似乎被嚴重破壞了。我在錯誤報告和其他閑聊中找不到任何幫助。
套餐:
"ajv": "^8.8.2",
"ajv-errors": "^3.0.0",
"ajv-formats": "^2.1.1",
需要明確的是,我可以讓它在 Express API 中正常作業,宣告如下:
const Ajv = require("ajv");
const ajv = new Ajv({ allErrors: true, strict: false });
const ajvFormats = require("ajv-formats")(ajv);
const ajvErrors = require("ajv-errors")(ajv);
但是,我也在 React 專案中使用它,這就是它的不足之處。像這樣宣告:
import Ajv from "ajv";
import AjvFormats from "ajv-formats";
import AjvErrors from "ajv-errors";
const ajv = new Ajv({
allErrors: true,
strict: false,
strictTypes: false,
code: { optimize: false }
});
AjvErrors(ajv);
AjvFormats(ajv);
無論 Ajv 建構式中使用的選項如何,都會產生以下錯誤:
TypeError: Cannot read properties of undefined (reading 'allErrors')
ajvErrors
src/index.ts:385
Module.<anonymous>
src/mod/validator.js:10
7 | strictTypes: false,
8 | code: { optimize: false }
9 | });
> 10 | AjvErrors(ajv);
11 | AjvFormats(ajv);
如果我注釋掉 AjvErrors(ajv) 行以查看格式是否有效,我會得到一個單獨且完全不同的 AjvFormats(ajv) 錯誤:
TypeError: Cannot read properties of undefined (reading 'code')
addFormats
src/index.ts:55
52 | if (items) {
53 | errors.items = {};
54 | for (let i = 0; i < items.length; i )
> 55 | errors.items[i] = [];
| ^ 56 | }
57 | return errors;
58 | }
View compiled
formatsPlugin
src/index.ts:42
39 | const schMessage = typeof sch == "string" ? sch : sch._;
40 | if (schMessage)
41 | processAllErrors(schMessage);
> 42 | if (!options.keepErrors)
| ^ 43 | removeUsedErrors();
44 | });
45 | function childErrorsConfig({ properties, items }) {
View compiled
Module.<anonymous>
src/mod/validator.js:11
8 | code: { optimize: false }
9 | });
10 | // AjvErrors(ajv);
> 11 | AjvFormats(ajv);
12 |
13 | const initValidationCache = async () => {
14 | let { entityType, schema } = window;
我是SOL嗎?這些專案都死了嗎?我在那里看到的錯誤報告中的活動很少。我已經投入了大量時間并圍繞這個庫撰寫了大量代碼,作為我的驗證庫,因為它每月下載數千萬次。看起來很安全!不是超級鼓舞人心。:(
uj5u.com熱心網友回復:
一個答案,如果沒有其他人有任何東西,就是找到包版本的最佳位置。當然,不要浪費時間試圖將其拼湊在一起會很好:
npm install ajv@7.2.3 ajv-errors@2.0.1 ajv-formats@2.1.1 --save
這有什么好笑的;它在 React 中作業得很好,如下所示:
import Ajv from "ajv";
import AjvFormats from "ajv-formats";
import AjvErrors from "ajv-errors";
const ajv = new Ajv({
allErrors: true,
strict: false
});
AjvFormats(ajv);
AjvErrors(ajv);
但是我的 Express API 中這些完全相同的軟體包版本現在會爆炸:
const Ajv = require("ajv");
const ajv = new Ajv({ allErrors: true, strict: false }); //this fails!
const AjvFormats = require("ajv-formats");
const AjvErrors = require("ajv-errors");
AjvFormats(ajv);
AjvErrors(ajv);
......這個錯誤......這似乎很荒謬。
Ajv 不是建構式
我不知所措,真的。我已經準備好回到 Joi 了,因為我使用了幾年從來沒有遇到過問題。
更新:
這解決了上述建構式錯誤的問題: TypeError: Ajv is not a constructor
AJV 和相關企業似乎是移動目標。我將這個版本一成不變,永不升級!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/395925.html
標籤:javascript json 验证 jsonschema 艾维
上一篇:電子郵件驗證改進
