realm-swift icon indicating copy to clipboard operation
realm-swift copied to clipboard

Support property validation rules

Open cfeckardt opened this issue 10 years ago • 13 comments

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.

cfeckardt avatar Apr 20 '15 14:04 cfeckardt

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

alazier avatar Apr 20 '15 15:04 alazier

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

  • Check constraints (for example: property > 5)
  • Null constraints (once support for null values are implemented, #628 )
  • Foreign key constraints (cascading updates and delete)
  • Temporarily disable constraints during a transaction (for batch updates)

cfeckardt avatar Apr 20 '15 15:04 cfeckardt

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

jpsim avatar May 01 '15 18:05 jpsim

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

segiddins avatar May 01 '15 18:05 segiddins

+1 for uniqueness constraint

Not that other constraints won't be useful :)

kunalsood avatar Jul 02 '15 11:07 kunalsood

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

drewcrawford avatar Aug 03 '15 08:08 drewcrawford

+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 avatar Dec 22 '16 20:12 AnthonyMDev

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

AnthonyMDev avatar Dec 22 '16 20:12 AnthonyMDev

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 avatar Oct 13 '18 01:10 Cassers

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.

Cassers avatar Feb 28 '19 02:02 Cassers

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.

AnthonyMDev avatar Feb 28 '19 14:02 AnthonyMDev

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

Cassers avatar Feb 28 '19 20:02 Cassers

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

daviddahl avatar Dec 14 '23 05:12 daviddahl