Cutelyst  2.5.0
validatorjson.cpp
1 /*
2  * Copyright (C) 2017-2018 Matthias Fehring <kontakt@buschmann23.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "validatorjson_p.h"
20 #include <QJsonDocument>
21 #include <QJsonParseError>
22 
23 using namespace Cutelyst;
24 
25 ValidatorJson::ValidatorJson(const QString &field, const Cutelyst::ValidatorMessages &messages, const QString &defValKey) :
26  ValidatorRule(*new ValidatorJsonPrivate(field, messages, defValKey))
27 {
28 }
29 
31 {
32 }
33 
35 {
36  ValidatorReturnType result;
37 
38  const QString v = value(params);
39 
40  if (!v.isEmpty()) {
41  QJsonParseError jpe;
42  const QJsonDocument json = QJsonDocument::fromJson(v.toUtf8(), &jpe);
43  if (json.isEmpty() || json.isNull()) {
44  result.errorMessage = validationError(c, jpe.errorString());
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()));
46  } else {
47  result.value.setValue<QJsonDocument>(json);
48  }
49  } else {
50  defaultValue(c, &result, "ValidatorJson");
51  }
52 
53  return result;
54 }
55 
56 QString ValidatorJson::genericValidationError(Context *c, const QVariant &errorData) const
57 {
58  QString error;
59  const QString _label = label(c);
60  const QString jsonError = errorData.toString();
61  if (_label.isEmpty()) {
62  if (!jsonError.isEmpty()) {
63  //: %1 will contain the json error
64  error = c->translate("Cutelyst::ValidatorJson", "Invalid JSON data: %1").arg(jsonError);
65  } else {
66  error = c->translate("Cutelyst::ValidatorJson", "Invalid JSON data.");
67  }
68  } else {
69  if (!jsonError.isEmpty()) {
70  //: %1 will contain the field label, %2 will contain the json error
71  error = c->translate("Cutelyst::ValidatorJson", "The data entered in the “%1” field is not valid JSON: %2").arg(_label, jsonError);
72  } else {
73  error = c->translate("Cutelyst::ValidatorJson", "The data entered in the “%1” field is not valid JSON.").arg(_label);
74  }
75  }
76  return error;
77 }
QMap< QString, QString > ParamsMultiMap
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) 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.
~ValidatorJson()
Deconstructs the json validator.
The Cutelyst Context.
Definition: context.h:50
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:414
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
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 &params) 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.
Definition: validatorrule.h:62
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 ...