33 #include "Requirement.h"
34 #include "StringRequirement.h"
35 #include "IntegerRequirement.h"
36 #include "NumberRequirement.h"
37 #include "DependencyRequirement.h"
38 #include "RequirementLoader.h"
50 std::map<std::string, std::unique_ptr<Requirement>>
properties_;
89 c.second->ClearErrors();
92 c.second->ClearErrors();
107 c.second->ClearNotices();
110 c.second->ClearNotices();
142 virtual void Parse(Value json,
const std::string& path)
override
148 if(json.isMember(
"additionalProperties"))
150 if(json[
"additionalProperties"].isBool())
151 moreProps_ = json[
"additionalProperties"].asBool();
152 else if(json[
"additionalProperties"].isObject())
153 json[
"properties"][
"additionalProperties"] = json[
"additionalProperties"];
157 if(json.isMember(
"properties") && json[
"properties"].isObject())
159 auto& props = json[
"properties"];
160 auto names = props.getMemberNames();
162 for(
auto& prop : props)
167 properties_[names[i]]->Parse(prop, path +
"/" + names[i]);
175 if(json.isMember(
"patternProperties") && json[
"patternProperties"].isObject())
177 auto& props = json[
"patternProperties"];
178 auto names = props.getMemberNames();
180 for(
auto& prop : props)
196 if(json.isMember(
"required") && json[
"required"].isArray())
198 for(
auto& requirement : json[
"required"])
199 required_.push_back(requirement.asString());
203 if(json.isMember(
"minProperties") && json[
"minProperties"].isUInt())
206 min_ = json[
"minProperties"].asInt();
210 if(json.isMember(
"maxProperties") && json[
"maxProperties"].isUInt())
213 max_ = json[
"maxProperties"].asInt();
217 if(json.isMember(
"dependencies") && json[
"dependencies"].isObject())
224 for(
auto& prop : json)
239 virtual void Validate(
const Value& json,
const std::string& path)
override
243 PushError(path +
": Must be of type \"object\"");
248 PushError(path +
": Object must contain at least " + std::to_string(
min_) +
" properties");
251 PushError(path +
": Object must contain at most " + std::to_string(
max_) +
" properties");
262 for(
const auto& notice :
dependency_->GetNotices())
269 auto names = json.getMemberNames();
271 for(
auto& prop : json)
276 requirement = it->second.get();
281 auto regex = std::regex(pattern.first, std::regex::ECMAScript);
282 if(std::regex_search(names[i], regex))
283 requirement = pattern.second.get();
288 requirement =
properties_[
"additionalProperties"].get();
292 requirement->
Validate(prop, path +
"/" + names[i]);
294 for(
const auto& error : requirement->
GetErrors())
297 for(
const auto& notice : requirement->
GetNotices())
301 PushError(path +
": Invalid property \"" + names[i] +
"\" specified");
303 rprops.erase(std::remove(rprops.begin(), rprops.end(),names[i]),rprops.end());
307 if(
required_.size() && rprops.size() != 0)
309 std::string msg = std::accumulate(rprops.begin(), rprops.end(), std::string(),
310 [](
const std::string& a,
const std::string& b) -> std::string {
311 return a + (a.length() > 0 ?
", " :
"") + b;
313 PushError(path +
": Missing properties: " + msg);
319 requirement->Validate(json, path);
320 if(requirement->HasErrors())
321 for(
const auto& error : requirement->GetErrors())
323 if(requirement->HasNotices())
324 for(
const auto& notice : requirement->GetNotices())
Requires dependencies to be met.
Requirements on an object.
unsigned int max_
Upper bound.
RequireList extended_
List of requirements.
virtual void Parse(Value json, const std::string &path) override
Parse JSON value to generate Requirement(s).
virtual void Reset() override
Reset Requirement.
virtual void ClearNotices() override
Clear notices on all Requirements.
std::map< std::string, std::unique_ptr< Requirement > > patternProps_
Map of patterns the object needs to match.
ObjectRequirement()
Constructor.
std::vector< std::string > required_
List of requirements.
std::map< std::string, std::unique_ptr< Requirement > > properties_
Map of properties the object needs to have.
~ObjectRequirement()
Destructor.
unsigned int min_
Lower bound.
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.
virtual void ClearErrors() override
Clear errors on all Requirements.
std::unique_ptr< DependencyRequirement > dependency_
Dependency requirement.
bool setMax_
If True upper bound is active.
bool moreProps_
If True, more properties need to be set.
bool setMin_
If True lower bound is active.
Helper class to load Requirement.
std::unique_ptr< Requirement > LoadExtended(const Value &json)
Extended Requirement loader.
std::unique_ptr< Requirement > LoadRequirement(const Value &json)
Load specific requirement.
Requirements on input files.
void PushNotice(const std::string ¬ice)
Add message to list of notices.
std::vector< std::string > GetNotices()
Get list of notices.
std::vector< std::string > GetErrors()
Get list of error messages.
virtual void ClearNotices()
Clear list of notice messages.
virtual void Validate(const Value &json, const std::string &path)=0
Validate that JSON value meets requirements.
virtual void ClearErrors()
Clear list of error messages.
virtual bool HasNotices()
Check if notices have been queued.
void PushError(const std::string &error)
Add error to list of error messages.
bool HasErrors()
Check if errors have occured.
std::vector< std::unique_ptr< Requirement > > RequireList
List of Requirements.