lib/util/params/params.rb in util-params-0.1.3 vs lib/util/params/params.rb in util-params-0.2.0
- old
+ new
@@ -6,187 +6,255 @@
# エラーフラグ
@is_error = false
# エラーリスト
@errors = []
end
-
- # パラメーター取得
- # param[in] name
- # param[in] default
- # param[in] isRequire
- # param[in] params (min_length, max_length, reg)
- def get_param_str name, default = nil, is_require = false, params = {}
-
- val = get_param_val name, default, is_require
-
+
+ module Type
+ INTEGER = 1
+ STRING = 2
+ FLOAT = 3
+ FILE = 4
+ BOOLEAN = 5
+ OBJECT = 6
+ ARRAY = 7
+ end
+
+ def get_params options
+ options = _symbolize_keys options
+
+ key = options[:key]
+ val = _load_val params, key, options[:default], options[:require]
+
return nil if val.nil?
- v = val.to_s
+
+ _validate key, options[:type], val, options
+ end
+
+ def get_int_params key, options
+ get_params options.merge(key: key, type: Type::INTEGER)
+ end
+
+ def get_str_params key, options
+ get_params options.merge(key: key, type: Type::STRING)
+ end
+
+ def get_float_params key, options
+ get_params options.merge(key: key, type: Type::FLOAT)
+ end
+
+ def get_file_params key, options
+ get_params options.merge(key: key, type: Type::FILE)
+ end
+
+ def get_bool_params key, options
+ get_params options.merge(key: key, type: Type::BOOLEAN)
+ end
+
+ def get_array_params key, options
+ get_params options.merge(key: key, type: Type::ARRAY)
+ end
+
+ def get_object_params key, options
+ get_params options.merge(key: key, type: Type::OBJECT)
+ end
+
+
+ # エラーがあるか
+ def has_params_error?
+ @is_error
+ end
+ # エラーメッセージ入りスト
+ def get_params_error
+ @errors.join ', '
+ end
- if params.key? :enum
- for e in params[:enum]
- return v if e === v
+ protected
+
+ def _load_val vals, key, default, is_require
+
+ unless vals.has_key? key
+ push_error "#{key.to_s} == nil" if is_require
+ return default
+ end
+
+ vals[key]
+ end
+
+ def _validate key_label, type, val, options
+ options ||= {}
+
+ case type
+ when Type::INTEGER
+ _validate_int key_label, val, options[:min], options[:max], options[:enum]
+ when Type::STRING
+ _validate_str key_label, val, options[:min], options[:max], options[:enum], options[:reg]
+ when Type::FLOAT
+ _validate_float key_label, val, options[:min], options[:max]
+ when Type::BOOLEAN
+ _validate_bool key_label, val
+ when Type::FILE
+ _validate_file key_label, val
+ when Type::OBJECT
+ _validate_object key_label, val, options[:elements]
+ when Type::ARRAY
+ vals = _validate_array key_label, val, options[:min], options[:max]
+ return nil if vals.nil?
+ elem_options = options[:element] || {}
+ elem_type = elem_options[:type] || Type::STRING
+
+ vals.map.with_index do |_, i|
+ elem_val = vals[i]
+ _validate "#{key_label}[#{i}]", elem_type, elem_val, elem_options
end
- push_error "#{name.to_s} == unknone val [#{v.to_s}]"
+ else
+ # do nothing
end
-
- if params.key?(:min_length) && (v.length < params[:min_length])
- push_error "#{name.to_s} len [#{v.to_s}] < #{params[:min_length].to_s}"
+
+ end
+
+ def _validate_int key, val, min, max, enum
+ return nil if val.blank?
+
+ if /[^\d]/ =~ val.to_s
+ push_error "#{key.to_s} type [#{val.to_s}] != integer"
end
-
- if params.key?(:max_length) && (v.length > params[:max_length])
- push_error "#{name.to_s} len [#{v.to_s}] > #{params[:max_length].to_s}"
+
+ v = val.to_i
+
+ if enum
+ for e in enum
+ return v if e === v
+ end
+ push_error "#{key.to_s} == unknone val [#{v.to_s}]"
end
-
- if params.key?(:reg) && !(/#{params[:reg]}/ =~ val)
- push_error "#{name.to_s} unmatch /#{params[:reg].to_s}/ =~ [#{v.to_s}]"
+
+ if min && (v < min)
+ push_error "#{key.to_s} val [#{v.to_s}] < #{min.to_s}"
end
-
+
+ if max && (v > max)
+ push_error "#{key.to_s} val [#{v.to_s}] > #{max.to_s}"
+ end
+
v
end
- #
- def get_param_strs name, default = [], is_require = false
- vals = get_param_val name, default, is_require
-
- return nil if vals.nil?
-
- vals
- end
- # パラメーター取得
- def get_param_int name, default = nil, is_require = false, params = {}
-
- val = get_param_val name, default, is_require
-
+ def _validate_str key, val, min, max, enum, reg
return nil if val.nil?
- return val if val.kind_of? Integer
-
- if /[^\d]/ =~ val
- push_error "#{name.to_s} type [#{val.to_s}] != integer"
- end
-
- v = val.to_i
-
- if params.key? :enum
- for e in params[:enum]
- logger.debug e
+
+ v = val.to_s
+
+ if enum
+ enum.each do |e|
return v if e === v
end
- push_error "#{name.to_s} == unknone val [#{v.to_s}]"
+ push_error "#{key.to_s} == unknone val [#{v.to_s}]"
end
-
- if params.key?(:min) && (v.length < params[:min])
- push_error "#{name.to_s} val [#{v.to_s}] < #{params[:min].to_s}"
+
+ if min && (v.length < min)
+ push_error "#{key.to_s}.length < #{min.to_s} ('#{v.to_s}')"
end
-
- if params.key?(:max) && (v.length > params[:max])
- push_error "#{name.to_s} val [#{v.to_s}] > #{params[:max].to_s}"
+
+ if max && (v.length > max)
+ push_error "#{key.to_s}.length > #{max.to_s} ('#{v.to_s}')"
end
+
+ if reg && !(/#{reg}/ =~ val)
+ push_error "#{key.to_s} unmatch /#{reg.to_s}/ =~ [#{v.to_s}]"
+ end
+
v
end
- def get_param_ints name, default = [], is_require = false
- vals = get_param_val name, default, is_require
- return nil if vals.nil?
-
- rs = []
-
- vals.each do |v|
-
- if params.key? :enum
- for e in params[:enum]
- next rs << v.to_i if e === v
- end
- push_error "#{name.to_s} == unknone val [#{v.to_s}]"
- end
-
- if params.key?(:min) && (v.length < params[:min])
- push_error "#{name.to_s} val [#{v.to_s}] < #{params[:min].to_s}"
- end
-
- if params.key?(:max) && (v.length > params[:max])
- push_error "#{name.to_s} val [#{v.to_s}] > #{params[:max].to_s}"
- end
-
- rs << v.to_i
+ def _validate_float key, val, min, max
+ return nil if val.blank?
+
+ if /[^\d.]/ =~ val
+ push_error "#{key.to_s} type [#{val.to_s}] != float"
end
-
- rs
- end
-
- def get_param_objects name, default = [], is_require = false
- vals = get_param_val name, default, is_require
- vals.map do |v|
- v.permit!.to_h.deep_symbolize_keys
+
+ v = val.to_f
+
+ if min && (v < min)
+ push_error "#{key.to_s} val [#{v.to_s}] < #{min.to_s}"
end
+
+ if max && (v > max)
+ push_error "#{key.to_s} val [#{v.to_s}] > #{max.to_s}"
+ end
+
+ v
end
-
- def get_param_object name, default = {}, is_require = false
- val = get_param_val name, default, is_require
- val.permit!.to_h.deep_symbolize_keys
- end
-
- #
- def get_param_bool name, default = nil, is_require = false
- val = get_param_val name, default, is_require
+
+ def _validate_bool key, val
return false if val.kind_of? FalseClass
return true if val.kind_of? TrueClass
return nil if val.blank?
return false if val == 'false'
return true if val == 'true'
val.to_i > 0
end
-
- # ファイル受け取り
- def get_file name, is_require = false
-
- f = params[name]
-
- if is_require
-
- if f.nil?
- push_error "#{name.to_s} == nil"
- return nil
- end
- if f.size.blank?
- push_error "#{name.to_s} == nil"
- return nil
- end
+
+ def _validate_file key, val
+ return nil if val.nil?
+
+ if f.size.blank?
+ push_error "#{key.to_s} == nil"
+ return nil
end
-
- return nil if f.nil?
- return nil if f.size.blank?
-
- f
+
+ return nil if val.nil?
+ return nil if val.size.blank?
+
+ val
end
- #
- def has_param? name
- params.include? name
+
+ def _validate_array key, val, min, max
+ return nil if val.nil?
+
+ unless val.kind_of? Array
+ push_error "#{key.to_s}.type != Array"
+ return nil
+ end
+
+ v = val
+
+ if min && (v.length < min)
+ push_error "#{key.to_s} val [#{v.to_s}.length] < #{min.to_s}"
+ end
+
+ if max && (v.length > max)
+ push_error "#{key.to_s} val [#{v.to_s}.length] > #{max.to_s}"
+ end
+
+ v
end
- # エラーがあるか
- def has_param_error?
- @is_error
+
+ def _validate_object key, val, elements
+ return nil if val.nil?
+
+ elements.map do |options|
+ options ||= {}
+ elem_key = options[:key]
+ elem_type = options[:type]
+ elem_default = options[:default]
+ elem_require = options[:require]
+ elem_val = _load_val val, elem_key, elem_default, elem_require
+
+ _validate "#{key}[#{elem_key}]", elem_type, elem_val, options
+ end
end
- # エラーメッセージ入りスト
- def get_error
- @errors.join ', '
+
+ def _symbolize_keys hash
+ hash.map{|k,v| [k.to_sym, v] }.to_h
end
-
- protected
-
+
# エラー追加
def push_error message
@is_error |= true
@errors.push message
end
- # パラメーター取得
- def get_param_val name, default, is_require
- unless params.include? name
- push_error "#{name.to_s} == nil" if is_require
- return default
- end
- params[name]
- end
-
end
end