cutelyst  3.9.1
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-2022 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 
11 using namespace Cutelyst;
12 
14  : d(new ValidatorResultPrivate)
15 {
16 }
17 
19  : d(other.d)
20 {
21 }
22 
24 {
25  d = other.d;
26  return *this;
27 }
28 
30 {
31 }
32 
34 {
35  return d->errors.empty();
36 }
37 
38 void ValidatorResult::addError(const QString &field, const QString &message)
39 {
40  QStringList fieldErrors = d->errors.value(field);
41  fieldErrors.append(message);
42  d->errors.insert(field, fieldErrors);
43 }
44 
46 {
47  QStringList strings;
48 
49  auto i = d->errors.constBegin();
50  while (i != d->errors.constEnd()) {
51  strings.append(i.value());
52  ++i;
53  }
54 
55  return strings;
56 }
57 
59 {
60  return d->errors;
61 }
62 
64 {
65  return d->errors.value(field);
66 }
67 
68 bool ValidatorResult::hasErrors(const QString &field) const
69 {
70  return d->errors.contains(field);
71 }
72 
74 {
75  QJsonObject json;
76 
77  if (!d->errors.empty()) {
78  auto i = d->errors.constBegin();
79  while (i != d->errors.constEnd()) {
80  json.insert(i.key(), QJsonValue(QJsonArray::fromStringList(i.value())));
81  ++i;
82  }
83  }
84 
85  return json;
86 }
87 
89 {
90  return QStringList(d->errors.keys());
91 }
92 
93 QVariantHash ValidatorResult::values() const
94 {
95  return d->values;
96 }
97 
99 {
100  return d->values.value(field);
101 }
102 
103 void ValidatorResult::addValue(const QString &field, const QVariant &value)
104 {
105  d->values.insert(field, value);
106 }
107 
108 QVariantHash ValidatorResult::extras() const
109 {
110  return d->extras;
111 }
112 
114 {
115  return d->extras.value(field);
116 }
117 
118 void ValidatorResult::addExtra(const QString &field, const QVariant &extra)
119 {
120  d->extras.insert(field, extra);
121 }
QStringList failedFields() const
Returns a list of fields with errors.
ValidatorResult & operator=(const ValidatorResult &other)
Assigns other to this ValidatorResult.
QJsonObject errorsJsonObject() const
Returns the dictionray containing fields with errors as JSON object.
bool hasErrors(const QString &field) const
Returns true if the field has validation errors.
QVariantHash values() const
Returns the values that have been extracted by the validators.
QHash< QString, QStringList > errors() const
Returns a dictionary containing fields with errors.
QVariantHash extras() const
Returns all extra data that has been extracted by the validators.
QStringList errorStrings() const
Returns a list of all error messages.
iterator insert(QLatin1StringView key, const QJsonValue &value)
QVariant value(const QString &field) const
Returns the extracted value for the input field.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
void addError(const QString &field, const QString &message)
Adds new error information to the internal QHash.
bool isValid() const
Returns true if the validation was successful.
~ValidatorResult()
Deconstructs the ValidatorResult.
void addValue(const QString &field, const QVariant &value)
Adds a new value extracted from the specified input field.
QVariant extra(const QString &field) const
Returns the extra data for the input field.
void append(QList< T > &&value)
ValidatorResult()
Constructs a new ValidatorResult.
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.
QJsonArray fromStringList(const QStringList &list)