validation for apis in laravel 4

22
Validation for APIs Good validation practises. API focused. Kirk Bushell

Upload: kirk-bushell

Post on 19-Aug-2014

411 views

Category:

Engineering


1 download

DESCRIPTION

In my first talk, I raise the concerns I have with current validation practises and what we can do to make it better.

TRANSCRIPT

Page 1: Validation for APIs in Laravel 4

Validation for APIsGood validation practises. API focused.

Kirk Bushell

Page 2: Validation for APIs in Laravel 4

Who the hell am I?

• Technical Lead for Tectonic Digital (www.tectonic.com.au)

• Software architect for http://awardforce.com

• Technical writer (see: http://kirkbushell.me)

• Open source contributor (Laravel 3/4, various other projects)

• http://github.com/kirkbushell

• Also known as Oddman on IRC, forums.etc.

• @kirkbushell

Page 3: Validation for APIs in Laravel 4

Disclaimers

Page 4: Validation for APIs in Laravel 4

This is my first talk.

Page 5: Validation for APIs in Laravel 4

Sorry.

Page 6: Validation for APIs in Laravel 4

In addition...

• I'll assume you know a couple of things about validation and

• You know or at least understand some of the concepts of Laravel 4, including:

• Dependency injection and

• The IoC feature

• I start work at 4.30am (apologies if I'm not quite with it)

Page 7: Validation for APIs in Laravel 4

Why?

• There's been a lot of talk in the community the last 6 months about validation

• How we can do it well - lots of different opinions

• I have a pretty strong opinion on this matter

Page 8: Validation for APIs in Laravel 4

What we're going to cover

• A brief history of MVC, the repository pattern and why validation should be its own domain

• Implementing validation within an API context

• Using exceptions for greater readability

• Catching exceptions to generate API responses automagically

• The end result - cleaner code

Page 9: Validation for APIs in Laravel 4

What I'm not going to talk about

• RESTful APIs

• The view layer

Page 10: Validation for APIs in Laravel 4

A brief history of everything MVC.

• A blessing to web development (and software development in general)

• Fat controllers, skinny models

• Skinny controllers, fat models

• Validation tied up in models (along with everything else)

• We still seem to be stuck on the previous point.

Page 11: Validation for APIs in Laravel 4

Introducing.. the repository pattern?

• Helped clean up models

• Query building and object management

• So, what about validation?

Page 12: Validation for APIs in Laravel 4

Validate all the things!

• Validation within models breaks the single responsibility principle

• Validation in models doesn't make much sense if you're using the repository pattern

• Implemented via the controller or a service (such as a user registration service)

Page 13: Validation for APIs in Laravel 4

Implementing validation

Page 14: Validation for APIs in Laravel 4

The validate method

Please read Jason Lewis' article: http://jasonlewis.me/article/laravel-advanced-validation

// Validate function from articleprotected function validate(){ $this->validator = Validator::make($this->input, $this->rules);

if ($this->validator->fails()) { throw new ValidateException($this->validator); }}

Page 15: Validation for APIs in Laravel 4

ValidateException

class ValidateException extends Exception{ protected $validator;

public function __construct(Validator $validator) { $this->message = 'Validation has failed, or something.'; $this->validator = $validator; }

public function getErrors() { return $this->validator->messages(); }}

Page 16: Validation for APIs in Laravel 4

Our code, our rules

Implement our own custom rules for user registration:

// UserValidator classclass UserValidator extends Validator{ public function register() { $this->rules = [ 'email' => ['required', 'email'], 'password' => ['required'] ];

$this->validate(); }}

Page 17: Validation for APIs in Laravel 4

Validate!

Within our controller, load up our validator and validate the input.

// UserControllerpublic function postRegister(){ $input = Input::get();

App::make('UserValidator', [$input])->register();

return User::create($input);}

Page 18: Validation for APIs in Laravel 4

Now what?

• When validation fails it will throw an exception:

if ($this->validator->fails()) { throw new ValidateException($this->validator); }

• We need to catch that exception and return a nice status code and error message, along with any validation errors.

• Laravel 4 provides an excellent way of managing this.

Page 19: Validation for APIs in Laravel 4

Laravel 4 error handling

Create an error handler that is specific to validation exceptions:

App::error(function(ValidateException $exception){ $errorResponse = [ 'message' => $exception->getMessage(), 'errors' => $exception->getErrors() ];

return Response::json($errorResponse, 422); // Unprocessable entity});

Page 20: Validation for APIs in Laravel 4

In conclusion

• Complex validation should be in its own domain

• Helps to clean up our code, making it more readable

• Let the framework handle exceptions for you!

• Best for large applications (not so applicable to small apps)

Page 21: Validation for APIs in Laravel 4

Thank you.

Page 22: Validation for APIs in Laravel 4

You can catch me at the following online locales:

• http://kirkbushell.me

• http://github.com/kirkbushell

• @kirkbushell