Home > Back-end >  is JSON schema validator working for PropretyNames?
is JSON schema validator working for PropretyNames?

Time:03-15

i'm using Everit package for JSON schema validation. my use case here is i should allow only property key name as upper case, value can be anything. here is the code i used.

schema.json

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "schema",
  "description": "List of Config",
  "type": "array",
  "items": {
    "propertyNames": {
      "pattern": "^[A-Z0-9_]*$"
    },
    "description": "List of Config",
    "type": "object",
    "properties": {
      "^[A-Z]": {
        "description": " Name",
        "type": "object",
        "properties": {
          "priority": {
            "description": "priority",
            "type": "string",
            "enum": [
              "high",
              "medium",
              "low"
            ]
          }
        },
        "required": [
          "priority"
        ]
      }
    }
  }
} 

product.json

[
  {"TEST":{"priority":"medium"}},
  {"testing":{"priority":"medium"}}
]

Java Code :

private boolean validateTemplateConfig(String emailTemplateConfig) {

        try {
            JSONObject jsonSchema = new JSONObject(
      new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json")));
    JSONObject jsonSubject = new JSONObject(
      new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product.json")));

            Schema schema = SchemaLoader.load(jsonSchema);
            schema.validate(jsonSubject);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
        return true;
}

pom.xml

<dependency>
    <groupId>org.everit.json</groupId>
    <artifactId>org.everit.json.schema</artifactId>
    <version>1.3.0</version>
</dependency>

as per schema definition. program should have thrown error for key "testing" since it is a lower case letter. but instead im getting success response. could you please help here.

i tried this the same schema with another portal (https://www.jsonschemavalidator.net/) . it is giving error.

could you please help here ? is there any problem with dependency. is this very old dependency ?

CodePudding user response:

You are using the draft-04 version of the specification, and propertyNames was only added in draft 6. I don't know what version your implementation defaults to if $schema is not included, but you should try that first, or change your $schema to indicate a different version. The latest published version is https://json-schema.org/draft/2020-12/schema.

CodePudding user response:

As pointed out in @Ether's answer: use a version of JSON Schema that supports the keywords in use (namely "propertyNames" having been introduced in Draft 6) as well as version of the validator library supporting that JSON Schema version (mentioned in comments to the same answer).

In order to then evaluate the contents of the matching schema (i.e., under "TEST" in your example), you should:
replace "properties": { "^[A-Z]": ...
with "patternProperties": { "^[A-Z]": ...

Note: using "patternProperties" in combination with "additionalProperties": false would also be possible under JSON Schema Draft 4. No need for "propertyNames" then.

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "title": "schema",
  "description": "List of Config",
  "type": "array",
  "items": {
    "propertyNames": {
      "pattern": "^[A-Z0-9_]*$"
    },
    "description": "List of Config",
    "type": "object",
    "patternProperties": {
      "^[A-Z]": {
        "description": " Name",
        "type": "object",
        "properties": {
          "priority": {
            "description": "priority",
            "type": "string",
            "enum": [
              "high",
              "medium",
              "low"
            ]
          }
        },
        "required": [
          "priority"
        ]
      }
    }
  }
}
  • Related