blob: be7b35ccf73200ffcf14091ace8cd210132cafde [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
James Feista3c180a2018-08-09 16:06:04 -070028bool findFiles(const fs::path &dirPath, const std::string &matchString,
29 std::vector<fs::path> &foundPaths)
James Feist3cb5fec2018-01-23 14:41:51 -080030{
James Feista3c180a2018-08-09 16:06:04 -070031 if (!fs::exists(dirPath))
James Feist3cb5fec2018-01-23 14:41:51 -080032 return false;
33
James Feista3c180a2018-08-09 16:06:04 -070034 std::regex search(matchString);
James Feist3cb5fec2018-01-23 14:41:51 -080035 std::smatch match;
James Feista3c180a2018-08-09 16:06:04 -070036 for (const auto &p : fs::directory_iterator(dirPath))
James Feist3cb5fec2018-01-23 14:41:51 -080037 {
38 std::string path = p.path().string();
James Feistc95cb142018-02-26 10:41:42 -080039 if (std::regex_search(path, match, search))
James Feist3cb5fec2018-01-23 14:41:51 -080040 {
James Feista3c180a2018-08-09 16:06:04 -070041 foundPaths.emplace_back(p.path());
James Feist3cb5fec2018-01-23 14:41:51 -080042 }
43 }
44 return true;
James Feistb4383f42018-08-06 16:54:10 -070045}
46
47bool 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 Feist3cb5fec2018-01-23 14:41:51 -080060}