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

ReferenceError: require is not defined - Unable to create precompiled validators when schema has date fields #4248

Open
3 of 4 tasks
MattPorto opened this issue Jul 9, 2024 · 1 comment

Comments

@MattPorto
Copy link

Prerequisites

What theme are you using?

validator-ajv8

Version

5.19.1

Current Behavior

I cannot create a script that successfully calls createPrecompiledValidator when the schema has a date field.
It's because the code generated on the server side by compileSchemaValidatorsCode on line 4 has a require for ajv-formats/dist/formats, which results in the error: ReferenceError: require is not defined when it's evaluated on the client side.

  • I tried to pass the ajvFormats object to the script generator using the ajvOptionsOverrides.code options in many ways
  • Tried to use string interpolation to change the generated code and replace the require in many ways as well (before I discovered that I could do it with the format keywords)
  • Tried to pass the precompiled schema validator functions as a string to createPrecompiledValidator, so the code isn't evaluated on the client, and an error isn't raised.

My server-side script:

import { compileSchemaValidatorsCode } from "@rjsf/validator-ajv8/dist/compileSchemaValidators";

const schema = JSON.parse(process.argv[2])
const options = { 
  ajvOptionsOverrides: { 
    code: { esm: true } 
  }
}

return compileSchemaValidatorsCode(schema, options)

My client-side script:

import { createPrecompiledValidator } from "@rjsf/validator-ajv8";

const getPrecompiledValidator = async (precompiledValidatorUrl, schema) => {
  if (schema) {
    let validateFunctions = await import(precompiledValidatorUrl);
    return createPrecompiledValidator(validateFunctions, schema);
  }
};

Expected Behavior

Send the compiled schema validators code to the client side without receiving the ReferenceError: require is not defined.

Steps To Reproduce

No response

Environment

- OS: Ubuntu 22.04.3
- Node: 20.11.1
- npm: 10.2.4

Anything else?

The code works as expected if we don't need to use the ajv-formats (I mean if the schema doesn't have a date field, for example).

Hopefully, I'm missing something that my tired eyes couldn't get on the docs or the previous issues, so far I've spent many days trying to fix the unsafe-eval vulnerability by precompiling my schema validators on the server side.

Lastly, it would be great if I could precompile the validator completely on the server side instead of needing to do half of the process on the client side.

@MattPorto MattPorto added bug needs triage Initial label given, to be assigned correct labels and assigned labels Jul 9, 2024
@heath-freenome
Copy link
Member

heath-freenome commented Jul 12, 2024

@MattPorto Are your schemas changing often enough that you can't do the precompile at build time? There is an example in the docs for a basic on-the-fly precompiled schema that was designed for the server. Moreover, you really shouldn't need to run the precompiler on the client. Your server should updated the precompiled schema as a resource into your client. Maybe you build the server precompiler to compile them to a public directory that serves up the precompiled schemas directly to the browser. It may require that your bundler (webpack?) deal with serving it up so the require is handled properly

@nickgros nickgros added awaiting response and removed needs triage Initial label given, to be assigned correct labels and assigned labels Jul 12, 2024
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

3 participants