cutelyst 4.0.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-2023 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
13ValidatorJson::ValidatorJson(const QString &field,
14 const Cutelyst::ValidatorMessages &messages,
15 const QString &defValKey)
16 : ValidatorRule(*new ValidatorJsonPrivate(field, messages, defValKey))
17{
18}
19
21
23 const ParamsMultiMap &params) const
24{
26
27 const QString v = value(params);
28
29 if (!v.isEmpty()) {
30 QJsonParseError jpe;
31 const QJsonDocument json = QJsonDocument::fromJson(v.toUtf8(), &jpe);
32 if (json.isEmpty() || json.isNull()) {
33 result.errorMessage = validationError(c, jpe.errorString());
34 qCDebug(C_VALIDATOR).noquote() << jpe.errorString();
35 } else {
36 result.value.setValue(json);
37 }
38 } else {
39 defaultValue(c, &result);
40 }
41
42 return result;
43}
44
45QString ValidatorJson::genericValidationError(Context *c, const QVariant &errorData) const
46{
47 QString error;
48 const QString _label = label(c);
49 const QString jsonError = errorData.toString();
50 if (_label.isEmpty()) {
51 if (!jsonError.isEmpty()) {
52 //: %1 will contain the json error
53 error = c->translate("Cutelyst::ValidatorJson", "Invalid JSON data: %1").arg(jsonError);
54 } else {
55 error = c->translate("Cutelyst::ValidatorJson", "Invalid JSON data.");
56 }
57 } else {
58 if (!jsonError.isEmpty()) {
59 //: %1 will contain the field label, %2 will contain the json error
60 error = c->translate("Cutelyst::ValidatorJson",
61 "The data entered in the “%1” field is not valid JSON: %2")
62 .arg(_label, jsonError);
63 } else {
64 //: %1 will be replaced by the field label
65 error = c->translate("Cutelyst::ValidatorJson",
66 "The data entered in the “%1” field is not valid JSON.")
67 .arg(_label);
68 }
69 }
70 return error;
71}
The Cutelyst Context.
Definition: context.h:38
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.
void defaultValue(Context *c, ValidatorReturnType *result) 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