Skip to content

Commit 6f7dcb3

Browse files
committed
again, in relation to solving issue Tencent#784, use SizeType-typed variable to indicate a none-zero length string has been given in the schema as default value for the json property; added an unittest Object_Required_PassWithDefault
1 parent fa98b5b commit 6f7dcb3

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

include/rapidjson/schema.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ class Schema {
441441
maxLength_(~SizeType(0)),
442442
exclusiveMinimum_(false),
443443
exclusiveMaximum_(false),
444-
defaultValue_()
444+
defaultValueLength_(0)
445445
{
446446
typedef typename SchemaDocumentType::ValueType ValueType;
447447
typedef typename ValueType::ConstValueIterator ConstValueIterator;
@@ -640,7 +640,7 @@ class Schema {
640640
// Default
641641
if (const ValueType* v = GetMember(value, GetDefaultValueString()))
642642
if (v->IsString())
643-
defaultValue_ = v->GetString();
643+
defaultValueLength_ = v->GetStringLength();
644644

645645
}
646646

@@ -942,14 +942,9 @@ class Schema {
942942
if (hasRequired_) {
943943
context.error_handler.StartMissingProperties();
944944
for (SizeType index = 0; index < propertyCount_; index++)
945-
if (properties_[index].required && !context.propertyExist[index]){
946-
if (properties_[index].schema->defaultValue_.empty() || properties_[index].schema->defaultValue_ == "" ){
945+
if (properties_[index].required && !context.propertyExist[index])
946+
if (properties_[index].schema->defaultValueLength_ == 0 )
947947
context.error_handler.AddMissingProperty(properties_[index].name);
948-
} else {
949-
// std::cout << "default value of " << properties_[index].name.GetString()
950-
// << " is:" << properties_[index].schema->defaultValue_ << "\n";
951-
}
952-
}
953948
if (context.error_handler.EndMissingProperties())
954949
RAPIDJSON_INVALID_KEYWORD_RETURN(GetRequiredString());
955950
}
@@ -1441,7 +1436,7 @@ class Schema {
14411436
bool exclusiveMinimum_;
14421437
bool exclusiveMaximum_;
14431438

1444-
std::string defaultValue_;
1439+
SizeType defaultValueLength_;
14451440
};
14461441

14471442
template<typename Stack, typename Ch>

test/unittest/schematest.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,33 @@ TEST(SchemaValidator, Object_Required) {
10491049
"}}");
10501050
}
10511051

1052+
TEST(SchemaValidator, Object_Required_PassWithDefault) {
1053+
Document sd;
1054+
sd.Parse(
1055+
"{"
1056+
" \"type\": \"object\","
1057+
" \"properties\" : {"
1058+
" \"name\": { \"type\": \"string\", \"default\": \"William Shakespeare\" },"
1059+
" \"email\" : { \"type\": \"string\", \"default\": \"\" },"
1060+
" \"address\" : { \"type\": \"string\" },"
1061+
" \"telephone\" : { \"type\": \"string\" }"
1062+
" },"
1063+
" \"required\":[\"name\", \"email\"]"
1064+
"}");
1065+
SchemaDocument s(sd);
1066+
1067+
VALIDATE(s, "{ \"email\" : \"[email protected]\", \"address\" : \"Henley Street, Stratford-upon-Avon, Warwickshire, England\", \"authorship\" : \"in question\"}", true);
1068+
INVALIDATE(s, "{ \"name\": \"William Shakespeare\", \"address\" : \"Henley Street, Stratford-upon-Avon, Warwickshire, England\" }", "", "required", "",
1069+
"{ \"required\": {"
1070+
" \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1071+
" \"missing\": [\"email\"]"
1072+
"}}");
1073+
INVALIDATE(s, "{}", "", "required", "",
1074+
"{ \"required\": {"
1075+
" \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1076+
" \"missing\": [\"email\"]"
1077+
"}}");
1078+
}
10521079

10531080
TEST(SchemaValidator, Object_PropertiesRange) {
10541081
Document sd;

0 commit comments

Comments
 (0)