X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=src%2Frest%2Fmodel%2FModelBase.h;fp=src%2Frest%2Fmodel%2FModelBase.h;h=159b772ded1203e49e70ae7369e2167a8d325007;hb=a0bada6bf7af92d281c381092e7d6881c67d905f;hp=0000000000000000000000000000000000000000;hpb=488447aed60cc52b06d71d50bee4ecf10025f7e1;p=ric-plt%2Fxapp-frame-cpp.git diff --git a/src/rest/model/ModelBase.h b/src/rest/model/ModelBase.h new file mode 100644 index 0000000..159b772 --- /dev/null +++ b/src/rest/model/ModelBase.h @@ -0,0 +1,66 @@ +#ifndef XAPP_MODEL_ModelBase_H +#define XAPP_MODEL_ModelBase_H +#include +#include +#include +#include +#include + +using nlohmann::json_schema::json_validator; +using json = nlohmann::json; + +namespace xapp { +namespace model { + +std::invalid_argument invalid_parameter("Invalid Json Input"); + +template +bool _validate(const ModelType model) { + json _json = model; + json_validator validator; + validator.set_root_schema(model.validator_schema); + + try { + validator.validate(_json); + } catch (const std::exception& e) { + std::cerr << "Struct Validation failed, here is why: " << e.what() << "\n"; + throw; + } + return true; +} + +struct ModelBase { + json validator_schema = R"( + { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "ModelBase" + })"_json; + + bool validate_() { + return _validate(std::move(*this)); + } + + void validate_json(const json& _json) { + json_validator validator; + validator.set_root_schema(get_validator_schema()); + try { + validator.validate(_json); + } catch (const std::exception& e) { + throw; + } + return; + } + + virtual json get_validator_schema() const { return validator_schema; } +}; + +void from_json(const json& j, ModelBase& ref) { + return; +} +void to_json(json& j, const ModelBase& ref) { + return; +} + +} /*model*/ +} /*xapp*/ +#endif /*XAPP_MODEL_ModelBase_H*/