2016-07-20 17:16:06 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonValue>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QStringList>
|
2017-07-30 13:32:10 +02:00
|
|
|
#include <QPair>
|
2016-07-20 17:16:06 +02:00
|
|
|
|
|
|
|
/// JsonSchemaChecker is a very basic implementation of json schema.
|
|
|
|
/// The json schema definition draft can be found at
|
|
|
|
/// http://tools.ietf.org/html/draft-zyp-json-schema-03
|
|
|
|
///
|
|
|
|
/// The following keywords are supported:
|
|
|
|
/// - type
|
|
|
|
/// - required
|
|
|
|
/// - properties
|
|
|
|
/// - items
|
|
|
|
/// - enum
|
|
|
|
/// - minimum
|
|
|
|
/// - maximum
|
|
|
|
/// - addtionalProperties
|
|
|
|
/// - minItems
|
|
|
|
/// - maxItems
|
2017-10-12 11:55:03 +02:00
|
|
|
/// - minLength
|
|
|
|
/// - maxLength
|
2016-07-20 17:16:06 +02:00
|
|
|
|
|
|
|
class QJsonSchemaChecker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
QJsonSchemaChecker();
|
|
|
|
virtual ~QJsonSchemaChecker();
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @param schema The schema to use
|
|
|
|
/// @return true upon succes
|
|
|
|
///
|
|
|
|
bool setSchema(const QJsonObject & schema);
|
2017-07-30 13:32:10 +02:00
|
|
|
|
2016-07-20 17:16:06 +02:00
|
|
|
///
|
|
|
|
/// @brief Validate a JSON structure
|
|
|
|
/// @param value The JSON value to check
|
2017-07-30 13:32:10 +02:00
|
|
|
/// @param ignoreRequired Ignore the "required" keyword in hyperion schema. Default is false
|
2017-10-12 11:55:03 +02:00
|
|
|
/// @return The first boolean is true when the arguments is valid according to the schema. The second is true when the schema contains no errors
|
2017-07-30 13:32:10 +02:00
|
|
|
/// @return TODO: Check the Schema in SetSchema() function and remove the QPair result
|
|
|
|
///
|
|
|
|
QPair<bool, bool> validate(const QJsonObject & value, bool ignoreRequired = false);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Auto correct a JSON structure
|
|
|
|
/// @param value The JSON value to correct
|
|
|
|
/// @param ignoreRequired Ignore the "required" keyword in hyperion schema. Default is false
|
|
|
|
/// @return The corrected JSON structure
|
2016-07-20 17:16:06 +02:00
|
|
|
///
|
2017-07-30 13:32:10 +02:00
|
|
|
QJsonObject getAutoCorrectedConfig(const QJsonObject & value, bool ignoreRequired = false);
|
2016-07-20 17:16:06 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @return A list of error messages
|
|
|
|
///
|
2017-03-04 22:17:42 +01:00
|
|
|
const QStringList & getMessages() const;
|
2016-07-20 17:16:06 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
///
|
|
|
|
/// Validates a json-value against a given schema. Results are stored in the members of this
|
|
|
|
/// class (_error & _messages)
|
|
|
|
///
|
|
|
|
/// @param[in] value The value to validate
|
|
|
|
/// @param[in] schema The schema against which the value is validated
|
|
|
|
///
|
|
|
|
void validate(const QJsonValue &value, const QJsonObject & schema);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Adds the given message to the message-queue (with reference to current line-number)
|
|
|
|
///
|
|
|
|
/// @param[in] message The message to add to the queue
|
|
|
|
///
|
2017-03-04 22:17:42 +01:00
|
|
|
void setMessage(const QString & message);
|
2016-07-20 17:16:06 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
// attribute check functions
|
|
|
|
///
|
|
|
|
/// Checks if the given value is of the specified type. If the type does not match _error is set
|
|
|
|
/// to true and an error-message is added to the message-queue
|
|
|
|
///
|
|
|
|
/// @param[in] value The given value
|
|
|
|
/// @param[in] schema The specified type (as json-value)
|
|
|
|
///
|
2017-07-30 13:32:10 +02:00
|
|
|
void checkType(const QJsonValue & value, const QJsonValue & schema, const QJsonValue & defaultValue);
|
2017-10-12 11:55:03 +02:00
|
|
|
|
2016-07-20 17:16:06 +02:00
|
|
|
///
|
|
|
|
/// Checks is required properties of an json-object exist and if all properties are of the
|
|
|
|
/// correct format. If this is not the case _error is set to true and an error-message is added
|
|
|
|
/// to the message-queue.
|
|
|
|
///
|
|
|
|
/// @param[in] value The given json-object
|
|
|
|
/// @param[in] schema The schema of the json-object
|
|
|
|
///
|
|
|
|
void checkProperties(const QJsonObject & value, const QJsonObject & schema);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Verifies the additional configured properties of an json-object. If this is not the case
|
|
|
|
/// _error is set to true and an error-message is added to the message-queue.
|
|
|
|
///
|
|
|
|
/// @param value The given json-object
|
|
|
|
/// @param schema The schema for the json-object
|
|
|
|
/// @param ignoredProperties The properties that were ignored
|
|
|
|
///
|
|
|
|
void checkAdditionalProperties(const QJsonObject & value, const QJsonValue & schema, const QStringList & ignoredProperties);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Checks if the given value is larger or equal to the specified value. If this is not the case
|
|
|
|
/// _error is set to true and an error-message is added to the message-queue.
|
|
|
|
///
|
|
|
|
/// @param[in] value The given value
|
|
|
|
/// @param[in] schema The minimum value (as json-value)
|
|
|
|
///
|
2017-07-30 13:32:10 +02:00
|
|
|
void checkMinimum(const QJsonValue & value, const QJsonValue & schema, const QJsonValue & defaultValue);
|
2016-07-20 17:16:06 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Checks if the given value is smaller or equal to the specified value. If this is not the
|
|
|
|
/// case _error is set to true and an error-message is added to the message-queue.
|
|
|
|
///
|
|
|
|
/// @param[in] value The given value
|
|
|
|
/// @param[in] schema The maximum value (as json-value)
|
|
|
|
///
|
2017-07-30 13:32:10 +02:00
|
|
|
void checkMaximum(const QJsonValue & value, const QJsonValue & schema, const QJsonValue & defaultValue);
|
2017-03-24 10:17:36 +01:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Checks if the given value is hugher than the specified value. If this is the
|
|
|
|
/// case _error is set to true and an error-message is added to the message-queue.
|
|
|
|
///
|
2017-10-12 11:55:03 +02:00
|
|
|
/// @param value The given value
|
2017-03-24 10:17:36 +01:00
|
|
|
/// @param schema The minimum size specification (as json-value)
|
|
|
|
///
|
2017-07-30 13:32:10 +02:00
|
|
|
void checkMinLength(const QJsonValue & value, const QJsonValue & schema, const QJsonValue & defaultValue);
|
2017-03-24 10:17:36 +01:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Checks if the given value is smaller than the specified value. If this is the
|
|
|
|
/// case _error is set to true and an error-message is added to the message-queue.
|
|
|
|
///
|
2017-10-12 11:55:03 +02:00
|
|
|
/// @param value The given value
|
2017-03-24 10:17:36 +01:00
|
|
|
/// @param schema The maximum size specification (as json-value)
|
|
|
|
///
|
2017-07-30 13:32:10 +02:00
|
|
|
void checkMaxLength(const QJsonValue & value, const QJsonValue & schema, const QJsonValue & defaultValue);
|
2016-07-20 17:16:06 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Validates all the items of an array.
|
|
|
|
///
|
|
|
|
/// @param value The json-array
|
|
|
|
/// @param schema The schema for the items in the array
|
|
|
|
///
|
|
|
|
void checkItems(const QJsonValue & value, const QJsonObject & schema);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Checks if a given array has at least a minimum number of items. If this is not the case
|
|
|
|
/// _error is set to true and an error-message is added to the message-queue.
|
|
|
|
///
|
|
|
|
/// @param value The json-array
|
|
|
|
/// @param schema The minimum size specification (as json-value)
|
|
|
|
///
|
2017-07-30 13:32:10 +02:00
|
|
|
void checkMinItems(const QJsonValue & value, const QJsonValue & schema, const QJsonValue & defaultValue);
|
2016-07-20 17:16:06 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Checks if a given array has at most a maximum number of items. If this is not the case
|
|
|
|
/// _error is set to true and an error-message is added to the message-queue.
|
|
|
|
///
|
|
|
|
/// @param value The json-array
|
|
|
|
/// @param schema The maximum size specification (as json-value)
|
|
|
|
///
|
2017-07-30 13:32:10 +02:00
|
|
|
void checkMaxItems(const QJsonValue & value, const QJsonValue & schema, const QJsonValue & defaultValue);
|
2016-07-20 17:16:06 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Checks if a given array contains only unique items. If this is not the case
|
|
|
|
/// _error is set to true and an error-message is added to the message-queue.
|
|
|
|
///
|
|
|
|
/// @param value The json-array
|
|
|
|
/// @param schema Bool to enable the check (as json-value)
|
|
|
|
///
|
|
|
|
void checkUniqueItems(const QJsonValue & value, const QJsonValue & schema);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Checks if an enum value is actually a valid value for that enum. If this is not the case
|
|
|
|
/// _error is set to true and an error-message is added to the message-queue.
|
|
|
|
///
|
|
|
|
/// @param value The enum value
|
|
|
|
/// @param schema The enum schema definition
|
|
|
|
///
|
2017-07-30 13:32:10 +02:00
|
|
|
void checkEnum(const QJsonValue & value, const QJsonValue & schema, const QJsonValue & defaultValue);
|
2016-07-20 17:16:06 +02:00
|
|
|
|
2020-03-27 23:13:58 +01:00
|
|
|
///
|
|
|
|
/// @brief Return the "default" value as string. If not found, an empty string is output
|
|
|
|
///
|
|
|
|
/// @param value The JSON value to search
|
|
|
|
/// @return The "default" value as string
|
|
|
|
///
|
|
|
|
QString getDefaultValue(const QJsonValue & value);
|
|
|
|
|
2016-07-20 17:16:06 +02:00
|
|
|
private:
|
|
|
|
/// The schema of the entire json-configuration
|
|
|
|
QJsonObject _qSchema;
|
2016-10-09 22:22:17 +02:00
|
|
|
/// ignore the required value in json schema
|
|
|
|
bool _ignoreRequired;
|
2017-07-30 13:32:10 +02:00
|
|
|
/// Auto correction variable
|
|
|
|
QString _correct;
|
|
|
|
/// The auto corrected json-configuration
|
|
|
|
QJsonObject _autoCorrected;
|
2016-07-20 17:16:06 +02:00
|
|
|
/// The current location into a json-configuration structure being checked
|
2017-03-04 22:17:42 +01:00
|
|
|
QStringList _currentPath;
|
2016-07-20 17:16:06 +02:00
|
|
|
/// The result messages collected during the schema verification
|
2017-03-04 22:17:42 +01:00
|
|
|
QStringList _messages;
|
2016-07-20 17:16:06 +02:00
|
|
|
/// Flag indicating an error occured during validation
|
|
|
|
bool _error;
|
2017-07-30 13:32:10 +02:00
|
|
|
/// Flag indicating an schema error occured during validation
|
|
|
|
bool _schemaError;
|
2016-07-20 17:16:06 +02:00
|
|
|
};
|