mali icon indicating copy to clipboard operation
mali copied to clipboard

Adding validation support with AJV inside Mali.js

Open anonrig opened this issue 4 years ago • 8 comments

Initially I wanted to add Ajv support just like Fastify to Mali.js but before that I released it on Github on https://github.com/anonrig/mali-ajv.

I wanted to ask the community whether having JSON schema validation support for Mali.js inputs/requests would be a good idea.

anonrig avatar Jul 24 '21 10:07 anonrig

Initially I wanted to add Ajv support just like Fastify to Mali.js but before that I released it on Github on https://github.com/anonrig/mali-ajv.

I wanted to ask the community whether having JSON schema validation support for Mali.js inputs/requests would be a good idea.

Yes, I currently use Ajv in Mali as request validation. Official Document point that use single instance & cache pre-compile schema will more memory efficiency.

HsinHeng avatar Jul 26 '21 09:07 HsinHeng

@HsinHeng I've updated mali-ajv to use pre-compiled schemas and released 0.2.0 version (https://github.com/anonrig/mali-ajv/releases/tag/0.2.0).

Would you like us to add a default support for Ajv inside Mali?

anonrig avatar Jul 26 '21 10:07 anonrig

@HsinHeng I've updated mali-ajv to use pre-compiled schemas and released 0.2.0 version (https://github.com/anonrig/mali-ajv/releases/tag/0.2.0).

Would you like us to add a default support for Ajv inside Mali?

It sounds great to support request validation & you pick a good validation lib.

HsinHeng avatar Jul 26 '21 10:07 HsinHeng

@HsinHeng I've updated mali-ajv to use pre-compiled schemas and released 0.2.0 version (https://github.com/anonrig/mali-ajv/releases/tag/0.2.0).

Would you like us to add a default support for Ajv inside Mali?

A small request is, can we pass customized grpc status code or error object if failed validation.

maybe INVALID_ARGUMENT, FAILED_PRECONDITION are suits for several use cases.

HsinHeng avatar Jul 26 '21 10:07 HsinHeng

Hey @HsinHeng

Just released 0.3.0 with the following feature. (https://github.com/anonrig/mali-ajv/releases/tag/0.3.0)

anonrig avatar Jul 26 '21 14:07 anonrig

Hey @HsinHeng

Just released 0.3.0 with the following feature. (https://github.com/anonrig/mali-ajv/releases/tag/0.3.0)

It looks great. Thanks for your help. Another request is, could it support customized ajv instance options like fastify.io provided,

const fastify = require('fastify')({
  ajv: {
    customOptions: {
      nullable: false // Refer to [ajv options](https://ajv.js.org/#options)
    },
    plugins: [
      require('ajv-merge-patch')
      [require('ajv-keywords'), 'instanceof'];
      // Usage: [plugin, pluginOptions] - Plugin with options
      // Usage: plugin - Plugin without options
    ]
  }
})

details document is here.

because i used some options in my projects.

maybe interface of options parameter could like fastify.io.

app.ajv = { customOptions, plugins };

or 

const app = new Mali({
   ajv: {
       customOptions,
       plugins,
   },
})

Thanks your kindly help again.

HsinHeng avatar Jul 27 '21 01:07 HsinHeng

Any update on this issue? Seems like a good idea to have validation with AJV 🤗

jscek avatar Sep 21 '21 08:09 jscek

I've built a separate package to support this called mali-ajv but I'm not really quite sure to integrate it into the core of Mali.

anonrig avatar Sep 21 '21 14:09 anonrig