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

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

Open
3 of 4 tasks
tysenmoore-xse opened this issue Jan 3, 2025 · 2 comments
Open
3 of 4 tasks

Comments

@tysenmoore-xse
Copy link

tysenmoore-xse commented Jan 3, 2025

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

@tysenmoore-xse tysenmoore-xse added bug needs triage Initial label given, to be assigned correct labels and assigned labels Jan 3, 2025
@heath-freenome
Copy link
Member

@tysenmoore-xse This seems more like an issue to file against AJV as we are merely wrapping their engine.

@heath-freenome heath-freenome added ajv and removed needs triage Initial label given, to be assigned correct labels and assigned labels Jan 10, 2025
@tysenmoore-xse
Copy link
Author

tysenmoore-xse commented Jan 14, 2025

@heath-freenome Thanks for the response.

I have made changes and AJV has no problem with esm: true.

I am using:
"ajv": "8.17.1",
"@rjsf/validator-ajv8": "5.23.1",

/* eslint-disable max-len */
// @ts-check
"use strict";

const fs             = require( "fs" );
const path           = require( "path" );
const Ajv            = require( "ajv" );
const Ajv20          = require( "ajv/dist/2020" );
const standaloneCode = require( "ajv/dist/standalone" ).default;
const cfgSchema      = require( "../src/schema/cfgSchema.json" );

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

// import { compile, compileFromFile } from "json-schema-to-typescript";
const schema2ts = require( "json-schema-to-typescript" );

const AVJ_OUTPUT_PATH  = path.join( __dirname, "../src/schema/validateSchema.js" );
const RJSF_OUTPUT_PATH = path.join( __dirname, "../src/schema/validateSchema_rjsf.js" );

// ------------------------------------------------------------------------------------
// AJV
// ------------------------------------------------------------------------------------

// For ESM, the export name needs to be a valid export name, it can not be
// `export const #/definitions/__ROOT = ...;` so we
// need to provide a mapping between a valid name and the $id field. Below will generate
// `export const cfgSchema = ...;`
// This mapping would not have been needed if the `$ids` was just `cfgSchema` instead of
// `#/definitions/__ROOT`

let AjvClass = Ajv;
if ( cfgSchema["$schema"].indexOf( "draft/2020-12" ) !== -1 ) {
  AjvClass = Ajv20;
}

const ajv        = new AjvClass( { schemas: [cfgSchema], code: { source: true, esm: true }} );
const moduleCode = standaloneCode( ajv );

// Now you can write the module code to file
fs.writeFileSync( AVJ_OUTPUT_PATH, moduleCode );
console.log( "Step 1: AJV precompilation complete" );

// ------------------------------------------------------------------------------------
// 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: false }
  }
};

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

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

No branches or pull requests

2 participants