networknt / json-schema-validator

A fast Java JSON schema validator that supports draft V4, V6, V7, V2019-09 and V2020-12
Apache License 2.0
822 stars 323 forks source link

date and date-time #979

Closed KOFKOY closed 6 months ago

KOFKOY commented 6 months ago

example: my jsonschema: "restock_date" : { "title" : "Restock Date", "description" : "Date that product will be restocked", "editable" : true, "hidden" : false, "examples" : [ "2020-05-05" ], "type" : "string", "oneOf" : [ { "type" : "string", "format" : "date" }, { "type" : "string", "format" : "date-time" } ] }

Data that needs to be verified: "restock_date" : "2024-02-23"

===============================

This verification will report an error with error code 1022. It should satisfy all the conditions in oneOf at the same time

KOFKOY commented 6 months ago

version 1.3.3

justin-tay commented 6 months ago

Your schema is not valid and there is insufficient information to reproduce. As you are using the format keyword it is possible you are using a dialect where format assertions aren't enabled and therefore the data matches both as the type is string.

KOFKOY commented 6 months ago

Your schema is not valid and there is insufficient information to reproduce. As you are using the format keyword it is possible you are using a dialect where format assertions aren't enabled and therefore the data matches both as the type is string.

This is a jsonschema file downloaded from Amazon. I can't change its format. I'll try another version or look at the source code.

KOFKOY commented 6 months ago

After testing, I found that version 1.1.0 does not have this problem with time verification.

justin-tay commented 6 months ago

Closing as this cannot be reproduced.

package com.networknt.schema;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import com.networknt.schema.SpecVersion.VersionFlag;

public class Issue969Test {
    @Test
    void test() {
        SchemaValidatorsConfig config = new SchemaValidatorsConfig();
        config.setFormatAssertionsEnabled(true);
        String schemaData = "{\r\n"
                + "  \"oneOf\": [\r\n"
                + "    {\r\n"
                + "      \"format\": \"date\"\r\n"
                + "    },\r\n"
                + "    {\r\n"
                + "      \"format\": \"date-time\"\r\n"
                + "    }\r\n"
                + "  ]\r\n"
                + "}";
        JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
        assertTrue(schema.validate("\"2024-02-23\"", InputFormat.JSON).isEmpty());
        assertFalse(schema.validate("\"2024-02-30\"", InputFormat.JSON).isEmpty());
        assertTrue(schema.validate("\"2021-12-31T23:59:59Z\"", InputFormat.JSON).isEmpty());
        assertFalse(schema.validate("\"2021-12-31T23:59:59\"", InputFormat.JSON).isEmpty());
    }
}