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 | |
| 28 | bool find_files(const fs::path &dir_path, const std::string &match_string, |
| 29 | std::vector<fs::path> &found_paths, unsigned int symlink_depth) |
| 30 | { |
| 31 | if (!fs::exists(dir_path)) |
| 32 | return false; |
| 33 | |
| 34 | fs::directory_iterator end_itr; |
| 35 | std::regex search(match_string); |
| 36 | std::smatch match; |
| 37 | for (auto &p : fs::recursive_directory_iterator(dir_path)) |
| 38 | { |
| 39 | std::string path = p.path().string(); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 40 | if (std::regex_search(path, match, search)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 41 | { |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 42 | found_paths.emplace_back(p.path()); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 43 | } |
| 44 | // since we're using a recursve iterator, these should only be symlink |
| 45 | // dirs |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 46 | else if (is_directory(p) && symlink_depth) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 47 | { |
| 48 | find_files(p.path(), match_string, found_paths, symlink_depth - 1); |
| 49 | } |
| 50 | } |
| 51 | return true; |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame^] | 52 | } |
| 53 | |
| 54 | bool validateJson(const nlohmann::json &schemaFile, const nlohmann::json &input) |
| 55 | { |
| 56 | valijson::Schema schema; |
| 57 | valijson::SchemaParser parser; |
| 58 | valijson::adapters::NlohmannJsonAdapter schemaAdapter(schemaFile); |
| 59 | parser.populateSchema(schemaAdapter, schema); |
| 60 | valijson::Validator validator; |
| 61 | valijson::adapters::NlohmannJsonAdapter targetAdapter(input); |
| 62 | if (!validator.validate(schema, targetAdapter, NULL)) |
| 63 | { |
| 64 | return false; |
| 65 | } |
| 66 | return true; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 67 | } |