|
28 | 28 |
|
29 | 29 | {{/optionalVars}} |
30 | 30 | {{#hasEnums}} |
31 | | - class EnumAttributeValidator |
32 | | - getter datatype : String |
33 | | - getter allowable_values : Array(String) |
34 | | - |
35 | | - def initialize(datatype, allowable_values) |
36 | | - @datatype = datatype |
37 | | - @allowable_values = allowable_values.map do |value| |
38 | | - case datatype.to_s |
39 | | - when /Integer/i |
40 | | - value.to_i |
41 | | - when /Float/i |
42 | | - value.to_f |
43 | | - else |
44 | | - value |
45 | | - end |
| 31 | + abstract class EnumAttributeValidator |
| 32 | + def valid?(value) |
| 33 | + !value || @allowable_values.includes?(value) |
| 34 | + end |
| 35 | + |
| 36 | + def message |
| 37 | + "invalid value for \"#{@attribute}\", must be one of #{@allowable_values}." |
| 38 | + end |
| 39 | + |
| 40 | + def to(_type, value) |
| 41 | + case _type |
| 42 | + when Int32 |
| 43 | + value.to_i32 |
| 44 | + when Int64 |
| 45 | + value.to_i64 |
| 46 | + when Float32 |
| 47 | + value.to_f32 |
| 48 | + when Float64 |
| 49 | + value.to_f64 |
| 50 | + else |
| 51 | + value.to_s |
46 | 52 | end |
47 | 53 | end |
| 54 | + end |
48 | 55 |
|
49 | | - def valid?(value) |
50 | | - !value || allowable_values.includes?(value) |
| 56 | + {{#vars}} |
| 57 | + {{#isEnum}} |
| 58 | + {{^isContainer}} |
| 59 | + class EnumAttributeValidatorFor{{#lambdaTitlecase}}{{{name}}}{{/lambdaTitlecase}} < EnumAttributeValidator |
| 60 | + @attribute : String |
| 61 | + @allowable_values : Array(Int32 | Int64 | Float32 | Float64 | String) |
| 62 | + |
| 63 | + def initialize |
| 64 | + @attribute = "{{{name}}}" |
| 65 | + @allowable_values = [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}].map { |value| to({{{dataType}}}, value)} |
51 | 66 | end |
52 | 67 | end |
53 | 68 |
|
| 69 | + {{/isContainer}} |
| 70 | + {{/isEnum}} |
| 71 | + {{/vars}} |
| 72 | + |
54 | 73 | {{/hasEnums}} |
55 | 74 | {{#anyOf}} |
56 | 75 | {{#-first}} |
|
89 | 108 | {{/discriminator}} |
90 | 109 | # Initializes the object |
91 | 110 | # @param [Hash] attributes Model attributes in the form of hash |
92 | | - def initialize({{#requiredVars}}@{{{name}}} : {{{dataType}}}{{^-last}}, {{/-last}}{{/requiredVars}}{{#hasRequired}}{{#hasOptional}}, {{/hasOptional}}{{/hasRequired}}{{#optionalVars}}@{{{name}}} : {{{dataType}}}?{{^-last}}, {{/-last}}{{/optionalVars}}) |
| 111 | + def initialize({{#requiredVars}}@{{{name}}} : {{{dataType}}}{{^-last}}, {{/-last}}{{/requiredVars}}{{#hasRequired}}{{#hasOptional}}, {{/hasOptional}}{{/hasRequired}}{{#optionalVars}}@{{{name}}} : {{{dataType}}}? = nil{{^-last}}, {{/-last}}{{/optionalVars}}) |
93 | 112 | end |
94 | 113 |
|
95 | 114 | # Show invalid properties with the reasons. Usually used together with valid? |
96 | 115 | # @return Array for valid properties with the reasons |
97 | 116 | def list_invalid_properties |
98 | 117 | invalid_properties = {{^parent}}Array(String).new{{/parent}}{{#parent}}super{{/parent}} |
99 | 118 | {{#vars}} |
| 119 | + {{#isEnum}} |
| 120 | + {{^isContainer}} |
| 121 | + {{{name}}}_validator = EnumAttributeValidatorFor{{#lambdaTitlecase}}{{{name}}}{{/lambdaTitlecase}}.new |
| 122 | + if !{{{name}}}_validator.valid?(@{{{name}}}) |
| 123 | + message = {{{name}}}_validator.message |
| 124 | + invalid_properties.push(message) |
| 125 | + end |
| 126 | + |
| 127 | + {{/isContainer}} |
| 128 | + {{/isEnum}} |
100 | 129 | {{#hasValidation}} |
101 | 130 | {{#maxLength}} |
102 | | - if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.size > {{{maxLength}}} |
| 131 | + if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.try &.to_s.try &.size.try &.> {{{maxLength}}} |
103 | 132 | invalid_properties.push("invalid value for \"{{{name}}}\", the character length must be smaller than or equal to {{{maxLength}}}.") |
104 | 133 | end |
105 | 134 |
|
106 | 135 | {{/maxLength}} |
107 | 136 | {{#minLength}} |
108 | | - if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.size < {{{minLength}}} |
| 137 | + if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.try &.to_s.try &.size.try &.< {{{minLength}}} |
109 | 138 | invalid_properties.push("invalid value for \"{{{name}}}\", the character length must be greater than or equal to {{{minLength}}}.") |
110 | 139 | end |
111 | 140 |
|
112 | 141 | {{/minLength}} |
113 | 142 | {{#maximum}} |
114 | | - if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{{maximum}}} |
| 143 | + if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.try &.>{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{{maximum}}} |
115 | 144 | invalid_properties.push("invalid value for \"{{{name}}}\", must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{{maximum}}}.") |
116 | 145 | end |
117 | 146 |
|
118 | 147 | {{/maximum}} |
119 | 148 | {{#minimum}} |
120 | | - if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{{minimum}}} |
| 149 | + if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.try &.<{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{{minimum}}} |
121 | 150 | invalid_properties.push("invalid value for \"{{{name}}}\", must be greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{{minimum}}}.") |
122 | 151 | end |
123 | 152 |
|
124 | 153 | {{/minimum}} |
125 | 154 | {{#pattern}} |
126 | | - pattern = Regexp.new({{{pattern}}}) |
127 | | - if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} !~ pattern |
| 155 | + pattern = {{{pattern}}} |
| 156 | + if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.try &.!~ pattern |
128 | 157 | invalid_properties.push("invalid value for \"{{{name}}}\", must conform to the pattern #{pattern}.") |
129 | 158 | end |
130 | 159 |
|
131 | 160 | {{/pattern}} |
132 | 161 | {{#maxItems}} |
133 | | - if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.size > {{{maxItems}}} |
| 162 | + if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.try &.size.try &.> {{{maxItems}}} |
134 | 163 | invalid_properties.push("invalid value for \"{{{name}}}\", number of items must be less than or equal to {{{maxItems}}}." |
135 | 164 | end |
136 | 165 |
|
137 | 166 | {{/maxItems}} |
138 | 167 | {{#minItems}} |
139 | | - if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.size < {{{minItems}}} |
| 168 | + if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.try &.size.try &.< {{{minItems}}} |
140 | 169 | invalid_properties.push("invalid value for \"{{{name}}}\", number of items must be greater than or equal to {{{minItems}}}." |
141 | 170 | end |
142 | 171 |
|
|
152 | 181 | {{#vars}} |
153 | 182 | {{#isEnum}} |
154 | 183 | {{^isContainer}} |
155 | | - {{{name}}}_validator = EnumAttributeValidator.new("{{{dataType}}}", [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}]) |
| 184 | + {{{name}}}_validator = EnumAttributeValidatorFor{{#lambdaTitlecase}}{{{name}}}{{/lambdaTitlecase}}.new |
156 | 185 | return false unless {{{name}}}_validator.valid?(@{{{name}}}) |
157 | 186 | {{/isContainer}} |
158 | 187 | {{/isEnum}} |
159 | 188 | {{#hasValidation}} |
160 | 189 | {{#maxLength}} |
161 | | - return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.size > {{{maxLength}}} |
| 190 | + return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.try &.to_s.try &.size.try &.> {{{maxLength}}} |
162 | 191 | {{/maxLength}} |
163 | 192 | {{#minLength}} |
164 | | - return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.size < {{{minLength}}} |
| 193 | + return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.try &.to_s.try &.size.try &.< {{{minLength}}} |
165 | 194 | {{/minLength}} |
166 | 195 | {{#maximum}} |
167 | | - return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{{maximum}}} |
| 196 | + return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.try &.>{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{{maximum}}} |
168 | 197 | {{/maximum}} |
169 | 198 | {{#minimum}} |
170 | | - return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{{minimum}}} |
| 199 | + return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.try &.<{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{{minimum}}} |
171 | 200 | {{/minimum}} |
172 | 201 | {{#pattern}} |
173 | | - return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} !~ Regexp.new({{{pattern}}}) |
| 202 | + return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.try &.!~ {{{pattern}}} |
174 | 203 | {{/pattern}} |
175 | 204 | {{#maxItems}} |
176 | | - return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.size > {{{maxItems}}} |
| 205 | + return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.try &.size.try &.> {{{maxItems}}} |
177 | 206 | {{/maxItems}} |
178 | 207 | {{#minItems}} |
179 | | - return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.size < {{{minItems}}} |
| 208 | + return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.try &.size.try &.< {{{minItems}}} |
180 | 209 | {{/minItems}} |
181 | 210 | {{/hasValidation}} |
182 | 211 | {{/vars}} |
183 | 212 | {{#anyOf}} |
184 | 213 | {{#-first}} |
185 | 214 | _any_of_found = false |
186 | 215 | self.class.openapi_any_of.each do |_class| |
187 | | - _any_of = {{moduleName}}.const_get(_class).build_from_hash(self.to_hash) |
| 216 | + _any_of = {{moduleName}}.const_get(_class).build_from_hash(self.to_h) |
188 | 217 | if _any_of.valid? |
189 | 218 | _any_of_found = true |
190 | 219 | end |
|
205 | 234 | # Custom attribute writer method checking allowed values (enum). |
206 | 235 | # @param [Object] {{{name}}} Object to be assigned |
207 | 236 | def {{{name}}}=({{{name}}}) |
208 | | - validator = EnumAttributeValidator.new("{{{dataType}}}", [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}]) |
| 237 | + validator = EnumAttributeValidatorFor{{#lambdaTitlecase}}{{{name}}}{{/lambdaTitlecase}}.new |
209 | 238 | unless validator.valid?({{{name}}}) |
210 | | - raise ArgumentError.new("invalid value for \"{{{name}}}\", must be one of #{validator.allowable_values}.") |
| 239 | + raise ArgumentError.new(validator.message) |
211 | 240 | end |
212 | 241 | @{{{name}}} = {{{name}}} |
213 | 242 | end |
|
244 | 273 |
|
245 | 274 | {{/minimum}} |
246 | 275 | {{#pattern}} |
247 | | - pattern = Regexp.new({{{pattern}}}) |
| 276 | + pattern = {{{pattern}}} |
248 | 277 | if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}} !~ pattern |
249 | 278 | raise ArgumentError.new("invalid value for \"{{{name}}}\", must conform to the pattern #{pattern}.") |
250 | 279 | end |
|
0 commit comments