poro_validator 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c80c300291ed684162f3d39b73486788444c542e
4
- data.tar.gz: c174bca416c79b2daad6503aa1d0f212238e107e
3
+ metadata.gz: e4d1445d3377247ce8342c629d67f82f53b68863
4
+ data.tar.gz: fd555af7b7243571bea098d4e783cfdda6e46581
5
5
  SHA512:
6
- metadata.gz: ee06bdb65a1513e9850a2ee6f6732698870bce95751224aff49f853a270b77dd38331a5ead47c2dedecfc81a42513114e58e91331184d5edd41b14f8aa93d076
7
- data.tar.gz: 48b39457c43c797397c946723e4a7880b17af87c5f839df66534ce855add51668543012a21e9cd3ffd6f82fd1d8be441597ee67da40e350e4991779fe60168ed
6
+ metadata.gz: a543978ea06a2f0251a28f6708f85d6bb70e66ab379b2ae458de4ced67ae6130ab9d1febb26382ae266be042eded0b91372b137e140b8cfa029ececd18494edf
7
+ data.tar.gz: bb8bd86588f9fb3cdd300a2241be206cbc82a6dab07c2082000eb8115140dfb2a52c8ef7becec53f32c2f4b01e553e6c614c8597edb3bd1c223073c6875bfece
data/README.md CHANGED
@@ -9,39 +9,79 @@
9
9
  PoroValidator is a lightweight validation library for your
10
10
  **P**lain **O**ld **R**uby **O**bjects (hence PoroValidator).
11
11
 
12
- I always believed that validation is a seperate concern and should not be defined in the object
13
- that you are going to be validating. This validator library aims to seperate the validation to a
14
- seperate concern giving it great flexibility and scalability.
12
+ Based on the [Single Responsibility Principle] approach.
15
13
 
