Commit 6f7dcb30 authored by bogaotory's avatar bogaotory

again, in relation to solving issue #784, use `SizeType`-typed variable to…

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`
parent fa98b5b4
......@@ -441,7 +441,7 @@ public:
maxLength_(~SizeType(0)),
exclusiveMinimum_(false),
exclusiveMaximum_(false),
defaultValue_()
defaultValueLength_(0)
{
typedef typename SchemaDocumentType::ValueType ValueType;
typedef typename ValueType::ConstValueIterator ConstValueIterator;
......@@ -640,7 +640,7 @@ public:
// Default
if (const ValueType* v = GetMember(value, GetDefaultValueString()))
if (v->IsString())
defaultValue_ = v->GetString();
defaultValueLength_ = v->GetStringLength();
}
......@@ -942,14 +942,9 @@ public:
if (hasRequired_) {
context.error_handler.StartMissingProperties();
for (SizeType index = 0; index < propertyCount_; index++)
if (properties_[index].required && !context.propertyExist[index]){
if (properties_[index].schema->defaultValue_.empty() || properties_[index].schema->defaultValue_ == "" ){
if (properties_[index].required && !context.propertyExist[index])
if (properties_[index].schema->defaultValueLength_ == 0 )
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())
RAPIDJSON_INVALID_KEYWORD_RETURN(GetRequiredString());
}
......@@ -1441,7 +1436,7 @@ private:
bool exclusiveMinimum_;
bool exclusiveMaximum_;
std::string defaultValue_;
SizeType defaultValueLength_;
};
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) {
Document sd;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment