Skip to content

if-then does not respect enum values #386

@0xShamil

Description

@0xShamil

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions