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

precompiled validator errors on anyOf #3677

Closed
4 tasks done
cwendtxealth opened this issue May 15, 2023 · 11 comments · Fixed by #3687
Closed
4 tasks done

precompiled validator errors on anyOf #3677

cwendtxealth opened this issue May 15, 2023 · 11 comments · Fixed by #3687
Labels
awaiting response bug needs reproducible example Missing a link to a reproduction in the playground, CodeSandbox, JSFiddle, etc.

Comments

@cwendtxealth
Copy link
Contributor

cwendtxealth commented May 15, 2023

Prerequisites

What theme are you using?

core

Version

5.7.0

Current Behavior

Using precompiled schema validation feature with anyOf errors when setting the anyOf field. Simple any of schema

{
  "type": "object",
  "properties": {
    "live": {
      "anyOf": [
        {
          "type": "boolean"
        },
        {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      ]
    }
  }
}

When compiled and used with precompile feature, I get this error when trying to set the field with either boolean or string array

Uncaught Error: No precompiled validator function was found for the given schema for "79bb9845"
    at AJV8PrecompiledValidator2.getValidator (precompiledValidator.ts:79:13)
    at AJV8PrecompiledValidator2.isValid (precompiledValidator.ts:159:28)
    at getMatchingOption (getMatchingOption.ts:87:21)
    at getFirstMatchingOption (getFirstMatchingOption.ts:26:10)
    at getClosestMatchingOption.ts:137:19
    at Array.reduce (<anonymous>)
    at getClosestMatchingOption (getClosestMatchingOption.ts:135:35)
    at SchemaUtils2.getClosestMatchingOption$1 [as getClosestMatchingOption] (createSchemaUtils.ts:148:12)
    at AnyOfField2.getMatchingOption (MultiSchemaField.tsx:100:32)
    at AnyOfField2.componentDidUpdate (MultiSchemaField.tsx:76:35)

Expected Behavior

Form loads and handles anyOf for precompiled schemas.

Steps To Reproduce

No response

Environment

- OS:
- Node:
- npm:

Anything else?

No response

@cwendtxealth cwendtxealth added bug needs triage Initial label given, to be assigned correct labels and assigned labels May 15, 2023
@heath-freenome
Copy link
Member

@cwendtxealth can you provide a reproducible codesandbox.io for this? Otherwise we can't figure out how to fix it.

@heath-freenome heath-freenome added awaiting response needs reproducible example Missing a link to a reproduction in the playground, CodeSandbox, JSFiddle, etc. and removed needs triage Initial label given, to be assigned correct labels and assigned labels May 16, 2023
@cwendtxealth
Copy link
Contributor Author

cwendtxealth commented May 16, 2023

@heath-freenome here is codesandbox.io for the anyOf error. If you click the live boolean or try to switch to string array and add item it crashes. From the error message it seems like the hash id generated for the compiled schema is different then the hash idea used to find the schema validator?

@cwendtxealth
Copy link
Contributor Author

Here is an example of if/then compile errors because of subschema merge. This codesandbox tries to compile the if/then schema that is used in the playground

@cwendtxealth
Copy link
Contributor Author

I believe I found the issue for anyOf error. In utils getClosestMatchingOption it sets a JUNK_OPTION schema as an option and passes it to getFirstMatchingOption which will then try to find that id in the compiled schema. That will throw an error because that junk schema was never in the original schema that was compiled.

I think one solution should be catching the error in AJV8PrecompiledValidator.isValid and returning false so that it doesn't crash the whole component. This is a similar approach to the base AJV8Validator.isValid.

This doesn't solve the if/then condition error for precompiled but I can open another ticket with more investigation into that. I think the anyOf can be solved with this solution.

@cwendtxealth cwendtxealth changed the title precompiled validator errors on anyOf and allOf if/then precompiled validator errors on anyOf May 22, 2023
@kenvenin
Copy link

Still experiencing this issue on 5.10.0, any hints?

@heath-freenome
Copy link
Member

@kenvenin Are you able to provide a reproducible test case?

@kenvenin
Copy link

@heath-freenome Sorry for the late reponse, providing same sandbox but with versions changed to 5.10.0

@cwendtxealth
Copy link
Contributor Author

@kenvenin that seems to be the old sandbox where the compile validate did not match the schema correctly. Do you have a schema sample that you are see failing? All of the complex schemas I have been using have been able to be compiled and work now. There are also a couple big fixes in 5.11.0 for compiled schema.

@kenvenin
Copy link

kenvenin commented Aug 1, 2023

I apologize, my issue is probably different, I can compile the schema but when trying to import it I see the same error "Uncaught Error: No precompiled validator function was found for the given schema for...". I'm using vite so I think it's an issue with how imports from cjs are handled in my project (I can see hashed exports that start with numbers are not being imported). Would it be troubling to ask you for an example that successfully loads a precompiled schema?

@cwendtxealth
Copy link
Contributor Author

So I ran into a similar problem because I was using the precompiled validator function dynamically without writing it to a file and could not use any tooling to transpile the javascript code. What I did is add var exports = {}; to the front of the module and export default exports; to the end of the module. In the compiled module generation it adds all of the schema functions to the exports function.

@heath-freenome
Copy link
Member

There is also a fix coming to help deal with on-the-fly precompiled schemas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting response bug needs reproducible example Missing a link to a reproduction in the playground, CodeSandbox, JSFiddle, etc.
Projects
None yet
3 participants