SSAGES  0.9.3
Software Suite for Advanced General Ensemble Simulations
StringRequirement.h
1 
26 #pragma once
27 
28 #include <regex>
29 #include "Requirement.h"
30 
31 namespace Json
32 {
34 
38  {
39  private:
40  bool minSet_;
41  bool maxSet_;
42  bool rgxSet_;
43  size_t minLength_;
44  size_t maxLength_;
45  std::regex regex_;
46  std::string expr_;
47  std::string path_;
48  std::vector<std::string> enum_;
49 
50  public:
53  minSet_(false), maxSet_(false), rgxSet_(false),
54  minLength_(0), maxLength_(0), regex_(), expr_(), path_(), enum_(0)
55  {}
56 
58  virtual void Reset() override
59  {
60  minSet_ = false;
61  maxSet_ = false;
62  rgxSet_ = false;
63  minLength_ = 0;
64  maxLength_ = 0;
65  regex_ = "";
66  expr_ = "";
67  path_ = "";
68  enum_.clear();
69  ClearErrors();
70  ClearNotices();
71  }
72 
74 
78  virtual void Parse(Value json, const std::string& path) override
79  {
80  Reset();
81 
82  path_ = path;
83  if(json.isMember("minLength") && json["minLength"].isUInt())
84  {
85  minSet_ = true;
86  minLength_ = json["minLength"].asUInt();
87  }
88 
89  if(json.isMember("maxLength") && json["maxLength"].isUInt())
90  {
91  maxSet_ = true;
92  maxLength_ = json["maxLength"].asUInt();
93 
94  }
95 
96  if(json.isMember("pattern") && json["pattern"].isString())
97  {
98  rgxSet_ = true;
99  expr_ = json["pattern"].asString();
100  regex_ = std::regex(expr_, std::regex::ECMAScript);
101  }
102 
103  if(json.isMember("enum") && json["enum"].isArray())
104  {
105  for(const auto& val : json["enum"])
106  enum_.push_back(val.asString());
107  }
108  }
109 
111 
120  virtual void Validate(const Value& json, const std::string& path) override
121  {
122  if(!json.isString())
123  {
124  PushError(path + ": Must be of type \"string\"");
125  return;
126  }
127 
128  if(minSet_ && json.asString().length() < minLength_)
129  PushError(path + ": Length must be greater than " + std::to_string(minLength_));
130 
131  if(maxSet_ && json.asString().length() > maxLength_)
132  PushError(path + ": Length must be less than " + std::to_string(minLength_));
133 
134  if(rgxSet_ && !std::regex_match(json.asString(), regex_))
135  PushError(path + ": String must match regular expression \"" + expr_ + "\"");
136 
137  if(enum_.size() && std::find(enum_.begin(),enum_.end(), json.asString()) == enum_.end())
138  PushError(path + ": String is not a valid entry");
139  }
140  };
141 }
Requirements on input files.
Definition: Requirement.h:40
virtual void ClearNotices()
Clear list of notice messages.
Definition: Requirement.h:110
virtual void ClearErrors()
Clear list of error messages.
Definition: Requirement.h:95
void PushError(const std::string &error)
Add error to list of error messages.
Definition: Requirement.h:53
Requirements on strings.
size_t minLength_
Minimum string length;.
std::string path_
Path for JSON path specification.
std::vector< std::string > enum_
Enum values.
virtual void Validate(const Value &json, const std::string &path) override
Validate string value.
virtual void Reset() override
Reset Requirement.
bool minSet_
If True, minimum length requirement is active.
std::regex regex_
Regular expression to match string to.
std::string expr_
Expression.
size_t maxLength_
Maximum string length;.
bool maxSet_
If True, maximum length requirement is active.
virtual void Parse(Value json, const std::string &path) override
Parse JSON value to generate Requirement.
bool rgxSet_
If True, string has to match regular expression.
StringRequirement()
Constructor.