24 #include "cafAssert.h"
26 #include <nlohmann/json.hpp>
102 template <
typename DataType>
120 virtual std::pair<bool, std::string>
validate(
const DataType& value )
const = 0;
128 template <
typename DataType>
136 , m_minimum( minimum )
137 , m_maximum( maximum )
143 if ( jsonFieldObject.is_object() && jsonFieldObject.contains(
"valid-range" ) )
145 auto jsonRange = jsonFieldObject[
"valid-range"];
146 CAFFA_ASSERT( jsonRange.is_object() );
147 if ( jsonRange.contains(
"min" ) && jsonRange.contains(
"max" ) )
149 m_minimum = jsonRange[
"min"];
150 m_maximum = jsonRange[
"max"];
157 CAFFA_ASSERT( jsonFieldObject.is_object() );
158 auto jsonRange = nlohmann::json::object();
159 jsonRange[
"min"] = m_minimum;
160 jsonRange[
"max"] = m_maximum;
161 jsonFieldObject[
"valid-range"] = jsonRange;
164 std::pair<bool, std::string>
validate(
const DataType& value )
const override
166 bool valid = m_minimum <= value && value <= m_maximum;
169 std::stringstream ss;
170 ss <<
"The value " << value <<
" is outside the limits [" << m_minimum <<
", " << m_maximum <<
"]";
171 return std::make_pair(
false, ss.str() );
173 return std::make_pair(
true,
"" );
176 static std::unique_ptr<RangeValidator<DataType>>
177 create( DataType minimum, DataType maximum, FailureSeverity
failureSeverity = FailureSeverity::VALIDATOR_ERROR )
179 return std::make_unique<RangeValidator<DataType>>( minimum, maximum,
failureSeverity );
An abstract field validator interface for validating field values.
Definition: cafFieldValidator.h:42
FailureSeverity
The severity of failure. Essentially tells the application how to treat a validator failure: VALIDATO...
Definition: cafFieldValidator.h:52
virtual void writeToJson(nlohmann::json &jsonFieldObject, const Serializer &serializer) const =0
Write the validator to JSON.
FailureSeverity failureSeverity() const
Get the severity of a failure of the validator.
Definition: cafFieldValidator.h:90
FieldValidatorInterface(FailureSeverity failureSeverity)
Construct a new Field Validator Interface object.
Definition: cafFieldValidator.h:63
virtual void readFromJson(const nlohmann::json &jsonFieldObject, const Serializer &serializer)=0
Read the validator from JSON.
Used to validate the value of data fields Implementations need the the validate method as well as rea...
Definition: cafFieldValidator.h:104
virtual std::pair< bool, std::string > validate(const DataType &value) const =0
Validate the value.
Simple range validator.
Definition: cafFieldValidator.h:130
void writeToJson(nlohmann::json &jsonFieldObject, const caffa::Serializer &serializer) const override
Write the validator to JSON.
Definition: cafFieldValidator.h:155
void readFromJson(const nlohmann::json &jsonFieldObject, const caffa::Serializer &serializer) override
Read the validator from JSON.
Definition: cafFieldValidator.h:141
std::pair< bool, std::string > validate(const DataType &value) const override
Validate the value.
Definition: cafFieldValidator.h:164
Definition: cafSerializer.h:36
Main Caffa namespace.
Definition: __init__.py:1