0

(This question is not duplicate of regex to validate ipv4

It is not asking "somehow". It is asking to do that with rapidjson library:

rapidjson

I have already tried options of regex to validate ipv4, but with rapidjson did not work.)

I am using rapidjson and I want to validate JSON against schema.

rapidjson ignores "format" of "string" but it is indicated that it does support "pattern". So, I try this pattern to validate ipv4 format:

^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\\.(?!$)|$)){4}$

This normally is able to validate ipv4 in a json (draft#4) (checked with other json validators).

However, rapidjson still fails: It returns OK for an invalid ip string.

Here is complete schema and test data of C++:

std::string strschema = R"({"type":"object","properties":{"ip":{"type":"string","pattern":"^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\\.(?!$)|$)){4}$"}}, "required":["ip"]})";
std::string json = R"({"ip":"hahaha"})";

rapidjson::Document schema;
schema.Parse(strschema.c_str());
rapidjson::SchemaDocument schemaDocument(schema);
rapidjson::SchemaValidator schemaValidator(schemaDocument);

rapidjson::Document doc;
doc.Parse(json.c_str());

bool b = doc.Accept(schemaValidator);

How can I validate an ipv4 format in rapidjson using pattern, not with std::regex but with internal regex RAPIDJSON_SCHEMA_USE_INTERNALREGEX?

Mert Mertce
  • 1,049
  • 7
  • 34
  • Well, maybe you should copy the value with key "ip" outside of the json document and then compare it with the regex expression you have – Alvov1 Mar 06 '23 at 09:35

0 Answers0