cutelyst  5.0.1
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 
20 namespace Cutelyst {
21 
22 class ValidatorResultPrivate;
23 
75 class CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorResult
76 {
77 public:
85 
89  ValidatorResult(const ValidatorResult &other) noexcept;
90 
95  ValidatorResult(ValidatorResult &&other) noexcept;
96 
100  ValidatorResult &operator=(const ValidatorResult &other) noexcept;
101 
105  ValidatorResult &operator=(ValidatorResult &&other) noexcept;
106 
110  ~ValidatorResult() noexcept;
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 
249 private:
250  QSharedDataPointer<ValidatorResultPrivate> d;
251 };
252 
259 class CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT AwaitedValidatorResult
260 {
261 public:
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 
281  explicit AwaitedValidatorResult(Context *c)
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 
296 protected:
297  friend class Validator;
298  std::function<void(const ValidatorResult &result)> callback;
299 
300 private:
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
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
The Cutelyst Context.
Definition: context.h:42
Coroutine awaitable for ValidatorResult.
The Cutelyst namespace holds all public Cutelyst API.
bool disconnect(const QMetaObject::Connection &connection)
Validation processor for input data.
Definition: validator.h:308
Provides information about performed validations.
void destroyed(QObject *obj)