SSAGES  0.1
A MetaDynamics Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
OneOfRequirement.h
1 
26 #pragma once
27 
28 #include "Requirement.h"
29 #include "RequirementLoader.h"
30 
31 namespace Json
32 {
34 
38  {
39  private:
41 
42  public:
45 
47  virtual void ClearErrors() override
48  {
49  for(auto& r : reqs_)
50  r->ClearErrors();
51 
53  }
54 
56  virtual void ClearNotices() override
57  {
58  for(auto& r : reqs_)
59  r->ClearNotices();
60 
62  }
63 
65  virtual void Reset() override
66  {
67  ClearErrors();
68  ClearNotices();
69  reqs_.clear();
70  }
71 
73 
77  virtual void Parse(Value json, const std::string& path) override
78  {
79  Reset();
80  RequirementLoader loader;
81 
82  auto& head = json.isMember("oneOf") ? json["oneOf"] : json;
83 
84  for(auto& val : head)
85  if(auto req = loader.LoadRequirement(val))
86  {
87  reqs_.push_back(std::move(req));
88  reqs_.back()->Parse(val, path);
89  }
90 
91  }
92 
94 
98  virtual void Validate(const Value& json, const std::string& path) override
99  {
100  int validated = 0;
101  for(auto& r : reqs_)
102  {
103  r->Validate(json, path);
104  if(!r->HasErrors())
105  ++validated;
106  }
107 
108  if(validated > 1)
109  PushError(path + ": Input must validate against only one schema");
110  else if(validated == 0)
111  for(auto& r : reqs_)
112  {
113  if(r->HasErrors())
114  for(const auto& error : r->GetErrors())
115  PushError(error);
116 
117  if(r->HasNotices())
118  for(const auto& notice : r->GetNotices())
119  PushNotice(notice);
120  }
121  }
122  };
123 }
void PushError(const std::string &error)
Add error to list of error messages.
Definition: Requirement.h:53
virtual void ClearErrors() override
Clear list of error messages for all Requirements.
virtual void Reset() override
Reset requirement.
RequireList reqs_
List of Requirements.
virtual void ClearErrors()
Clear list of error messages.
Definition: Requirement.h:95
std::unique_ptr< Requirement > LoadRequirement(const Value &json)
Load specific requirement.
Requires exactly one of a list of Requirements to hold.
OneOfRequirement()
Constructor.
Helper class to load Requirement.
virtual void Parse(Value json, const std::string &path) override
Parse JSON value to generate Requirement.
virtual void ClearNotices()
Clear list of notice messages.
Definition: Requirement.h:110
virtual void Validate(const Value &json, const std::string &path) override
Validate that only one of the Requirements hold.
Requirements on input files.
Definition: Requirement.h:39
std::vector< std::unique_ptr< Requirement >> RequireList
List of Requirements.
Definition: Requirement.h:117
void PushNotice(const std::string &notice)
Add message to list of notices.
Definition: Requirement.h:62
virtual void ClearNotices() override
Clear list of notices for all Requirements.