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

Unable to create precompiled validators due to compileSchemaValidators #4082

Closed
3 of 4 tasks
mpenndev opened this issue Feb 5, 2024 · 2 comments · Fixed by #4108
Closed
3 of 4 tasks

Unable to create precompiled validators due to compileSchemaValidators #4082

mpenndev opened this issue Feb 5, 2024 · 2 comments · Fixed by #4108

Comments

@mpenndev
Copy link
Contributor

mpenndev commented Feb 5, 2024

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 mpenndev added bug needs triage Initial label given, to be assigned correct labels and assigned labels Feb 5, 2024
@heath-freenome heath-freenome added awaiting response and removed needs triage Initial label given, to be assigned correct labels and assigned labels Feb 16, 2024
@heath-freenome
Copy link
Member

@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;

@mpenndev
Copy link
Contributor Author

mpenndev commented Feb 27, 2024

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

I've opened a small PR to update the docs

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

Successfully merging a pull request may close this issue.

2 participants