Skip to content

Commit 23c4ff1

Browse files
committed
Improved WsjcppJsonRpc20ParamDef (more validators)
1 parent f27a7f1 commit 23c4ff1

File tree

10 files changed

+594
-31
lines changed

10 files changed

+594
-31
lines changed

src.wsjcpp/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Automaticly generated by wsjcpp@v0.1.5
22
cmake_minimum_required(VERSION 3.0)
33

4-
add_definitions(-DWSJCPP_APP_VERSION="v0.0.1")
4+
add_definitions(-DWSJCPP_APP_VERSION="v0.0.2")
55
add_definitions(-DWSJCPP_APP_NAME="wsjcpp-jsonrpc2")
66

77
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
@@ -28,7 +28,7 @@ list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_core/wsjcpp_resources_manager.c
2828
list (APPEND WSJCPP_INCLUDE_DIRS "./src.wsjcpp/nlohmann_json/")
2929
list (APPEND WSJCPP_SOURCES "./src.wsjcpp/nlohmann_json/json.hpp")
3030

31-
# wsjcpp-validators:v0.1.0
31+
# wsjcpp-validators:v0.1.2
3232
list (APPEND WSJCPP_INCLUDE_DIRS "./src.wsjcpp/wsjcpp_validators/")
3333
list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_validators/wsjcpp_validators.h")
3434
list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_validators/wsjcpp_validators.cpp")

src.wsjcpp/wsjcpp_validators/wsjcpp.hold.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ cmake_minimum_required: 3.0
33
cmake_cxx_standard: 11
44

55
name: wsjcpp-validators
6-
version: v0.1.0
6+
version: v0.1.2
77
description: Classes for data validation
88
issues: https://github.com/wsjcpp/wsjcpp-validators/issues
99

@@ -24,10 +24,15 @@ authors:
2424

2525
dependencies:
2626
- name: "wsjcpp-core"
27-
version: "v0.1.1"
27+
version: "v0.1.7"
2828
url: "https://github.com/wsjcpp/wsjcpp-core:master"
2929
origin: "https://github.com/"
3030
installation-dir: "./src.wsjcpp/wsjcpp_core"
31+
- name: "nlohmann/json"
32+
version: "v3.9.1"
33+
url: "https://github.com/nlohmann/json:develop"
34+
origin: "https://github.com/"
35+
installation-dir: "./src.wsjcpp/nlohmann_json"
3136

3237
distribution:
3338
- source-file: "src/wsjcpp_validators.h"
@@ -69,3 +74,7 @@ unit-tests:
6974
description: "test valid ipv4"
7075
- name: "isValidIpV6"
7176
description: "test valid ipv6"
77+
- name: "ValidatorIntegerMinValue"
78+
description: "Test validator"
79+
- name: "ValidatorIntegerMaxValue"
80+
description: "Test validator"

