Module: Wireway::Standard
Overview
接口请求参数标准
Instance Method Summary collapse
-
#attribute_necessary(ideal:, reality:) ⇒ Object
参数项检查.
- #check_data_format(described:, reality:) ⇒ Object
-
#default_value(ideal:, reality:) ⇒ Object
参数默认值检查.
-
#inspect_all(ideal:, reality:) ⇒ Object
全自检项检查.
-
#value_type(ideal:, reality:) ⇒ Object
参数值格式检查.
Instance Method Details
#attribute_necessary(ideal:, reality:) ⇒ Object
参数项检查
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/wireway/core/standard.rb', line 25 def attribute_necessary(ideal:, reality:) # 检查必须参数是否缺失 necessary_standards = ideal.select{|standard| standard[:necessary] == true} necessary_standards.each do |standard| key = standard[:key] unless (reality[key.to_sym] || reality[key]).present? return [false, "#{standard[:name]}(#{key})为必须项,不能为空"] end end # 检查参数值是否符合正则式要求 reality.each do |key, value| standard = ideal.select{|standard| [key, key.to_sym, key.to_s].uniq.include?(standard[:key])}[0] # return [false, "未找到关于参数:#{key} 的定义"] unless standard next unless standard if standard[:regular] && (value.to_s =~ standard[:regular]).nil? tmp = "参数:#{standard[:name]}(#{key})的参数值(#{value})不满足要求" tmp+= ", 请参考: #{standard[:explain]}" if standard[:explain].present? tmp+= ", 示例值: #{standard[:example]}" if standard[:example].present? tmp+= ", 默认值: #{standard[:default]}" if standard[:default].present? tmp+='.' return [false, tmp] end end [true, ''] end |
#check_data_format(described:, reality:) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/wireway/core/standard.rb', line 116 def check_data_format(described:, reality:) data_format = {} if (reality[:data_format] == ['original']) && (reality[:key_type] == 'zh') described[:data_structure].map do |key, value| data_format.merge!([[value, key]].to_h) end reality[:data_format] = data_format return [true, ''] elsif (reality[:data_format] == ['original']) described[:data_structure].each do |key, value| data_format.merge!([[key, key]].to_h) end reality[:data_format] = data_format return [true, ''] end data_format = reality[:data_format] data_format = data_format.map(&:to_sym) data_structure = {} described[:data_structure].map{|key, value| data_structure.merge!([[key, key], [value.to_sym, key]].to_h)} hit_items = data_structure.select{|key, value| data_format.include?(key)} missed = (data_format - hit_items.keys).join(',') return [false, "所提供数据格式中:#{missed},未找到对应匹配数据项"] if missed.present? reality[:data_format] = hit_items [true, ''] end |
#default_value(ideal:, reality:) ⇒ Object
参数默认值检查
55 56 57 58 59 60 61 62 63 |
# File 'lib/wireway/core/standard.rb', line 55 def default_value(ideal:, reality:) ideal.select{|standard| !standard[:default].nil?}.each do |standard| unless reality[standard[:key].to_sym].present? reality.merge!({standard[:key].to_sym => standard[:default]}) end end [true, ''] end |
#inspect_all(ideal:, reality:) ⇒ Object
全自检项检查
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/wireway/core/standard.rb', line 7 def inspect_all(ideal:, reality:) ideal_reality = {ideal: ideal, reality: reality} res = default_value(ideal_reality) return res unless res[0] res = value_type(ideal_reality) return res unless res[0] res = attribute_necessary(ideal_reality) return res unless res[0] # res = check_data_format(described: described, reality: reality) # return res unless res[0] [true, ''] end |
#value_type(ideal:, reality:) ⇒ Object
参数值格式检查
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/wireway/core/standard.rb', line 66 def value_type(ideal:, reality:) ideal.each do |standard| unless standard[:value_type].present? return [false, "自描述参数标准检查异常: #{standard[:name]}(#{standard[:key]})未设置对应参数值类型(value_type)."] end end ideal.each do |standard| old_value = reality[standard[:key].to_sym] case standard[:value_type].to_sym when :string_or_array, :array_or_string new_value = (JSON.parse(old_value) rescue false) new_value = old_value.split(/, |,/) unless new_value when :array error_msg = [false, "参数:#{standard[:key]},参数类型不符合Json数组要求."] next if old_value.is_a?(Array) new_value = (JSON.parse(old_value) rescue (return error_msg)) return error_msg unless new_value.is_a?(Array) when :key_value error_msg = [false, "参数:#{standard[:key]},参数类型不符合Json键值对要求."] next if old_value.is_a?(Hash) new_value = (JSON.parse(old_value) rescue (return error_msg)) return error_msg unless new_value.is_a?(Hash) when :boolean new_value = [true, 'true', '1', 1].include?(old_value) when :integer max_value = standard[:max] return [false, "参数:#{standard[:key]},参数值:#{old_value} 大于接口定义最大值:#{max_value}."] if max_value.present? && (old_value.to_i > max_value) next if old_value.is_a?(Integer) if old_value == '0' new_value = 0 else return [false, "参数:#{standard[:key]},参数类型不符合数字要求."] if ((old_value.to_i rescue 0) == 0) new_value = old_value.to_i end else next end unless (old_value == new_value) reality.merge!({standard[:key].to_sym => new_value}) end end [true, ''] end |