java-json-tools / json-schema-validator

A JSON Schema validation implementation in pure Java, which aims for correctness and performance, in that order
http://json-schema-validator.herokuapp.com/
Other
1.63k stars 399 forks source link

Comments are not allowed in Referenced JSONs #278

Open abhi4151 opened 6 years ago

abhi4151 commented 6 years ago

Hello, Is there any way to allow the comments in the referenced Json FIles:

Using Feature.Allow_Comments ignore the comments in the immediate json schema using below code:

public static ProcessingReport validateJson(String jsonSchemaNode, String jsonText)
            throws ProcessingException {
        JsonFactory jsonFactory = new JsonFactory();
        jsonFactory.enable(JsonParser.Feature.ALLOW_COMMENTS);
        jsonFactory.enable(JsonParser.Feature.ALLOW_YAML_COMMENTS);
        ObjectMapper mapper = new ObjectMapper(jsonFactory)
                .configure(JsonParser.Feature.ALLOW_YAML_COMMENTS, true)
                .configure(JsonParser.Feature.ALLOW_COMMENTS, true);
        JsonNode jsonSchema = null;
        final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
        try {
            jsonSchema = mapper.readTree(jsonSchemaNode);
            return factory.getJsonSchema(jsonSchema).validate(mapper.readTree(jsonText));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

But if Json file refers another schema file. The comments are not ignored in the imported json schema file.

{
    /*Hello, 
    I'm ABhishekt*/
    //This is a comment
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "SubmitProcess",
    "description": "Sample COmmand",
    "type": "object",   
    "allOf": [{ "$ref": "resource:/JsonSchema/Schema.json#"}]
}

So if there's any comment in the imported Schema.json. It doesn't allow comments in that file.

Please confirm if this could be an enhancement.

It gives an error:

--- BEGIN MESSAGES ---
fatal: content at URI "resource:/JsonSchema/Schema.json#" is not valid JSON
    level: "fatal"
    uri: "resource:/JsonSchema/Schema.json#"
    parsingMessage: "Unexpected character ('/' (code 47)): was expecting double-quote to start field name"
    info: "other messages follow (if any)"
---  END MESSAGES  ---

Here is there anyway that I could either pass configuration or pre-configured factory or mapper to JsonSchema.validate method, so that it could allow comments in all referenced JSON Schemas.