src.wsjcpp/wsjcpp_validators/wsjcpp_validators.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,3 +715,85 @@ bool WsjcppValidatorHex::isValid(const std::string &sValue, std::string &sError)
715715
}
716716
return true;
717717
}
718+
719+
720+
// ----------------------------------------------------------------------
721+
// WsjcppValidatorIntegerBase
722+
723+
WsjcppValidatorIntegerBase::WsjcppValidatorIntegerBase(const std::string &sTypeName) {
724+
TAG = "WsjcppValidatorIntegerBase";
725+
m_sTypeName = sTypeName;
726+
}
727+
728+
// ----------------------------------------------------------------------
729+
730+
WsjcppValidatorType WsjcppValidatorIntegerBase::getBaseType() {
731+
return WsjcppValidatorType::WSJCPP_VALIDATOR_INTEGER;
732+
}
733+
734+
// ----------------------------------------------------------------------
735+
736+
std::string WsjcppValidatorIntegerBase::getTypeName() {
737+
return m_sTypeName;
738+
}
739+
740+
// ----------------------------------------------------------------------
741+
// WsjcppValidatorIntegerMinValue
742+
743+
WsjcppValidatorIntegerMinValue::WsjcppValidatorIntegerMinValue(int nMinValue)
744+
: WsjcppValidatorIntegerBase("integer_min_value") {
745+
TAG = "WsjcppValidatorIntegerMinValue";
746+
m_nMinValue = nMinValue;
747+
}
748+
749+
// ----------------------------------------------------------------------
750+
751+
bool WsjcppValidatorIntegerMinValue::isValid(int nValue, std::string &sError) {
752+
if (nValue < m_nMinValue) {
753+
sError = "Value must be more or equal then " + std::to_string(m_nMinValue);
754+
return false;
755+
}
756+
return true;
757+
}
758+
759+
// ----------------------------------------------------------------------
760+
// WsjcppValidatorIntegerMaxValue
761+
762+
WsjcppValidatorIntegerMaxValue::WsjcppValidatorIntegerMaxValue(int nMaxValue)
763+
: WsjcppValidatorIntegerBase("integer_max_value") {
764+
TAG = "WsjcppValidatorIntegerMaxValue";
765+
m_nMaxValue = nMaxValue;
766+
}
767+
768+
// ----------------------------------------------------------------------
769+
770+
bool WsjcppValidatorIntegerMaxValue::isValid(int nValue, std::string &sError) {
771+
if (nValue > m_nMaxValue) {
772+
sError = "Value must be less or equal then " + std::to_string(m_nMaxValue);
773+
return false;
774+
}
775+
return true;
776+
}
777+
778+
// ----------------------------------------------------------------------
779+
// WsjcppValidatorJsonBase
780+
781+
WsjcppValidatorJsonBase::WsjcppValidatorJsonBase(const std::string &sTypeName) {
782+
TAG = "WsjcppValidatorJsonBase";
783+
m_sTypeName = sTypeName;
784+
}
785+
786+
// ----------------------------------------------------------------------
787+
788+
WsjcppValidatorType WsjcppValidatorJsonBase::getBaseType() {
789+
return WsjcppValidatorType::WSJCPP_VALIDATOR_JSON;
790+
}
791+
792+
// ----------------------------------------------------------------------
793+
794+
std::string WsjcppValidatorJsonBase::getTypeName() {
795+
return m_sTypeName;
796+
}
797+
798+
799+
// ----------------------------------------------------------------------

src.wsjcpp/wsjcpp_validators/wsjcpp_validators.h

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
#include <string>
55
#include <vector>
66
#include <regex>
7+
#include <json.hpp>
78

89
enum WsjcppValidatorType {
910
WSJCPP_VALIDATOR_STRING,
10-
WSJCPP_VALIDATOR_NUMBER
11+
WSJCPP_VALIDATOR_INTEGER,
12+
WSJCPP_VALIDATOR_JSON
1113
};
1214

1315
// ----------------------------------------------------------------------
@@ -188,4 +190,55 @@ class WsjcppValidatorHex : public WsjcppValidatorStringBase {
188190

189191
// ----------------------------------------------------------------------
190192

193+
class WsjcppValidatorIntegerBase {
194+
public:
195+
WsjcppValidatorIntegerBase(const std::string &typeName);
196+
virtual WsjcppValidatorType getBaseType();
197+
virtual std::string getTypeName();
198+
virtual bool isValid(int nValue, std::string &sError) = 0;
199+
protected:
200+
std::string TAG;
201+
private:
202+
std::string m_sTypeName;
203+
};
204+
205+
// ----------------------------------------------------------------------
206+
207+
class WsjcppValidatorIntegerMinValue : public WsjcppValidatorIntegerBase {
208+
public:
209+
WsjcppValidatorIntegerMinValue(int nMinValue);
210+
virtual bool isValid(int nValue, std::string &sError) override;
211+
212+
private:
213+
std::string TAG;
214+
int m_nMinValue;
215+
};
216+
217+
// ----------------------------------------------------------------------
218+
219+
class WsjcppValidatorIntegerMaxValue : public WsjcppValidatorIntegerBase {
220+
public:
221+
WsjcppValidatorIntegerMaxValue(int nMaxValue);
222+
virtual bool isValid(int nValue, std::string &sError) override;
223+
224+
private:
225+
std::string TAG;
226+
int m_nMaxValue;
227+
};
228+
229+
// ----------------------------------------------------------------------
230+
231+
class WsjcppValidatorJsonBase {
232+
public:
233+
WsjcppValidatorJsonBase(const std::string &typeName);
234+
virtual WsjcppValidatorType getBaseType();
235+
virtual std::string getTypeName();
236+
virtual bool isValid(const nlohmann::json &nValue, std::string &sError) = 0;
237+
238+
protected:
239+
std::string TAG;
240+
private:
241+
std::string m_sTypeName;
242+
};
243+
191244
#endif // WSJCPP_VALIDATOR_H

0 commit comments

Comments
 (0)