Ajv: standaloneCode fails to include all referenced methods

Created on 9 Dec 2020  路  11Comments  路  Source: ajv-validator/ajv

What version of Ajv are you using? Does the issue happen if you use the latest version?
v7.0.0-rc.0

Ajv options object

{
    $data: true,
    strict: false,
    loadSchema,
    code: {
      source: true,
    },
  }

Your code

I added the source code as a zip here. I tried to simplify the FHIR schema but I failed to reproduce the issue when I did so so sorry for that!
reproduce_issue.zip
You can find the source code in this repl as well.

const Ajv = require('ajv').default;
const standaloneCode = require('ajv/dist/standalone').default;
const jsonSchemaDraft06 = require('ajv/lib/refs/json-schema-draft-06.json');
const fs = require('fs');
const path = require('path');
const util = require('util');
const _replace = require('lodash/fp/replace');
const _flow = require('lodash/fp/flow');
const requireFromString = require('require-from-string');
const fhirQSchema = require('./fhir.questionnaire.schema.json');

const readFile = util.promisify(fs.readFile);
const getSchemaPath = _flow(
  _replace(
    'http://dummy.com',
    path.resolve(__dirname, './'),
  ),
);
const loadSchema = async (uri) => {
  const data = await readFile(getSchemaPath(uri), 'utf8');
  return JSON.parse(data);
};

const test = async () => {
  const ajv = new Ajv({
    $data: true,
    strict: false,
    loadSchema,
    code: {
      source: true,
      lines: true,
    },
  });

  ajv.addMetaSchema(jsonSchemaDraft06);
  const validate = await ajv.compileAsync(fhirQSchema);
  validate({});

  const moduleCode = standaloneCode(ajv, validate);
  const standaloneValidate = requireFromString(moduleCode);
  standaloneValidate({});
};


//this throws ReferenceError: validate23 is not defined
test().catch(e => console.error(e));

What results did you expect?
The standalone code generation to succeed.
Instead requireFromString fails with error ReferenceError: validate23 is not defined.

Thank you for creating this great package and for helping me out!

bug report

All 11 comments

Thank you. Would the issue remain if you remove async schema compilation or is it specific to async schema loading?

It would help having self-contained example, without any IO (and async can be mocked if it's async specific).

I also remember FHIR schemas had some issues with incorrect schema IDs - it was in gitter and in some issues - maybe it was addressed at source...

can reproduce without async loading: https://repl.it/@EvgenyPoberezki/ReplicateAjvStandaloneCodeError#index.js

you don't need a wrapping schema btw, you can just:

ajv.addSchema(require('./fhir.r4.schema.json'))
const validate = ajv.getSchema("http://hl7.org/fhir/json-schema/4.0#/definitions/Questionnaire")

It doesn't change the issue though - standalone code fails on this schema - need to dig deeper - not today.

you don't need a wrapping schema btw, you can just:

ajv.addSchema(require('./fhir.r4.schema.json'))
const validate = ajv.getSchema("http://hl7.org/fhir/json-schema/4.0#/definitions/Questionnaire")

It doesn't change the issue though - standalone code fails on this schema - need to dig deeper - not today.

Yes, normally there is more added in that schema but I removed it trying to simplify things

The minimal failing schema is:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://hl7.org/fhir/json-schema/4.0",
  "$ref": "#/definitions/Questionnaire",
  "definitions": {
    "ResourceList": {
      "oneOf": [
        {
          "$ref": "#/definitions/Questionnaire"
        }
      ]
    },
    "Questionnaire": {
      "properties": {
        "contained": {
          "$ref": "#/definitions/ResourceList"
        }
      }
    }
  }
}

if $ref in root is removed or oneOf is removed, it works...

The fix is merged to master - @Beretta1979 please test with your code if you can, I will deploy later.

And thank you - good catch - I am happy you found it before it was released as the main version.

Thank you so much! I will test ASAP

Hi @epoberezkin I can confirm that the compilation now completes successfully at my side!

It's now released in v7.0.0-rc.2

Was this page helpful?
0 / 5 - 0 ratings