json-schema-validator icon indicating copy to clipboard operation
json-schema-validator copied to clipboard

if-then does not respect enum values

Open 0xShamil opened this issue 4 years ago • 2 comments

Hi.

The if-then construct with the combination of allOf does not respect the enums defined in the property value. The properties value as provided in the if construct is alone considered valid.

Considering the example from the conditionals page:

{
  "type": "object",
  "properties": {
    "street_address": {
      "type": "string"
    },
    "country": {
      "enum": ["United States of America", "Canada", "Netherlands"]
    }
  },
  "allOf": [
    {
      "if": {
        "properties": { "country": { "const": "United States of America" } }
      },
      "then": {
        "properties": { "postal_code": { "pattern": "[0-9]{5}(-[0-9]{4})?" } }
      }
    },
    {
      "if": {
        "properties": { "country": { "const": "Canada" } }
      },
      "then": {
        "properties": { "postal_code": { "pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" } }
      }
    },
    {
      "if": {
        "properties": { "country": { "const": "Netherlands" } }
      },
      "then": {
        "properties": { "postal_code": { "pattern": "[0-9]{4} [A-Z]{2}" } }
      }
    }
  ]
}

The JsonSchema is constructed as follows:

  JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
  SchemaValidatorsConfig config = new SchemaValidatorsConfig();
  config.setFailFast(true);
  InputStream stream = // read schema
  JsonSchema schema = factory.getSchema(stream, config);

If I take the first example

{
  "street_address": "1600 Pennsylvania Avenue NW",
  "country": "United States of America",
  "postal_code": "20500"
}

then I get the following error:

com.networknt.schema.JsonSchemaException: $.country: must be a constant value Canada
	at com.networknt.schema.BaseJsonValidator.buildValidationMessage(BaseJsonValidator.java:130)
	at com.networknt.schema.ConstValidator.validate(ConstValidator.java:44)
	at com.networknt.schema.JsonSchema.validate(JsonSchema.java:224)
	at com.networknt.schema.PropertiesValidator.validate(PropertiesValidator.java:75)
	at com.networknt.schema.JsonSchema.validate(JsonSchema.java:224)
	at com.networknt.schema.IfValidator.validate(IfValidator.java:54)
	at com.networknt.schema.JsonSchema.validate(JsonSchema.java:224)
	at com.networknt.schema.AllOfValidator.validate(AllOfValidator.java:44)
	at com.networknt.schema.JsonSchema.validate(JsonSchema.java:224)
	at com.networknt.schema.BaseJsonValidator.validate(BaseJsonValidator.java:101)

If I instead use the second example with the country as Canada,

{
  "street_address": "24 Sussex Drive",
  "country": "Canada",
  "postal_code": "K1M 1M4"
}

then I get the error with United States of America:

com.networknt.schema.JsonSchemaException: $.country: must be a constant value United States of America
	at com.networknt.schema.BaseJsonValidator.buildValidationMessage(BaseJsonValidator.java:130)
	at com.networknt.schema.ConstValidator.validate(ConstValidator.java:44)
	at com.networknt.schema.JsonSchema.validate(JsonSchema.java:224)
	at com.networknt.schema.PropertiesValidator.validate(PropertiesValidator.java:75)
	at com.networknt.schema.JsonSchema.validate(JsonSchema.java:224)
	at com.networknt.schema.IfValidator.validate(IfValidator.java:54)
	at com.networknt.schema.JsonSchema.validate(JsonSchema.java:224)
	at com.networknt.schema.AllOfValidator.validate(AllOfValidator.java:44)
	at com.networknt.schema.JsonSchema.validate(JsonSchema.java:224)
	at com.networknt.schema.BaseJsonValidator.validate(BaseJsonValidator.java:101)

0xShamil avatar Mar 16 '21 18:03 0xShamil

@0xShamil Thanks a lot for raising the issue with details. I will take a look at it once I have some free cycles.

stevehu avatar Mar 18 '21 14:03 stevehu

if set the failFast=false , (config.setFailFast(false)), this case will pass. The fail-fast mechanism would not wait for the "if /properties/const" condition to walk over.

LiuKay avatar Mar 30 '21 02:03 LiuKay

This was resolved in pull-request #816

fdutton avatar Jun 11 '23 21:06 fdutton