Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Validations in Ruby
Krzysztof Wawer
March 23, 2016
Programming
0
37
Validations in Ruby
Krzysztof Wawer
March 23, 2016
Tweet
Share
More Decks by Krzysztof Wawer
See All by Krzysztof Wawer
The Good Architecture
wafcio
0
160
GIT - Good Practices
wafcio
0
32
Slim Ruby on Rails: Controllers & Middlewares
wafcio
1
74
Dry System
wafcio
0
65
dry-validations
wafcio
0
100
Generating a CSV file with ROM
wafcio
0
110
Trailblazer bez trailblazera
wafcio
0
83
ActiveRecord, DataMapper czy Agregacja encji
wafcio
0
67
Jak (nie) używać Service Object
wafcio
0
82
Other Decks in Programming
See All in Programming
Get Ready for Jakarta EE 10
ivargrimstad
0
2k
Microsoft Teams の 会議アプリ開発のはじめかた / How to start Microsoft Teams app development
karamem0
0
1.5k
How useEvent would change our applications
koba04
1
1.4k
Blazor WebAssembly – Dynamische Formulare und Inhalte in Aktion
patrickjahr
0
150
Let's make a contract: the art of designing a Java API
mariofusco
0
160
microCMS × imgixを活用して品質とレスポンスを両立したポートフォリオサイトを作成した話
takehitogoto
0
390
Practical Advanced Kotlin in Practice
rock3r
3
130
既存のプロジェクトにKMMを導入するための対応策
martysuzuki
2
290
mrubyを1300円のボードで動かそう
yuuu
0
180
Monadic Java
mariofusco
4
250
Yumemi.apk #6 ~ゆめみのAndroidエンジニア 日頃の成果大発表会!~ Session 2
blendthink
1
200
TDX22: User-Mode DB Ops
ca_peterson
3
1.1k
Featured
See All Featured
Six Lessons from altMBA
skipperchong
14
1.3k
A designer walks into a library…
pauljervisheath
196
16k
4 Signs Your Business is Dying
shpigford
169
20k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
236
1M
KATA
mclloyd
7
8.6k
Building a Scalable Design System with Sketch
lauravandoore
447
30k
Done Done
chrislema
174
14k
Designing with Data
zakiwarfel
91
3.9k
Stop Working from a Prison Cell
hatefulcrawdad
261
17k
Ruby is Unlike a Banana
tanoku
91
9.2k
Put a Button on it: Removing Barriers to Going Fast.
kastner
56
2.3k
Code Reviewing Like a Champion
maltzj
506
37k
Transcript
V ALIDATIONS IN Ruby Krzysztof W a wer 23.03.2016 TRUG
ABOUT ME • E-MAIL: krzysztof.w a wer@gmail.com • Twitter: https://twitter.com/w
afcio • GITHUB: https://github.com/w afcio
V ALIDATION ???
Ruby on rails ACTIVE MODEL & ACTIVE RECORD Active Model
Active Record
ACTIVE MODEL V ALIDATIONS • acceptance • confirmation • exclusion
• format • inclusion • length • numericality • presence • uniqueness*
UNIQUENESS V ALIDATION tester@example.com exists ? tester@example.com exists ? Email
doesn’t exist Email doesn’t exist Create tester@example.com Create tester@example.com Created! Created! 1 2
UNIQUENESS V ALIDATION
UNIQUENESS V ALIDATION tester@example.com exists ? tester@example.com exists ? Email
doesn’t exist Email doesn’t exist Create tester@example.com Create tester@example.com Created! ActiveRecord::RecordNotUnique 1 2
Ruby on Rails • Strong Parameters http://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters
STRONG PARAMETERS { name: „John”, surname: „Doe” } { name:
„John” }
STRONG PARAMETERS { name: „John”, surname: „Doe” } ActionController::UnpermittedParameters: found
unpermitted keys: surname ActionController::Parameters.action_on_unpermitted_parameters = :raise
HTTP Requests GET POST GET, POST (JSON) String String String
Integer …
HANAMI http://hanamirb.org https://github.com/hanami •hanami-controller •HANAMI-MODEL •HANAMI-HELPERS •HANAMI-MAILER •HANAMI-VIEW •HANAMI-V ALIDATIONS
•HANAMI-ASSETS •HANAMI-UTILS Luca Guidi
HANAMI-V ALIDATIONS https://github.com/hanami/v alidations#v alidations •ACCEPTANCE •CONFIRMATION •EXCLUSION •INCLUSION •PRESENCE
•FORMAT* •SIZE* •TYPE (Coercions)
HANAMI-V ALIDATIONS • numericality V ALIDATION DoesN’t Exist • „The
framework will treat as V alid any blank attributes, without PRESENCE, for both FORMAT and SIZE predicates.”
DRY https://github.com/dry-rb •DRY-AUTO_INJECT •DRY-COMPONENT •DRY-CONFIGURABLE •DRY-CONSTRUCTOR •DRY-CONTAINER •DRY-EQUALIZER •DRY-INITALIZER •DRY-LOGIC
•DRY-PIPELINE •DRY-TRANSACTION •DRY-TYPES •DRY-V ALIDATION
DRY-V ALIDATION LATEST VERSION: 0.7.0 RELEASE DATE: 16.03.2016 http://dry-rb.org/gems/dry-v alidation/
DRY-V ALIDATION When to use? V ALIDATION OF: • form
params • „GET” params • JSON params • Y AML DOCUMENTS • application configuration • replacement of ActiveRecord / ActiveModel::Validations • replacement of StrongParameters • …
???