views:

265

answers:

3

I wonder if already exist a generic library of validator suitable for a iPhone project (or better, bussines objects) for obj-c.

Things like:

Validator.IsNotEmpty..
Validator.IsValidEmail..
Validator.IsValidZipCode..
Validator.IsValidRegex..
Validator.IsInRange..
A: 

I haven't heard of anything. If it's something you want I would use one of the regex libraries available for Cocoa Touch, you should be able to find regex examples of those tests pretty easily.

Marc Charbonneau
+3  A: 

There is a standard validation pattern in Cocoa, including Cocoa Touch, called Key-Value Validation that you should conform to if you want to validate user input other than through an NSFormatter.

In essence, for a property name that you want validated, you implement a method following this pattern:

- (BOOL)validateName:(id *)ioValue error:(NSError **)error;

Then, when you want to validate a property before setting it from your UI, you just use:

NSString *proposedName = [[nameField.stringValue copy] autorelease];
NSError *nameError;

if ([person validateValue:&proposedName forKey:@"name" error:&nameError]) {
    person.name = proposedName;
} else {
    // present nameError to the user in a reasonable way
}

Note that you do not invoke -validateValue:forKey:error: or -validateName:error: method in your -setName: method. Instead, you invoke your -setName: method (as I do via dot syntax above) once you know the value you're passing for it will be valid.

Also, read the document on Key-Value Validation that I mentioned above to understand why proposedName is an autoreleased copy of a field's string value, instead of just its string value.

If you're implementing something more complicated than per-property validation, such as whole-object validation or even object graph validation, be sure to see how the Core Data framework on Mac OS X handles it.

Chris Hanson
A: 

Ok, I start to build it then.

My first take on chibi-ORM

mamcx
This should be a comment on an answer, not an answer itself.Also, the code at the link contains class methods, not instance methods, which are probably what you actually want.
Chris Hanson