cutelyst 5.0.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorresult.h
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2025 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5#ifndef CUTELYSTVALIDATORRESULT_H
6#define CUTELYSTVALIDATORRESULT_H
7
8#include <Cutelyst/Plugins/Utils/validator_export.h>
9#include <Cutelyst/context.h>
10#include <coroutine>
11#include <functional>
12
13#include <QJsonObject>
14#include <QPointer>
15#include <QSharedDataPointer>
16#include <QString>
17#include <QStringList>
18#include <QVariantHash>
19
20namespace Cutelyst {
21
22class ValidatorResultPrivate;
23
75class CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorResult
76{
77public:
85
89 ValidatorResult(const ValidatorResult &other) noexcept;
90
96
101
106
111
117 [[nodiscard]] bool isValid() const noexcept;
118
128 void addError(const QString &field, const QString &message);
129
133 [[nodiscard]] QStringList errorStrings() const;
134
142 [[nodiscard]] QHash<QString, QStringList> errors() const noexcept;
143
149 [[nodiscard]] QStringList errors(const QString &field) const noexcept;
150
156 [[nodiscard]] bool hasErrors(const QString &field) const noexcept;
157
168 [[nodiscard]] QJsonObject errorsJsonObject() const;
169
174 [[nodiscard]] QStringList failedFields() const;
175
181 explicit operator bool() const noexcept { return isValid(); }
182
194 [[nodiscard]] QVariantHash values() const noexcept;
195
206 [[nodiscard]] QVariant value(const QString &field) const noexcept;
207
214 void addValue(const QString &field, const QVariant &value);
215
226 [[nodiscard]] QVariantHash extras() const noexcept;
227
239 [[nodiscard]] QVariant extra(const QString &field) const noexcept;
240
247 void addExtra(const QString &field, const QVariant &extra);
248
249private:
250 QSharedDataPointer<ValidatorResultPrivate> d;
251};
252
259class CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT AwaitedValidatorResult
260{
261public:
262 bool await_ready() const noexcept { return m_hasResult; }
263
264 bool await_suspend(std::coroutine_handle<> h) noexcept
265 {
266 m_handle = h;
267 if (m_receiver) {
268 m_destroyConn = QObject::connect(m_receiver, &QObject::destroyed, [h, this] {
269 m_result.addError(
270 QString(), QStringLiteral("Internal Server Error: the context was destroyed."));
271 m_hasResult = true;
272 h.resume();
273 });
274 }
275
276 return !await_ready();
277 }
278
279 ValidatorResult await_resume() { return m_result; }
280
282 : m_receiver{c}
283 {
284 callback = [this](const ValidatorResult &result) {
285 m_result = result; // cppcheck-suppress useInitializationList
286 m_hasResult = true;
287
288 if (m_handle) {
289 m_handle.resume();
290 }
291 };
292 }
293
294 ~AwaitedValidatorResult() { QObject::disconnect(m_destroyConn); }
295
296protected:
297 friend class Validator;
298 std::function<void(const ValidatorResult &result)> callback;
299
300private:
301 QMetaObject::Connection m_destroyConn;
302 QPointer<Context> m_receiver;
303 ValidatorResult m_result;
304 std::coroutine_handle<> m_handle;
305 bool m_hasResult{false};
306};
307
308} // namespace Cutelyst
309
310#endif // CUTELYSTVALIDATORRESULT_H
Coroutine awaitable for ValidatorResult.
The Cutelyst Context.
Definition context.h:42
Provides information about performed validations.
ValidatorResult & operator=(const ValidatorResult &other) noexcept
ValidatorResult(const ValidatorResult &other) noexcept
ValidatorResult & operator=(ValidatorResult &&other) noexcept
ValidatorResult(ValidatorResult &&other) noexcept
Validation processor for input data.
Definition validator.h:309
The Cutelyst namespace holds all public Cutelyst API.
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void destroyed(QObject *obj)
bool disconnect(const QMetaObject::Connection &connection)