From 6f7dcb30d9d662bada0e7e2ebd75846b8c5b91f3 Mon Sep 17 00:00:00 2001 From: bogaotory Date: Fri, 1 Jun 2018 21:16:26 +0100 Subject: [PATCH] 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` --- include/rapidjson/schema.h | 15 +++++---------- test/unittest/schematest.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index ab55452..dc0af78 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -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 diff --git a/test/unittest/schematest.cpp b/test/unittest/schematest.cpp index c64bf78..0c61a8a 100644 --- a/test/unittest/schematest.cpp +++ b/test/unittest/schematest.cpp @@ -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;