again, in relation to solving issue #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
This commit is contained in:
parent
fa98b5b4b6
commit
6f7dcb30d9
@ -441,7 +441,7 @@ public:
|
|||||||
maxLength_(~SizeType(0)),
|
maxLength_(~SizeType(0)),
|
||||||
exclusiveMinimum_(false),
|
exclusiveMinimum_(false),
|
||||||
exclusiveMaximum_(false),
|
exclusiveMaximum_(false),
|
||||||
defaultValue_()
|
defaultValueLength_(0)
|
||||||
{
|
{
|
||||||
typedef typename SchemaDocumentType::ValueType ValueType;
|
typedef typename SchemaDocumentType::ValueType ValueType;
|
||||||
typedef typename ValueType::ConstValueIterator ConstValueIterator;
|
typedef typename ValueType::ConstValueIterator ConstValueIterator;
|
||||||
@ -640,7 +640,7 @@ public:
|
|||||||
// Default
|
// Default
|
||||||
if (const ValueType* v = GetMember(value, GetDefaultValueString()))
|
if (const ValueType* v = GetMember(value, GetDefaultValueString()))
|
||||||
if (v->IsString())
|
if (v->IsString())
|
||||||
defaultValue_ = v->GetString();
|
defaultValueLength_ = v->GetStringLength();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -942,14 +942,9 @@ public:
|
|||||||
if (hasRequired_) {
|
if (hasRequired_) {
|
||||||
context.error_handler.StartMissingProperties();
|
context.error_handler.StartMissingProperties();
|
||||||
for (SizeType index = 0; index < propertyCount_; index++)
|
for (SizeType index = 0; index < propertyCount_; index++)
|
||||||
if (properties_[index].required && !context.propertyExist[index]){
|
if (properties_[index].required && !context.propertyExist[index])
|
||||||
if (properties_[index].schema->defaultValue_.empty() || properties_[index].schema->defaultValue_ == "" ){
|
if (properties_[index].schema->defaultValueLength_ == 0 )
|
||||||
context.error_handler.AddMissingProperty(properties_[index].name);
|
context.error_handler.AddMissingProperty(properties_[index].name);
|
||||||
} else {
|
|
||||||
// std::cout << "default value of " << properties_[index].name.GetString()
|
|
||||||
// << " is:" << properties_[index].schema->defaultValue_ << "\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (context.error_handler.EndMissingProperties())
|
if (context.error_handler.EndMissingProperties())
|
||||||
RAPIDJSON_INVALID_KEYWORD_RETURN(GetRequiredString());
|
RAPIDJSON_INVALID_KEYWORD_RETURN(GetRequiredString());
|
||||||
}
|
}
|
||||||
@ -1441,7 +1436,7 @@ private:
|
|||||||
bool exclusiveMinimum_;
|
bool exclusiveMinimum_;
|
||||||
bool exclusiveMaximum_;
|
bool exclusiveMaximum_;
|
||||||
|
|
||||||
std::string defaultValue_;
|
SizeType defaultValueLength_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename Stack, typename Ch>
|
template<typename Stack, typename Ch>
|
||||||
|
@ -1049,6 +1049,33 @@ TEST(SchemaValidator, Object_Required) {
|
|||||||
"}}");
|
"}}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(SchemaValidator, Object_Required_PassWithDefault) {
|
||||||
|
Document sd;
|
||||||
|
sd.Parse(
|
||||||
|
"{"
|
||||||
|
" \"type\": \"object\","
|
||||||
|
" \"properties\" : {"
|
||||||
|
" \"name\": { \"type\": \"string\", \"default\": \"William Shakespeare\" },"
|
||||||
|
" \"email\" : { \"type\": \"string\", \"default\": \"\" },"
|
||||||
|
" \"address\" : { \"type\": \"string\" },"
|
||||||
|
" \"telephone\" : { \"type\": \"string\" }"
|
||||||
|
" },"
|
||||||
|
" \"required\":[\"name\", \"email\"]"
|
||||||
|
"}");
|
||||||
|
SchemaDocument s(sd);
|
||||||
|
|
||||||
|
VALIDATE(s, "{ \"email\" : \"bill@stratford-upon-avon.co.uk\", \"address\" : \"Henley Street, Stratford-upon-Avon, Warwickshire, England\", \"authorship\" : \"in question\"}", true);
|
||||||
|
INVALIDATE(s, "{ \"name\": \"William Shakespeare\", \"address\" : \"Henley Street, Stratford-upon-Avon, Warwickshire, England\" }", "", "required", "",
|
||||||
|
"{ \"required\": {"
|
||||||
|
" \"instanceRef\": \"#\", \"schemaRef\": \"#\","
|
||||||
|
" \"missing\": [\"email\"]"
|
||||||
|
"}}");
|
||||||
|
INVALIDATE(s, "{}", "", "required", "",
|
||||||
|
"{ \"required\": {"
|
||||||
|
" \"instanceRef\": \"#\", \"schemaRef\": \"#\","
|
||||||
|
" \"missing\": [\"email\"]"
|
||||||
|
"}}");
|
||||||
|
}
|
||||||
|
|
||||||
TEST(SchemaValidator, Object_PropertiesRange) {
|
TEST(SchemaValidator, Object_PropertiesRange) {
|
||||||
Document sd;
|
Document sd;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user