Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add quotes while printing const string value to support empty string #845

Closed
rishabh413 opened this issue Aug 10, 2023 · 2 comments
Closed

Comments

@rishabh413
Copy link

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\": \"\" },"
        + "          { \"const\": \"a\" },"
        + "          { \"const\": \"b\" }"
        + "        ]"
        + "      }"
        + "  }"
        + "}";

String serializedData = "{ \"myKey\" : \"c\"}";
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 
$.myKey: must be a constant value a
$.myKey: must be a constant value b

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 ""
$.myKey: must be a constant value "a"
$.myKey: must be a constant value "b"

so that we can clearly see empty string.

I believe we need to change this line to:

const = {0}: must be a constant value "{1}"
@rishabh413
Copy link
Author

rishabh413 commented Mar 8, 2024

Hi @justin-tay

Looks like this message is already fixed as part of #969. PTAL .

If you agree, please close the issue or I can close it.

@stevehu
Copy link
Contributor

stevehu commented Mar 8, 2024

@rishabh413 Thanks for your confirmation.

@stevehu stevehu closed this as completed Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants