mirror of
https://github.com/Tencent/rapidjson.git
synced 2025-03-09 11:09:32 +01:00
Add SchemaValidatingReader ::IsValid()
This commit is contained in:
parent
28e6a40fc6
commit
55d2867841
@ -1863,7 +1863,7 @@ public:
|
||||
typedef typename SchemaDocumentType::PointerType PointerType;
|
||||
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>
|
||||
bool operator()(Handler& handler) {
|
||||
@ -1871,7 +1871,7 @@ public:
|
||||
GenericSchemaValidator<SchemaDocumentType, Handler> validator(sd_, handler);
|
||||
parseResult_ = reader.template Parse<parseFlags>(is_, validator);
|
||||
|
||||
if (validator.IsValid()) {
|
||||
if ((isValid_ = validator.IsValid())) {
|
||||
invalidSchemaPointer_ = PointerType();
|
||||
invalidSchemaKeyword_ = 0;
|
||||
invalidDocumentPointer_ = PointerType();
|
||||
@ -1886,6 +1886,7 @@ public:
|
||||
}
|
||||
|
||||
const ParseResult& GetParseResult() const { return parseResult_; }
|
||||
bool IsValid() const { return isValid_; }
|
||||
const PointerType& GetInvalidSchemaPointer() const { return invalidSchemaPointer_; }
|
||||
const Ch* GetInvalidSchemaKeyword() const { return invalidSchemaKeyword_; }
|
||||
const PointerType& GetInvalidDocumentPointer() const { return invalidDocumentPointer_; }
|
||||
@ -1898,6 +1899,7 @@ private:
|
||||
PointerType invalidSchemaPointer_;
|
||||
const Ch* invalidSchemaKeyword_;
|
||||
PointerType invalidDocumentPointer_;
|
||||
bool isValid_;
|
||||
};
|
||||
|
||||
RAPIDJSON_NAMESPACE_END
|
||||
|
@ -15,6 +15,7 @@
|
||||
#include "unittest.h"
|
||||
#include "rapidjson/schema.h"
|
||||
#include "rapidjson/stringbuffer.h"
|
||||
#include "rapidjson/writer.h"
|
||||
|
||||
#ifdef __clang__
|
||||
RAPIDJSON_DIAG_PUSH
|
||||
@ -1094,7 +1095,7 @@ TEST(SchemaValidator, TestSuite) {
|
||||
// ADD_FAILURE();
|
||||
}
|
||||
|
||||
TEST(SchemaValidatingReader, Valid) {
|
||||
TEST(SchemaValidatingReader, Simple) {
|
||||
Document sd;
|
||||
sd.Parse("{ \"type\": \"string\", \"enum\" : [\"red\", \"amber\", \"green\"] }");
|
||||
SchemaDocument s(sd);
|
||||
@ -1104,6 +1105,7 @@ TEST(SchemaValidatingReader, Valid) {
|
||||
SchemaValidatingReader<kParseDefaultFlags, StringStream, UTF8<> > reader(ss, s);
|
||||
d.Populate(reader);
|
||||
EXPECT_TRUE(reader.GetParseResult());
|
||||
EXPECT_TRUE(reader.IsValid());
|
||||
EXPECT_TRUE(d.IsString());
|
||||
EXPECT_STREQ("red", d.GetString());
|
||||
}
|
||||
@ -1118,6 +1120,7 @@ TEST(SchemaValidatingReader, Invalid) {
|
||||
SchemaValidatingReader<kParseDefaultFlags, StringStream, UTF8<> > reader(ss, s);
|
||||
d.Populate(reader);
|
||||
EXPECT_FALSE(reader.GetParseResult());
|
||||
EXPECT_FALSE(reader.IsValid());
|
||||
EXPECT_EQ(kParseErrorTermination, reader.GetParseResult().Code());
|
||||
EXPECT_STREQ("maxLength", reader.GetInvalidSchemaKeyword());
|
||||
EXPECT_TRUE(reader.GetInvalidSchemaPointer() == SchemaDocument::PointerType(""));
|
||||
@ -1125,6 +1128,28 @@ TEST(SchemaValidatingReader, Invalid) {
|
||||
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__
|
||||
RAPIDJSON_DIAG_POP
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user