cutelyst  5.0.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorjson.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2025 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatorjson_p.h"
7 
8 #include <QJsonArray>
9 #include <QJsonDocument>
10 #include <QJsonObject>
11 #include <QJsonParseError>
12 
13 using namespace Cutelyst;
14 
16  ExpectedType expectedType,
17  const Cutelyst::ValidatorMessages &messages,
18  const QString &defValKey)
19  : ValidatorRule(*new ValidatorJsonPrivate(field, expectedType, messages, defValKey))
20 {
21 }
22 
24 
26  const ParamsMultiMap &params) const
27 {
28  ValidatorReturnType result;
29 
30  Q_D(const ValidatorJson);
31 
32  const QString v = value(params);
33 
34  if (!v.isEmpty()) {
35  QJsonParseError jpe;
36  const QJsonDocument json = QJsonDocument::fromJson(v.toUtf8(), &jpe);
37  if (jpe.error == QJsonParseError::NoError) {
38  if (d->expectedType == ExpectedType::Array && !json.isArray()) {
39  result.errorMessage = validationError(c);
40  qCDebug(C_VALIDATOR).noquote()
41  << debugString(c) << "A JSON array is expected but not provided";
42  } else if (d->expectedType == ExpectedType::Object && !json.isObject()) {
43  result.errorMessage = validationError(c);
44  qCDebug(C_VALIDATOR).noquote()
45  << debugString(c) << "A JSON object is expected but not provided";
46  } else {
47  switch (d->expectedType) {
48  using enum ValidatorJson::ExpectedType;
49  case Array:
50  result.value.setValue(json.array());
51  break;
52  case Object:
53  result.value.setValue(json.object());
54  break;
55  case All:
56  result.value.setValue(json);
57  break;
58  }
59  }
60  } else {
61  result.errorMessage = validationError(c, jpe.errorString());
62  qCDebug(C_VALIDATOR).noquote() << debugString(c) << jpe.errorString();
63  }
64  } else {
65  defaultValue(c, &result);
66  }
67 
68  return result;
69 }
70 
72 {
73  cb(validate(c, params));
74 }
75 
77 {
78  const QString _label = label(c);
79  if (errorData.isNull()) {
80  Q_D(const ValidatorJson);
81  if (d->expectedType == ExpectedType::Array) {
82  if (_label.isEmpty()) {
83  //% "Not a JSON array."
84  return c->qtTrId("cutelyst-valjson-genvalerr-exparray");
85  } else {
86  //: %1 will be replaced by the field label
87  //% "The data entered in the “%1” field is not a JSON array."
88  return c->qtTrId("cutelyst-valjson-genvalerr-exparray-label");
89  }
90  } else {
91  if (_label.isEmpty()) {
92  //% "Not a JSON object."
93  return c->qtTrId("cutelyst-valjson-genvalerr-expobject");
94  } else {
95  //: %1 will be replaced by the field label
96  //% "The data entered in the “%1” field is not a JSON object."
97  return c->qtTrId("cutelyst-valjson-genvalerr-expobject-label");
98  }
99  }
100  } else {
101  const QString jsonError = errorData.toString();
102  if (_label.isEmpty()) {
103  if (!jsonError.isEmpty()) {
104  //: %1 will contain the json error
105  //% "Invalid JSON data: %1"
106  return c->qtTrId("cutelyst-valjson-genvalerr-data").arg(jsonError);
107  } else {
108  //% "Invalid JSON data."
109  return c->qtTrId("cutelyst-valjson-genvalerr");
110  }
111  } else {
112  if (!jsonError.isEmpty()) {
113  //: %1 will contain the field label, %2 will contain the json error
114  //% "The data entered in the “%1” field is not valid JSON: %2"
115  return c->qtTrId("cutelyst-valjson-genvalerr-data-label").arg(_label, jsonError);
116  } else {
117  //: %1 will be replaced by the field label
118  //% "The data entered in the “%1” field is not valid JSON."
119  return c->qtTrId("cutelyst-valjson-genvalerr-label").arg(_label);
120  }
121  }
122  }
123 }
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
QJsonArray array() const const
Stores custom error messages and the input field label.
QJsonObject object() const const
bool isObject() const const
The Cutelyst Context.
Definition: context.h:42
void defaultValue(Context *c, ValidatorReturnType *result) const
bool isNull() const const
bool isEmpty() const const
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
The Cutelyst namespace holds all public Cutelyst API.
QString debugString(const Context *c) const
Base class for all validator rules.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
QString value(const ParamsMultiMap &params) const
QString label(const Context *c) const
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
Definition: validatorrule.h:82
bool isArray() const const
QString validationError(Context *c, const QVariant &errorData={}) const
QString qtTrId(const char *id, int n=-1) const
Definition: context.h:658
ValidatorJson(const QString &field, ExpectedType expectedType=ExpectedType::All, const ValidatorMessages &messages={}, const QString &defValKey={})
Contains the result of a single input parameter validation.
Definition: validatorrule.h:52
QString errorString() const const
QString arg(Args &&... args) const const
QString toString() const const
Checks if the inut data is valid JSON.
Definition: validatorjson.h:36
void setValue(QVariant &&value)
QByteArray toUtf8() const const