19 #include "validatorjson_p.h" 20 #include <QJsonDocument> 21 #include <QJsonParseError> 26 ValidatorRule(*new ValidatorJsonPrivate(field, messages, defValKey))
38 const QString v =
value(params);
42 const QJsonDocument json = QJsonDocument::fromJson(v.toUtf8(), &jpe);
43 if (json.isEmpty() || json.isNull()) {
45 qCDebug(C_VALIDATOR,
"ValidatorJson: Validation failed for field %s at %s::%s with the following error: %s", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), qPrintable(jpe.errorString()));
47 result.
value.setValue<QJsonDocument>(json);
59 const QString _label =
label(c);
60 const QString jsonError = errorData.toString();
61 if (_label.isEmpty()) {
62 if (!jsonError.isEmpty()) {
64 error = c->
translate(
"Cutelyst::ValidatorJson",
"Invalid JSON data: %1").arg(jsonError);
66 error = c->
translate(
"Cutelyst::ValidatorJson",
"Invalid JSON data.");
69 if (!jsonError.isEmpty()) {
71 error = c->
translate(
"Cutelyst::ValidatorJson",
"The data entered in the “%1” field is not valid JSON: %2").arg(_label, jsonError);
74 error = c->
translate(
"Cutelyst::ValidatorJson",
"The data entered in the “%1” field is not valid JSON.").arg(_label);
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) const override
Performs the validation and returns the result.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
ValidatorJson(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new json validator.
Stores custom error messages and the input field label.
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
The Cutelyst namespace holds all public Cutelyst API.
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
QString value(const ParamsMultiMap ¶ms) const
Returns the value of the field from the input params.
QString field() const
Returns the name of the field to validate.
Contains the result of a single input parameter validation.
~ValidatorJson() override
Deconstructs the json validator.
void defaultValue(Context *c, ValidatorReturnType *result, const char *validatorName) const
I a defValKey has been set in the constructor, this will try to get the default value from the stash ...