Validations in Ruby

Validations in Ruby

794f638b6f7f5132d5a13230e61c9db2?s=128

Krzysztof Wawer

March 23, 2016
Tweet

Transcript

  1. V ALIDATIONS IN Ruby Krzysztof W a wer 23.03.2016 TRUG

  2. ABOUT ME • E-MAIL: krzysztof.w a wer@gmail.com • Twitter:
 https://twitter.com/w

    afcio • GITHUB:
 https://github.com/w afcio
  3. V ALIDATION ???

  4. Ruby on rails ACTIVE MODEL & ACTIVE RECORD Active Model

    Active Record
  5. ACTIVE MODEL V ALIDATIONS • acceptance • confirmation • exclusion

    • format • inclusion • length • numericality • presence • uniqueness*
  6. 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
  7. UNIQUENESS V ALIDATION

  8. 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
  9. Ruby on Rails • Strong Parameters http://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters

  10. STRONG PARAMETERS { name: „John”, surname: „Doe” } { name:

    „John” }
  11. STRONG PARAMETERS { name: „John”, surname: „Doe” } ActionController::UnpermittedParameters: found

    unpermitted keys: surname ActionController::Parameters.action_on_unpermitted_parameters = :raise
  12. HTTP Requests GET POST GET, POST (JSON) String String String

    Integer …
  13. 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
  14. HANAMI-V ALIDATIONS https://github.com/hanami/v alidations#v alidations •ACCEPTANCE •CONFIRMATION •EXCLUSION •INCLUSION •PRESENCE

    •FORMAT* •SIZE* •TYPE (Coercions)
  15. 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.”
  16. 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
  17. DRY-V ALIDATION LATEST VERSION: 0.7.0 RELEASE DATE: 16.03.2016 http://dry-rb.org/gems/dry-v alidation/

  18. 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 • …
  19. ???