25std::shared_ptr<const valijson::Schema> loadSchema(
const QString& schemaResourcePath, QString&
errorString)
28 static QHash<QString, std::shared_ptr<const valijson::Schema>> schemaCache;
30 if (
const auto cached = schemaCache.value(schemaResourcePath)) {
34 QFile schemaFile(schemaResourcePath);
35 if (!schemaFile.open(QIODevice::ReadOnly)) {
36 errorString = QStringLiteral(
"Failed to open schema resource: %1").arg(schemaResourcePath);
40 QJsonParseError parseError;
41 const QJsonDocument schemaDoc = QJsonDocument::fromJson(schemaFile.readAll(), &parseError);
42 if (parseError.error != QJsonParseError::NoError) {
43 errorString = QStringLiteral(
"Failed to parse schema %1: %2").arg(schemaResourcePath, parseError.errorString());
49 QJsonObject schemaObj = schemaDoc.object();
50 QJsonObject schemaProps = schemaObj.value(QStringLiteral(
"properties")).toObject();
51 if (schemaProps.contains(QStringLiteral(
"version"))) {
52 QJsonObject versionSchema = schemaProps.value(QStringLiteral(
"version")).toObject();
53 versionSchema.remove(QStringLiteral(
"minimum"));
54 versionSchema.remove(QStringLiteral(
"maximum"));
55 schemaProps[QStringLiteral(
"version")] = versionSchema;
56 schemaObj[QStringLiteral(
"properties")] = schemaProps;
59 auto schema = std::make_shared<valijson::Schema>();
61 valijson::SchemaParser parser;
62 const valijson::adapters::QtJsonAdapter schemaAdapter(schemaObj);
63 parser.populateSchema(schemaAdapter, *schema);
64 }
catch (
const std::exception& e) {
66 QStringLiteral(
"Failed to load schema %1: %2").arg(schemaResourcePath, QString::fromUtf8(e.what()));
70 schemaCache.insert(schemaResourcePath, schema);
80 const auto schema = loadSchema(schemaResourcePath,
errorString);
85 valijson::Validator validator;
86 valijson::ValidationResults results;
87 const valijson::adapters::QtJsonAdapter docAdapter(doc.isArray() ? QJsonValue(doc.array())
88 : QJsonValue(doc.object()));
90 if (validator.validate(*schema, docAdapter, &results)) {
95 valijson::ValidationResults::Error
error;
96 while (results.popError(
error)) {
97 failures.append(QStringLiteral(
"%1: %2").arg(QString::fromStdString(
error.jsonPointer),
98 QString::fromStdString(
error.description)));