6#include "validatorjson_p.h"
8#include <QJsonDocument>
9#include <QJsonParseError>
15 const QString &defValKey)
16 :
ValidatorRule(*new ValidatorJsonPrivate(field, messages, defValKey))
29 const QString v =
value(params);
33 const QJsonDocument json = QJsonDocument::fromJson(v.toUtf8(), &jpe);
34 if (json.isEmpty() || json.isNull()) {
37 "ValidatorJson: Validation failed for field %s at %s::%s with the following "
40 qPrintable(c->controllerName()),
41 qPrintable(c->actionName()),
42 qPrintable(jpe.errorString()));
44 result.
value.setValue(json);
56 const QString _label =
label(c);
57 const QString jsonError = errorData.toString();
58 if (_label.isEmpty()) {
59 if (!jsonError.isEmpty()) {
61 error = c->
translate(
"Cutelyst::ValidatorJson",
"Invalid JSON data: %1").arg(jsonError);
63 error = c->
translate(
"Cutelyst::ValidatorJson",
"Invalid JSON data.");
66 if (!jsonError.isEmpty()) {
68 error = c->
translate(
"Cutelyst::ValidatorJson",
69 "The data entered in the “%1” field is not valid JSON: %2")
70 .arg(_label, jsonError);
73 error = c->
translate(
"Cutelyst::ValidatorJson",
74 "The data entered in the “%1” field is not valid JSON.")
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
~ValidatorJson() override
Deconstructs the json validator.
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) const override
Performs the validation and returns the result.
ValidatorJson(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new json validator.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString field() const
Returns the name of the field to validate.
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 ...
QString value(const ParamsMultiMap ¶ms) const
Returns the value of the field from the input params.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
The Cutelyst namespace holds all public Cutelyst API.
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.