DATA::VERIFIER
                      &
MESSAGE::STACK
Invigorating The Forgotten Bits Of Your Web App
CORY G WATSON: GPHAT

•   Magazines.com: Group Director, Development

•   Cold Hard Code: Creative Director

•   Contribut...
BIASES: I HAVE THEM

•   I’m a closet designer

•   I hate All-In-One Form handlers

•   Messaging & Validation are not se...
VALIDATION: PRIMER

•   Client side is advancing.

•   It won’t replace server side.*

•   Duplication of effort.
VALIDATION:
     DATA::FORMVALIDATOR

•   8+ years old

•   Big, Complex
DATA::VERIFIER
D:V – FEATURES
•   Reusable constraints and coercion

•   Required, optional & dependents

•   Length checking

•   Global...
MOOSE
WHY MOOSE?
•   Simplicity

•   Type Constraints

•   Coercion

•   Serialization
GETTING STARTED
my $dv = Data::Verifier->new(…);
my $results = $dv->verify(%params);
if($results->success) {
    # o/
} el...
PROFILE
              profile => %fields
•   Simple HashRef

•   A key for each field

•   Opposite of D:FV, fields before r...
FIELD KEYS
•   coerce            •   min_length

•   coercion          •   post_check

•   dependent         •   required
...
EASY ONES
•   required: Optional? Then it’s not required!

•   max_length => $x

•   min_length => $y

•   type => $moose_...
EXAMPLE
filters => [ qw(trim) ],
profile => {
  email => {
    min_length => 5
    type => EmailAddress,
    required => 1...
COERCE

•   Same as Moose’s coerce flag

•   true tells D:V to use Moose coercions

•   Occludes coercion
COERCION

•   Allows one-off coercions

•   Does not define a global Moose coercion

•   Saves cluttering your ::Types with ...
COERCION (CONT)
coercion => Data::Verifier::coercion(
  from => ‘Int’,
  via => sub {
    DateTime->from_epoch(epoch => $_...
DEPENDENT


•   Field A depends on Field B

•   More? Field A can depend on an entire profile.

•   Creates a new D:V, veri...
DEPENDENT
password => {
  dependent => {
    password2 => {
      required => 1
    }
  }
}
FILTERS
              filters => [ qw(trim) ]



•   Comes with some built-ins.

•   Also allows CodeRefs

•   Global and ...
POST_CHECK


•   Checked at end of verification.

•   Accepts a CodeRef

•   Given Results object as argument.

•   $passwo...
POST_CHECK (CONT)
post_check => sub {
  my ($r) = @_;
  return $r->get_value(‘pass1’)
   eq $r->get_value(‘pass2’);
}
RESULT METHODS
•   get_original_value # unmodified

•   get_value # filters, coercions

•   is_missing, is_valid

•   More!
SERIALIZATION
•   MooseX::Storage

•   caveat: value isn’t serialized, arbitrary type

•   use get_original_value

•   val...
MESS-AGING

•   Error variable

•   Error ArrayRef

•   Error plugin?
MESSAGING

•   Context

•   Level

•   Localization
MESSAGE
•   id: Localization

•   level: ‘error’, ‘info’

•   params: [ ‘gphat@coldhardcode’ ]

•   scope: ‘login_form’

•...
MESSAGE::STACK
MESSAGE::STACK

•   List of messages

•   Order is retained

•   Convenience methods

•   Many methods return sub-Stacks
ADD A MESSAGE

$stack->add(
 id     => ‘invalid_email’,
 scope => ‘login_form’,
 subject=> ‘email’,
 level => ‘error’
);
BEING INFORMED

•   count

•   id, level, scope, subject

•   has_* (boolean)

•   for_* (subset Message::Stack)
Error Message!
                     Error Message!




         Line Item 1             Line Item 1
       Delivery Option...
Error Message!

      Line Item 1       Line Item 1
    Delivery Options    Gift Options




                       Error ...
METHODS

•   Errors: for_level(‘error’);

•   Sub-Form: for_scope(‘line-item1’);

•   Specific field: has_subject(‘email’);
...
HINTS

•   Templates can get messy.

•   Works well with macros.

•   Use a lot or a little.

•   foreach($stack->messages)
DATA::VERIFIER
      +
MESSAGE::STACK
DATA::MANAGER
DATA::MANAGER
my $dm = Data::Manager->new;

my $v = Data::Verifier->new...;

$dm->set_verifier(‘foo’, $v);

$dm->verify(‘f...
DATA::MANAGER (CONT)

•   A single Message::Stack

•   Multiple Data::Verifier objects (scopes)

•   success

•   messages_...
SUMMARY
•   Simplified validation

•   Reusable types

•   Serialization for redirects.

•   Messaging is important.

•   C...
QUESTIONS?
COLOPHON
•   Titling: Tungsten 72pt, Hoefler & Frere-Jones

•   Body: Hoefler Text 38pt, Hoefler & Frere-Jones

•   Diagrams:...
Upcoming SlideShare
Loading in …5
×

Data::Verifier and Message::Stack

1,059 views

Published on

Revitalizing the forgotten bits of your web application. Given at Perl Oasis 2010 in Orlando, Florida on January 16th.

Published in: Technology
1 Comment
1 Like
Statistics
Notes
  • awesome
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
1,059
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide
  • Data::Verifier and Message::Stack

    1. 1. DATA::VERIFIER & MESSAGE::STACK Invigorating The Forgotten Bits Of Your Web App
    2. 2. CORY G WATSON: GPHAT • Magazines.com: Group Director, Development • Cold Hard Code: Creative Director • Contributor and Color Commentator on various projects.
    3. 3. BIASES: I HAVE THEM • I’m a closet designer • I hate All-In-One Form handlers • Messaging & Validation are not sexy
    4. 4. VALIDATION: PRIMER • Client side is advancing. • It won’t replace server side.* • Duplication of effort.
    5. 5. VALIDATION: DATA::FORMVALIDATOR • 8+ years old • Big, Complex
    6. 6. DATA::VERIFIER
    7. 7. D:V – FEATURES • Reusable constraints and coercion • Required, optional & dependents • Length checking • Global and per-field filters
    8. 8. MOOSE
    9. 9. WHY MOOSE? • Simplicity • Type Constraints • Coercion • Serialization
    10. 10. GETTING STARTED my $dv = Data::Verifier->new(…); my $results = $dv->verify(%params); if($results->success) { # o/ } else { # :( }
    11. 11. PROFILE profile => %fields • Simple HashRef • A key for each field • Opposite of D:FV, fields before required/optional
    12. 12. FIELD KEYS • coerce • min_length • coercion • post_check • dependent • required • filters • type • max_length
    13. 13. EASY ONES • required: Optional? Then it’s not required! • max_length => $x • min_length => $y • type => $moose_type
    14. 14. EXAMPLE filters => [ qw(trim) ], profile => { email => { min_length => 5 type => EmailAddress, required => 1 } }
    15. 15. COERCE • Same as Moose’s coerce flag • true tells D:V to use Moose coercions • Occludes coercion
    16. 16. COERCION • Allows one-off coercions • Does not define a global Moose coercion • Saves cluttering your ::Types with coercions
    17. 17. COERCION (CONT) coercion => Data::Verifier::coercion( from => ‘Int’, via => sub { DateTime->from_epoch(epoch => $_) } )
    18. 18. DEPENDENT • Field A depends on Field B • More? Field A can depend on an entire profile. • Creates a new D:V, verifies it, merges it with this one.
    19. 19. DEPENDENT password => { dependent => { password2 => { required => 1 } } }
    20. 20. FILTERS filters => [ qw(trim) ] • Comes with some built-ins. • Also allows CodeRefs • Global and per-field
    21. 21. POST_CHECK • Checked at end of verification. • Accepts a CodeRef • Given Results object as argument. • $password1 eq $password2 scenarios
    22. 22. POST_CHECK (CONT) post_check => sub { my ($r) = @_; return $r->get_value(‘pass1’) eq $r->get_value(‘pass2’); }
    23. 23. RESULT METHODS • get_original_value # unmodified • get_value # filters, coercions • is_missing, is_valid • More!
    24. 24. SERIALIZATION • MooseX::Storage • caveat: value isn’t serialized, arbitrary type • use get_original_value • validity and missing is retained
    25. 25. MESS-AGING • Error variable • Error ArrayRef • Error plugin?
    26. 26. MESSAGING • Context • Level • Localization
    27. 27. MESSAGE • id: Localization • level: ‘error’, ‘info’ • params: [ ‘gphat@coldhardcode’ ] • scope: ‘login_form’ • subject: ‘email’ • text: ‘Invalid email address.’
    28. 28. MESSAGE::STACK
    29. 29. MESSAGE::STACK • List of messages • Order is retained • Convenience methods • Many methods return sub-Stacks
    30. 30. ADD A MESSAGE $stack->add( id => ‘invalid_email’, scope => ‘login_form’, subject=> ‘email’, level => ‘error’ );
    31. 31. BEING INFORMED • count • id, level, scope, subject • has_* (boolean) • for_* (subset Message::Stack)
    32. 32. Error Message! Error Message! Line Item 1 Line Item 1 Delivery Options Gift Options Line Item 2 Line Item 2 Delivery Options Gift Options CRAPPY FORM: WHERE ARE
    33. 33. Error Message! Line Item 1 Line Item 1 Delivery Options Gift Options Error Message! Line Item 2 Line Item 2 Delivery Options Gift Options GOOD FORM: PRECISE
    34. 34. METHODS • Errors: for_level(‘error’); • Sub-Form: for_scope(‘line-item1’); • Specific field: has_subject(‘email’); • Cat: c.loc(message.id, message.params)
    35. 35. HINTS • Templates can get messy. • Works well with macros. • Use a lot or a little. • foreach($stack->messages)
    36. 36. DATA::VERIFIER + MESSAGE::STACK
    37. 37. DATA::MANAGER
    38. 38. DATA::MANAGER my $dm = Data::Manager->new; my $v = Data::Verifier->new...; $dm->set_verifier(‘foo’, $v); $dm->verify(‘foo’, { ... }); my $res => $dm->get_results(‘foo’);
    39. 39. DATA::MANAGER (CONT) • A single Message::Stack • Multiple Data::Verifier objects (scopes) • success • messages_for_scope
    40. 40. SUMMARY • Simplified validation • Reusable types • Serialization for redirects. • Messaging is important. • Context, context, context!
    41. 41. QUESTIONS?
    42. 42. COLOPHON • Titling: Tungsten 72pt, Hoefler & Frere-Jones • Body: Hoefler Text 38pt, Hoefler & Frere-Jones • Diagrams: OmniGraffle, The Omni Group • Presentation: Keynote ’09, Apple, Inc • Theme: Modified “Showroom”

    ×