parser-js: Add validation of duplicated tags
Reason/Context
We need to validate if users did not provide 2 tags with the same name on the same level. Tags can be provided in many objects, we should avoid duplicates only in a given object not across the whole document.
Description
- I don’t think we can do it with JSON Schema. I found something like https://groups.google.com/forum/#!topic/json-schema/z34YqedG-1s but it won’t work for us as we use
"additionalProperties": false
https://json-schema.org/understanding-json-schema/reference/combining.html Unfortunately, now the schema will reject everything. This is because the Properties refers to the entire schema. And that entire schema includes no properties, and knows nothing about the properties in the subschemas inside of the allOf array. This shortcoming is perhaps one of the biggest surprises of the combining operations in JSON schema: it does not behave like inheritance in an object-oriented language. There are some proposals to address this in the next version of the JSON schema specification.
- we need to add a custom validation of tags wherever tags are possible
- example files that have duplicates but the parser doesn’t throw an error:
- https://github.com/asyncapi/tck/blob/master/tests/asyncapi-2.0/Operation Object/invalid-duplicate-tags.yaml
- https://github.com/asyncapi/tck/blob/master/tests/asyncapi-2.0/AsyncAPI Object/invalid-duplicate-tags.yaml
- https://github.com/asyncapi/tck/blob/master/tests/asyncapi-2.0/Message Object/invalid-duplicate-tags.yaml
- https://github.com/asyncapi/tck/blob/master/tests/asyncapi-2.0/Message Trait Object/invalid-duplicate-tags.yaml
- https://github.com/asyncapi/tck/blob/master/tests/asyncapi-2.0/Operation Trait Object/invalid-duplicate-tags.yaml
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 16 (8 by maintainers)
Commits related to this issue
- feat: add validation for duplicated tags Issue: #143 — committed to BOLT04/parser-js by BOLT04 3 years ago
- test: add tests for duplicate tag validation Issue: #143 — committed to BOLT04/parser-js by BOLT04 3 years ago
- refactor: fix lint and validationErrors array Feedback from #143 — committed to BOLT04/parser-js by BOLT04 3 years ago
@BOLT04 At the moment
message
andoperation
can be only present inchannel
object. You can extend the function for channels validation, but your current implementation with a separate function just for tags is a better idea in my opinion. If you look at current validator implementation, you will see that channels validation checks things which can only be defined for channel, hence one validation place - tags can be in different places so separate function is better 😃@BOLT04
validateTags
is better idea than update existing 😃@BOLT04 Hi! Yes, you’re thinking right about place where new code for validation should be 😃
Refers to the plain JSON Schema (https://json-schema.org/), not to our Schema (in the meaning of AsyncAPI), so of course you can use
parsedJSON
, because it is exactly AsyncAPI document saved as JSON 😃Yes, tests will be needed, if you want you can use the test cases from tck. We have issue to use all cases from tck, but not now so feel free to use them.
Also, please have in mind that
tags
don’t occur only inchannels
(to be precise in thepublish
|subscribe
fields) but also in the other parts of AsyncAPI spec:subscribe
|publish
) objectso probably running validation in the
validateChannels
won’t be enough.Also, your example solution is good 👍🏼 Go ahead with implementation, of course, if you want 😃