cutelyst 3.9.1
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
8#include <QJsonDocument>
9#include <QJsonParseError>
10
11using namespace Cutelyst;
12
14 const Cutelyst::ValidatorMessages &messages,
15 const QString &defValKey)
16 : ValidatorRule(*new ValidatorJsonPrivate(field, messages, defValKey))
17{
18}
19
23
25 const ParamsMultiMap &params) const
26{
28
29 const QString v = value(params);
30
31 if (!v.isEmpty()) {
33 const QJsonDocument json = QJsonDocument::fromJson(v.toUtf8(), &jpe);
34 if (json.isEmpty() || json.isNull()) {
35 result.errorMessage = validationError(c, jpe.errorString());
36 qCDebug(C_VALIDATOR,
37 "ValidatorJson: Validation failed for field %s at %s::%s with the following "
38 "error: %s",
39 qPrintable(field()),
40 qPrintable(c->controllerName()),
41 qPrintable(c->actionName()),
42 qPrintable(jpe.errorString()));
43 } else {
44 result.value.setValue(json);
45 }
46 } else {
47 defaultValue(c, &result, "ValidatorJson");
48 }
49
50 return result;
51}
52
54{
55 QString error;
56 const QString _label = label(c);
57 const QString jsonError = errorData.toString();
58 if (_label.isEmpty()) {
59 if (!jsonError.isEmpty()) {
60 //: %1 will contain the json error
61 error = c->translate("Cutelyst::ValidatorJson", "Invalid JSON data: %1").arg(jsonError);
62 } else {
63 error = c->translate("Cutelyst::ValidatorJson", "Invalid JSON data.");
64 }
65 } else {
66 if (!jsonError.isEmpty()) {
67 //: %1 will contain the field label, %2 will contain the json error
68 error = c->translate("Cutelyst::ValidatorJson",
69 "The data entered in the “%1” field is not valid JSON: %2")
70 .arg(_label, jsonError);
71 } else {
72 //: %1 will be replaced by the field label
73 error = c->translate("Cutelyst::ValidatorJson",
74 "The data entered in the “%1” field is not valid JSON.")
75 .arg(_label);
76 }
77 }
78 return error;
79}
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:490
~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.
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 ...
ValidatorRule(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ValidatorRule with the given parameters.
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
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
bool isEmpty() const const
bool isNull() const const
QString errorString() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool isEmpty() const const
QByteArray toUtf8() const const
void setValue(const T &value)
QString toString() const const
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.