16
- It is framework agnostic and can be used on any plain old ruby object/entities.
14
+ This validator library implements the single responsibility approach from the
15
+ [S.O.L.I.D] principle.
16
+
17
+ By using this approach we can create validations for our object that can be
18
+ easily tested, maintained and scaled.
19
+
20
+ ##TL;DR
21
+
22
+ - [Features](#features)
23
+ - [Installation](#installation)
24
+ - [Synopsis](#synopsis)
25
+ - [Validators](#validators)
26
+ - [WIKI]
27
+
28
+ ### Author's POV
29
+ I always believed an object's validation is a separate concern and not have
30
+ that functionality be embedded in the object which the validity is in
31
+ question. (ActiveRecord objects validates itself.)
32
+
33
+ The library I created is framework agnostic and can be used on any plain old
34
+ ruby objects (as long as the object responds to the attribute that requires
35
+ validation it will validate it, this includes **Hash Objects**).
36
+
37
+ Here's an example scenario:
38
+
39
+ When working with [Ruby on Rails] because of [ActiveRecord]'s integration we
40
+ only validate an object after it's ActiveRecord object is loaded. But with
41
+ this library it gives you the ability to validate an object at the boundary
42
+ level of your application (when an incoming payload comes through your API -
43
+ think of the parameters that come in when a request is made). Instead of
44
+ initializing an full [ActiveRecord] object just to validate the object, you
45
+ are have now the option of validating the incoming parameters directly!
46
+
47
+ How you handle the state of that validation (whether it's valid or not) is up
48
+ to you (e.g, return the object so the user's form is still filled and not blank).
17
49
 
18
50
  #### ActiveRecord/ActiveModel::Validations
19
- While [ActiveModel::Validations] is great if you've got simple validation logic,
20
- it doesn't cut it for complex validations needs. When you have different validation
21
- for the same object at each point in it's life cycle, you need something more flexible.
22
-
23
- The problem with [ActiveModel::Validations] is that it hooks pretty deep into [ActiveRecord].
24
- The main use case for [ActiveModel::Validations] is to prevent bad data hitting your
25
- database - which isn't always the case (sometimes we want to allow bad data to go through).
26
- PoroValidator decouples your validation logic from your object structure. With
27
- PoroValidator you can define different validation rules for different contexts.
28
- So instead of *objective* validation where the validation is defined in the object
29
- you want to validate we define it in a seperate class making it *subjective*.
51
+ [ActiveModel::Validations] is great for simple validation logic. However, as
52
+ your application grows you want to have more complex validations (think of
53
+ normalized tables where your application has numerous join tables) when
54
+ processing the incoming parameters which are most likely going to be nested we
55
+ would need a validator that can handle nested objects fast and return the
56
+ associated errors for the invalid attributes properly. Another scenario is
57
+ throughout the life cycle of an object we would also want different validations
58
+ like if the validation is dependent on the state of an object. So we need a validator
59
+ that's more flexible, enter [PoroValidator].
60
+
61
+ Another issue with [ActiveModel::Validations] is that it hooks pretty deep into
62
+ [ActiveRecord]. The main use case for [ActiveModel::Validations] within the
63
+ [ActiveRecord] object is to prevent bad data hitting your database - which
64
+ isn't always the case (sometimes we want to allow bad data to go through).
65
+ PoroValidator decouples your validation logic from your object structure.
66
+ With PoroValidator you can define different validation rules for different
67
+ contexts. So instead of the object validating itself by definining the
68
+ validation within the object, we create separate validation classes that gives
69
+ us more flexibility while adhering the the [Single Responsibility Principle].
30
70
 
31
71
  ## Features ##
32
72
  - **Familiar, simple and consistent API.**
33
73
  - **Framework agnostic, all you need is a PORO**.
34
- - **Validate Hash objects! Good for params, json objects what have you!**
35
- - **Validation logic is decoupled from business logic by creating seperate *validator* classes
36
- which allows easy testing for the validator class**.
37
- - **No magic, caller is in control.**
38
- - **Invalid validations does not necessitate inability to persist to database**.
39
- - [**Conditional Validations via `:if` and `:unless`.**](#conditional-validations)
40
- - [**Nested validations for nested object structures - nicely handling nested errors.**](#nested-validations)
41
- - [**Composable validations by reusing existing validators.**](#composable-validations)
42
- - [**Create custom validators.**](#custom-validators)
43
- - **Overrideable messages either via the ```:message``` option or through the configuration.**
44
- - **Easily test validator classes.**
74
+ - **No magic, caller is in control, invalid validations does not necessitate
75
+ inability to persist to database**.
76
+ - [**Conditional Validations**](#conditional-validations)
77
+ - [**Nested validations**](#nested-validations)
78
+ - [**Composable/DRY validations**](#composable-validations)
79
+ - [**Custom validators.**](#custom-validators)
80
+ - **Configurable messages**
81
+ - **SpecHelper for testing validation classes [WIP]
82
+ (https://github.com/magicalbanana/poro_validator/issues/25)**
83
+
84
+ Feel free the peruse the [WIKI] pages for more information!
45
85
 
46
86
  ## Installation ##
47
87
 
@@ -63,7 +103,7 @@ Or install it yourself as:
63
103
  $ gem install poro_validator
64
104
  ```
65
105
 
66
- ## Usage ##
106
+ ## Synopsis ##
67
107
 
68
108
  ### Creating and using a validator ###
69
109
  ```ruby
@@ -74,11 +114,17 @@ class CustomerValidator
74
114
  validates :last_name, presence: true
75
115
  validates :first_name, presence: true
76
116
  validates :age, numeric: { min: 18 }
117
+ validates :address do
118
+ validates :line1, presence :true
119
+ validates :line2, presence :true
120
+ validates :city, presence: true, inclusion: AmericanCities.all
121
+ validates :zip_code, format: /[0-9]/
122
+ end
77
123
  end
78
124
 
79
125
  validator = CustomerValidator.new
80
126
 
81
- # Validate entity
127
+ # Validate an ruby object/entity
82
128
  customer = CustomerDetail.new
83
129
  validator.valid?(customer) # => false
84
130
  validator.errors.full_messages # => ["last name is not present", "..."]
@@ -215,7 +261,10 @@ define it via the [PoroValidator.configuration](#error-messages)
215
261
  ### Error Messages ###
216
262
 
217
263
  #### #configure
218
- The ```message``` configuration object, allows you to change the default error message produced by each validator. The message must be in the form of a lambda or Proc, and may or may not receive an argument. Use the example below for reference when customizing messages.
264
+ The `message` configuration object, allows you to change the default error
265
+ message produced by each validator. The message must be in the form of a `lambda`
266
+ or `Proc`, and may or may not receive an argument. Use the example below for
267
+ reference when customizing messages.
219
268
 
220
269
  ```ruby
221
270
  PoroValidator.configure do |config|
@@ -227,7 +276,8 @@ end
227
276
  ```
228
277
 
229
278
  #### #on method
230
- The `on` method is used to acccess the error messages related to a key or attribute/method.
279
+ The `on` method is used to acccess the error messages related to a key or
280
+ attribute/method.
231
281
 
232
282
  ##### unnested validations
233
283
  Pass in either a symbol or a string
@@ -246,7 +296,7 @@ validator.errors.on({address: {country: {coordinates: {planent: :name}}}})
246
296
  ```
247
297
 
248
298
  ## Conditional Validations ##
249
- You can pass in conditional
299
+ You can pass in conditions
250
300
 
251
301
  ```ruby
252
302
  class CustomerValidator
@@ -258,6 +308,9 @@ class CustomerValidator
258
308
  validates :age, presence: { if: :entity_method }
259
309
  validates :address do
260
310
  validates :line1, presence: { if: 'entity.nested_entity.method' }
311
+ validates :line2, presence: {
312
+ [if: foo, unless: :boo, if: 'entity.nested_entity_method']
313
+ }
261
314
  end
262
315
  ```
263
316
 
@@ -360,11 +413,15 @@ validator.errors.full_messages # => [
360
413
 
361
414
  ## Contributing
362
415
 
363
- Bug reports and pull requests are welcome on GitHub at https://github.com/magicalbanana/poro_validator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
416
+ Bug reports and pull requests are welcome on GitHub at [PoroValidator].
417
+ This project is intended to be a safe, welcoming space for collaboration, and
418
+ contributors are expected to adhere to the [Contributor Covenant]
419
+ (contributor-covenant.org) code of conduct.
364
420
 
365
421
  ## License
366
422
 
367
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
423
+ The gem is available as open source under the terms of the [MIT License]
424
+ (http://opensource.org/licenses/MIT).
368
425
 
369
426
  ## Copyright
370
427
 
@@ -385,3 +442,8 @@ Copyright (c) 2015 Kareem Gan
385
442
 
386
443
  [ActiveModel::Validations]: http://api.rubyonrails.org/classes/ActiveModel/Validations.html
387
444
  [ActiveRecord]: http://guides.rubyonrails.org/active_record_validations.html
445
+ [S.O.L.I.D]: https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)
446
+ [Single Responsibility Principle]: https://en.wikipedia.org/wiki/Single_responsibility_principle
447
+ [Ruby on Rails]: http://rubyonrails.org/
448
+ [PoroValidator]: https://github.com/magicalbanana/poro_validator
449
+ [WIKI]: https://github.com/magicalbanana/poro_validator/wiki
@@ -1,3 +1,3 @@
1
1
  module PoroValidator
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: poro_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kareem Gan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-12 00:00:00.000000000 Z
11
+ date: 2016-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler