You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 ajvFormats object to the script generator using the ajvOptionsOverrides.code options in many ways
Tried to use string interpolation to change the generated code and replace the require in many ways as well (before I discovered that I could do it with the format keywords)
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.
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.
The text was updated successfully, but these errors were encountered:
@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
Prerequisites
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 arequire
forajv-formats/dist/formats
, which results in the error:ReferenceError: require is not defined
when it's evaluated on the client side.ajvFormats
object to the script generator using theajvOptionsOverrides.code
options in many waysrequire
in many ways as well (before I discovered that I could do it with theformat
keywords)createPrecompiledValidator
, so the code isn't evaluated on the client, and an error isn't raised.My server-side script:
My client-side script:
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
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.
The text was updated successfully, but these errors were encountered: