|
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}} |
|
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 | 131 | if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.try &.to_s.try &.size.try &.> {{{maxLength}}} |
|
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}} |
|
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 |
|
0 commit comments