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 | |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 17 | #include "filesystem.hpp" |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 18 | |
| 19 | #include <Utils.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 20 | #include <fstream> |
| 21 | #include <regex> |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 22 | #include <valijson/adapters/nlohmann_json_adapter.hpp> |
| 23 | #include <valijson/schema.hpp> |
| 24 | #include <valijson/schema_parser.hpp> |
| 25 | #include <valijson/validator.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 26 | |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 27 | namespace fs = std::filesystem; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 28 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 29 | bool findFiles(const fs::path& dirPath, const std::string& matchString, |
| 30 | std::vector<fs::path>& foundPaths) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 31 | { |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 32 | if (!fs::exists(dirPath)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 33 | return false; |
| 34 | |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 35 | std::regex search(matchString); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 36 | std::smatch match; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 37 | for (const auto& p : fs::directory_iterator(dirPath)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 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 | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 42 | foundPaths.emplace_back(p.path()); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | return true; |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 46 | } |
| 47 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 48 | bool validateJson(const nlohmann::json& schemaFile, const nlohmann::json& input) |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 49 | { |
| 50 | valijson::Schema schema; |
| 51 | valijson::SchemaParser parser; |
| 52 | valijson::adapters::NlohmannJsonAdapter schemaAdapter(schemaFile); |
| 53 | parser.populateSchema(schemaAdapter, schema); |
| 54 | valijson::Validator validator; |
| 55 | valijson::adapters::NlohmannJsonAdapter targetAdapter(input); |
| 56 | if (!validator.validate(schema, targetAdapter, NULL)) |
| 57 | { |
| 58 | return false; |
| 59 | } |
| 60 | return true; |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 61 | } |