proxygen
JSONSchemaTester.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015-present Facebook, Inc.
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  */
17 #include <folly/json.h>
18 #include <fstream>
19 #include <sstream>
20 #include <string>
21 
30 int main(int argc, char** argv) {
31  if (argc < 2) {
32  printf("Usage: %s <testfile> [testfile2]...\n", argv[0]);
33  return -1;
34  }
35  for (int i = 1; i < argc; ++i) {
36  printf("FILE: %s\n", argv[i]);
37  std::ifstream fin(argv[i]);
38  std::stringstream buffer;
39  buffer << fin.rdbuf();
40  const folly::dynamic d = folly::parseJson(buffer.str());
41  for (const auto& item : d) {
42  printf("TEST: %s\n", item["description"].c_str());
43  auto v = folly::jsonschema::makeValidator(item["schema"]);
44  for (const auto& t : item["tests"]) {
45  printf("\t%s... ", t["description"].c_str());
46  auto ew = v->try_validate(t["data"]);
47  bool had_error = !static_cast<bool>(ew);
48  if (had_error == t["valid"].asBool()) {
49  printf("passed\n");
50  } else {
51  printf("FAILED\n");
52  }
53  }
54  }
55  }
56 }
std::vector< uint8_t > buffer(kBufferSize+16)
dynamic parseJson(StringPiece range)
Definition: json.cpp:900
char ** argv
std::unique_ptr< Validator > makeValidator(const dynamic &schema)
int main(int argc, char **argv)