cutelyst  3.7.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorjson.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatorjson_p.h"
7 #include <QJsonDocument>
8 #include <QJsonParseError>
9 
10 using namespace Cutelyst;
11 
12 ValidatorJson::ValidatorJson(const QString &field, const Cutelyst::ValidatorMessages &messages, const QString &defValKey) :
13  ValidatorRule(*new ValidatorJsonPrivate(field, messages, defValKey))
14 {
15 }
16 
18 {
19 }
20 
22 {
23  ValidatorReturnType result;
24 
25  const QString v = value(params);
26 
27  if (!v.isEmpty()) {
28  QJsonParseError jpe;
29  const QJsonDocument json = QJsonDocument::fromJson(v.toUtf8(), &jpe);
30  if (json.isEmpty() || json.isNull()) {
31  result.errorMessage = validationError(c, jpe.errorString());
32  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()));
33  } else {
34  result.value.setValue(json);
35  }
36  } else {
37  defaultValue(c, &result, "ValidatorJson");
38  }
39 
40  return result;
41 }
42 
43 QString ValidatorJson::genericValidationError(Context *c, const QVariant &errorData) const
44 {
45  QString error;
46  const QString _label = label(c);
47  const QString jsonError = errorData.toString();
48  if (_label.isEmpty()) {
49  if (!jsonError.isEmpty()) {
50  //: %1 will contain the json error
51  error = c->translate("Cutelyst::ValidatorJson", "Invalid JSON data: %1").arg(jsonError);
52  } else {
53  error = c->translate("Cutelyst::ValidatorJson", "Invalid JSON data.");
54  }
55  } else {
56  if (!jsonError.isEmpty()) {
57  //: %1 will contain the field label, %2 will contain the json error
58  error = c->translate("Cutelyst::ValidatorJson", "The data entered in the “%1” field is not valid JSON: %2").arg(_label, jsonError);
59  } else {
60  //: %1 will be replaced by the field label
61  error = c->translate("Cutelyst::ValidatorJson", "The data entered in the “%1” field is not valid JSON.").arg(_label);
62  }
63  }
64  return error;
65 }
The Cutelyst Context.
Definition: context.h:39
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:477
~ValidatorJson() override
Deconstructs the json validator.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) 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 &params) 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.
Definition: Mainpage.dox:8
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49