James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2017 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <Utils.hpp> |
| 18 | #include <experimental/filesystem> |
| 19 | #include <fstream> |
| 20 | #include <regex> |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 21 | #include <valijson/adapters/nlohmann_json_adapter.hpp> |
| 22 | #include <valijson/schema.hpp> |
| 23 | #include <valijson/schema_parser.hpp> |
| 24 | #include <valijson/validator.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 25 | |
| 26 | namespace fs = std::experimental::filesystem; |
| 27 | |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 28 | bool findFiles(const fs::path &dirPath, const std::string &matchString, |
| 29 | std::vector<fs::path> &foundPaths) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 30 | { |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 31 | if (!fs::exists(dirPath)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 32 | return false; |
| 33 | |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 34 | std::regex search(matchString); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 35 | std::smatch match; |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 36 | for (const auto &p : fs::directory_iterator(dirPath)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 37 | { |
| 38 | std::string path = p.path().string(); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 39 | if (std::regex_search(path, match, search)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 40 | { |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 41 | foundPaths.emplace_back(p.path()); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | return true; |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | bool validateJson(const nlohmann::json &schemaFile, const nlohmann::json &input) |
| 48 | { |
| 49 | valijson::Schema schema; |
| 50 | valijson::SchemaParser parser; |
| 51 | valijson::adapters::NlohmannJsonAdapter schemaAdapter(schemaFile); |
| 52 | parser.populateSchema(schemaAdapter, schema); |
| 53 | valijson::Validator validator; |
| 54 | valijson::adapters::NlohmannJsonAdapter targetAdapter(input); |
| 55 | if (!validator.validate(schema, targetAdapter, NULL)) |
| 56 | { |
| 57 | return false; |
| 58 | } |
| 59 | return true; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 60 | } |