realm / realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite
https://realm.io
Apache License 2.0
16.27k stars 2.14k forks source link

Support property validation rules #1769

Open cfeckardt opened 9 years ago

cfeckardt commented 9 years ago

As a user, I would like to be able to put constraints on certain columns without making them primary keys.

For example, a uniqueness constraint on an e-mail field.

alazier commented 9 years ago

Can you think of other constraints you might want support for?

cfeckardt commented 9 years ago

Uniqueness constraints is definitely, the most critical, however the dream would be:

jpsim commented 9 years ago

Perhaps a class method like +(BOOL)[RLMObject validateValue:forProperty:] would be the simplest way to support arbitrary validation rules, although adding that method would likely have a significant performance impact, even when that method isn't overridden.

We could explicitly support specific types of constraints for improved performance, but that doesn't seem like a scalable approach.

For the time being, you can use an ignored property to validate properties before setting them:

@interface Person : RLMObject
@property NSString *name;
@property NSString *persistedName;
@end
@implementation Person
- (NSString *)name {
  return self.persistedName;
}
- (void)setName:(NSString *)name {
  if (/* custom validation rules */) {
    self.persistedName = name;
  } else {
    // handle invalid value
  }
}

+ (NSArray *)ignoredProperties {
  return @[@"name"];
}
@end
segiddins commented 9 years ago

@jpsim we could always use key-value validation if we wanted to provide arbitrary validation hooks?

kunalsood commented 9 years ago

+1 for uniqueness constraint

Not that other constraints won't be useful :)

drewcrawford commented 9 years ago

+1 for uniqueness constraint. I'm presently rolling my own.

AnthonyMDev commented 7 years ago

+1 for validation. I would really like to be able to do custom validation code.

It would be nice if there was a function similar to CoreData's validateForInsert() on Object that could be overridden for validation.

AnthonyMDev commented 7 years ago

I would be willing to make a PR to include a validateForInsert() function. Would that be something that you guys are interested in?

Cassers commented 5 years ago

Implementing unique fields, which allow the identification of a record by a field that is not the primary key in massive insertions would be very useful.

Cassers commented 5 years ago

Any news about this?, this is one of the biggest limitations of realm at this time, I have used it for 2 years and it is very necessary to create unique fields.

AnthonyMDev commented 5 years ago

I think the understanding is that most enforced validation rules would have a significant performance impact. Likely even when the validation isn't being used.

When you need to validate data, you should do your own validation prior to writing your Realm Objects to the DB.

Cassers commented 5 years ago

I have seen the suggestion of including a method that allows to choose a field as a unique key to insert records, if implemented something like that would cover a large part of the current problems. And I believe that it would not affect the structure or the performance of the rest of things

daviddahl commented 9 months ago

So is this still not implemented? No arbitrary columns (String) can be unique?