Skip to content

compileSchemaValidators error when compiling for "esm" when schema contains "oneOf" #4436

@tysenmoore-xse

Description

@tysenmoore-xse

Prerequisites

What theme are you using?

bootstrap-4

Version

5.23.1

Current Behavior

I am able to run compileSchemaValidators for commonJS output generation just fine. However, when I try to compile for esm: true the same schema generates an error. I have found that if I remove the "oneOf" statement the error goes away.

Sample schema:

{
    "$id": "cfgSchema",
    "$schema": "http://json-schema.org/draft-07/schema",
    "title": "Project title",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "auth": {
            "$ref": "#/$defs/Auth"
        }
    },
    "required": [],
    "$defs": {
        "Auth": {
            "type": "object",
            "description": "Authentication related settings for the app.",
            "additionalProperties": false,
            "properties": {
                "fakeAuth": {
                    "type": "boolean",
                    "default": false
                },
                "username": {
                    "type": "string",
                    "default": ""
                },
                "password": {
                    "type": "string",
                    "default": ""
                },
                "fakeDevice": {
                    "type": "string",
                    "default": ""
                },
                "fakeSubDevice": {
                    "type": "string",
                    "default": ""
                }
            },
            "oneOf": [
                {
                    "title": "Fake Authentication",
                    "description": "If fakeAuth == true, then:",
                    "properties": {
                        "fakeAuth": { "const": true }
                    },
                    "required": ["fakeDevice", "fakeSubDevice"]
                },
                {
                    "title": "IP Authentication",
                    "description": "If fakeAuth == false, then:",
                    "properties": {
                        "fakeAuth": { "const": false }
                    },
                    "required": ["username", "password"]
                }
            ],
            "title": "Auth"
        }        
    }
}

Generation code:

"use strict";

const path = require("path");
const cfgSchema = require("./cfgSchema.json");

const compileSchemaValidators = require("@rjsf/validator-ajv8/dist/compileSchemaValidators").default;

const RJSF_OUTPUT_PATH = path.join(__dirname, "./validateSchema_rjsf.js");

// ------------------------------------------------------------------------------------
// react-jsonschema-form
// ------------------------------------------------------------------------------------

const options = {
  ajvOptionsOverrides: {
    // using "esm: true" results in a crash:
    // Error: CodeGen: invalid export name: -634615b0, use explicit $id name mapping
    // at getEsmExportName (C:\repos\MMA_Ng\node_modules\ajv\dist\compile\codegen\code.js:149:11)
    // at multiExportsCode (C:\repos\MMA_Ng\node_modules\ajv\dist\standalone\index.js:40:81)
    // at standaloneCode (C:\repos\MMA_Ng\node_modules\ajv\dist\standalone\index.js:14:15)
    // at compileSchemaValidatorsCode (C:\repos\MMA_Ng\node_modules\@rjsf\validator-ajv8\dist\compileSchemaValidators.js:91:40)
    // at compileSchemaValidators (C:\repos\MMA_Ng\node_modules\@rjsf\validator-ajv8\dist\compileSchemaValidators.js:97:22)
    // at Object.<anonymous> (C:\repos\MMA_Ng\tasks\precompileSchema.js:70:3)
    // at Module._compile (node:internal/modules/cjs/loader:1469:14)
    // at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)
    // at Module.load (node:internal/modules/cjs/loader:1288:32)
    // at Module._load (node:internal/modules/cjs/loader:1104:12)
    code: { source: true, esm: true }
  }
};

try {
  // React-jsonschema-form requires its own precompiled format, so compile again
  compileSchemaValidators(cfgSchema, RJSF_OUTPUT_PATH, options);
}
catch (err) {
  console.error("RJSF precompilation failed...");
  console.error(err);
}

My full schema has several "oneOf" statements so I had to delete definitions to find the originating error-- the numeric reference (e.g. -634615b0) offered nothing useful.

Expected Behavior

I expect the compiler to generate an ES compatible validator.

Steps To Reproduce

Using the attached zip file in Windows:

  1. extract the zip to a directory (e.g. c:\tmp\rjsfissue)
  2. cd rjsfissue
  3. npm install
  4. npm start
  5. Errored result:
$ npm start

> [email protected] start
> node ./precompileSchema.js

parsing the schema
RJSF precompilation failed...
Error: CodeGen: invalid export name: -634615b0, use explicit $id name mapping
    at getEsmExportName (C:\tmp\rsjffIssue\node_modules\ajv\dist\compile\codegen\code.js:149:11)
    at multiExportsCode (C:\tmp\rsjffIssue\node_modules\ajv\dist\standalone\index.js:40:81)
    at standaloneCode (C:\tmp\rsjffIssue\node_modules\ajv\dist\standalone\index.js:14:15)
    at compileSchemaValidatorsCode (C:\tmp\rsjffIssue\node_modules\@rjsf\validator-ajv8\dist\compileSchemaValidators.js:91:40)
    at compileSchemaValidators (C:\tmp\rsjffIssue\node_modules\@rjsf\validator-ajv8\dist\compileSchemaValidators.js:97:22)
    at Object.<anonymous> (C:\tmp\rsjffIssue\precompileSchema.js:34:3)
    at Module._compile (node:internal/modules/cjs/loader:1469:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)
    at Module.load (node:internal/modules/cjs/loader:1288:32)
    at Module._load (node:internal/modules/cjs/loader:1104:12)

Environment

- OS: Windows 11
- Node: 20.18.0
- npm: 10.8.2

Anything else?

rsjffIssue.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions