Skip to content

Commit fa98b5b

Browse files
committed
in relation to solving issue Tencent#784, this commit enables the schema to recognise the "default" property, and avoids a missing property error when a default is given in the schema
1 parent af223d4 commit fa98b5b

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

include/rapidjson/schema.h

Lines changed: 19 additions & 3 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+
defaultValue_()
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+
defaultValue_ = v->GetString();
644+
638645
}
639646

640647
~Schema() {
@@ -935,8 +942,14 @@ class Schema {
935942
if (hasRequired_) {
936943
context.error_handler.StartMissingProperties();
937944
for (SizeType index = 0; index < propertyCount_; index++)
938-
if (properties_[index].required && !context.propertyExist[index])
939-
context.error_handler.AddMissingProperty(properties_[index].name);
945+
if (properties_[index].required && !context.propertyExist[index]){
946+
if (properties_[index].schema->defaultValue_.empty() || properties_[index].schema->defaultValue_ == "" ){
947+
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+
}
940953
if (context.error_handler.EndMissingProperties())
941954
RAPIDJSON_INVALID_KEYWORD_RETURN(GetRequiredString());
942955
}
@@ -1046,6 +1059,7 @@ class Schema {
10461059
RAPIDJSON_STRING_(ExclusiveMinimum, 'e', 'x', 'c', 'l', 'u', 's', 'i', 'v', 'e', 'M', 'i', 'n', 'i', 'm', 'u', 'm')
10471060
RAPIDJSON_STRING_(ExclusiveMaximum, 'e', 'x', 'c', 'l', 'u', 's', 'i', 'v', 'e', 'M', 'a', 'x', 'i', 'm', 'u', 'm')
10481061
RAPIDJSON_STRING_(MultipleOf, 'm', 'u', 'l', 't', 'i', 'p', 'l', 'e', 'O', 'f')
1062+
RAPIDJSON_STRING_(DefaultValue, 'd', 'e', 'f', 'a', 'u', 'l', 't')
10491063

10501064
#undef RAPIDJSON_STRING_
10511065

@@ -1426,6 +1440,8 @@ class Schema {
14261440
SValue multipleOf_;
14271441
bool exclusiveMinimum_;
14281442
bool exclusiveMaximum_;
1443+
1444+
std::string defaultValue_;
14291445
};
14301446

14311447
template<typename Stack, typename Ch>

0 commit comments

Comments
 (0)