Validating Your Existence

A presentation at RubyMotion Inspect in July 2015 in Paris, France by Gant Laborde

Slide 1

Slide 1

Your Existence Your Existence

Slide 2

Slide 2

Proofreeded buy Proofreeded buy Jamon Holmgren Jamon Holmgren

Slide 3

Slide 3

Who am I? Who am I?

Slide 4

Slide 4

@GantLaborde @GantLaborde Public Spe ak er Writ er Coder

Slide 5

Slide 5

Slide 6

Slide 6

Title Text Title Text

Slide 7

Slide 7

Slide 8

Slide 8

Trusting User Input Trusting User Input

Slide 9

Slide 9

Crazy or Lazy? Crazy or Lazy?

Slide 10

Slide 10

Start with ideal API Start with ideal API

Slide 11

Slide 11

Have smart people Have smart people help! help!

Slide 12

Slide 12

Ideal Validation? Ideal Validation?

We can validate information

valid?(

98.6 , : number )

#=> true

Get user input T HEN v alidat e it in some place.

Slide 13

Slide 13

Stop them at the Stop them at the Source Source

Slide 14

Slide 14

Slide 15

Slide 15

Ideal Validation? Ideal Validation? t ak e 2

We can validate user input at the source

@username_textfield.validates(

:number ).on( :invalid ) do

Alert the user!

end

Slide 16

Slide 16

Slide 17

Slide 17

Slide 18

Slide 18

Validation as a Utility Validation as a Utility 1/5

H a n d l e

c o m m o n

v a l i d a t i o n

n e e d s

r m q . v a l i d a t i o n . v a l i d ? ( ' t e s t @ t e s t . c o m ' ,

: e m a i l )

r e t u r n s

t r u e

r m q . v a l i d a t i o n . v a l i d ? ( ' 5 0 4 5 5 5 8 9 8 9 ' ,

: u s p h o n e )

r e t u r n s

t r u e

r m q . v a l i d a t i o n . v a l i d ? ( ' 9 0 2 1 0 ' ,

: u s z i p )

r e t u r n s

f a l s e

Slide 19

Slide 19

Validation as a Utility Validation as a Utility 2/5   :email   :url   :dat eiso   :number :digits   :ip v4 :time     :uszip :ukzip :usphone   :str ong_p assw or d :has_upper   :has_l o w er   :pr esenc e :l ength  

Slide 20

Slide 20

Validation as a Utility Validation as a Utility 3/5 # Make

it

Flexable

rmq .validation .valid ?( 'test' , :length, exact_length : 4 ) #true

rmq .validation .valid ?( 'test' , :length, min_length : 4 ) #true

rmq .validation .valid ?( 'test' , :length, max_length : 4 ) #true

rmq .validation .valid ?( 'test' , :length, min_length : 2 , max_length : 7 ) #true

rmq .validation .valid ?( 'test' , :length, exact_length : 2 .. 7 ) #true

rmq .validation .valid ?( ' test ' , :length, max_length : 5 , strip : true) #true

Slide 21

Slide 21

Validation as a Utility Validation as a Utility 4/5

R o l l

y o u r

o w n !

r m q . v a l i d a t i o n . v a l i d ? ( ' n a c h o ' ,

: c u s t o m ,

r e g e x :

R e g e x p . n e w ( ' n a c h o s ? ' ) )

t r u e

Slide 22

Slide 22

Things that validation has to think about? Things that validation has to think about? allow_blank white_list Validation as a Utility Validation as a Utility 5/5

Slide 23

Slide 23

Friday Friday

Slide 24

Slide 24

Validation as a Rule Validation as a Rule Built on the Utility

Slide 25

Slide 25

Validation as a Rule Validation as a Rule  

A d d

a

r u l e

t o

a

t e x t f i e l d

r m q ( @ u s e r _ e m a i l ) . v a l i d a t e s ( : e m a i l )

A d d

r u l e

a s s

w e

a p p e n d

f i e l d s

r m q . a p p e n d ( U I T e x t F i e l d ,

: w e i g h t ) . v a l i d a t e s ( : d i g i t s )

