| 46 | JsonValidator::JsonValidator() {} |
| 47 | |
| 48 | bool JsonValidator::Check(const QString &json_data) { |
| 49 | assert(!json_data.isEmpty()); |
| 50 | |
| 51 | if (!schema) { |
| 52 | const QJsonDocument schema_document = ParseJsonFile(":/vkconfig/schema.json"); |
| 53 | |
| 54 | schema.reset(new Schema); |
| 55 | |
| 56 | SchemaParser parser; |
| 57 | QtJsonAdapter schema_adapter(schema_document.object()); |
| 58 | parser.populateSchema(schema_adapter, *schema); |
| 59 | } |
| 60 | |
| 61 | QJsonParseError json_parse_error; |
| 62 | const QJsonDocument json_document = QJsonDocument::fromJson(json_data.toUtf8(), &json_parse_error); |
| 63 | if (json_parse_error.error != QJsonParseError::NoError) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | Validator validator(Validator::kWeakTypes); |
| 68 | QtJsonAdapter document_adapter(json_document.object()); |
| 69 | |
| 70 | ValidationResults results; |
| 71 | if (!validator.validate(*schema, document_adapter, &results)) { |
| 72 | ValidationResults::Error error; |
| 73 | unsigned int error_num = 1; |
| 74 | while (results.popError(error)) { |
| 75 | std::string context; |
| 76 | std::vector<std::string>::iterator itr = error.context.begin(); |
| 77 | for (; itr != error.context.end(); itr++) { |
| 78 | context += *itr; |
| 79 | } |
| 80 | |
| 81 | // if (error_num <= 3) { |
| 82 | std::string log = format("Error #%d\n", error_num); |
| 83 | log += "\t context: " + context + "\n"; |
| 84 | log += "\t desc: " + error.description + "\n\n"; |
| 85 | |
| 86 | message += log.c_str(); |
| 87 | //} |
| 88 | |
| 89 | ++error_num; |
| 90 | } |
| 91 | |
| 92 | message += format("Total Error Count: %d\n", error_num).c_str(); |
| 93 | |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | #else |
| 101 |
no test coverage detected