Electroneum
schemavalidator.cpp
Go to the documentation of this file.
1 // Schema Validator example
2 
3 // The example validates JSON text from stdin with a JSON schema specified in the argument.
4 
5 #include "rapidjson/error/en.h"
7 #include "rapidjson/schema.h"
10 
11 using namespace rapidjson;
12 
13 int main(int argc, char *argv[]) {
14  if (argc != 2) {
15  fprintf(stderr, "Usage: schemavalidator schema.json < input.json\n");
16  return EXIT_FAILURE;
17  }
18 
19  // Read a JSON schema from file into Document
20  Document d;
21  char buffer[4096];
22 
23  {
24  FILE *fp = fopen(argv[1], "r");
25  if (!fp) {
26  printf("Schema file '%s' not found\n", argv[1]);
27  return -1;
28  }
29  FileReadStream fs(fp, buffer, sizeof(buffer));
30  d.ParseStream(fs);
31  if (d.HasParseError()) {
32  fprintf(stderr, "Schema file '%s' is not a valid JSON\n", argv[1]);
33  fprintf(stderr, "Error(offset %u): %s\n",
34  static_cast<unsigned>(d.GetErrorOffset()),
36  fclose(fp);
37  return EXIT_FAILURE;
38  }
39  fclose(fp);
40  }
41 
42  // Then convert the Document into SchemaDocument
43  SchemaDocument sd(d);
44 
45  // Use reader to parse the JSON in stdin, and forward SAX events to validator
46  SchemaValidator validator(sd);
47  Reader reader;
48  FileReadStream is(stdin, buffer, sizeof(buffer));
49  if (!reader.Parse(is, validator) && reader.GetParseErrorCode() != kParseErrorTermination) {
50  // Schema validator error would cause kParseErrorTermination, which will handle it in next step.
51  fprintf(stderr, "Input is not a valid JSON\n");
52  fprintf(stderr, "Error(offset %u): %s\n",
53  static_cast<unsigned>(reader.GetErrorOffset()),
55  }
56 
57  // Check the validation result
58  if (validator.IsValid()) {
59  printf("Input JSON is valid.\n");
60  return EXIT_SUCCESS;
61  }
62  else {
63  printf("Input JSON is invalid.\n");
64  StringBuffer sb;
65  validator.GetInvalidSchemaPointer().StringifyUriFragment(sb);
66  fprintf(stderr, "Invalid schema: %s\n", sb.GetString());
67  fprintf(stderr, "Invalid keyword: %s\n", validator.GetInvalidSchemaKeyword());
68  sb.Clear();
69  validator.GetInvalidDocumentPointer().StringifyUriFragment(sb);
70  fprintf(stderr, "Invalid document: %s\n", sb.GetString());
71  // Detailed violation report is available as a JSON value
72  sb.Clear();
74  validator.GetError().Accept(w);
75  fprintf(stderr, "Error report:\n%s\n", sb.GetString());
76  return EXIT_FAILURE;
77  }
78 }
ParseErrorCode GetParseError() const
Get the ParseErrorCode of last parsing.
Definition: document.h:2397
virtual bool IsValid() const
Checks whether the current state is valid.
Definition: schema.h:1855
ValueType & GetError()
Gets the error object.
Definition: schema.h:1858
size_t GetErrorOffset() const
Get the position of last parsing error in input, 0 otherwise.
Definition: document.h:2400
JSON Schema Validator.
Definition: fwd.h:145
const Ch * GetInvalidSchemaKeyword() const
Gets the keyword of invalid schema.
Definition: schema.h:1867
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text.
Definition: reader.h:557
File byte stream for input using fread().
bool HasParseError() const
Whether a parse error has occurred in the last parsing.
Definition: document.h:2394
PointerType GetInvalidDocumentPointer() const
Gets the JSON pointer pointed to the invalid value.
Definition: schema.h:1872
Writer with indentation and spacing.
Definition: fwd.h:100
int main(int argc, char *argv[])
main RapidJSON namespace
RAPIDJSON_NAMESPACE_BEGIN const RAPIDJSON_ERROR_CHARTYPE * GetParseError_En(ParseErrorCode parseErrorCode)
Maps error code of parsing into error message.
Definition: en.h:36
JSON schema document.
Definition: fwd.h:136
ParseErrorCode GetParseErrorCode() const
Get the ParseErrorCode of last parsing.
Definition: reader.h:683
const Ch * GetString() const
Definition: stringbuffer.h:73
PointerType GetInvalidSchemaPointer() const
Gets the JSON pointer pointed to the invalid schema.
Definition: schema.h:1862
Parsing was terminated.
Definition: error.h:88
GenericDocument & ParseStream(InputStream &is)
Parse JSON text from an input stream (with Encoding conversion)
Definition: document.h:2265
size_t GetErrorOffset() const
Get the position of last parsing error in input, 0 otherwise.
Definition: reader.h:686