Open bamkrs opened 3 years ago
Let's have an abstract base BodyValidator
class BodyValidator {
public:
virtual ~BodyValidator() = default;
virtual oatpp::OutgoingResponse validate(const oatpp::Void& polymorph) = 0;
}
Thus we can have predefined validators and we can inherit other validators:
class NotNull : public BodyValidator {
public:
oatpp::OutgoingResponse validate(const oatpp::Void& polymorph) override {
auto dispatcher = static_cast<const oatpp::data::mapping::type::__class::AbstractObject::PolymorphicDispatcher*>(polymorph.valueType->polymorphicDispatcher);
auto fields = dispatcher->getProperties()->getList();
auto object = static_cast<oatpp::BaseObject*>(polymorph.get());
for (auto const& field : fields) {
auto value = field->get(object);
OATPP_HTTP_ASSERT(value, Status::CODE_400, oatpp::String("Field '" + field->name + "' not set."));
}
}
}
Those could be implemented very easily:
1. User-Defined Validator
Could be a nice class with static functions i.E.
2. BODY_DTO extension
Extend the BODY_DTO macro with a third parameter that accepts the validator
3. Have an endpoint use the validator
Its a very simple API which won't kill our performance. It has some drawbacks (i.E. relying on static functions) but there are workarounds for this to. Or we could come up with some Interface were we could pass actual instances with.