r m q . a p p e n d ( U I T e x t F i e l d ,

: n a m e ) . v a l i d a t e s ( : p r e s e n c e )

C h e c k

t h a t

i n p u t

r m q ( @ u s e r _ e m a i l ) . v a l i d ?

C h e c k

e v e r y

a p p l i e d

r u l e

r m q . a l l . v a l i d ?

Slide 26

Slide 26

Who's Bad? Who's Bad?

Useless at first

rmq(some_selection_criteria).invalid 

rmq(some_selection_criteria).valid

moment of judgement

rmq.all.valid?

All invalid views as an array

rmq.all.invalid 

All valid views as an array

rmq.all.valid

Slide 27

Slide 27

Who's Bad? Who's Bad?

I react depending on my own state

rmq.append(UITextField).validates( :digits ).on( :valid ) do

#valid

puts "WOWZERZ field is valid <3 <3 <3"

end .on( :invalid ) do

|invalid|

#invalid

puts "NOOOOOOOOOOOOO!!!1!1"

end

Slide 28

Slide 28

Easy Come - Easy Go Easy Come - Easy Go

Bye bye Guy

rmq(some_selection).clear_validations!

Later alligator

rmq.all.clear_validations!

Slide 29

Slide 29

Informative Errors Informative Errors

Slide 30

Slide 30

We can be friendly We can be friendly validation_errors

R e t u r n s

a r r a y

o f

e r r o r

m e s s a g e s

f o r

e v e r y

i n v a l i d

i t e m

i n

s e l e c t i o n

r m q . a l l . v a l i d a t i o n _ e r r o r s

E . G

[ " V a l i d a t i o n

E r r o r

i n p u t

r e q u i r e s

v a l i d

e m a i l . " ]

Slide 31

Slide 31

We can be friendly We can be friendly custom error messages custom error messages

d o

t h i s

i n

y o u r

s t y l e s h e e t

d e f

u s e r ( s t )

c u s t o m

v a l i d a t i o n

i m a g e s

s t . v a l i d a t i o n _ e r r o r s

=

{

e m a i l :

" P l e a s e

c h e c k

y o u r

u s e r

e m a i l

a n d

t r y

a g a i n "

}

e n d

o v e r r i d e s

" V a l i d a t i o n

E r r o r

i n p u t

r e q u i r e s

v a l i d

e m a i l . "

Slide 32

Slide 32

Custom Validations Custom Validations with Selection Rules!?! with Selection Rules!?! Custom Validations Custom Validations with Selection Rules!?! with Selection Rules!?!

Slide 33

Slide 33

Custom Validation Custom Validation Rules Rules   the e asy w ay - simple r egex

a p p e n d ( U I T e x t F i e l d ) . v a l i d a t e s ( : c u s t o m ,

r e g e x :

/ ^ t e s t $ / )

Slide 34

Slide 34

Custom Validation Custom Validation Rules Rules

r m q . v a l i d a t i o n . a d d _ v a l i d a t o r ( : f i r s t _ l e t t e r )

d o

| v a l u e ,

o p t s |

v a l u e . s t a r t _ w i t h ? ( o p t s [ : l e t t e r ] )

e n d No w use it!

s o m e _ f i e l d

=

a p p e n d ( U I T e x t F i e l d ) . v a l i d a t e s ( : f i r s t _ l e t t e r ,

l e t t e r :

' x ' )

s o m e _ f i e l d . d a t a ( " t e s t " )

s o m e _ f i e l d . v a l i d ?

=

f a l s e

s o m e _ f i e l d . d a t a ( " x e n o p h o b i a " )

s o m e _ f i e l d . v a l i d ?

=

t r u e

Slide 35

Slide 35

Mark Rick ert (aka the "Bad Bo y of Rub yMotion" )

Slide 36

Slide 36

Hangin' with BroBama Hangin' with BroBama

Slide 37

Slide 37

The Future of The Future of Validation Validation

Slide 38

Slide 38

Thanks! Thanks! Thanks! Thanks!