add more object_schema validation

This commit is contained in:
Ari Lazier 2015-07-28 11:45:19 -07:00
parent 373375fa1b
commit 136f9a4640
2 changed files with 87 additions and 45 deletions

View File

@ -164,6 +164,7 @@ std::vector<ObjectSchemaValidationException> ObjectStore::verify_object_schema(G
ObjectSchema table_schema(group, target_schema.name);
// check to see if properties are the same
Property *primary = nullptr;
for (auto& current_prop : table_schema.properties) {
auto target_prop = target_schema.property_for_name(current_prop.name);
@ -175,9 +176,38 @@ std::vector<ObjectSchemaValidationException> ObjectStore::verify_object_schema(G
exceptions.emplace_back(MismatchedPropertiesException(table_schema.name, current_prop, *target_prop));
continue;
}
// check object_type existence
if (current_prop.object_type.length() && schema.find(current_prop.object_type) == schema.end()) {
exceptions.emplace_back(InvalidPropertyException(table_schema.name, current_prop,
"Target type '" + current_prop.object_type + "' doesn't exist for property '" + current_prop.name + "',"));
"Target type '" + current_prop.object_type + "' doesn't exist for property '" + current_prop.name + "'."));
}
// check nullablity
if (current_prop.is_nullable) {
if (current_prop.type != PropertyTypeObject) {
exceptions.emplace_back(InvalidPropertyException(table_schema.name, current_prop,
"Only 'Object' property types are nullable"));
}
}
else if (current_prop.type == PropertyTypeObject) {
exceptions.emplace_back(InvalidPropertyException(table_schema.name, current_prop,
"'Object' property '" + current_prop.name + "' must be nullable."));
}
// check primary keys
if (current_prop.is_primary) {
if (primary) {
exceptions.emplace_back(InvalidPropertyException(table_schema.name, current_prop, "Duplicate primary keys."));
}
primary = &current_prop;
}
// check indexable
if (current_prop.is_indexed) {
if (current_prop.type != PropertyTypeString && current_prop.type != PropertyTypeInt) {
exceptions.emplace_back(PropertyTypeNotIndexableException(table_schema.name, current_prop));
}
}
// create new property with aligned column
@ -447,19 +477,19 @@ SchemaValidationException::SchemaValidationException(std::vector<ObjectSchemaVal
}
PropertyTypeNotIndexableException::PropertyTypeNotIndexableException(std::string object_type, Property &property) :
ObjectSchemaValidationException(object_type), m_property(property)
ObjectSchemaPropertyException(object_type, property)
{
m_what = "Can't index property " + object_type + "." + property.name + ": indexing a property of type '" + string_for_property_type(property.type) + "' is currently not supported";
}
ExtraPropertyException::ExtraPropertyException(std::string object_type, Property &property) :
ObjectSchemaValidationException(object_type), m_property(property)
ObjectSchemaPropertyException(object_type, property)
{
m_what = "Property '" + property.name + "' has been added to latest object model.";
}
MissingPropertyException::MissingPropertyException(std::string object_type, Property &property) :
ObjectSchemaValidationException(object_type), m_property(property)
ObjectSchemaPropertyException(object_type, property)
{
m_what = "Property '" + property.name + "' is missing from latest object model.";
}
@ -490,7 +520,7 @@ ChangedPrimaryKeyException::ChangedPrimaryKeyException(std::string object_type,
}
InvalidPrimaryKeyException::InvalidPrimaryKeyException(std::string object_type, std::string primary) :
ObjectSchemaValidationException(object_type), m_primary(primary)
ObjectSchemaValidationException(object_type), m_primary_key(primary)
{
m_what = "Specified primary key property '" + primary + "' does not exist.";
}

View File

@ -33,7 +33,7 @@ namespace realm {
};
class ObjectStore {
public:
public:
// Schema version used for uninitialized Realms
static const uint64_t NotVersioned;
@ -73,7 +73,7 @@ namespace realm {
// deletes the table for the given type
static void delete_data_for_object(Group *group, const StringData &object_type);
private:
private:
// set a new schema version
static void set_schema_version(Group *group, uint64_t version);
@ -119,11 +119,11 @@ namespace realm {
// Base exception
class ObjectStoreException : public std::exception {
public:
public:
ObjectStoreException() = default;
ObjectStoreException(const std::string &what) : m_what(what) {}
virtual const char* what() const noexcept { return m_what.c_str(); }
protected:
protected:
std::string m_what;
};
@ -131,85 +131,97 @@ namespace realm {
class MigrationException : public ObjectStoreException {};
class InvalidSchemaVersionException : public MigrationException {
public:
public:
InvalidSchemaVersionException(uint64_t old_version, uint64_t new_version);
private:
uint64_t old_version() { return m_old_version; }
uint64_t new_version() { return m_new_version; }
private:
uint64_t m_old_version, m_new_version;
};
class DuplicatePrimaryKeyValueException : public MigrationException {
public:
public:
DuplicatePrimaryKeyValueException(std::string object_type, Property &property);
private:
std::string object_type() { return m_object_type; }
Property &property() { return m_property; }
private:
std::string m_object_type;
Property m_property;
};
// Schema validation exceptions
class SchemaValidationException : public ObjectStoreException {
public:
public:
SchemaValidationException(std::vector<ObjectSchemaValidationException> errors);
private:
std::vector<ObjectSchemaValidationException> &validation_errors() { return m_validation_errors; }
private:
std::vector<ObjectSchemaValidationException> m_validation_errors;
};
class ObjectSchemaValidationException : public ObjectStoreException {
public:
public:
ObjectSchemaValidationException(std::string object_type) : m_object_type(object_type) {}
ObjectSchemaValidationException(std::string object_type, std::string message) :
m_object_type(object_type) { m_what = message; }
protected:
std::string object_type() { return m_object_type; }
protected:
std::string m_object_type;
};
class PropertyTypeNotIndexableException : public ObjectSchemaValidationException {
public:
class ObjectSchemaPropertyException : public ObjectSchemaValidationException {
public:
ObjectSchemaPropertyException(std::string object_type, Property &property) :
ObjectSchemaValidationException(object_type), m_property(property) {}
Property &property() { return m_property; }
private:
Property m_property;
};
class PropertyTypeNotIndexableException : public ObjectSchemaPropertyException {
public:
PropertyTypeNotIndexableException(std::string object_type, Property &property);
private:
Property m_property;
};
class ExtraPropertyException : public ObjectSchemaValidationException {
public:
class ExtraPropertyException : public ObjectSchemaPropertyException {
public:
ExtraPropertyException(std::string object_type, Property &property);
private:
Property m_property;
};
class MissingPropertyException : public ObjectSchemaValidationException {
public:
class MissingPropertyException : public ObjectSchemaPropertyException {
public:
MissingPropertyException(std::string object_type, Property &property);
private:
Property m_property;
};
class InvalidPropertyException : public ObjectSchemaPropertyException {
public:
InvalidPropertyException(std::string object_type, Property &property, std::string message) :
ObjectSchemaPropertyException(object_type, property) { m_what = message; }
};
class MismatchedPropertiesException : public ObjectSchemaValidationException {
public:
public:
MismatchedPropertiesException(std::string object_type, Property &old_property, Property &new_property);
private:
Property &old_property() { return m_old_property; }
Property &new_property() { return m_new_property; }
private:
Property m_old_property, m_new_property;
};
class ChangedPrimaryKeyException : public ObjectSchemaValidationException {
public:
public:
ChangedPrimaryKeyException(std::string object_type, std::string old_primary, std::string new_primary);
private:
std::string old_primary() { return m_old_primary; }
std::string new_primary() { return m_new_primary; }
private:
std::string m_old_primary, m_new_primary;
};
class InvalidPrimaryKeyException : public ObjectSchemaValidationException {
public:
InvalidPrimaryKeyException(std::string object_type, std::string primary);
private:
std::string m_primary;
};
class InvalidPropertyException : public ObjectSchemaValidationException {
public:
InvalidPropertyException(std::string object_type, Property &property, std::string message) :
ObjectSchemaValidationException(object_type), m_property(property) { m_what = message; }
private:
Property m_property;
public:
InvalidPrimaryKeyException(std::string object_type, std::string primary_key);
std::string primary_key() { return m_primary_key; }
private:
std::string m_primary_key;
};
}