cutelyst 4.0.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
53QHash<QString, QStringList> ValidatorResult::errors() const noexcept
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)
Adds new extra data that came up when validating the input field.
QStringList failedFields() const
Returns a list of fields with errors.
ValidatorResult & operator=(const ValidatorResult &other) noexcept
Assigns other to this ValidatorResult.
QJsonObject errorsJsonObject() const
Returns the dictionray containing fields with errors as JSON object.
bool hasErrors(const QString &field) const noexcept
Returns true if the field has validation errors.
void addValue(const QString &field, const QVariant &value)
Adds a new value extracted from the specified input field.
QVariant value(const QString &field) const noexcept
Returns the extracted value for the input field.
QHash< QString, QStringList > errors() const noexcept
Returns a dictionary containing fields with errors.
void addError(const QString &field, const QString &message)
Adds new error information to the internal QHash.
QVariant extra(const QString &field) const noexcept
Returns the extra data for the input field.
ValidatorResult()
Constructs a new ValidatorResult.
QStringList errorStrings() const
Returns a list of all error messages.
QVariantHash values() const noexcept
Returns the values that have been extracted by the validators.
~ValidatorResult() noexcept
Deconstructs the ValidatorResult.
QVariantHash extras() const noexcept
Returns all extra data that has been extracted by the validators.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8