I've been using AJV for validation, for a few months now. Was on v6.x for a while and now need to upgrade to get formatting and custom error messages to work. Unfortunately, it seems horribly broken. I can't find any help in bug reports and other chatter out there.
Packages:
"ajv": "^8.8.2",
"ajv-errors": "^3.0.0",
"ajv-formats": "^2.1.1",
To be clear, I can get this to work in Express APIs just fine, declared like so:
const Ajv = require("ajv");
const ajv = new Ajv({ allErrors: true, strict: false });
const ajvFormats = require("ajv-formats")(ajv);
const ajvErrors = require("ajv-errors")(ajv);
However, I'm also using it in React projects and that's where it barfs. Declared like so:
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);
No matter the options used in the Ajv constructor, it yields the following error:
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);
If I comment out the AjvErrors(ajv) line to see if formats will work, I get a separate and entirely different error for 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;
Am I SOL? Are these projects dead? There's very little activity in the bug reports I'm seeing out there. I've already invested a ton of time and wrote a lot of code around this lib, as my validation library, because it's downloaded tens-of-millions of times per month. Seemed safe! Not super encouraging. :(
CodePudding user response:
One answer, if no one else has anything, is finding the package version sweet spot. Sure would be nice to not waste hours trying to piece this together:
npm install [email protected] [email protected] [email protected] --save
What's hilarious about this is; it works in React just fine like so:
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);
But these exact same package versions in my Express API now bombs:
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);
...with this error..which seems absurd.
Ajv is not a constructor
I'm at a loss, really. I'm ready to swing back to Joi because I never had a problem with several years of use.
UPDATE:
This resolves the problem with constructor error above: TypeError: Ajv is not a constructor
AJV and related appear to be moving targets. I'm setting this version in stone and never upgrading!