Commit 55d28678 authored by Milo Yip's avatar Milo Yip

Add SchemaValidatingReader ::IsValid()

parent 28e6a40f
...@@ -1863,7 +1863,7 @@ public: ...@@ -1863,7 +1863,7 @@ public:
typedef typename SchemaDocumentType::PointerType PointerType; typedef typename SchemaDocumentType::PointerType PointerType;
typedef typename InputStream::Ch Ch; typedef typename InputStream::Ch Ch;
SchemaValidatingReader(InputStream& is, const SchemaDocumentType& sd) : is_(is), sd_(sd), invalidSchemaKeyword_() {} SchemaValidatingReader(InputStream& is, const SchemaDocumentType& sd) : is_(is), sd_(sd), invalidSchemaKeyword_(), isValid_(true) {}
template <typename Handler> template <typename Handler>
bool operator()(Handler& handler) { bool operator()(Handler& handler) {
...@@ -1871,7 +1871,7 @@ public: ...@@ -1871,7 +1871,7 @@ public:
GenericSchemaValidator<SchemaDocumentType, Handler> validator(sd_, handler); GenericSchemaValidator<SchemaDocumentType, Handler> validator(sd_, handler);
parseResult_ = reader.template Parse<parseFlags>(is_, validator); parseResult_ = reader.template Parse<parseFlags>(is_, validator);
if (validator.IsValid()) { if ((isValid_ = validator.IsValid())) {
invalidSchemaPointer_ = PointerType(); invalidSchemaPointer_ = PointerType();
invalidSchemaKeyword_ = 0; invalidSchemaKeyword_ = 0;
invalidDocumentPointer_ = PointerType(); invalidDocumentPointer_ = PointerType();
...@@ -1886,6 +1886,7 @@ public: ...@@ -1886,6 +1886,7 @@ public:
} }
const ParseResult& GetParseResult() const { return parseResult_; } const ParseResult& GetParseResult() const { return parseResult_; }
bool IsValid() const { return isValid_; }
const PointerType& GetInvalidSchemaPointer() const { return invalidSchemaPointer_; } const PointerType& GetInvalidSchemaPointer() const { return invalidSchemaPointer_; }
const Ch* GetInvalidSchemaKeyword() const { return invalidSchemaKeyword_; } const Ch* GetInvalidSchemaKeyword() const { return invalidSchemaKeyword_; }
const PointerType& GetInvalidDocumentPointer() const { return invalidDocumentPointer_; } const PointerType& GetInvalidDocumentPointer() const { return invalidDocumentPointer_; }
...@@ -1898,6 +1899,7 @@ private: ...@@ -1898,6 +1899,7 @@ private:
PointerType invalidSchemaPointer_; PointerType invalidSchemaPointer_;
const Ch* invalidSchemaKeyword_; const Ch* invalidSchemaKeyword_;
PointerType invalidDocumentPointer_; PointerType invalidDocumentPointer_;
bool isValid_;
}; };
RAPIDJSON_NAMESPACE_END RAPIDJSON_NAMESPACE_END
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "unittest.h" #include "unittest.h"
#include "rapidjson/schema.h" #include "rapidjson/schema.h"
#include "rapidjson/stringbuffer.h" #include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#ifdef __clang__ #ifdef __clang__
RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PUSH
...@@ -1094,7 +1095,7 @@ TEST(SchemaValidator, TestSuite) { ...@@ -1094,7 +1095,7 @@ TEST(SchemaValidator, TestSuite) {
// ADD_FAILURE(); // ADD_FAILURE();
} }
TEST(SchemaValidatingReader, Valid) { TEST(SchemaValidatingReader, Simple) {
Document sd; Document sd;
sd.Parse("{ \"type\": \"string\", \"enum\" : [\"red\", \"amber\", \"green\"] }"); sd.Parse("{ \"type\": \"string\", \"enum\" : [\"red\", \"amber\", \"green\"] }");
SchemaDocument s(sd); SchemaDocument s(sd);
...@@ -1104,6 +1105,7 @@ TEST(SchemaValidatingReader, Valid) { ...@@ -1104,6 +1105,7 @@ TEST(SchemaValidatingReader, Valid) {
SchemaValidatingReader<kParseDefaultFlags, StringStream, UTF8<> > reader(ss, s); SchemaValidatingReader<kParseDefaultFlags, StringStream, UTF8<> > reader(ss, s);
d.Populate(reader); d.Populate(reader);
EXPECT_TRUE(reader.GetParseResult()); EXPECT_TRUE(reader.GetParseResult());
EXPECT_TRUE(reader.IsValid());
EXPECT_TRUE(d.IsString()); EXPECT_TRUE(d.IsString());
EXPECT_STREQ("red", d.GetString()); EXPECT_STREQ("red", d.GetString());
} }
...@@ -1118,6 +1120,7 @@ TEST(SchemaValidatingReader, Invalid) { ...@@ -1118,6 +1120,7 @@ TEST(SchemaValidatingReader, Invalid) {
SchemaValidatingReader<kParseDefaultFlags, StringStream, UTF8<> > reader(ss, s); SchemaValidatingReader<kParseDefaultFlags, StringStream, UTF8<> > reader(ss, s);
d.Populate(reader); d.Populate(reader);
EXPECT_FALSE(reader.GetParseResult()); EXPECT_FALSE(reader.GetParseResult());
EXPECT_FALSE(reader.IsValid());
EXPECT_EQ(kParseErrorTermination, reader.GetParseResult().Code()); EXPECT_EQ(kParseErrorTermination, reader.GetParseResult().Code());
EXPECT_STREQ("maxLength", reader.GetInvalidSchemaKeyword()); EXPECT_STREQ("maxLength", reader.GetInvalidSchemaKeyword());
EXPECT_TRUE(reader.GetInvalidSchemaPointer() == SchemaDocument::PointerType("")); EXPECT_TRUE(reader.GetInvalidSchemaPointer() == SchemaDocument::PointerType(""));
...@@ -1125,6 +1128,28 @@ TEST(SchemaValidatingReader, Invalid) { ...@@ -1125,6 +1128,28 @@ TEST(SchemaValidatingReader, Invalid) {
EXPECT_TRUE(d.IsNull()); EXPECT_TRUE(d.IsNull());
} }
TEST(SchemaValidatingWriter, Simple) {
Document sd;
sd.Parse("{\"type\":\"string\",\"minLength\":2,\"maxLength\":3}");
SchemaDocument s(sd);
Document d;
StringBuffer sb;
Writer<StringBuffer> writer(sb);
GenericSchemaValidator<SchemaDocument, Writer<StringBuffer> > validator(s, writer);
d.Parse("\"red\"");
EXPECT_TRUE(d.Accept(validator));
EXPECT_TRUE(validator.IsValid());
EXPECT_STREQ("\"red\"", sb.GetString());
sb.Clear();
validator.Reset();
d.Parse("\"ABCD\"");
EXPECT_FALSE(d.Accept(validator));
EXPECT_FALSE(validator.IsValid());
}
#ifdef __clang__ #ifdef __clang__
RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_POP
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment