cutelyst 4.3.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorresult.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatorresult_p.h"
7
8#include <QJsonArray>
9#include <QJsonValue>
10
11using namespace Cutelyst;
12
14 : d(new ValidatorResultPrivate)
15{
16}
17
18ValidatorResult::ValidatorResult(const ValidatorResult &other) noexcept = default;
19
20ValidatorResult::ValidatorResult(ValidatorResult &&other) noexcept = default;
21
22ValidatorResult &ValidatorResult::operator=(const ValidatorResult &other) noexcept = default;
23
25
26ValidatorResult::~ValidatorResult() noexcept = default;
27
28bool ValidatorResult::isValid() const noexcept
29{
30 return d->errors.empty();
31}
32
33void ValidatorResult::addError(const QString &field, const QString &message)
34{
35 QStringList fieldErrors = d->errors.value(field);
36 fieldErrors.append(message);
37 d->errors.insert(field, fieldErrors);
38}
39
41{
42 QStringList strings;
43
44 auto i = d->errors.constBegin();
45 while (i != d->errors.constEnd()) {
46 strings.append(i.value());
47 ++i;
48 }
49
50 return strings;
51}
52
54{
55 return d->errors;
56}
57
58QStringList ValidatorResult::errors(const QString &field) const noexcept
59{
60 return d->errors.value(field);
61}
62
63bool ValidatorResult::hasErrors(const QString &field) const noexcept
64{
65 return d->errors.contains(field);
66}
67
69{
70 QJsonObject json;
71
72 if (!d->errors.empty()) {
73 auto i = d->errors.constBegin();
74 while (i != d->errors.constEnd()) {
75 json.insert(i.key(), QJsonValue(QJsonArray::fromStringList(i.value())));
76 ++i;
77 }
78 }
79
80 return json;
81}
82
84{
85 return QStringList(d->errors.keys());
86}
87
88QVariantHash ValidatorResult::values() const noexcept
89{
90 return d->values;
91}
92
93QVariant ValidatorResult::value(const QString &field) const noexcept
94{
95 return d->values.value(field);
96}
97
98void ValidatorResult::addValue(const QString &field, const QVariant &value)
99{
100 d->values.insert(field, value);
101}
102
103QVariantHash ValidatorResult::extras() const noexcept
104{
105 return d->extras;
106}
107
108QVariant ValidatorResult::extra(const QString &field) const noexcept
109{
110 return d->extras.value(field);
111}
112
113void ValidatorResult::addExtra(const QString &field, const QVariant &extra)
114{
115 d->extras.insert(field, extra);
116}
Provides information about performed validations.
void addExtra(const QString &field, const QVariant &extra)
QStringList failedFields() const
Returns a list of fields with errors.
ValidatorResult & operator=(const ValidatorResult &other) noexcept
QJsonObject errorsJsonObject() const
bool hasErrors(const QString &field) const noexcept
void addValue(const QString &field, const QVariant &value)
QVariant value(const QString &field) const noexcept
QHash< QString, QStringList > errors() const noexcept
void addError(const QString &field, const QString &message)
QVariant extra(const QString &field) const noexcept
QStringList errorStrings() const
QVariantHash values() const noexcept
QVariantHash extras() const noexcept
The Cutelyst namespace holds all public Cutelyst API.
QJsonArray fromStringList(const QStringList &list)
QJsonObject::iterator insert(QLatin1StringView key, const QJsonValue &value)
void append(QList::parameter_type value)
T value(qsizetype i) const const
T value() const const