Cutelyst  2.3.0
validatorresult.cpp
1 /*
2  * Copyright (C) 2017-2018 Matthias Fehring <kontakt@buschmann23.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "validatorresult_p.h"
20 #include <QJsonValue>
21 #include <QJsonArray>
22 
23 using namespace Cutelyst;
24 
26  d(new ValidatorResultPrivate)
27 {
28 }
29 
31  d(other.d)
32 {
33 }
34 
36 {
37  d = other.d;
38  return *this;
39 }
40 
42 {
43 }
44 
46 {
47  return d->errors.empty();
48 }
49 
50 void ValidatorResult::addError(const QString &field, const QString &message)
51 {
52  QStringList fieldErrors = d->errors.value(field);
53  fieldErrors.append(message);
54  d->errors.insert(field, fieldErrors);
55 }
56 
57 QStringList ValidatorResult::errorStrings() const
58 {
59  QStringList strings;
60 
61  auto i = d->errors.constBegin();
62  while (i != d->errors.constEnd()) {
63  strings.append(i.value());
64  ++i;
65  }
66 
67  return strings;
68 }
69 
70 QHash<QString, QStringList> ValidatorResult::errors() const
71 {
72  return d->errors;
73 }
74 
75 bool ValidatorResult::hasErrors(const QString &field) const
76 {
77  return d->errors.contains(field);
78 }
79 
81 {
82  QJsonObject json;
83 
84  if (!d->errors.empty()) {
85  auto i = d->errors.constBegin();
86  while (i != d->errors.constEnd()) {
87  json.insert(i.key(), QJsonValue(QJsonArray::fromStringList(i.value())));
88  ++i;
89  }
90  }
91 
92  return json;
93 }
94 
95 QStringList ValidatorResult::failedFields() const
96 {
97  return QStringList(d->errors.keys());
98 }
99 
100 QVariantHash ValidatorResult::values() const
101 {
102  return d->values;
103 }
104 
105 QVariant ValidatorResult::value(const QString &field) const
106 {
107  return d->values.value(field);
108 }
109 
110 void ValidatorResult::addValue(const QString &field, const QVariant &value)
111 {
112  d->values.insert(field, value);
113 }
114 
115 QVariantHash ValidatorResult::extras() const
116 {
117  return d->extras;
118 }
119 
120 QVariant ValidatorResult::extra(const QString &field) const
121 {
122  return d->extras.value(field);
123 }
124 
125 void ValidatorResult::addExtra(const QString &field, const QVariant &extra)
126 {
127  d->extras.insert(field, extra);
128 }
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.
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.
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.