jarvelov / vue-form-json-schema

Create forms using JSON schema. Bring your components!
https://jarvelov.gitbook.io/vue-form-json-schema/
353 stars 54 forks source link

Ajv $async property cannot be set in the schema object #51

Open dangrima90 opened 4 years ago

dangrima90 commented 4 years ago

Hi again, been testing out the library together with Ajv asynchronous validations. Basically the idea is that certain form fields might need an API call to be validated further.

Example:

According to Ajv's documentation this can be setup with the use of keywords and formats by using asynchronous functions that return a promise with a true / false value. (https://github.com/epoberezkin/ajv#asynchronous-validation)

When trying to set this up together with vue-form-json-schema I'm always getting this error:

[Vue warn]: Error in created hook: "Error: async format in sync schema".

Example code:

Schema

{
  type: 'object',
    $async: true,
    properties: {
    email: {
      type: 'string',
      format: 'checkIfExists'
    }
  }
}

Vue Form JSON Schema Options

{
  castToSchemaType: true,
  ajv: {
    options: {
      formats: {
        checkIfExists: {
          async: true,
          validate: (data) => new Promise ((resolve) => {
            // API call...
            // return Promise()
          })
        }
      }
    }
  }
}

I investigated the error and eventually found out that the issue is coming from the getVfjsValidationSchema getter. https://github.com/jarvelov/vue-form-json-schema/blob/96a0f64105b91c9bcdd7e4dff61b4918b6608b24/src/vfjs-global-mixin/methods/vfjs-validation/getters.js#L82

In here there's a "base" object schema, that sets the type and properties array but there doesn't seem to be anything related to $async.

Is this intentional as in something that is not supported on purpose, or is it a bug?

Thank you in advance!

jarvelov commented 4 years ago

Hi,

I've looked into it and this is a bug. However I do not yet know the scope of the bug. I'll investigate tonight and let you know what I find out.

dangrima90 commented 4 years ago

Great thank you!

jarvelov commented 4 years ago

Okay, I've had a look, and unfortunately this isn't something I can fix until the next major version. I need to convert many synchronous functions to be asynchronous and this will break a lot of the internals which will introduce changes to the API.

I have wanted to rewrite to support async better for a long time and this issue will probably give me the push to dive into it, however this will take some time and I do not yet know when I'll be done with the rewrite. I have a list of things that I want to go into the next major version, which pushes back the release date further.

I want to provide this feature but I can't promise when it will be available, so the be frank: If you're evaluating different form libraries and this is a feature you need to support now, then I would recommend you to check out another library. If it is a 'maybe sometime in a month or so' then the chances are that I'll be able to provide a prerelease version you can try out, but I don't expect the next major version to be stable until sometime Q3 2020.

dangrima90 commented 4 years ago

Thank you for the update. Will try to find another library as it is a feature that's needed now. Having said that, please do keep this issue updated, would like to know when this is available.