Caffa  1.1.0
C++ Application Framework for Embedded Systems with introspection
cafFieldValidator.h
1 //##################################################################################################
2 //
3 // Custom Visualization Core library
4 // Copyright (C) 2021- 3D-Radar AS
5 //
6 // This library may be used under the terms of the GNU Lesser General Public License as follows:
7 //
8 // GNU Lesser General Public License Usage
9 // This library is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU Lesser General Public License as published by
11 // the Free Software Foundation; either version 2.1 of the License, or
12 // (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful, but WITHOUT ANY
15 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 // FITNESS FOR A PARTICULAR PURPOSE.
17 //
18 // See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
19 // for more details.
20 //
21 //##################################################################################################
22 #pragma once
23 
24 #include "cafAssert.h"
25 
26 #include <nlohmann/json.hpp>
27 
28 #include <memory>
29 #include <sstream>
30 #include <string>
31 #include <utility>
32 
33 namespace caffa
34 {
35 class Serializer;
36 
42 {
43 public:
51  enum class FailureSeverity
52  {
53  VALIDATOR_WARNING,
54  VALIDATOR_ERROR,
55  VALIDATOR_CRITICAL
56  };
57 
64  : m_failureSeverity( failureSeverity )
65  {
66  }
67 
68  virtual ~FieldValidatorInterface() = default;
75  virtual void readFromJson( const nlohmann::json& jsonFieldObject, const Serializer& serializer ) = 0;
76 
83  virtual void writeToJson( nlohmann::json& jsonFieldObject, const Serializer& serializer ) const = 0;
84 
90  FailureSeverity failureSeverity() const { return m_failureSeverity; }
91 
92 private:
93  FailureSeverity m_failureSeverity;
94 };
95 
102 template <typename DataType>
104 {
105 public:
107 
108  FieldValidator( FailureSeverity failureSeverity = FailureSeverity::VALIDATOR_ERROR )
110  {
111  }
112 
120  virtual std::pair<bool, std::string> validate( const DataType& value ) const = 0;
121 };
122 
128 template <typename DataType>
129 class RangeValidator : public FieldValidator<DataType>
130 {
131 public:
133 
134  RangeValidator( DataType minimum, DataType maximum, FailureSeverity failureSeverity = FailureSeverity::VALIDATOR_ERROR )
136  , m_minimum( minimum )
137  , m_maximum( maximum )
138  {
139  }
140 
141  void readFromJson( const nlohmann::json& jsonFieldObject, const caffa::Serializer& serializer ) override
142  {
143  if ( jsonFieldObject.is_object() && jsonFieldObject.contains( "valid-range" ) )
144  {
145  auto jsonRange = jsonFieldObject["valid-range"];
146  CAFFA_ASSERT( jsonRange.is_object() );
147  if ( jsonRange.contains( "min" ) && jsonRange.contains( "max" ) )
148  {
149  m_minimum = jsonRange["min"];
150  m_maximum = jsonRange["max"];
151  }
152  }
153  }
154 
155  void writeToJson( nlohmann::json& jsonFieldObject, const caffa::Serializer& serializer ) const override
156  {
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;
162  }
163 
164  std::pair<bool, std::string> validate( const DataType& value ) const override
165  {
166  bool valid = m_minimum <= value && value <= m_maximum;
167  if ( !valid )
168  {
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() );
172  }
173  return std::make_pair( true, "" );
174  }
175 
176  static std::unique_ptr<RangeValidator<DataType>>
177  create( DataType minimum, DataType maximum, FailureSeverity failureSeverity = FailureSeverity::VALIDATOR_ERROR )
178  {
179  return std::make_unique<RangeValidator<DataType>>( minimum, maximum, failureSeverity );
180  }
181 
182 private:
183  DataType m_minimum;
184  DataType m_maximum;
185 };
186 
187 } // namespace caffa
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