proxygen
JSONSchemaTester.cpp File Reference
#include <folly/experimental/JSONSchema.h>
#include <folly/json.h>
#include <fstream>
#include <sstream>
#include <string>

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

int main ( int  argc,
char **  argv 
)

A binary that supports testing against the official tests from: https://github.com/json-schema/JSON-Schema-Test-Suite

Use it like: ./jsonschema_tester /path/to/test.json

Definition at line 30 of file JSONSchemaTester.cpp.

References buffer(), i, folly::jsonschema::makeValidator(), folly::parseJson(), and folly::pushmi::detail::t.

30  {
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)