Skip to content

Show value of const data in validation message if its a string #846

@rishabh413

Description

@rishabh413

Below code:

import static com.networknt.schema.SpecVersion.VersionFlag.V202012;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.JsonSchemaFactory;

String schema =
    "{"
        + "  \"type\": \"object\","
        + "  \"properties\": {"
        + "    \"myKey\" : {"
        + "        \"type\" : \"string\","
        + "        \"oneOf\" : ["
        + "          { \"const\": \"aa\" },"
        + "          { \"const\": \"bb\" }"
        + "        ]"
        + "      }"
        + "  }"
        + "}";

String serializedData = "{ \"myKey\" : \"cc\"}";
JsonNode data = new ObjectMapper().readTree(serializedData);

JsonSchemaFactory.getInstance(V202012)
    .getSchema(schema)
    .validate(data)
    .forEach(System.out::println);

gives below validation errors:

$.myKey: should be valid to one and only one schema, but 0 are valid
$.myKey: must be a constant value aa
$.myKey: must be a constant value bb

It would be very helpful if validation errors were:

$.myKey: should be valid to one and only one schema, but 0 are valid
$.myKey: must be a constant value aa but is cc
$.myKey: must be a constant value bb but is cc

so that we can clearly see what has mismatched directly from error.

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