blob: 66841defefdb83aaa98f801c00318ca91ebd7644 [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
Ed Tanous072e25d2018-12-16 21:45:20 -080017#include "filesystem.hpp"
James Feista465ccc2019-02-08 12:51:01 -080018
19#include <Utils.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080020#include <fstream>
21#include <regex>
James Feistb4383f42018-08-06 16:54:10 -070022#include <valijson/adapters/nlohmann_json_adapter.hpp>
23#include <valijson/schema.hpp>
24#include <valijson/schema_parser.hpp>
25#include <valijson/validator.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080026
Ed Tanous072e25d2018-12-16 21:45:20 -080027namespace fs = std::filesystem;
James Feist3cb5fec2018-01-23 14:41:51 -080028
James Feista465ccc2019-02-08 12:51:01 -080029bool findFiles(const fs::path& dirPath, const std::string& matchString,
30 std::vector<fs::path>& foundPaths)
James Feist3cb5fec2018-01-23 14:41:51 -080031{
James Feista3c180a2018-08-09 16:06:04 -070032 if (!fs::exists(dirPath))
James Feist3cb5fec2018-01-23 14:41:51 -080033 return false;
34
James Feista3c180a2018-08-09 16:06:04 -070035 std::regex search(matchString);
James Feist3cb5fec2018-01-23 14:41:51 -080036 std::smatch match;
James Feista465ccc2019-02-08 12:51:01 -080037 for (const auto& p : fs::directory_iterator(dirPath))
James Feist3cb5fec2018-01-23 14:41:51 -080038 {
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 Feista3c180a2018-08-09 16:06:04 -070042 foundPaths.emplace_back(p.path());
James Feist3cb5fec2018-01-23 14:41:51 -080043 }
44 }
45 return true;
James Feistb4383f42018-08-06 16:54:10 -070046}
47
James Feista465ccc2019-02-08 12:51:01 -080048bool validateJson(const nlohmann::json& schemaFile, const nlohmann::json& input)
James Feistb4383f42018-08-06 16:54:10 -070049{
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 Tanous072e25d2018-12-16 21:45:20 -080061}