Skip to content

TagUniquenessValidationRule not working properly #587

Open
@latalkdesk

Description

@latalkdesk

The validation of the TagUniquenessValidationRule is giving NullPointerException when document does not contain tags.
Also it is not validating the correct spec rule as it is comparing the current node tag with the root node tags.
It should validate the current list of tags if they are unique by name.

For instance this is a slice of a valid Document:

  "components": {
    "messages": {
      "message1": {
        "name": "message_name",
        "tags": [{ "name": "tag_unique" }]
      }
    }
  },
  "tags": [ { "name": "tag_unique" } ]

And this is an invalid tag definition:

  "tags": [ { "name": "tag_unique" }, { "name": "tag_unique" } ]

The class

public void visitTag(Tag node) {
    List<Tag> tags = ((Document) node.root()).getTags();  <--- Fetching the wrong tags. Should be the parent node.
    int tcount = 0;
    for (Tag tag : tags) {            // <--- NullPointerException when document does not have tags
        if (equals(tag.getName(), node.getName())) {
            tcount++;
        }
    }
    this.reportIf(tcount > 1, node, node.getName(), map("tagName", node.getName()));
}

Fetching the parent Node requires checking the Node type to fetch tags as there is no common Interface for Node with tags.
We could modify the traversal and add the current list to the context when visiting lists and then access it in the rule. 🤷

What is the best way to proceed?

Thanks! 👍

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions