Add support for 'description' and 'title' attributes

This commit is contained in:
Tristan Penman 2015-12-21 23:20:29 +11:00
parent 9ec9a37222
commit 0227384534
2 changed files with 58 additions and 1 deletions

View File

@ -129,6 +129,22 @@ public:
return true;
}
/**
* @brief Get the description for this schema
*
* @throw std::runtime_error if the description has not been set
*
* @return schema description
*/
std::string getDescription() const
{
if (description) {
return *description;
}
throw std::runtime_error("Schema does not have a description");
}
std::string getId() const
{
if (id) {
@ -166,6 +182,17 @@ public:
throw std::runtime_error("Schema does not have a title.");
}
/**
* @brief Returns a boolean value that indicates whether the description
* has been set or not
*
* @return boolean value
*/
bool hasDescription() const
{
return description != boost::none;
}
/**
* @brief Returns a boolean value that indicates whether the id has been
* set or not.
@ -193,6 +220,19 @@ public:
return relative;
}
/**
* @brief Set the description for this schema
*
* The description will not be used for validation, but may be used as part
* of the user interface used to interact with a schema.
*
* @param description new description
*/
void setDescription(const std::string &description)
{
this->description = description;
}
void setId(const std::string &id)
{
this->id = id;
@ -217,6 +257,9 @@ private:
/// List of pointers to constraints that apply to this schema.
boost::ptr_vector<Constraint> constraints;
/// Schema description (optional)
boost::optional<std::string> description;
/// Id to apply when resolving the schema URI.
boost::optional<std::string> id;

View File

@ -162,6 +162,15 @@ private:
itr->second, fetchDoc));
}
if ((itr = object.find("description")) != object.end()) {
if (itr->second.maybeString()) {
schema.setDescription(itr->second.asString());
} else {
throw std::runtime_error(
"'description' attribute should have a string value");
}
}
if ((itr = object.find("divisibleBy")) != object.end()) {
if (version == kDraft3) {
schema.addConstraint(makeMultipleOfConstraint(itr->second));
@ -290,7 +299,12 @@ private:
}
if ((itr = object.find("title")) != object.end()) {
if (itr->second.maybeString()) {
schema.setTitle(itr->second.asString());
} else {
throw std::runtime_error(
"'title' attribute should have a string value");
}
}
if ((itr = object.find("type")) != object.end()) {