rjsf-team / react-jsonschema-form

A React component for building Web forms from JSON Schema.
https://rjsf-team.github.io/react-jsonschema-form/
Apache License 2.0
14.38k stars 2.19k forks source link

Works on local env but when deployed I get Error compiling schema #4369

Open arnaumanyosa opened 3 weeks ago

arnaumanyosa commented 3 weeks ago

Prerequisites

What theme are you using?

mui

What is your question?

I have multiple schemas that I get from a REST API endpoint and I use to generate different forms. I do nothing with validators, just passing the one imported directly

import Form from '@rjsf/mui'
import validator from '@rjsf/validator-ajv8'

<Form
        schema={dynamicSchema}   //Dynamic schema that I get from a REST API endpoint
        formData={formData}
        validator={validator}   //the one imported from '@rjsf/validator-ajv8'
        liveValidate={true}
        ...
      >
        <></>
</Form>

This works perfectly on my local. But on production environment I get a very long error message on my browser console that starts like:

Error compiling schema, function code: const schema0 = scope.schema[0];return function validate0(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;if(data && typeof data == "object" && !Array.isArray(data)) ... 1- Why I don't have this error on my local? 2- I read the documentation and discussions like this one, seems like I have to build a custom validator for each of my schemas. Is that correct? 3- Seems like this custom validators should be build on the server. Can I build them on the browser? How?

nickgros commented 1 week ago

@arnaumanyosa It's hard to say why you are seeing errors in one environment but not in another because we know nothing about your application. It's probably because you don't have a content security policy in dev mode, but you do in production.

If your CSP allows unsafe-eval then you can just pass the schema with the standard ajv8 validator to the Form component. The motivation for the precompiled validators feature is to move compile schemas compilation to the server where the CSP does not apply. This uses node APIs, so there is no way to precompile a validator in the browser.