Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about how to refactor my code post Path refactor #932

Closed
mikebell90 opened this issue Jan 26, 2024 · 3 comments
Closed

Question about how to refactor my code post Path refactor #932

mikebell90 opened this issue Jan 26, 2024 · 3 comments

Comments

@mikebell90
Copy link

After the large refactor of paths by @justin-tay , being fairly neophytish in my understanding of JsonSchema and many of the objects therein, my code broke. Basically I used validationMessage.getPath() in two places

  1. An informational message. There I suspect the normal toString of the new path object will be fine
  2. I adapted a helpful post by one of the users of this library earlier to determine the line at which the violation caused in the original YAML. The code that broke was
JsonNode findJsonNode(ValidationMessage msg, JsonNode rootNode) {
        // Construct the JSONPointer.
        JsonPointer pathPtr = JsonPointer.valueOf(msg.getPath());
        // Now see if we can find the node.
        return rootNode.at(pathPtr);
    }
LocationDetails getLocationDetails(ValidationMessage msg, JsonNode rootNode) {
        LocationDetails retval = null;
        JsonNode node = findJsonNode(msg, rootNode);
        if (node instanceof LocationProvider) {
            retval = ((LocationProvider) node).getLocationDetails();
        }
        return retval == null ? LocationDetails.EMPTY : retval;
    }

The code breaks because JsonPointer requires a string here. I've given additional context here, and can provide more

I'm not sure how to adjust this, and would appreciate assistance

@justin-tay
Copy link
Contributor

justin-tay commented Jan 26, 2024

I'm not really sure if the node is the schema data or the input data.

If it is the schema data you can try the following

JsonNode findJsonNode(ValidationMessage msg, JsonNode rootNode) {
    // Construct the JSONPointer.
    JsonPointer pathPtr = JsonPointer.valueOf(msg.getSchemaLocation().getFragment().toString());
    // Now see if we can find the node.
    return rootNode.at(pathPtr);
}

If it is the input data you can try the following assuming you configured the PathType to PathType.JSON_POINTER as by default it is PathType.JSON_PATH.

JsonNode findJsonNode(ValidationMessage msg, JsonNode rootNode) {
    // Construct the JSONPointer.
    JsonPointer pathPtr = JsonPointer.valueOf(msg.getInstanceLocation().toString());
    // Now see if we can find the node.
    return rootNode.at(pathPtr);
}

@mikebell90
Copy link
Author

Thanks I’ll dig deeper. This is very helpful

@mikebell90
Copy link
Author

That did the trick thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants