Add some missing property type validation

This commit is contained in:
Thomas Goyne 2017-09-26 14:43:44 -07:00
parent fbf5da388b
commit c9a3548f32
2 changed files with 32 additions and 21 deletions

View File

@ -73,8 +73,8 @@ typename T::Object Schema<T>::dict_for_property_array(ContextType ctx, const Obj
static inline void parse_property_type(StringData object_name, Property& prop, StringData type) static inline void parse_property_type(StringData object_name, Property& prop, StringData type)
{ {
using realm::PropertyType; using realm::PropertyType;
if (!type) { if (!type || !type.size()) {
throw std::logic_error(util::format("Property %1.%2 must have a non-empty type", object_name, prop.name)); throw std::logic_error(util::format("Property '%1.%2' must have a non-empty type", object_name, prop.name));
} }
if (type.ends_with("[]")) { if (type.ends_with("[]")) {
prop.type |= PropertyType::Array; prop.type |= PropertyType::Array;
@ -107,6 +107,12 @@ static inline void parse_property_type(StringData object_name, Property& prop, S
prop.type |= PropertyType::Data; prop.type |= PropertyType::Data;
} }
else if (type == "list") { else if (type == "list") {
if (is_nullable(prop.type)) {
throw std::logic_error(util::format("List property '%1.%2' cannot be optional", object_name, prop.name));
}
if (is_array(prop.type)) {
throw std::logic_error(util::format("List property '%1.%2' must have a non-list value type", object_name, prop.name));
}
prop.type |= PropertyType::Object | PropertyType::Array; prop.type |= PropertyType::Object | PropertyType::Array;
} }
else if (type == "linkingObjects") { else if (type == "linkingObjects") {

View File

@ -106,26 +106,21 @@ module.exports = {
TestCase.assertThrowsContaining(() => new Realm({schema: [{properties: {intCol: 'int'}}]}), TestCase.assertThrowsContaining(() => new Realm({schema: [{properties: {intCol: 'int'}}]}),
"Failed to read ObjectSchema: name must be of type 'string', got (undefined)"); "Failed to read ObjectSchema: name must be of type 'string', got (undefined)");
// linkingObjects property where the source property is missing function assertPropertyInvalid(prop, message) {
TestCase.assertThrowsContaining(() => { TestCase.assertThrowsContaining(() => {
new Realm({schema: [{ new Realm({schema: [{name: 'InvalidObject', properties: { int: 'int', bad: prop }}]});
name: 'InvalidObject', }, message, 1);
properties: { }
linkingObjects: {type:'linkingObjects', objectType: 'InvalidObject', property: 'nosuchproperty'}
}
}]});
}, "Property 'InvalidObject.nosuchproperty' declared as origin of linking objects property 'InvalidObject.linkingObjects' does not exist");
// linkingObjects property where the source property is not a link assertPropertyInvalid({type:'list[]', objectType: 'InvalidObject'},
TestCase.assertThrowsContaining(() => { "List property 'InvalidObject.bad' must have a non-list value type");
new Realm({schema: [{ assertPropertyInvalid({type:'list?', objectType: 'InvalidObject'},
name: 'InvalidObject', "List property 'InvalidObject.bad' cannot be optional");
properties: { assertPropertyInvalid('', "Property 'InvalidObject.bad' must have a non-empty type");
integer: 'int', assertPropertyInvalid({type:'linkingObjects', objectType: 'InvalidObject', property: 'nosuchproperty'},
linkingObjects: {type:'linkingObjects', objectType: 'InvalidObject', property: 'integer'} "Property 'InvalidObject.nosuchproperty' declared as origin of linking objects property 'InvalidObject.bad' does not exist");
} assertPropertyInvalid({type:'linkingObjects', objectType: 'InvalidObject', property: 'int'},
}]}); "Property 'InvalidObject.int' declared as origin of linking objects property 'InvalidObject.bad' is not a link");
}, "Property 'InvalidObject.integer' declared as origin of linking objects property 'InvalidObject.linkingObjects' is not a link")
// linkingObjects property where the source property links elsewhere // linkingObjects property where the source property links elsewhere
TestCase.assertThrowsContaining(() => { TestCase.assertThrowsContaining(() => {
@ -142,6 +137,16 @@ module.exports = {
} }
}]}); }]});
}, "Property 'InvalidObject.link' declared as origin of linking objects property 'InvalidObject.linkingObjects' links to type 'IntObject'") }, "Property 'InvalidObject.link' declared as origin of linking objects property 'InvalidObject.linkingObjects' links to type 'IntObject'")
{
new Realm({schema: [{
name: 'Object',
properties: {
// weird but valid
objectList: {type:'object[]', objectType: 'Object'}
}
}]});
}
}, },
testRealmConstructorInMemory: function() { testRealmConstructorInMemory: function() {