cutelyst 5.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-2025 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 cb(validate(c, params));
74}
75
77{
78 const QString _label = label(c);
79 if (errorData.isNull()) {
80 Q_D(const ValidatorJson);
81 if (d->expectedType == ExpectedType::Array) {
82 if (_label.isEmpty()) {
83 //% "Not a JSON array."
84 return c->qtTrId("cutelyst-valjson-genvalerr-exparray");
85 } else {
86 //: %1 will be replaced by the field label
87 //% "The data entered in the “%1” field is not a JSON array."
88 return c->qtTrId("cutelyst-valjson-genvalerr-exparray-label");
89 }
90 } else {
91 if (_label.isEmpty()) {
92 //% "Not a JSON object."
93 return c->qtTrId("cutelyst-valjson-genvalerr-expobject");
94 } else {
95 //: %1 will be replaced by the field label
96 //% "The data entered in the “%1” field is not a JSON object."
97 return c->qtTrId("cutelyst-valjson-genvalerr-expobject-label");
98 }
99 }
100 } else {
101 const QString jsonError = errorData.toString();
102 if (_label.isEmpty()) {
103 if (!jsonError.isEmpty()) {
104 //: %1 will contain the json error
105 //% "Invalid JSON data: %1"
106 return c->qtTrId("cutelyst-valjson-genvalerr-data").arg(jsonError);
107 } else {
108 //% "Invalid JSON data."
109 return c->qtTrId("cutelyst-valjson-genvalerr");
110 }
111 } else {
112 if (!jsonError.isEmpty()) {
113 //: %1 will contain the field label, %2 will contain the json error
114 //% "The data entered in the “%1” field is not valid JSON: %2"
115 return c->qtTrId("cutelyst-valjson-genvalerr-data-label").arg(_label, jsonError);
116 } else {
117 //: %1 will be replaced by the field label
118 //% "The data entered in the “%1” field is not valid JSON."
119 return c->qtTrId("cutelyst-valjson-genvalerr-label").arg(_label);
120 }
121 }
122 }
123}
The Cutelyst Context.
Definition context.h:42
QString qtTrId(const char *id, int n=-1) const
Definition context.h:657
Checks if the inut data is valid JSON.
ValidatorJson(const QString &field, ExpectedType expectedType=ExpectedType::All, const ValidatorMessages &messages={}, const QString &defValKey={})
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(const Context *c) const
QString debugString(const Context *c) const
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) 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.