Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Validating data in Ember

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

Validating data in Ember

Talks about validation in general and specifically in Ember and some caveats and tips for using both, client and server side validations.
mentions ember-cp-validations and ember-changeset-validations addons

Avatar for vitebski

vitebski

August 27, 2018

Other Decks in Programming

Transcript

  1. First application using Ember started on 2014: • Ember 1.6.0.beta.2

    – Ember data 1.0.0.beta.7 – Ember validation 1.0.0.beta.2 Ember history in Redis Labs 2
  2. • Ember 3.3 (in-house) – 13 routes and 5 models

    • Ember 2.16.0 (onprem) – 24 routes and 31 models • Ember 2.13 (in-house) – 9 routes and 0 models • Ember 2.11 (service) – 55 routes and 39 models Ember today in Redis Labs 3
  3. “Form validation helps us to ensure that users fill out

    forms in the correct format, making sure that submitted data will work successfully with our applications.” source: https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Form_validation 4
  4. Pros - Quick feedback - no need to perform request

    to server - Better UX - immediate awareness of what wrong and how to fix it - Reduce load on server - validation logic executed on client - Faster and easier to implement Client-side validations 5
  5. Cons - Duplication logic - multiple interfaces: REST API, Mobile

    app, desktop app - Less secure - if only implemented on client side Client-side validations 6
  6. Pros - More secured - if UI bypassed server will

    stop harmful request - Some validations can only be done on server - registering user with unique email Server-side validations 7
  7. Cons - Duplication logic - multiple interfaces: REST API, Mobile

    app, desktop app Server-side validations 8
  8. Always do all validations on server first and only add

    necessary UI validations to improve user experience. 9
  9. Using HTML5 input new features: - The required attribute -

    The min and max attributes - New text INPUT types: color, date, datetime-local, email, month, number, range, search, tel, time, url, week - The pattern attribute: specifies a regular expression to validate control’s value - message customization using setCustomValidity DOM API Client-side validations - methods 10
  10. 12

  11. 13

  12. 14

  13. We can do it better - using EmberJS CP (Computed

    Properties) Client-side validations - home made 15
  14. 16

  15. 19

  16. 20

  17. 21

  18. 22

  19. ember-changeset-validations - being very generic - lacks any support for

    server-side validations ember-cp-validations - “Easily integrated with Ember Data's DS.Errors” Client-side and Server-Side validations 23
  20. Consuming server-side validation errors using Ember Data Client-side and Server-Side

    validations 24 - Populate Model’s errors key - Population depends on adapter used - errors property on DS.Model of DS.Errors type, filled with errors payload returned from server - depends on adapter - Built-in adapters: RESTAdapter and JSONAPIAdapter - server must return 422 and JSON with errors - can be customized - Custom Adapter can implement different logic