3#include <QtCore/QObject>
8QString jsonValueTypeToString(QJsonValue::Type type)
12 QJsonValue::Type type;
16 static constexpr const TypeToString typeToStringMap[] = {
17 {QJsonValue::Null,
"NULL"}, {QJsonValue::Bool,
"Bool"}, {QJsonValue::Double,
"Double"},
18 {QJsonValue::String,
"String"}, {QJsonValue::Array,
"Array"}, {QJsonValue::Object,
"Object"},
19 {QJsonValue::Undefined,
"Undefined"},
22 for (
const TypeToString& entry : typeToStringMap) {
23 if (type == entry.type) {
28 return QObject::tr(
"Unknown type: %1").arg(type);
31bool validateMissingKeys(
const QStringList& missingKeys, QString&
errorString)
33 if (missingKeys.isEmpty()) {
37 QObject::tr(
"The following required keys are missing: %1").arg(missingKeys.join(QStringLiteral(
", ")));
41bool validateValueType(
const QString& key, QJsonValue::Type actual, QJsonValue::Type expected, QString&
errorString)
44 if (expected == QJsonValue::Undefined || actual == expected ||
45 (actual == QJsonValue::Double && expected == QJsonValue::Null)) {
48 errorString = QObject::tr(
"Incorrect value type - key:type:expected %1:%2:%3")
49 .arg(key, jsonValueTypeToString(actual), jsonValueTypeToString(expected));
59 QStringList missingKeys;
60 for (
const QString& key :
keys) {
61 if (!jsonObject.contains(key)) {
62 missingKeys.append(key);
65 return validateMissingKeys(missingKeys,
errorString);
68bool validateKeyTypes(
const QJsonObject& jsonObject,
const QStringList&
keys,
const QList<QJsonValue::Type>& types,
71 if (
keys.count() != types.count()) {
73 QObject::tr(
"Mismatched key and type list sizes: keys=%1 types=%2").arg(
keys.count()).arg(types.count());
77 for (qsizetype i = 0; i < types.size(); ++i) {
78 const auto value = jsonObject.constFind(
keys[i]);
79 if (value != jsonObject.constEnd() && !validateValueType(
keys[i], value->type(), types[i],
errorString)) {
88 QStringList missingKeys;
90 const QString key = QString::fromUtf8(info.key);
91 if (info.required && !jsonObject.contains(key)) {
92 missingKeys.append(key);
95 if (!validateMissingKeys(missingKeys,
errorString)) {
99 const QString key = QString::fromUtf8(info.key);
100 const auto value = jsonObject.constFind(key);
101 if (value != jsonObject.constEnd() && !validateValueType(key, value->type(), info.type,
errorString)) {
114 QSet<QString> expectedKeys;
115 expectedKeys.reserve(keyInfo.size());
117 expectedKeys.insert(QString::fromUtf8(info.key));
120 for (
auto it = jsonObject.constBegin(); it != jsonObject.constEnd(); ++it) {
121 if (!expectedKeys.contains(it.key())) {
122 errorString = QObject::tr(
"Unknown key: %1").arg(it.key());
std::array< MsgOutKey, 4 > keys
bool validateKeyTypes(const QJsonObject &jsonObject, const QStringList &keys, const QList< QJsonValue::Type > &types, QString &errorString)
bool validateKeys(const QJsonObject &jsonObject, const QList< KeyValidateInfo > &keyInfo, QString &errorString)
Validates that all required keys are present and that listed keys have the expected type.
bool validateKeysStrict(const QJsonObject &jsonObject, const QList< KeyValidateInfo > &keyInfo, QString &errorString)
Validates keys like validateKeys but also rejects any keys not listed in keyInfo.
bool validateRequiredKeys(const QJsonObject &jsonObject, const QStringList &keys, QString &errorString)
Validates that all listed keys are present in the object.