cutelyst 4.3.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 <QJsonArray>
9#include <QJsonDocument>
10#include <QJsonObject>
11#include <QJsonParseError>
12
13using namespace Cutelyst;
14
16 ExpectedType expectedType,
17 const Cutelyst::ValidatorMessages &messages,
18 const QString &defValKey)
19 : ValidatorRule(*new ValidatorJsonPrivate(field, expectedType, messages, defValKey))
20{
21}
22
24
26 const ParamsMultiMap &params) const
27{
29
30 Q_D(const ValidatorJson);
31
32 const QString v = value(params);
33
34 if (!v.isEmpty()) {
36 const QJsonDocument json = QJsonDocument::fromJson(v.toUtf8(), &jpe);
37 if (jpe.error == QJsonParseError::NoError) {
38 if (d->expectedType == ExpectedType::Array && !json.isArray()) {
39 result.errorMessage = validationError(c);
40 qCDebug(C_VALIDATOR).noquote()
41 << debugString(c) << "A JSON array is expected but not provided";
42 } else if (d->expectedType == ExpectedType::Object && !json.isObject()) {
43 result.errorMessage = validationError(c);
44 qCDebug(C_VALIDATOR).noquote()
45 << debugString(c) << "A JSON object is expected but not provided";
46 } else {
47 switch (d->expectedType) {
49 case Array:
50 result.value.setValue(json.array());
51 break;
52 case Object:
53 result.value.setValue(json.object());
54 break;
55 case All:
56 result.value.setValue(json);
57 break;
58 }
59 }
60 } else {
61 result.errorMessage = validationError(c, jpe.errorString());
62 qCDebug(C_VALIDATOR).noquote() << debugString(c) << jpe.errorString();
63 }
64 } else {
65 defaultValue(c, &result);
66 }
67
68 return result;
69}
70
72{
73 const QString _label = label(c);
74 if (errorData.isNull()) {
75 Q_D(const ValidatorJson);
76 if (d->expectedType == ExpectedType::Array) {
77 if (_label.isEmpty()) {
78 //% "Not a JSON array."
79 return c->qtTrId("cutelyst-valjson-genvalerr-exparray");
80 } else {
81 //: %1 will be replaced by the field label
82 //% "The data entered in the “%1” field is not a JSON array."
83 return c->qtTrId("cutelyst-valjson-genvalerr-exparray-label");
84 }
85 } else {
86 if (_label.isEmpty()) {
87 //% "Not a JSON object."
88 return c->qtTrId("cutelyst-valjson-genvalerr-expobject");
89 } else {
90 //: %1 will be replaced by the field label
91 //% "The data entered in the “%1” field is not a JSON object."
92 return c->qtTrId("cutelyst-valjson-genvalerr-expobject-label");
93 }
94 }
95 } else {
96 const QString jsonError = errorData.toString();
97 if (_label.isEmpty()) {
98 if (!jsonError.isEmpty()) {
99 //: %1 will contain the json error
100 //% "Invalid JSON data: %1"
101 return c->qtTrId("cutelyst-valjson-genvalerr-data").arg(jsonError);
102 } else {
103 //% "Invalid JSON data."
104 return c->qtTrId("cutelyst-valjson-genvalerr");
105 }
106 } else {
107 if (!jsonError.isEmpty()) {
108 //: %1 will contain the field label, %2 will contain the json error
109 //% "The data entered in the “%1” field is not valid JSON: %2"
110 return c->qtTrId("cutelyst-valjson-genvalerr-data-label").arg(_label, jsonError);
111 } else {
112 //: %1 will be replaced by the field label
113 //% "The data entered in the “%1” field is not valid JSON."
114 return c->qtTrId("cutelyst-valjson-genvalerr-label").arg(_label);
115 }
116 }
117 }
118}
The Cutelyst Context.
Definition context.h:42
QString qtTrId(const char *id, int n=-1) const
Definition context.h:656
Checks if the inut data is valid JSON.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorJson(const QString &field, ExpectedType expectedType=ExpectedType::All, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
QString debugString(Context *c) const
The Cutelyst namespace holds all public Cutelyst API.
QJsonArray array() const const
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
bool isArray() const const
bool isObject() const const
QJsonObject object() const const
QString errorString() const const
QString arg(Args &&... args) const const
bool isEmpty() const const
QByteArray toUtf8() const const
bool isNull() const const
void setValue(QVariant &&value)
QString toString() const const
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.