pboettch / json-schema-validator

JSON schema validator for JSON for Modern C++
Other
511 stars 143 forks source link

regex match error? #131

Closed kelaicai closed 4 years ago

kelaicai commented 4 years ago

image I want check the url is valid but regex is right and occurs some exceptions,why?

pboettch commented 4 years ago

Could you share a test-case (schema + instance) which shows the problem?

Here is an example which works for me:

Instance:

{
    "url": "http://www.google.com?source=abc"
}

Schema:

{
    "$id": "http://xxx.local/schemas/mySchema.json",
    "$schema": "http://json-schema.org/draft-07/schema#",

    "type": "object",
    "properties": {
        "url": {
            "type": "string",
            "pattern": "^(http|https)://.*"
        }
    }
}
kelaicai commented 4 years ago

schema like this

{
    "properties":{
        "imgurl":{
            "items":{
                "maxItems":1,
                "minItems":1,
                "properties":{
                    "#text":{
                        "pattern":"http.*",
                        "type":"string"
                    }
                },
                "type":"object"
            },
            "type":"array"
        }
    },
    "required":[
        "imgurl"
    ],
    "type":"object"
}

data like this

{
   "imgurl":[
        {
            "#text":"http://www.google.com"
        }
    ]
}

has any problem? i passed it by [https://json-schema-validator.herokuapp.com/]() it pased.., and i don't know where is error?

pboettch commented 4 years ago

Your schema and data works with the current master-branch of this project. Do you have regular expression activated? What is the C++-version you are using?

Here's a simplified example:

Schema:

{
    "pattern":"http.*",
    "type":"string"
}

data:

"http://www.google.com"
kelaicai commented 4 years ago

gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39) and C++11 nlohmann::json 3.0.0

it work well when I check it in code , ` json js=json::object(); js["imgurl"]=json::array(); json o=json::object(); o["#text"]="http:://www.google.com"; js["imgurl"].push_back(o);

std::string schemastr="{\"properties\":{\"imgurl\":{\"items\":{\"maxItems\":1,\"minItems\":1,\"properties\":{\"#text\":{\"type\":\"string\",\"pattern\":\"http.*\"}},\"type\":\"object\"},\"type\":\"array\"}},\"required\":[\"imgurl\"],\"type\":\"object\"}";
json_schema::json_validator validator;
bool flag=false;
try {
    validator.set_root_schema(schemastr); // insert root-schema
    flag=true;
} catch (const std::exception &e) {
    std::cerr << "Validation of schema failed, here is why: " << e.what() << "\n";
    return EXIT_FAILURE;
}

` it work well in this code and no exception happened ;it seems check logic is right ,let me try to update the schema lib tomorrow to see the result! thanks a lot!

pboettch commented 4 years ago

Ah OK, I see: To make regex work with GCC 4.8 you need to install boost-regex on your system.

pboettch commented 4 years ago

See here: https://github.com/pboettch/json-schema-validator/blob/master/CMakeLists.txt#L128

kelaicai commented 4 years ago

hi ,it work well now; It may be occured by low version gcc use C++11’s std::regex_search ,it can't match regular pattern "http.*" with url and return 0 for me when i use g++4.8. boost::regex_search can match url links ,after I update my g++ to 4.9 or higher complier and compile it ,there is no error happend, so it's occured by std::regex ,and higher compile with boost can solve this problem. thank you!