cutelyst 4.3.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
|
Checks if the inut data is valid JSON. More...
#include <Cutelyst/Plugins/Utils/validatorjson.h>
Public Types | |
enum class | ExpectedType { All , Array , Object } |
Public Member Functions | |
ValidatorJson (const QString &field, ExpectedType expectedType=ExpectedType::All, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString()) | |
~ValidatorJson () override | |
Public Member Functions inherited from Cutelyst::ValidatorRule | |
ValidatorRule (const QString &field, const ValidatorMessages &messages={}, const QString &defValKey={}, QByteArrayView validatorName=nullptr) | |
virtual | ~ValidatorRule () |
Deconstructs the ValidatorRule. | |
Protected Member Functions | |
QString | genericValidationError (Context *c, const QVariant &errorData=QVariant()) const override |
ValidatorReturnType | validate (Context *c, const ParamsMultiMap ¶ms) const override |
Protected Member Functions inherited from Cutelyst::ValidatorRule | |
ValidatorRule (ValidatorRulePrivate &dd) | |
QString | debugString (Context *c) const |
void | defaultValue (Context *c, ValidatorReturnType *result) const |
QString | field () const noexcept |
virtual QString | genericParsingError (Context *c, const QVariant &errorData={}) const |
virtual QString | genericValidationDataError (Context *c, const QVariant &errorData={}) const |
QString | label (Context *c) const |
QString | parsingError (Context *c, const QVariant &errorData={}) const |
bool | trimBefore () const noexcept |
QString | validationDataError (Context *c, const QVariant &errorData={}) const |
QString | validationError (Context *c, const QVariant &errorData={}) const |
QString | value (const ParamsMultiMap ¶ms) const |
This tries to load the input field string into a QJsonDocument and checks if it is not null and not empty and if it complies to the expected type.
Definition at line 38 of file validatorjson.h.
|
strong |
Defines the expected JSON root type that will be accepted as valid.
Enumerator | |
---|---|
All | Accepts both, object and array, as valid. If valid, ValidatorReturnType::value will contain a QJsonDocument. |
Array | Only accepts the input as valid if the root is of type array. If valid, ValidatorReturnType::value will contain a QJsonArray. |
Object | Only accepts the input as valid if the root is of type object. If valid, ValidatorReturnType::value will contain a QJsonObject. |
Definition at line 44 of file validatorjson.h.
ValidatorJson::ValidatorJson | ( | const QString & | field, |
ExpectedType | expectedType = ExpectedType::All , |
||
const ValidatorMessages & | messages = ValidatorMessages() , |
||
const QString & | defValKey = QString() |
||
) |
Constructs a new ValidatorJson object with the given parameters.
field | Name of the input field to validate. |
expectedType | Expected JSON root type. |
messages | Custom error message if validation fails. |
defValKey | Stash key containing a default value if input field is empty. This value will NOT be validated. |
Definition at line 15 of file validatorjson.cpp.
|
overridedefault |
Destroys the ValidatorJson object.
|
overrideprotectedvirtual |
Returns a generic error message if validation failed.
Reimplemented from Cutelyst::ValidatorRule.
Definition at line 71 of file validatorjson.cpp.
References QString::arg(), Array, QString::isEmpty(), QVariant::isNull(), Cutelyst::ValidatorRule::label(), Cutelyst::Context::qtTrId(), and QVariant::toString().
|
overrideprotectedvirtual |
Performs the validation on the input params and returns the result.
If validation succeeded, ValidatorReturnType::value will contain a QJsonDocument if expectedType is set to ExpectedType::All, a QJsonArray if expectedType is set to ExpectedType::Array, or a QJsonObject if expectedType is set to ExpectedType::Object.
Implements Cutelyst::ValidatorRule.
Definition at line 25 of file validatorjson.cpp.
References All, Array, QJsonDocument::array(), Cutelyst::ValidatorRule::debugString(), Cutelyst::ValidatorRule::defaultValue(), Cutelyst::ValidatorReturnType::errorMessage, QJsonParseError::errorString(), QJsonDocument::fromJson(), QJsonDocument::isArray(), QString::isEmpty(), QJsonDocument::isObject(), QJsonParseError::NoError, Object, QJsonDocument::object(), QVariant::setValue(), QString::toUtf8(), Cutelyst::ValidatorRule::validationError(), Cutelyst::ValidatorReturnType::value, and Cutelyst::ValidatorRule::value().