react-jsonschema-form
react-jsonschema-form copied to clipboard
ReferenceError: require is not defined - Unable to create precompiled validators when schema has date fields
Prerequisites
- [X] I have searched the existing issues
- [X] I understand that providing a SSCCE example is tremendously useful to the maintainers.
- [X] I have read the documentation
- [ ] Ideally, I'm providing a sample JSFiddle, Codesandbox.io or preferably a shared playground link demonstrating the issue.
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
ajvFormatsobject to the script generator using theajvOptionsOverrides.codeoptions in many ways - Tried to use string interpolation to change the generated code and replace the
requirein many ways as well (before I discovered that I could do it with theformatkeywords) - 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 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
This issue has been automatically marked as possibly close because it has not had recent activity. It will be closed if no further activity occurs. Please leave a comment if this is still an issue for you. Thank you.
This issue was closed because of lack of recent activity. Reopen if you still need assistance.