asyncapi-codegen icon indicating copy to clipboard operation
asyncapi-codegen copied to clipboard

feature: support JSON-Schema validation

Open TheSadlig opened this issue 1 year ago • 2 comments

Adding support for basic json-schema validations. Related to #131

A few notes:

  • The list of validations supported by asyncapi can be found here
  • All validations that were straightforward to implement should be here. Most non-implemented validations are because they rely on nested schema (Contains, AllOf, Items...) or are simply not supported by validator/v10
  • I didn't implement anything for asyncapi v2, since currently, the schema does not contain the fields necessary for the validation. I would advise having a common struct for both v2/v3 schema to simplify reusing the GenerateValidateTags code for both
  • We only generate the required validate tag in the struct, and no effort is made to include the actual validation in the middleware or anything (not required for my use case, and it would probably let the user configure the validation; felt more complicated that necessary)

Please let me know if you need any change or information

Thanks TheSadlig

TheSadlig avatar May 11 '24 03:05 TheSadlig

Hello @TheSadlig ! Thanks for the PR ! It is really valuable ☺️ I'll review it in the next days !

lerenn avatar May 13 '24 12:05 lerenn

@lerenn Actually, my use-case requires asyncapi v2, so I updated the PR to include this, moving the fields related to validations to a struct that is embedded in both V2 and V3. It avoids having to duplicate the Validation field generation for both versions.

Whenever you have the time ;)

TheSadlig avatar May 13 '24 23:05 TheSadlig

Just fixed the linting to avoid another ping-pong, so we're good to go !

lerenn avatar May 14 '24 10:05 lerenn