Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'code' of undefined #12

Closed
leoglobant opened this issue Feb 4, 2021 · 13 comments
Closed

Cannot read property 'code' of undefined #12

leoglobant opened this issue Feb 4, 2021 · 13 comments

Comments

@leoglobant
Copy link

I'm using @middy/validator for which I add the plugin with an empty object {}.
Unfortunately I get an error: TypeError: Cannot read property 'code' of undefined
Pointing to line https://github.com/ajv-validator/ajv-formats/blob/master/src/index.ts#L55
Please help!

@mfolkeseth
Copy link

I had the same issue. Try apply formats to the instance:

const Ajv = require('ajv').default;
const AjvFormats = require('ajv-formats');
const ajv = new Ajv();
AjvFormats(ajv);
const validateSchema = ajv.compile(schema);

@cywalker
Copy link

cywalker commented Feb 9, 2021

I am having the same issue, throws error on last line.
addFormats uses ajv.opts.code but ajv object does not have .opts or .opts.code (it does have ._opts but not ._opts.code)

import Ajv from 'ajv'; //'../node_modules/ajv/lib/ajv.js'; v7.0.4
import addFormats from 'ajv-formats'; //"../node_modules/ajv-formats/dist/index.js"; v1.5.1
const ajv = new Ajv({
  $data: true,
  allErrors: true,
});
console.log(ajv);
addFormats(ajv);

error => Cannot read property 'code' of undefined
errors at node_modules/ajv-formats/dist/index.js:30:1;

  28 |     var _a;
  29 |     var _b;
> 30 |     (_a = (_b = ajv.opts.code).formats) !== null && _a !== void 0 ? _a : (_b.formats = codegen_1._ `require("ajv-formats/dist/formats").${exportName}`);
     | ^
  31 |     for (const f of list)
  32 |         ajv.addFormat(f, fs[f]);
  33 | }

ajv object;

Ajv {_opts: {…}, logger: console, _schemas: {…}, _refs: {…}, _fragments: {…}, …}
RULES: (5) [{…}, {…}, {…}, {…}, {…}, all: {…}, types: {…}, keywords: {…}, custom: {…}]
logger: console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …}
_cache: Cache {_cache: {…}}
_compilations: []
_formats: {date: /^\d\d\d\d-[0-1]\d-[0-3]\d$/, time: /^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)?$/i, date-time: /^\d\d\d\d-[0-1]\d-[0-3]\d[t\s](?:[0-2]\d:[0-5]\d:…d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i, uri: /^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/)?[^\s]*$/i, uri-reference: /^(?:(?:[a-z][a-z0-9+\-.]*:)?\/?\/)?(?:[^\\\s#][^\s#]*)?(?:#[^\\\s]*)?$/i, …}
_fragments: {}
_getId: ƒ _get$Id(schema)
_loadingSchemas: {}
_metaOpts: {$data: true, allErrors: true, loopRequired: Infinity, serialize: ƒ}
_opts:
$data: true
allErrors: true
loopRequired: Infinity
serialize: ƒ (data, opts)
__proto__: Object
_refs: {https://raw.githubusercontent.com/ajv-validator/ajv/master/lib/refs/data.json: SchemaObject, http://json-schema.org/draft-07/schema: SchemaObject, http://json-schema.org/schema: "http://json-schema.org/draft-07/schema"}
_schemas: {https://raw.githubusercontent.com/ajv-validator/ajv/master/lib/refs/data.json: SchemaObject, http://json-schema.org/draft-07/schema: SchemaObject}
__proto__: Object

Not sue if any of this helps find / fix the issue but I hope so!

@smeijer
Copy link

smeijer commented Feb 16, 2021

Ran into the same issue. Make sure to install v7. For some reason npm i ajv installed version 6, which threw the error.

Fixed with:

npm i ajv@7.1.0

@cywalker
Copy link

Thanks, got sorted via gitter post.
The issue was npm had installed v6 but npm v was reporting v7 that is inside ajv-formats package.
...which also explains some of the other unexpected behavior I was having prior to this fix.

@anonrig
Copy link

anonrig commented Jul 14, 2021

I'm having the same issue with Ajv 8 running on top of Fastify's latest version. Any fixes related to this?

@epoberezkin
Copy link
Member

It has to be submitted to fastify too.

Maybe, the solution is to expose ajv instance in fastify and/or to pass options...

@anonrig
Copy link

anonrig commented Aug 1, 2021

I've sent a pull request to bump the dependencies, but it seems that the repo is unmaintained atm.

@vgjenks
Copy link

vgjenks commented Dec 22, 2021

Same issue in latest AJV and formats - broken for me. Any hope in getting this fixed? We're basing a very large project on AJV and this really concerning. It breaks every time I update packages.

@epoberezkin
Copy link
Member

I’m not 100% sure what is the right fix here.
It works correctly from clean install if I understood it correctly, but starts importing incorrect version after the update.

If that’s the case it’s more of annoying npm “feature” rather than the bug to be fixed in ajv…

I am open to ideas what’s the correct mitigation would be here.

@Prakash14one
Copy link

Ran into the same issue. Make sure to install v7. For some reason npm i ajv installed version 6, which threw the error.

Fixed with:

npm i ajv@7.1.0

its working thanks

@lorenzogrv
Copy link

Same problem here, with ajv version 8.x

After some frustrated attemps, removing everything ajv-related and installing again worked. That is

npm rm -w our/tool ajv ajv-errors ajv-formats
npm install -w our/tool ajv ajv-errors ajv-formats

Hope it helps. Very strange 🤔

@lorenzogrv
Copy link

After upgrading other non-related deps, got the same problem. My proposed workaround did not work. I suspect the problem arises from ajv-formats package having ajv within dependencies, when it should have it only within peerDependencies

@coderextreme
Copy link

Not really seeing a fix or workaround above? I'm trying to migrate to ES6, ajv 8.12.0. Only adding one parameter to addFormats.

import Ajv2020 from "../../../node_modules/ajv/dist/2020.js";
import addFormats from "../../../node_modules/ajv-formats/dist/index.js";
import localize from "../../../node_modules/ajv-i18n/localize/index.js";

const ajv = Ajv2020;
addFormats(ajv);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

10 participants