Skip to content

Commit c0daf75

Browse files
authored
Merge pull request Tencent#1279 from bogaotory/master
Avoid `MissingProperty` Error from `SchemaValidator` when a none-zero length value is given as `default` for the property
2 parents af223d4 + 6f7dcb3 commit c0daf75

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

include/rapidjson/schema.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ class Schema {
440440
minLength_(0),
441441
maxLength_(~SizeType(0)),
442442
exclusiveMinimum_(false),
443-
exclusiveMaximum_(false)
443+
exclusiveMaximum_(false),
444+
defaultValueLength_(0)
444445
{
445446
typedef typename SchemaDocumentType::ValueType ValueType;
446447
typedef typename ValueType::ConstValueIterator ConstValueIterator;
@@ -635,6 +636,12 @@ class Schema {
635636
if (const ValueType* v = GetMember(value, GetMultipleOfString()))
636637
if (v->IsNumber() && v->GetDouble() > 0.0)
637638
multipleOf_.CopyFrom(*v, *allocator_);
639+
640+
// Default
641+
if (const ValueType* v = GetMember(value, GetDefaultValueString()))
642+
if (v->IsString())
643+
defaultValueLength_ = v->GetStringLength();
644+
638645
}
639646

640647
~Schema() {
@@ -936,7 +943,8 @@ class Schema {
936943
context.error_handler.StartMissingProperties();
937944
for (SizeType index = 0; index < propertyCount_; index++)
938945
if (properties_[index].required && !context.propertyExist[index])
939-
context.error_handler.AddMissingProperty(properties_[index].name);
946+
if (properties_[index].schema->defaultValueLength_ == 0 )
947+
context.error_handler.AddMissingProperty(properties_[index].name);
940948
if (context.error_handler.EndMissingProperties())
941949
RAPIDJSON_INVALID_KEYWORD_RETURN(GetRequiredString());
942950
}
@@ -1046,6 +1054,7 @@ class Schema {
10461054
RAPIDJSON_STRING_(ExclusiveMinimum, 'e', 'x', 'c', 'l', 'u', 's', 'i', 'v', 'e', 'M', 'i', 'n', 'i', 'm', 'u', 'm')
10471055
RAPIDJSON_STRING_(ExclusiveMaximum, 'e', 'x', 'c', 'l', 'u', 's', 'i', 'v', 'e', 'M', 'a', 'x', 'i', 'm', 'u', 'm')
10481056
RAPIDJSON_STRING_(MultipleOf, 'm', 'u', 'l', 't', 'i', 'p', 'l', 'e', 'O', 'f')
1057+
RAPIDJSON_STRING_(DefaultValue, 'd', 'e', 'f', 'a', 'u', 'l', 't')
10491058

10501059
#undef RAPIDJSON_STRING_
10511060

@@ -1426,6 +1435,8 @@ class Schema {
14261435
SValue multipleOf_;
14271436
bool exclusiveMinimum_;
14281437
bool exclusiveMaximum_;
1438+
1439+
SizeType defaultValueLength_;
14291440
};
14301441

14311442
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)