2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <folly/experimental/JSONSchema.h>
17 #include <folly/json.h>
22 * A binary that supports testing against the official tests from:
23 * https://github.com/json-schema/JSON-Schema-Test-Suite
26 * ./jsonschema_tester /path/to/test.json
29 int main(int argc, char** argv) {
31 printf("Usage: %s <testfile> [testfile2]...\n", argv[0]);
34 for (int i = 1; i < argc; ++i) {
35 printf("FILE: %s\n", argv[i]);
36 std::ifstream fin(argv[i]);
37 std::stringstream buffer;
38 buffer << fin.rdbuf();
39 const folly::dynamic d = folly::parseJson(buffer.str());
40 for (const auto& item : d) {
41 printf("TEST: %s\n", item["description"].c_str());
42 auto v = folly::jsonschema::makeValidator(item["schema"]);
43 for (const auto& t : item["tests"]) {
44 printf("\t%s... ", t["description"].c_str());
45 auto ew = v->try_validate(t["data"]);
46 bool had_error = !static_cast<bool>(ew);
47 if (had_error == t["valid"].asBool()) {