Added strip, removed dangerous datablock::is_valid (non-const version)

This commit is contained in:
Maarten L. Hekkelman
2025-06-02 08:52:58 +02:00
parent 3bdcf21c69
commit bcf33df701
4 changed files with 49 additions and 31 deletions

View File

@@ -223,6 +223,11 @@ class category
/// @return Returns true is all validations pass
bool validate_links() const;
/**
* @brief Strip removes items from this category that are invalid according to the assigned validator
*/
void strip();
/// @brief Equality operator, returns true if @a rhs is equal to this
/// @param rhs The object to compare with
/// @return True if the data contained is equal

View File

@@ -128,15 +128,6 @@ class datablock : public std::list<category>
*/
bool is_valid() const;
/**
* @brief Validates the content of this datablock and all its content
* and updates or removes the audit_conform category to match the result.
*
* @return true If the content is valid
* @return false If the content is not valid
*/
bool is_valid();
/**
* @brief Validates all contained data for valid links between parents and children
* as defined in the validator
@@ -146,6 +137,14 @@ class datablock : public std::list<category>
*/
bool validate_links() const;
/**
* @brief Strip removes all categories and items that are invalid according
* to the assigned validator. Will also add a valid audit_conform block.
*
* @return true if the remaining datablock is valid
*/
bool strip();
// --------------------------------------------------------------------
/**

View File

@@ -914,6 +914,20 @@ bool category::validate_links() const
return result;
}
void category::strip()
{
std::vector<std::string> to_be_removed;
for (auto &item : m_items)
{
if (item.m_validator == nullptr)
to_be_removed.push_back(item.m_name);
}
for (auto item : to_be_removed)
remove_item(item);
}
// --------------------------------------------------------------------
row_handle category::operator[](const key_type &key)

View File

@@ -78,17 +78,32 @@ bool datablock::is_valid() const
return result;
}
bool datablock::is_valid()
bool datablock::validate_links() const
{
if (m_validator == nullptr)
throw std::runtime_error("Validator not specified for datablock data_" + name());
bool result = true;
for (auto &cat : *this)
result = cat.is_valid() and result;
const_cast<category &>(cat).update_links(*this);
for (auto &cat : *this)
result = cat.validate_links() and result;
return result;
}
bool datablock::strip()
{
bool result = true;
// remove all categories that have no validator
erase(std::remove_if(begin(), end(), [](category &c) { return c.get_validator() == nullptr; }), end());
// then strip the remaining categories
for (auto &cat : *this)
cat.strip();
// Add or remove the audit_conform block here.
if (result)
if (is_valid())
{
// If the dictionary declares an audit_conform category, put it in,
// but only if it does not exist already!
@@ -101,22 +116,7 @@ bool datablock::is_valid()
}
}
else
erase(std::find_if(begin(), end(), [](category &cat)
{ return cat.name() == "audit_conform"; }),
end());
return result;
}
bool datablock::validate_links() const
{
bool result = true;
for (auto &cat : *this)
const_cast<category &>(cat).update_links(*this);
for (auto &cat : *this)
result = cat.validate_links() and result;
result = false;
return result;
}