Giter Club home page Giter Club logo

objcmodelvalidation's Introduction

Objective-C Model Validator

Build Status Coverage Status Cocoapods Version CocoaPods Platform

Objective-C Model Validator is a collection of classes and categories to add easy model validation to your application.

Idea

This project is inspired from the famous ActiveRecord Ruby framework.

Validation in Objective-C requires some effort to do and I needed it hardly. Knowing ActiveRecord made me do this.

How to use

Import using cocoapods

pod 'ObjcModelValidation', '~> 0.2'

Create your model

#import "ValidationModel.h"

@interface SampleModel : ValidationModel

@property (nonatomic, strong) NSString *nameString;
@property (nonatomic, strong) NSNumber *ageNumber;

@end

Note that it inherits from ValidationModel.

All properties used in this model are used to validate depending on their class.

#import "SampleModel.h"

@implementation SampleModel

-(NSError *)validateAgeNumber:(NSNumber *)ageNumber{
    if ([ageNumber greaterThan:@18] && [ageNumber presence]) {
        return nil;
    }
    
    return [self errorWithLocalizedMessage:NSLocalizedString(@"You should be older than 18.", @"")];
}
    
-(NSError *)validateNameString:(NSString *)nameString{
    if ([nameString lengthMin:4]) {
        return nil;
    }

    return [self errorWithLocalizedMessage:NSLocalizedString(@"Your name should be at least 4 characters long", @"")];
}

@end

Note how the syntax of each validation method is: -(NSError *)validate<CapitalizedVariableName>:(<VariableClass> *)<variableName>; This only works for objects at the moment. So if you store NSInteger or BOOL anywhere use NSNumber instead.

Implement a save method

To start validating your models create them and save. Each model has its own save method, which returns a NSArray containing NSError objects.

Example:

SampleModel *sample = [[SampleModel alloc] init];
sample.nameString = @"Piet";
sample.ageNumber = @22;
    
NSArray *errors = [sample save];
if (errors.count != 0) {
        
    PBLog(@"I got some errors here.");
        
    for (NSError *error in errors) {
            
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:error.localizedDescription delegate:nil cancelButtonTitle:@"Hide" otherButtonTitles:nil];
        [alertView show];
            
    }
}

Note when overriding save you have to call [super save];. Otherwise validations are not called.

If everything fits you will see a log in the console.

Use validators

NSObject validators

Checks for value to exist or not.

This method should be overriden by other validation classes to get specific presence validation for a class.

-(BOOL)presence;

Checks for the speicified method and executes custom validation.

-(BOOL)validatesWithSelector:(SEL)selector;

NSNumber validators

Checks if the referenceNumber is greater than self

-(BOOL)greaterThan:(NSNumber *)referenceNumber;

Checks if the referenceNumber is smaller than self

-(BOOL)lessThan:(NSNumber *)referenceNumber;

NSString validators

Takes an array of strings and searches for it in self.

Returns YES if strings where not found in string.

-(BOOL)exclusion:(NSArray *)exclusionArray;

Takes an regex string and validates it.

-(BOOL)format:(NSString *)regexExpression;

Takes an array of strings and searches for it in self.

Returns YES if strings was found in string.

-(BOOL)inclusion:(NSArray *)inclusionArray;

Validates with minimum length

-(BOOL)lengthMin:(NSInteger)length;

Validates with maximum length

-(BOOL)lengthMax:(NSInteger)length;

Validates with range

-(BOOL)lengthIn:(NSInteger)lengthMin and:(NSInteger)lengthMax;

Validates with exact range

-(BOOL)lengthIs:(NSInteger)length;

Validates with if the string consists only of numbers

-(BOOL)numericality;

Contributers highly welcome

Fork the Repository, write a test and send a pull request.

Contact

nerdish by nature

@piet_nbn

objcmodelvalidation's People

Contributors

pietbrauer avatar

Watchers

James Cloos avatar Ryota Tomomura avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.