Skip to content

Validate array only the first elemet #926

@r-alto

Description

@r-alto

Only the first element of an array is validated, see "isInValidNotOk".

package json;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersionDetector;
import com.networknt.schema.ValidationMessage;
import java.util.Set;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class TestValidator {
  private static String jsonschema = "";
  private static String jsonValid = "";
  private static String jsonInValidOk = "";
  private static String jsonInValidNotOk = "";
  
  @BeforeAll
  static void init() {
    jsonschema =
        "{"
            + "  \"$schema\": \"http://json-schema.org/draft-04/schema#\","
            + "  \"type\": \"object\","
            + "  \"properties\": {"
            + "    \"items\": {"
            + "      \"type\": \"array\","
            + "      \"items\": ["
            + "        {"
            + "          \"type\": \"object\","
            + "          \"properties\": {"
            + "            \"name\": {"
            + "              \"type\": \"string\""
            + "            },"
            + "            \"city\": {"
            + "              \"type\": \"string\""
            + "            }"
            + "          },"
            + "          \"required\": ["
            + "            \"name\","
            + "            \"city\""
            + "          ]"
            + "        }"
            + "      ]"
            + "    }"
            + "  },"
            + "  \"required\": ["
            + "    \"items\""
            + "  ]"
            + "}";

    jsonValid =
        "{"
            + "  \"items\":["
            + "    { "
            + "      \"name\" : \"Test1\","
            + "      \"city\" : \"city1\""
            + "    },"
            + "    { "
            + "      \"name\" : \"Test2\","
            + "      \"city\" : \"city2\""
            + "    }"
            + "  ]"
            + "}";

    jsonInValidOk =
        "{"
            + "  \"items\":["
            + "    { "
            + "      \"name\" : \"Test1\""
            + "    },"
            + "    { "
            + "      \"name\" : \"Test2\","
            + "      \"city\" : \"city2\""
            + "    }"
            + "  ]"
            + "}";

    jsonInValidNotOk = "{"
        + "  \"items\":["
        + "    { "
        + "      \"name\" : \"Test1\","
        + "      \"city\" : \"city1\""
        + "    },"
        + "    { "
        + "      \"name\" : \"Test2\""
        + "    }"
        + "  ]"
        + "}";
  }

  @Test
  void isValid() throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode schema = mapper.readTree( jsonschema );
    JsonNode json = mapper.readTree( jsonValid );
    JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersionDetector.detect(schema));
    Set<ValidationMessage> errors = factory.getSchema(schema).validate( json );
    Assertions.assertTrue( errors.isEmpty() );
  }

  @Test
  void isInValidOk() throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode schema = mapper.readTree( jsonschema );
    JsonNode json = mapper.readTree( jsonInValidOk ); // "city" is defined as required
    JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersionDetector.detect(schema));
    Set<ValidationMessage> errors = factory.getSchema(schema).validate( json );
    Assertions.assertFalse( errors.isEmpty() );
  }

  @Test
  void isInValidNotOk() throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode schema = mapper.readTree( jsonschema );
    JsonNode json = mapper.readTree( jsonInValidNotOk ); // "city" is defined as required
    JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersionDetector.detect(schema));
    Set<ValidationMessage> errors = factory.getSchema(schema).validate( json );
    Assertions.assertFalse( errors.isEmpty() );
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions