jsonforms icon indicating copy to clipboard operation
jsonforms copied to clipboard

Support async validation with ajv

Open edgarmueller opened this issue 6 years ago • 4 comments

Describe the bug If ajv is configured to use asynchronous validation, error reporting is not working correctly, see https://github.com/epoberezkin/ajv#asynchronous-validation for more info.

To Reproduce Use any example with $async set

Expected behavior Error reporting should not break.

edgarmueller avatar Apr 25 '19 13:04 edgarmueller

Are there any updates on this issue? Have a hacky workaround going but it's not ideal. I see there is a solution ready in your PRs!

mlandry8 avatar Jul 25 '19 15:07 mlandry8

@mlandry8 Except the open draft PR there isn't unfortunately, we simply didn't find the time to finish it. But we'll try to do so for the next minor release (which would be 2.4.0). If you already have something and you think it's a valuable addition to the framework, we always welcome contributions! Maybe you could compare it to the existing PR and check which approach would be better.

edgarmueller avatar Jul 25 '19 15:07 edgarmueller

I am stuck with this problem, too. How could I work around this problem until it is fixed in the next milestone? @mlandry8 You mentioned a hacky workaround - could you give a hint on how to do it? Thanks!

luziew avatar Oct 29 '21 06:10 luziew

Update: While JSON Forms does not support async validation directly, there is a straightforward workaround by managing the errors outside of JSON Forms:

  • Hand over validationMode="NoValidation" to turn off the built-in validation
  • Create an AJV instance outside of JSON Forms, e.g. via createAjv() exported by @jsonforms/core
  • Compile your schema with the ajv instance, e.g. const validator = myAjv.compile(schema) (this is costly, so the validator should be memoized to not compile the schema again with every change)
  • Whenever data is received from JSON Forms, use the validator to validate and store the errors in some state
  • Handover the determined errors to JSON Forms via additionalProps={myErrors}

sdirix avatar Apr 03 '24 14:04 sdirix