Remove RAPIDJSON_SCHEMA_HAS_REGEX
This commit is contained in:
parent
314ee2281c
commit
6b3244ead3
@ -18,20 +18,18 @@
|
|||||||
#include "document.h"
|
#include "document.h"
|
||||||
#include <cmath> // HUGE_VAL, abs, floor
|
#include <cmath> // HUGE_VAL, abs, floor
|
||||||
|
|
||||||
|
#define RAPIDJSON_SCHEMA_USE_STDREGEX 0
|
||||||
|
|
||||||
#if !defined(RAPIDJSON_SCHEMA_USE_STDREGEX) && (__cplusplus >=201103L || (defined(_MSC_VER) && _MSC_VER >= 1800))
|
#if !defined(RAPIDJSON_SCHEMA_USE_STDREGEX) && (__cplusplus >=201103L || (defined(_MSC_VER) && _MSC_VER >= 1800))
|
||||||
#define RAPIDJSON_SCHEMA_USE_STDREGEX 1
|
#define RAPIDJSON_SCHEMA_USE_STDREGEX 1
|
||||||
|
#else
|
||||||
|
#define RAPIDJSON_SCHEMA_USE_STDREGEX 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if RAPIDJSON_SCHEMA_USE_STDREGEX
|
#if RAPIDJSON_SCHEMA_USE_STDREGEX
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if RAPIDJSON_SCHEMA_USE_STDREGEX // or some other implementation
|
|
||||||
#define RAPIDJSON_SCHEMA_HAS_REGEX 1
|
|
||||||
#else
|
|
||||||
#define RAPIDJSON_SCHEMA_HAS_REGEX 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(__GNUC__)
|
#if defined(__GNUC__)
|
||||||
RAPIDJSON_DIAG_PUSH
|
RAPIDJSON_DIAG_PUSH
|
||||||
RAPIDJSON_DIAG_OFF(effc++)
|
RAPIDJSON_DIAG_OFF(effc++)
|
||||||
@ -92,20 +90,16 @@ template <typename Encoding>
|
|||||||
struct SchemaValidationContext {
|
struct SchemaValidationContext {
|
||||||
SchemaValidationContext(const BaseSchema<Encoding>* s) :
|
SchemaValidationContext(const BaseSchema<Encoding>* s) :
|
||||||
schema(s), valueSchema(), notValidator(), objectDependencies(),
|
schema(s), valueSchema(), notValidator(), objectDependencies(),
|
||||||
#if RAPIDJSON_SCHEMA_HAS_REGEX
|
|
||||||
patternPropertiesSchemas(),
|
patternPropertiesSchemas(),
|
||||||
patternPropertiesSchemaCount(),
|
patternPropertiesSchemaCount(),
|
||||||
valuePatternValidatorType(kPatternValidatorOnly),
|
valuePatternValidatorType(kPatternValidatorOnly),
|
||||||
#endif
|
|
||||||
inArray(false)
|
inArray(false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
~SchemaValidationContext() {
|
~SchemaValidationContext() {
|
||||||
delete notValidator;
|
delete notValidator;
|
||||||
#if RAPIDJSON_SCHEMA_HAS_REGEX
|
|
||||||
delete patternPropertiesSchemas;
|
delete patternPropertiesSchemas;
|
||||||
#endif
|
|
||||||
delete[] objectDependencies;
|
delete[] objectDependencies;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,13 +109,11 @@ struct SchemaValidationContext {
|
|||||||
SchemaValidatorArray<Encoding> anyOfValidators;
|
SchemaValidatorArray<Encoding> anyOfValidators;
|
||||||
SchemaValidatorArray<Encoding> oneOfValidators;
|
SchemaValidatorArray<Encoding> oneOfValidators;
|
||||||
SchemaValidatorArray<Encoding> dependencyValidators;
|
SchemaValidatorArray<Encoding> dependencyValidators;
|
||||||
#if RAPIDJSON_SCHEMA_HAS_REGEX
|
|
||||||
SchemaValidatorArray<Encoding> patternPropertiesValidators;
|
SchemaValidatorArray<Encoding> patternPropertiesValidators;
|
||||||
const BaseSchema<Encoding>** patternPropertiesSchemas;
|
const BaseSchema<Encoding>** patternPropertiesSchemas;
|
||||||
SizeType patternPropertiesSchemaCount;
|
SizeType patternPropertiesSchemaCount;
|
||||||
PatternValidatorType valuePatternValidatorType;
|
PatternValidatorType valuePatternValidatorType;
|
||||||
PatternValidatorType objectPatternValidatorType;
|
PatternValidatorType objectPatternValidatorType;
|
||||||
#endif
|
|
||||||
GenericSchemaValidator<Encoding, BaseReaderHandler<>, CrtAllocator>* notValidator;
|
GenericSchemaValidator<Encoding, BaseReaderHandler<>, CrtAllocator>* notValidator;
|
||||||
SizeType objectRequiredCount;
|
SizeType objectRequiredCount;
|
||||||
SizeType arrayElementIndex;
|
SizeType arrayElementIndex;
|
||||||
@ -141,10 +133,8 @@ public:
|
|||||||
type_((1 << kTotalSchemaType) - 1), // typeless
|
type_((1 << kTotalSchemaType) - 1), // typeless
|
||||||
properties_(),
|
properties_(),
|
||||||
additionalPropertiesSchema_(),
|
additionalPropertiesSchema_(),
|
||||||
#if RAPIDJSON_SCHEMA_HAS_REGEX
|
|
||||||
patternProperties_(),
|
patternProperties_(),
|
||||||
patternPropertyCount_(),
|
patternPropertyCount_(),
|
||||||
#endif
|
|
||||||
propertyCount_(),
|
propertyCount_(),
|
||||||
requiredCount_(),
|
requiredCount_(),
|
||||||
minProperties_(),
|
minProperties_(),
|
||||||
@ -159,9 +149,7 @@ public:
|
|||||||
minItems_(),
|
minItems_(),
|
||||||
maxItems_(SizeType(~0)),
|
maxItems_(SizeType(~0)),
|
||||||
additionalItems_(true),
|
additionalItems_(true),
|
||||||
#if RAPIDJSON_SCHEMA_USE_STDREGEX
|
|
||||||
pattern_(),
|
pattern_(),
|
||||||
#endif
|
|
||||||
minLength_(0),
|
minLength_(0),
|
||||||
maxLength_(~SizeType(0)),
|
maxLength_(~SizeType(0)),
|
||||||
minimum_(-HUGE_VAL),
|
minimum_(-HUGE_VAL),
|
||||||
@ -242,7 +230,6 @@ public:
|
|||||||
properties_[index].typeless = false;
|
properties_[index].typeless = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if RAPIDJSON_SCHEMA_HAS_REGEX
|
|
||||||
if (const ValueType* v = GetMember(value, "patternProperties")) {
|
if (const ValueType* v = GetMember(value, "patternProperties")) {
|
||||||
patternProperties_ = new PatternProperty[v->MemberCount()];
|
patternProperties_ = new PatternProperty[v->MemberCount()];
|
||||||
patternPropertyCount_ = 0;
|
patternPropertyCount_ = 0;
|
||||||
@ -253,7 +240,6 @@ public:
|
|||||||
patternPropertyCount_++;
|
patternPropertyCount_++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
if (required && required->IsArray())
|
if (required && required->IsArray())
|
||||||
for (ConstValueIterator itr = required->Begin(); itr != required->End(); ++itr)
|
for (ConstValueIterator itr = required->Begin(); itr != required->End(); ++itr)
|
||||||
@ -322,10 +308,8 @@ public:
|
|||||||
AssignIfExist(minLength_, value, "minLength");
|
AssignIfExist(minLength_, value, "minLength");
|
||||||
AssignIfExist(maxLength_, value, "maxLength");
|
AssignIfExist(maxLength_, value, "maxLength");
|
||||||
|
|
||||||
#if RAPIDJSON_SCHEMA_HAS_REGEX
|
|
||||||
if (const ValueType* v = GetMember(value, "pattern"))
|
if (const ValueType* v = GetMember(value, "pattern"))
|
||||||
pattern_ = CreatePattern(*v);
|
pattern_ = CreatePattern(*v);
|
||||||
#endif // RAPIDJSON_SCHEMA_HAS_REGEX
|
|
||||||
|
|
||||||
// Number
|
// Number
|
||||||
ConstMemberIterator minimumItr = value.FindMember("minimum");
|
ConstMemberIterator minimumItr = value.FindMember("minimum");
|
||||||
@ -354,9 +338,7 @@ public:
|
|||||||
delete not_;
|
delete not_;
|
||||||
delete [] properties_;
|
delete [] properties_;
|
||||||
delete additionalPropertiesSchema_;
|
delete additionalPropertiesSchema_;
|
||||||
#if RAPIDJSON_SCHEMA_HAS_REGEX
|
|
||||||
delete [] patternProperties_;
|
delete [] patternProperties_;
|
||||||
#endif
|
|
||||||
delete additionalItemsSchema_;
|
delete additionalItemsSchema_;
|
||||||
delete itemsList_;
|
delete itemsList_;
|
||||||
for (SizeType i = 0; i < itemsTupleCount_; i++)
|
for (SizeType i = 0; i < itemsTupleCount_; i++)
|
||||||
@ -516,10 +498,8 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if RAPIDJSON_SCHEMA_HAS_REGEX
|
if (pattern_ && !IsPatternMatch(pattern_, str, length))
|
||||||
if (pattern_ && !IsPatternMatch(*pattern_, str, length))
|
|
||||||
return false;
|
return false;
|
||||||
#endif
|
|
||||||
|
|
||||||
return !enum_.IsArray() || CheckEnum(GenericValue<Encoding>(str, length).Move());
|
return !enum_.IsArray() || CheckEnum(GenericValue<Encoding>(str, length).Move());
|
||||||
}
|
}
|
||||||
@ -546,14 +526,12 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Key(Context& context, const Ch* str, SizeType len, bool) const {
|
bool Key(Context& context, const Ch* str, SizeType len, bool) const {
|
||||||
#if RAPIDJSON_SCHEMA_HAS_REGEX
|
|
||||||
if (patternProperties_) {
|
if (patternProperties_) {
|
||||||
context.patternPropertiesSchemaCount = 0;
|
context.patternPropertiesSchemaCount = 0;
|
||||||
for (SizeType i = 0; i < patternPropertyCount_; i++)
|
for (SizeType i = 0; i < patternPropertyCount_; i++)
|
||||||
if (patternProperties_[i].pattern && IsPatternMatch(*patternProperties_[i].pattern, str, len))
|
if (patternProperties_[i].pattern && IsPatternMatch(patternProperties_[i].pattern, str, len))
|
||||||
context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = patternProperties_[i].schema;
|
context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = patternProperties_[i].schema;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
SizeType index;
|
SizeType index;
|
||||||
if (FindPropertyIndex(str, len, &index)) {
|
if (FindPropertyIndex(str, len, &index)) {
|
||||||
@ -590,11 +568,7 @@ public:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if RAPIDJSON_SCHEMA_HAS_REGEX
|
|
||||||
return context.patternPropertiesSchemaCount != 0; // patternProperties are not additional properties
|
return context.patternPropertiesSchemaCount != 0; // patternProperties are not additional properties
|
||||||
#else
|
|
||||||
return false;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EndObject(Context& context, SizeType memberCount) const {
|
bool EndObject(Context& context, SizeType memberCount) const {
|
||||||
@ -693,10 +667,15 @@ private:
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool IsPatternMatch(const std::basic_regex<Ch>& pattern, const Ch *str, SizeType length) {
|
static bool IsPatternMatch(const std::basic_regex<Ch>* pattern, const Ch *str, SizeType length) {
|
||||||
std::match_results<const Ch*> r;
|
std::match_results<const Ch*> r;
|
||||||
return std::regex_search(str, str + length, r, pattern);
|
return std::regex_search(str, str + length, r, pattern);
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
template <typename ValueType>
|
||||||
|
void* CreatePattern(const ValueType&) { return 0; }
|
||||||
|
|
||||||
|
static bool IsPatternMatch(const void*, const Ch *, SizeType) { return true; }
|
||||||
#endif // RAPIDJSON_SCHEMA_USE_STDREGEX
|
#endif // RAPIDJSON_SCHEMA_USE_STDREGEX
|
||||||
|
|
||||||
void AddType(const Value& type) {
|
void AddType(const Value& type) {
|
||||||
@ -790,7 +769,6 @@ private:
|
|||||||
bool typeless;
|
bool typeless;
|
||||||
};
|
};
|
||||||
|
|
||||||
#if RAPIDJSON_SCHEMA_HAS_REGEX
|
|
||||||
struct PatternProperty {
|
struct PatternProperty {
|
||||||
PatternProperty() : schema(), pattern() {}
|
PatternProperty() : schema(), pattern() {}
|
||||||
~PatternProperty() {
|
~PatternProperty() {
|
||||||
@ -801,9 +779,10 @@ private:
|
|||||||
BaseSchema<Encoding>* schema;
|
BaseSchema<Encoding>* schema;
|
||||||
#if RAPIDJSON_SCHEMA_USE_STDREGEX
|
#if RAPIDJSON_SCHEMA_USE_STDREGEX
|
||||||
std::basic_regex<Ch>* pattern;
|
std::basic_regex<Ch>* pattern;
|
||||||
|
#else
|
||||||
|
void *pattern;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
#endif
|
|
||||||
|
|
||||||
MemoryPoolAllocator<> allocator_;
|
MemoryPoolAllocator<> allocator_;
|
||||||
GenericValue<Encoding> enum_;
|
GenericValue<Encoding> enum_;
|
||||||
@ -815,10 +794,8 @@ private:
|
|||||||
|
|
||||||
Property* properties_;
|
Property* properties_;
|
||||||
BaseSchema<Encoding>* additionalPropertiesSchema_;
|
BaseSchema<Encoding>* additionalPropertiesSchema_;
|
||||||
#if RAPIDJSON_SCHEMA_HAS_REGEX
|
|
||||||
PatternProperty* patternProperties_;
|
PatternProperty* patternProperties_;
|
||||||
SizeType patternPropertyCount_;
|
SizeType patternPropertyCount_;
|
||||||
#endif
|
|
||||||
SizeType propertyCount_;
|
SizeType propertyCount_;
|
||||||
SizeType requiredCount_;
|
SizeType requiredCount_;
|
||||||
SizeType minProperties_;
|
SizeType minProperties_;
|
||||||
@ -837,6 +814,8 @@ private:
|
|||||||
|
|
||||||
#if RAPIDJSON_SCHEMA_USE_STDREGEX
|
#if RAPIDJSON_SCHEMA_USE_STDREGEX
|
||||||
std::basic_regex<Ch>* pattern_;
|
std::basic_regex<Ch>* pattern_;
|
||||||
|
#else
|
||||||
|
void* pattern_;
|
||||||
#endif
|
#endif
|
||||||
SizeType minLength_;
|
SizeType minLength_;
|
||||||
SizeType maxLength_;
|
SizeType maxLength_;
|
||||||
|
@ -149,7 +149,6 @@ TEST(SchemaValidator, String_LengthRange) {
|
|||||||
VALIDATE(s, "\"ABCD\"", false);
|
VALIDATE(s, "\"ABCD\"", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if RAPIDJSON_SCHEMA_HAS_REGEX
|
|
||||||
TEST(SchemaValidator, String_Pattern) {
|
TEST(SchemaValidator, String_Pattern) {
|
||||||
Document sd;
|
Document sd;
|
||||||
sd.Parse("{\"type\":\"string\",\"pattern\":\"^(\\\\([0-9]{3}\\\\))?[0-9]{3}-[0-9]{4}$\"}");
|
sd.Parse("{\"type\":\"string\",\"pattern\":\"^(\\\\([0-9]{3}\\\\))?[0-9]{3}-[0-9]{4}$\"}");
|
||||||
@ -160,7 +159,6 @@ TEST(SchemaValidator, String_Pattern) {
|
|||||||
VALIDATE(s, "\"(888)555-1212 ext. 532\"", false);
|
VALIDATE(s, "\"(888)555-1212 ext. 532\"", false);
|
||||||
VALIDATE(s, "\"(800)FLOWERS\"", false);
|
VALIDATE(s, "\"(800)FLOWERS\"", false);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
TEST(SchemaValidator, Integer) {
|
TEST(SchemaValidator, Integer) {
|
||||||
Document sd;
|
Document sd;
|
||||||
@ -387,8 +385,6 @@ TEST(SchemaValidator, Object_SchemaDependencies) {
|
|||||||
VALIDATE(s, "{\"name\": \"John Doe\", \"billing_address\" : \"555 Debtor's Lane\"}", true);
|
VALIDATE(s, "{\"name\": \"John Doe\", \"billing_address\" : \"555 Debtor's Lane\"}", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if RAPIDJSON_SCHEMA_HAS_REGEX
|
|
||||||
|
|
||||||
TEST(SchemaValidator, Object_PatternProperties) {
|
TEST(SchemaValidator, Object_PatternProperties) {
|
||||||
Document sd;
|
Document sd;
|
||||||
sd.Parse(
|
sd.Parse(
|
||||||
@ -429,8 +425,6 @@ TEST(SchemaValidator, Object_PatternProperties_AdditionalProperties) {
|
|||||||
VALIDATE(s, "{ \"keyword\": 42 }", false);
|
VALIDATE(s, "{ \"keyword\": 42 }", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // RAPIDJSON_SCHEMA_HAS_REGEX
|
|
||||||
|
|
||||||
TEST(SchemaValidator, Array) {
|
TEST(SchemaValidator, Array) {
|
||||||
Document sd;
|
Document sd;
|
||||||
sd.Parse("{\"type\":\"array\"}");
|
sd.Parse("{\"type\":\"array\"}");
|
||||||
@ -686,10 +680,8 @@ TEST(SchemaValidator, TestSuite) {
|
|||||||
"multipleOf.json",
|
"multipleOf.json",
|
||||||
"not.json",
|
"not.json",
|
||||||
"oneOf.json",
|
"oneOf.json",
|
||||||
#if RAPIDJSON_SCHEMA_HAS_REGEX
|
|
||||||
"pattern.json",
|
"pattern.json",
|
||||||
"patternProperties.json",
|
"patternProperties.json",
|
||||||
#endif
|
|
||||||
"properties.json",
|
"properties.json",
|
||||||
//"ref.json",
|
//"ref.json",
|
||||||
//"refRemote.json",
|
//"refRemote.json",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user