blob: 6db1157ec56f2a1f1000104c648684d501bfc912 [file] [log] [blame]
James Feist3cb5fec2018-01-23 14:41:51 -08001/*
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 Feistb4383f42018-08-06 16:54:10 -070021#include <valijson/adapters/nlohmann_json_adapter.hpp>
22#include <valijson/schema.hpp>
23#include <valijson/schema_parser.hpp>
24#include <valijson/validator.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080025
26namespace fs = std::experimental::filesystem;
27
28bool 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 Feistc95cb142018-02-26 10:41:42 -080040 if (std::regex_search(path, match, search))
James Feist3cb5fec2018-01-23 14:41:51 -080041 {
James Feistc95cb142018-02-26 10:41:42 -080042 found_paths.emplace_back(p.path());
James Feist3cb5fec2018-01-23 14:41:51 -080043 }
44 // since we're using a recursve iterator, these should only be symlink
45 // dirs
James Feistc95cb142018-02-26 10:41:42 -080046 else if (is_directory(p) && symlink_depth)
James Feist3cb5fec2018-01-23 14:41:51 -080047 {
48 find_files(p.path(), match_string, found_paths, symlink_depth - 1);
49 }
50 }
51 return true;
James Feistb4383f42018-08-06 16:54:10 -070052}
53
54bool 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 Feist3cb5fec2018-01-23 14:41:51 -080067}