userver: formats::json::Schema Class Reference
Loading...
Searching...
No Matches
formats::json::Schema Class Referencefinal

#include <userver/formats/json/schema.hpp>

Detailed Description

Contains a prepared JSON schema.

Usage example:

{
"type": "object",
"properties": {
"length": {"type": "number"},
"height": {"type": "number"}
},
"required": ["length", "height"]
}
)"));
{
{
"length": 10,
"height": 30
}
)");
auto result = schema.Validate(valid_json);
EXPECT_TRUE(result);
EXPECT_FALSE(result.IsError());
UEXPECT_NO_THROW(std::move(result).ThrowIfError());
}
{
{
"length": "WHAT",
"height": 30
}
)");
auto result = schema.Validate(invalid_json);
EXPECT_FALSE(result);
ASSERT_TRUE(result.IsError());
const auto error = std::move(result).GetError();
// The exact format of error details is unspecified.
EXPECT_THAT(std::string{error.GetValuePath()}, testing::HasSubstr("length"));
EXPECT_THAT(std::string{error.GetSchemaPath()}, testing::HasSubstr("properties"));
EXPECT_THAT(
std::string{error.GetDetailsString()},
testing::AllOf(testing::HasSubstr("number"), testing::HasSubstr("string"))
);
error.Throw(),
"Error at path 'length': "
R"({"expected":["number"],"actual":"string"})"
);
}

Definition at line 32 of file schema.hpp.

Classes

class  ValidationError
 
class  ValidationResult
 Contains error details (if any) from Schema::Validate. More...
 

Public Member Functions

 Schema (const Value &doc)
 
ValidationResult Validate (const Value &doc) const
 Validates JSON against the Schema.
 

Member Function Documentation

◆ Validate()

ValidationResult formats::json::Schema::Validate ( const Value & doc) const

Validates JSON against the Schema.

Returns
ValidationResult describing the validation error, if any.

The documentation for this class was generated from the following file: