1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "cast/protocol/castv2/validation.h"
6
7 #include <mutex> // NOLINT
8 #include <string>
9
10 #include "cast/protocol/castv2/receiver_schema_data.h"
11 #include "cast/protocol/castv2/streaming_schema_data.h"
12 #include "third_party/valijson/src/include/valijson/adapters/jsoncpp_adapter.hpp"
13 #include "third_party/valijson/src/include/valijson/schema.hpp"
14 #include "third_party/valijson/src/include/valijson/schema_parser.hpp"
15 #include "third_party/valijson/src/include/valijson/utils/jsoncpp_utils.hpp"
16 #include "third_party/valijson/src/include/valijson/validator.hpp"
17 #include "util/json/json_serialization.h"
18 #include "util/osp_logging.h"
19 #include "util/std_util.h"
20 #include "util/stringprintf.h"
21
22 namespace openscreen {
23 namespace cast {
24
25 namespace {
26
MapErrors(const valijson::ValidationResults & results)27 std::vector<Error> MapErrors(const valijson::ValidationResults& results) {
28 std::vector<Error> errors;
29 errors.reserve(results.numErrors());
30 for (const auto& result : results) {
31 const std::string context = Join(result.context, ", ");
32 errors.emplace_back(Error::Code::kJsonParseError,
33 StringPrintf("Node: %s, Message: %s", context.c_str(),
34 result.description.c_str()));
35
36 OSP_DVLOG << "JsonCpp validation error: "
37 << errors.at(errors.size() - 1).message();
38 }
39 return errors;
40 }
41
LoadSchema(const char * schema_json,valijson::Schema * schema)42 void LoadSchema(const char* schema_json, valijson::Schema* schema) {
43 Json::Value root = json::Parse(schema_json).value();
44 valijson::adapters::JsonCppAdapter adapter(root);
45 valijson::SchemaParser parser;
46 parser.populateSchema(adapter, *schema);
47 }
48
Validate(const Json::Value & document,const valijson::Schema & schema)49 std::vector<Error> Validate(const Json::Value& document,
50 const valijson::Schema& schema) {
51 valijson::Validator validator;
52 valijson::adapters::JsonCppAdapter document_adapter(document);
53 valijson::ValidationResults results;
54 if (validator.validate(schema, document_adapter, &results)) {
55 return {};
56 }
57 return MapErrors(results);
58 }
59
60 } // anonymous namespace
Validate(const Json::Value & document,const Json::Value & schema_root)61 std::vector<Error> Validate(const Json::Value& document,
62 const Json::Value& schema_root) {
63 valijson::adapters::JsonCppAdapter adapter(schema_root);
64 valijson::Schema schema;
65 valijson::SchemaParser parser;
66 parser.populateSchema(adapter, schema);
67
68 return Validate(document, schema);
69 }
70
ValidateStreamingMessage(const Json::Value & message)71 std::vector<Error> ValidateStreamingMessage(const Json::Value& message) {
72 static valijson::Schema schema;
73 static std::once_flag flag;
74 std::call_once(flag, [] { LoadSchema(kStreamingSchema, &schema); });
75 return Validate(message, schema);
76 }
77
ValidateReceiverMessage(const Json::Value & message)78 std::vector<Error> ValidateReceiverMessage(const Json::Value& message) {
79 static valijson::Schema schema;
80 static std::once_flag flag;
81 std::call_once(flag, [] { LoadSchema(kReceiverSchema, &schema); });
82 return Validate(message, schema);
83 }
84
85 } // namespace cast
86 } // namespace openscreen
87