SSAGES  0.8.3
Software Suite for Advanced General Ensemble Simulations
Public Member Functions | Private Attributes | List of all members
Json::StringRequirement Class Reference

Requirements on strings. More...

#include <StringRequirement.h>

Inheritance diagram for Json::StringRequirement:
Inheritance graph
[legend]

Public Member Functions

 StringRequirement ()
 Constructor.
 
virtual void Reset () override
 Reset Requirement.
 
virtual void Parse (Value json, const std::string &path) override
 Parse JSON value to generate Requirement. More...
 
virtual void Validate (const Value &json, const std::string &path) override
 Validate string value. More...
 
- Public Member Functions inherited from Json::Requirement
bool HasErrors ()
 Check if errors have occured. More...
 
std::vector< std::string > GetErrors ()
 Get list of error messages. More...
 
virtual void ClearErrors ()
 Clear list of error messages.
 
virtual bool HasNotices ()
 Check if notices have been queued. More...
 
std::vector< std::string > GetNotices ()
 Get list of notices. More...
 
virtual void ClearNotices ()
 Clear list of notice messages.
 
virtual ~Requirement ()
 Destructor.
 

Private Attributes

bool minSet_
 If True, minimum length requirement is active.
 
bool maxSet_
 If True, maximum length requirement is active.
 
bool rgxSet_
 If True, string has to match regular expression.
 
size_t minLength_
 Minimum string length;.
 
size_t maxLength_
 Maximum string length;.
 
std::regex regex_
 Regular expression to match string to.
 
std::string expr_
 Expression.
 
std::string path_
 Path for JSON path specification.
 
std::vector< std::string > enum_
 Enum values.
 

Additional Inherited Members

- Protected Member Functions inherited from Json::Requirement
void PushError (const std::string &error)
 Add error to list of error messages. More...
 
void PushNotice (const std::string &notice)
 Add message to list of notices. More...
 

Detailed Description

Requirements on strings.

Definition at line 37 of file StringRequirement.h.

Member Function Documentation

◆ Parse()

virtual void Json::StringRequirement::Parse ( Value  json,
const std::string &  path 
)
inlineoverridevirtual

Parse JSON value to generate Requirement.

Parameters
jsonJSON input value.
pathPath for JSON path specification.

Implements Json::Requirement.

Definition at line 78 of file StringRequirement.h.

References Reset().

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  }
bool rgxSet_
If True, string has to match regular expression.
std::vector< std::string > enum_
Enum values.
std::string path_
Path for JSON path specification.
std::regex regex_
Regular expression to match string to.
virtual void Reset() override
Reset Requirement.
bool maxSet_
If True, maximum length requirement is active.
std::string expr_
Expression.
size_t minLength_
Minimum string length;.
size_t maxLength_
Maximum string length;.
bool minSet_
If True, minimum length requirement is active.
Here is the call graph for this function:

◆ Validate()

virtual void Json::StringRequirement::Validate ( const Value &  json,
const std::string &  path 
)
inlineoverridevirtual

Validate string value.

Parameters
jsonJSON value to be validated.
pathPath for JSON path specification.

This function tests if the JSON value is of type string and if the string meets the requirements loaded via StringRequirement::Parse(). If the validation fails, one or more errors are appended to the list of errors.

Implements Json::Requirement.

Definition at line 120 of file StringRequirement.h.

References maxLength_, minLength_, Json::Requirement::PushError(), and regex_.

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  }
bool rgxSet_
If True, string has to match regular expression.
std::vector< std::string > enum_
Enum values.
void PushError(const std::string &error)
Add error to list of error messages.
Definition: Requirement.h:53
std::regex regex_
Regular expression to match string to.
bool maxSet_
If True, maximum length requirement is active.
std::string expr_
Expression.
size_t minLength_
Minimum string length;.
size_t maxLength_
Maximum string length;.
bool minSet_
If True, minimum length requirement is active.
Here is the call graph for this function:

The documentation for this class was generated from the following file: