react-jsonschema-form icon indicating copy to clipboard operation
react-jsonschema-form copied to clipboard

Unable to create precompiled validators due to compileSchemaValidators

Open mpenndev opened this issue 1 year ago • 1 comments

Prerequisites

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') raises SyntaxError: Cannot use import statement outside a module due to fs 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

  1. Initialise a new project with npm init -y
  2. Install deps npm i @rjsf/validator-ajv8
  3. Update package.json with "type": "module"
  4. Create a new JS file, e.g.
  5. 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);
  1. 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 avatar Feb 05 '24 18:02 mpenndev

@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 avatar Feb 16 '24 20:02 heath-freenome

@heath-freenome thank you, I was just missing .default!

I've opened a small PR to update the docs

mpenndev avatar Feb 27 '24 13:02 mpenndev