ajv-validator / ajv-errors

Custom error messages in JSON Schemas for Ajv validator
https://ajv.js.org
MIT License
282 stars 18 forks source link

Reference Error with relative JSON pointer #100

Open Skida12138 opened 3 years ago

Skida12138 commented 3 years ago

Package Versions:

node@v14.16.1 ajv@8.1.0 ajv-errors@3.0.0

Problem

Hi when I was trying belowed case I got a ReferenceError: data1 is not defined from my node. It seems that ajv's scheme compiler generates wrong code. When I change schema.foo.bar's type definition or reduce the nesting levels the same error will not occurred again.

const Ajv = require('ajv');
const ajv = new Ajv({ allErrors: true });
require('ajv-errors')(ajv);
const schema = {
  type: "object",
  properties: {
    foo: {
      type: "object",
      properties: {
        bar: {
          type: "string",
          errorMessage: "${0}",
        },
      },
    },
  },
};
const data = {
  foo: {
    bar: false,
  },
};
ajv.validate(schema, data);
console.log(ajv.errors);

Sincerely hope for replying.

Skida12138 commented 3 years ago

I figured out how to solve this, just use ajv with option code: { optimize: 0 }, seems ajv's optimizer will cut the unused name.