react-jsonschema-form
react-jsonschema-form copied to clipboard
Unable to create precompiled validators due to compileSchemaValidators
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?
core
Version
5.17.0
Current Behavior
I am unable to create a node script that can successfully call compileSchemaValidators
.
- Method from the docs:
import { compileSchemaValidators } from '@rjsf/validator-ajv8'
does not export the method - Method from similar issue:
import { compileSchemaValidators } from '@rjsf/validator-ajv8/lib/compileSchemaValidators';
raises a module not found error - Require syntax to try to avoid module problems:
const { compileSchemaValidators } = require('@rjsf/validator-ajv8/dist/compileSchemaValidators.esm')
raisesSyntaxError: Cannot use import statement outside a module
due tofs
import in@rjsf/validator-ajv8/lib/compileSchemaValidators.js:1
Expected Behavior
Be able to import compileSchemaValidators
in a JS file so that precompiled validators can be constructed.
If possible, with the require
syntax so that it doesn't have to be within a module.
Steps To Reproduce
- Initialise a new project with
npm init -y
- Install deps
npm i @rjsf/validator-ajv8
- Update
package.json
with"type": "module"
- Create a new JS file, e.g.
- Add contents to JS file:
// import { compileSchemaValidators } from '@rjsf/validator-ajv8'
// import { compileSchemaValidators } from "@rjsf/validator-ajv8/lib/compileSchemaValidators";
// const { compileSchemaValidators } = require('@rjsf/validator-ajv8')
// const { compileSchemaValidators } = require('@rjsf/validator-ajv8/dist/compileSchemaValidators')
// const { compileSchemaValidators } = require('@rjsf/validator-ajv8/dist/compileSchemaValidators.esm') // fs import issue
// const { compileSchemaValidators } = require('@rjsf/validator-ajv8/lib/compileSchemaValidators') // fs import issue
import compileSchemaValidators from "@rjsf/validator-ajv8/lib/compileSchemaValidators.js";
console.log(compileSchemaValidators);
- Run with
node myFile.js
Environment
- OS: Ubuntu 22.04
- Node: 20.5.1
- npm: 9.8.0
Originally tried to add as a script to my Create React App.
Also tried executing as a new npm project setup as a module.
Anything else?
Hopefully I'm missing something extremely obvious but so far I've lost hours just trying to be able to call the compileSchemaValidators
I also tried using the Ajv CLI but I'm guessing there is some difference in format as RJSF as I got No precompiled validator function was found for the given schema
when trying to validate with it.
It would also be great to be able to not provide a validator if desired, instead of having to do this to get around CSP.
@mpenndev I am confused why you are having the problem you mentioned. I have my own node-based script which works just fine. This is how I import it:
const compileSchemaValidators = require('@rjsf/validator-ajv8/dist/compileSchemaValidators').default;
@heath-freenome thank you, I was just missing .default
!
I've opened a small PR to update the docs