api_validator 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0e001388521bb57ebafb2c6fd2f6d0d130af547b
4
- data.tar.gz: 38696b838c273fc595523cd55ac31d7aec3e4029
3
+ metadata.gz: 8d32ef54839f650fd8e19746d176e26e241880b2
4
+ data.tar.gz: 67719c03e4cb48e79bc944d10bb98fba1b868161
5
5
  SHA512:
6
- metadata.gz: 59ecdf26a07d570f15006def55a5e4357406ee9e24125f3bce24bbd4979f45000a9e4bb041f304b186abca434391ed44cd53cf6f44a5894ffac29e66da7f4b6d
7
- data.tar.gz: 5b604d93767fc676ab63b52055af1d48fe73b3f8aeb478de5a7535ebb1d7aae6d5b0eac81e2430c7bdb99379609d6443096e43c36a4ac2571377ee6e1d42b171
6
+ metadata.gz: 7a7f4e019579c940dd6607c0933d13c076c6b3a36b2aebddecc8367e2b43b1757e09cbf9fa6463de397f5b8a0c10a48e31add4345fd875a048bd4cfb90b1daef
7
+ data.tar.gz: 020c51cdd554f68469b3c4836feefd6a412506906e52c1c0f3803e1cf655cc76807844b21e4059212a36f1dd01324d340f0d9b27ac2dd36d1cb4684d0e895e6f
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # api_validator
2
+ [![Gem Version](https://badge.fury.io/rb/api_validator.svg)](https://badge.fury.io/rb/api_validator)
2
3
  This gem helpful to validate api calls. You need to set rules and messages in yml file and rest of the things are handle by gem.
3
4
 
4
5
  ## Installation
@@ -25,7 +26,7 @@ Note : Here you can pass any name you want to set for initilizer file and valida
25
26
 
26
27
  ## Usage
27
28
 
28
- Before use of this gem you need to set ```ruby before_filter :request_validation ``` in controller where you want to validate request before reach to controller's method.
29
+ Before use of this gem you need to set ``` before_action :request_validation ``` in controller where you want to validate request before reach to controller's method.
29
30
 
30
31
  Also you need to set rules in the api_validator.yml.erb file. Example are mentioned in yml file.
31
32
 
@@ -40,13 +41,15 @@ controller_name:
40
41
  integer: true
41
42
  min_length: 5
42
43
  max_length: 15
43
- pattern: <%= /\A^[a-zA-Z\s'.-]*$\Z/.source %>
44
+ pattern: <%= /\A^[a-zA-Z\s'.-]*$\Z/.source %>
45
+ inclusion: "1..10"
44
46
  messages:
45
47
  presence: "Param1 must present."
46
48
  integer: "Param1 must contain integer only."
47
49
  min_length: "Param1 must have minimum length of 5."
48
50
  max_length: "Param1 must have maximum length of 15."
49
51
  pattern: "Invalid Param1"
52
+ inclusion: "The param1 must be between 1 and 10"
50
53
  ```
51
54
 
52
55
  Sample validation for JSON value in request parameter -
data/lib/api_validator.rb CHANGED
@@ -4,7 +4,7 @@ module ApiValidator
4
4
  INTEGER_REGEX = /^[1-9]([0-9]*)?$/
5
5
  # request validation method
6
6
  # Input - request params
7
- # Process - Validate params with there rules & definition
7
+ # Process - Validate params with there rules & defination
8
8
  # Output - report error on invalidity
9
9
  def request_validation
10
10
  if is_api_validator_applicable?(params[:controller], params[:action])
@@ -13,9 +13,9 @@ module ApiValidator
13
13
 
14
14
  validation_pd.keys.each do |key|
15
15
  next if params.has_key?(key) == false && validation_pd[key]["rules"].has_key?("presence") == false
16
- validation_pd[key]["rules"].each do |rule, definition|
16
+ validation_pd[key]["rules"].each do |rule, defination|
17
17
  # when param's value is JSON string then parse it and validate parameters
18
- if (rule == "json_string" and definition == true)
18
+ if (rule == "json_string" and defination == true)
19
19
  begin
20
20
  json_data = JSON.parse(params[key])
21
21
  json_data = [json_data] unless json_data.class == Array
@@ -23,9 +23,9 @@ module ApiValidator
23
23
  data.keys.each do |json_data_key|
24
24
  if validation_pd[key].has_key?("parameters")
25
25
  next unless validation_pd[key]["parameters"].has_key?(json_data_key)
26
- validation_pd[key]["parameters"][json_data_key]["rules"].each do |json_data_rule, json_data_definition|
26
+ validation_pd[key]["parameters"][json_data_key]["rules"].each do |json_data_rule, json_data_defination|
27
27
  #CAUTION: if nested JSON, this should be recursive
28
- return error_response(validation_pd[key]["parameters"][json_data_key]["messages"][json_data_rule]) if validate?(json_data_key, data[json_data_key], json_data_rule, json_data_definition, validation_pd[key]["parameters"][json_data_key])
28
+ return error_response(validation_pd[key]["parameters"][json_data_key]["messages"][json_data_rule]) if validate?(json_data_key, data[json_data_key], json_data_rule, json_data_defination, validation_pd[key]["parameters"][json_data_key])
29
29
  end
30
30
  end
31
31
  end
@@ -35,7 +35,7 @@ module ApiValidator
35
35
  end
36
36
  # when param's value is NOT JSON
37
37
  else
38
- return error_response(validation_pd[key]["messages"][rule]) if validate?(key, params[key], rule, definition, validation_pd[key])
38
+ return error_response(validation_pd[key]["messages"][rule]) if validate?(key, params[key], rule, defination, validation_pd[key])
39
39
  end
40
40
  end # param rule loop end
41
41
  end # params list loop end
@@ -59,6 +59,11 @@ module ApiValidator
59
59
  !!(value =~ pattern)
60
60
  end
61
61
 
62
+ def is_in_inclusion?(defination, value)
63
+ range = defination.split("..").map(&:to_i)
64
+ Range.new(range[0], range[-1]) === value.to_i
65
+ end
66
+
62
67
  def validate_array_string?(value, separator)
63
68
  parameter_ids = value.split(separator)
64
69
  parameter_ids.each do |parameter|
@@ -67,34 +72,37 @@ module ApiValidator
67
72
  return true
68
73
  end
69
74
 
70
- def validate?(key, value, rule, definition, dtd)
75
+ def validate?(key, value, rule, defination, dtd)
71
76
  is_error_found = false
72
77
  case
73
- when (rule == "ignore_if_present" and definition.present?)
78
+ when (rule == "ignore_if_present" and defination.present?)
74
79
  # return error if not present & defination absence
75
- is_error_found = true if value.present? == false and params[definition].present? == false
76
- when (rule == "presence" and definition == true)
80
+ is_error_found = true if value.present? == false and params[defination].present? == false
81
+ when (rule == "presence" and defination == true)
77
82
  # return error if not present
78
83
  is_error_found = true unless value.present?
79
- when (rule == "array_string" and definition.present?)
84
+ when (rule == "array_string" and defination.present?)
80
85
  # return error if array string invalid
81
- is_error_found = true unless validate_array_string?(value, definition)
82
- when (rule == "integer" and definition == true)
86
+ is_error_found = true unless validate_array_string?(value, defination)
87
+ when (rule == "integer" and defination == true)
83
88
  # return error if not match with integer
84
89
  is_error_found = true unless is_integer?(value)
85
- when (rule == "min_length" and definition > 0)
90
+ when (rule == "min_length" and defination > 0)
86
91
  # return error if minimum length is not achived
87
- is_error_found = true unless value.length >= definition
88
- when (rule == "max_length" and definition > 0)
92
+ is_error_found = true unless value.length >= defination
93
+ when (rule == "max_length" and defination > 0)
89
94
  # return error if maximum length is not achived
90
- is_error_found = true unless value.length <= definition
91
- when (rule == "max_value" and definition > 0)
92
- # return error if param's value is less or equal to definition
93
- is_error_found = true unless value.to_f <= definition
94
- when (rule == "pattern" and definition.present?)
95
+ is_error_found = true unless value.length <= defination
96
+ when (rule == "max_value" and defination > 0)
97
+ # return error if param's value is less or equal to defination
98
+ is_error_found = true unless value.to_f <= defination
99
+ when (rule=="inclusion" and defination.present?)
100
+ # return error if param's value is not in between defination
101
+ is_error_found = true unless is_in_inclusion?(defination, value)
102
+ when (rule == "pattern" and defination.present?)
95
103
  # return error if pattern doesn't match
96
104
  if dtd["rules"].has_key?("presence") == true || value.present?
97
- is_error_found = true unless is_pattern_match?(value, Regexp.new(definition))
105
+ is_error_found = true unless is_pattern_match?(value, Regexp.new(defination))
98
106
  end
99
107
  end
100
108
  return is_error_found
@@ -1,3 +1,3 @@
1
1
  module ApiValidator
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -9,12 +9,14 @@
9
9
  # min_length: 5
10
10
  # max_length: 15
11
11
  # pattern: <%#= "/\A^[a-zA-Z\s'.-]*$\Z/".source %>
12
+ # inclusion: "1..10"
12
13
  # messages:
13
14
  # presence: "Param1 must present."
14
15
  # integer: "Param1 must contain integer only."
15
16
  # min_length: "Param1 must have minimum length of 5."
16
17
  # max_length: "Param1 must have maximum length of 15."
17
18
  # pattern: "Invalid Param1"
19
+ # inclusion: "The param1 must be between 1 and 10"
18
20
  #
19
21
  # Also if have some paramters which accepts json as value and that json have some
20
22
  # which need to be validate you need to follow following pattern to make it applicable
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_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
  - sandipkaranjekar
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-29 00:00:00.000000000 Z
11
+ date: 2017-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler