Skip to content

Commit b3526fd

Browse files
committed
fix(crystal): update samples
1 parent 8e4b0f2 commit b3526fd

File tree

4 files changed

+118
-80
lines changed

4 files changed

+118
-80
lines changed

samples/client/petstore/crystal/src/petstore/models/category.cr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ module Petstore
3535
# @return Array for valid properties with the reasons
3636
def list_invalid_properties
3737
invalid_properties = Array(String).new
38-
pattern = Regexp.new(/^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$/)
39-
if !@name.nil? && @name !~ pattern
38+
pattern = /^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$/
39+
if !@name.nil? && @name.try &.!~ pattern
4040
invalid_properties.push("invalid value for \"name\", must conform to the pattern #{pattern}.")
4141
end
4242

@@ -46,14 +46,14 @@ module Petstore
4646
# Check to see if the all the properties in the model are valid
4747
# @return true if the model is valid
4848
def valid?
49-
return false if !@name.nil? && @name !~ Regexp.new(/^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$/)
49+
return false if !@name.nil? && @name.try &.!~ /^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$/
5050
true
5151
end
5252

5353
# Custom attribute writer method with validation
5454
# @param [Object] name Value to be assigned
5555
def name=(name)
56-
pattern = Regexp.new(/^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$/)
56+
pattern = /^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$/
5757
if !name.nil? && name !~ pattern
5858
raise ArgumentError.new("invalid value for \"name\", must conform to the pattern #{pattern}.")
5959
end

samples/client/petstore/crystal/src/petstore/models/format_test.cr

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -79,66 +79,66 @@ module Petstore
7979
# @return Array for valid properties with the reasons
8080
def list_invalid_properties
8181
invalid_properties = Array(String).new
82-
if !@integer.nil? && @integer > 100
82+
if !@integer.nil? && @integer.try &.> 100
8383
invalid_properties.push("invalid value for \"integer\", must be smaller than or equal to 100.")
8484
end
8585

86-
if !@integer.nil? && @integer < 10
86+
if !@integer.nil? && @integer.try &.< 10
8787
invalid_properties.push("invalid value for \"integer\", must be greater than or equal to 10.")
8888
end
8989

90-
if !@int32.nil? && @int32 > 200
90+
if !@int32.nil? && @int32.try &.> 200
9191
invalid_properties.push("invalid value for \"int32\", must be smaller than or equal to 200.")
9292
end
9393

94-
if !@int32.nil? && @int32 < 20
94+
if !@int32.nil? && @int32.try &.< 20
9595
invalid_properties.push("invalid value for \"int32\", must be greater than or equal to 20.")
9696
end
9797

98-
if @number > 543.2
98+
if @number.try &.> 543.2
9999
invalid_properties.push("invalid value for \"number\", must be smaller than or equal to 543.2.")
100100
end
101101

102-
if @number < 32.1
102+
if @number.try &.< 32.1
103103
invalid_properties.push("invalid value for \"number\", must be greater than or equal to 32.1.")
104104
end
105105

106-
if !@float.nil? && @float > 987.6
106+
if !@float.nil? && @float.try &.> 987.6
107107
invalid_properties.push("invalid value for \"float\", must be smaller than or equal to 987.6.")
108108
end
109109

110-
if !@float.nil? && @float < 54.3
110+
if !@float.nil? && @float.try &.< 54.3
111111
invalid_properties.push("invalid value for \"float\", must be greater than or equal to 54.3.")
112112
end
113113

114-
if !@double.nil? && @double > 123.4
114+
if !@double.nil? && @double.try &.> 123.4
115115
invalid_properties.push("invalid value for \"double\", must be smaller than or equal to 123.4.")
116116
end
117117

118-
if !@double.nil? && @double < 67.8
118+
if !@double.nil? && @double.try &.< 67.8
119119
invalid_properties.push("invalid value for \"double\", must be greater than or equal to 67.8.")
120120
end
121121

122-
pattern = Regexp.new(/[a-z]/i)
123-
if !@string.nil? && @string !~ pattern
122+
pattern = /[a-z]/i
123+
if !@string.nil? && @string.try &.!~ pattern
124124
invalid_properties.push("invalid value for \"string\", must conform to the pattern #{pattern}.")
125125
end
126126

127-
if @password.to_s.size > 64
127+
if @password.try &.to_s.try &.size.try &.> 64
128128
invalid_properties.push("invalid value for \"password\", the character length must be smaller than or equal to 64.")
129129
end
130130

131-
if @password.to_s.size < 10
131+
if @password.try &.to_s.try &.size.try &.< 10
132132
invalid_properties.push("invalid value for \"password\", the character length must be greater than or equal to 10.")
133133
end
134134

135-
pattern = Regexp.new(/^\d{10}$/)
136-
if !@pattern_with_digits.nil? && @pattern_with_digits !~ pattern
135+
pattern = /^\d{10}$/
136+
if !@pattern_with_digits.nil? && @pattern_with_digits.try &.!~ pattern
137137
invalid_properties.push("invalid value for \"pattern_with_digits\", must conform to the pattern #{pattern}.")
138138
end
139139

140-
pattern = Regexp.new(/^image_\d{1,3}$/i)
141-
if !@pattern_with_digits_and_delimiter.nil? && @pattern_with_digits_and_delimiter !~ pattern
140+
pattern = /^image_\d{1,3}$/i
141+
if !@pattern_with_digits_and_delimiter.nil? && @pattern_with_digits_and_delimiter.try &.!~ pattern
142142
invalid_properties.push("invalid value for \"pattern_with_digits_and_delimiter\", must conform to the pattern #{pattern}.")
143143
end
144144

@@ -148,21 +148,21 @@ module Petstore
148148
# Check to see if the all the properties in the model are valid
149149
# @return true if the model is valid
150150
def valid?
151-
return false if !@integer.nil? && @integer > 100
152-
return false if !@integer.nil? && @integer < 10
153-
return false if !@int32.nil? && @int32 > 200
154-
return false if !@int32.nil? && @int32 < 20
155-
return false if @number > 543.2
156-
return false if @number < 32.1
157-
return false if !@float.nil? && @float > 987.6
158-
return false if !@float.nil? && @float < 54.3
159-
return false if !@double.nil? && @double > 123.4
160-
return false if !@double.nil? && @double < 67.8
161-
return false if !@string.nil? && @string !~ Regexp.new(/[a-z]/i)
162-
return false if @password.to_s.size > 64
163-
return false if @password.to_s.size < 10
164-
return false if !@pattern_with_digits.nil? && @pattern_with_digits !~ Regexp.new(/^\d{10}$/)
165-
return false if !@pattern_with_digits_and_delimiter.nil? && @pattern_with_digits_and_delimiter !~ Regexp.new(/^image_\d{1,3}$/i)
151+
return false if !@integer.nil? && @integer.try &.> 100
152+
return false if !@integer.nil? && @integer.try &.< 10
153+
return false if !@int32.nil? && @int32.try &.> 200
154+
return false if !@int32.nil? && @int32.try &.< 20
155+
return false if @number.try &.> 543.2
156+
return false if @number.try &.< 32.1
157+
return false if !@float.nil? && @float.try &.> 987.6
158+
return false if !@float.nil? && @float.try &.< 54.3
159+
return false if !@double.nil? && @double.try &.> 123.4
160+
return false if !@double.nil? && @double.try &.< 67.8
161+
return false if !@string.nil? && @string.try &.!~ /[a-z]/i
162+
return false if @password.try &.to_s.try &.size.try &.> 64
163+
return false if @password.try &.to_s.try &.size.try &.< 10
164+
return false if !@pattern_with_digits.nil? && @pattern_with_digits.try &.!~ /^\d{10}$/
165+
return false if !@pattern_with_digits_and_delimiter.nil? && @pattern_with_digits_and_delimiter.try &.!~ /^image_\d{1,3}$/i
166166
true
167167
end
168168

@@ -239,7 +239,7 @@ module Petstore
239239
# Custom attribute writer method with validation
240240
# @param [Object] string Value to be assigned
241241
def string=(string)
242-
pattern = Regexp.new(/[a-z]/i)
242+
pattern = /[a-z]/i
243243
if !string.nil? && string !~ pattern
244244
raise ArgumentError.new("invalid value for \"string\", must conform to the pattern #{pattern}.")
245245
end
@@ -264,7 +264,7 @@ module Petstore
264264
# Custom attribute writer method with validation
265265
# @param [Object] pattern_with_digits Value to be assigned
266266
def pattern_with_digits=(pattern_with_digits)
267-
pattern = Regexp.new(/^\d{10}$/)
267+
pattern = /^\d{10}$/
268268
if !pattern_with_digits.nil? && pattern_with_digits !~ pattern
269269
raise ArgumentError.new("invalid value for \"pattern_with_digits\", must conform to the pattern #{pattern}.")
270270
end
@@ -275,7 +275,7 @@ module Petstore
275275
# Custom attribute writer method with validation
276276
# @param [Object] pattern_with_digits_and_delimiter Value to be assigned
277277
def pattern_with_digits_and_delimiter=(pattern_with_digits_and_delimiter)
278-
pattern = Regexp.new(/^image_\d{1,3}$/i)
278+
pattern = /^image_\d{1,3}$/i
279279
if !pattern_with_digits_and_delimiter.nil? && pattern_with_digits_and_delimiter !~ pattern
280280
raise ArgumentError.new("invalid value for \"pattern_with_digits_and_delimiter\", must conform to the pattern #{pattern}.")
281281
end

samples/client/petstore/crystal/src/petstore/models/order.cr

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,42 @@ module Petstore
3939
@[JSON::Field(key: "complete", type: Bool?, default: false, nillable: true, emit_null: false)]
4040
property complete : Bool?
4141

42-
class EnumAttributeValidator
43-
getter datatype : String
44-
getter allowable_values : Array(String)
45-
46-
def initialize(datatype, allowable_values)
47-
@datatype = datatype
48-
@allowable_values = allowable_values.map do |value|
49-
case datatype.to_s
50-
when /Integer/i
51-
value.to_i
52-
when /Float/i
53-
value.to_f
54-
else
55-
value
56-
end
42+
abstract class EnumAttributeValidator
43+
def valid?(value)
44+
!value || @allowable_values.includes?(value)
45+
end
46+
47+
def message
48+
"invalid value for \"#{@attribute}\", must be one of #{@allowable_values}."
49+
end
50+
51+
def to(_type, value)
52+
case _type
53+
when Int32
54+
value.to_i32
55+
when Int64
56+
value.to_i64
57+
when Float32
58+
value.to_f32
59+
when Float64
60+
value.to_f64
61+
else
62+
value.to_s
5763
end
5864
end
65+
end
5966

60-
def valid?(value)
61-
!value || allowable_values.includes?(value)
67+
class EnumAttributeValidatorForStatus < EnumAttributeValidator
68+
@attribute : String
69+
@allowable_values : Array(Int32 | Int64 | Float32 | Float64 | String)
70+
71+
def initialize
72+
@attribute = "status"
73+
@allowable_values = ["placed", "approved", "delivered"].map { |value| to(String, value)}
6274
end
6375
end
6476

77+
6578
# Initializes the object
6679
# @param [Hash] attributes Model attributes in the form of hash
6780
def initialize(@id : Int64? = nil, @pet_id : Int64? = nil, @quantity : Int32? = nil, @ship_date : Time? = nil, @status : String? = nil, @complete : Bool? = nil)
@@ -71,23 +84,29 @@ module Petstore
7184
# @return Array for valid properties with the reasons
7285
def list_invalid_properties
7386
invalid_properties = Array(String).new
87+
status_validator = EnumAttributeValidatorForStatus.new
88+
if !status_validator.valid?(@status)
89+
message = status_validator.message
90+
invalid_properties.push(message)
91+
end
92+
7493
invalid_properties
7594
end
7695

7796
# Check to see if the all the properties in the model are valid
7897
# @return true if the model is valid
7998
def valid?
80-
status_validator = EnumAttributeValidator.new("String", ["placed", "approved", "delivered"])
99+
status_validator = EnumAttributeValidatorForStatus.new
81100
return false unless status_validator.valid?(@status)
82101
true
83102
end
84103

85104
# Custom attribute writer method checking allowed values (enum).
86105
# @param [Object] status Object to be assigned
87106
def status=(status)
88-
validator = EnumAttributeValidator.new("String", ["placed", "approved", "delivered"])
107+
validator = EnumAttributeValidatorForStatus.new
89108
unless validator.valid?(status)
90-
raise ArgumentError.new("invalid value for \"status\", must be one of #{validator.allowable_values}.")
109+
raise ArgumentError.new(validator.message)
91110
end
92111
@status = status
93112
end

samples/client/petstore/crystal/src/petstore/models/pet.cr

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,42 @@ module Petstore
4040
@[JSON::Field(key: "status", type: String?, nillable: true, emit_null: false)]
4141
property status : String?
4242

43-
class EnumAttributeValidator
44-
getter datatype : String
45-
getter allowable_values : Array(String)
46-
47-
def initialize(datatype, allowable_values)
48-
@datatype = datatype
49-
@allowable_values = allowable_values.map do |value|
50-
case datatype.to_s
51-
when /Integer/i
52-
value.to_i
53-
when /Float/i
54-
value.to_f
55-
else
56-
value
57-
end
43+
abstract class EnumAttributeValidator
44+
def valid?(value)
45+
!value || @allowable_values.includes?(value)
46+
end
47+
48+
def message
49+
"invalid value for \"#{@attribute}\", must be one of #{@allowable_values}."
50+
end
51+
52+
def to(_type, value)
53+
case _type
54+
when Int32
55+
value.to_i32
56+
when Int64
57+
value.to_i64
58+
when Float32
59+
value.to_f32
60+
when Float64
61+
value.to_f64
62+
else
63+
value.to_s
5864
end
5965
end
66+
end
6067

61-
def valid?(value)
62-
!value || allowable_values.includes?(value)
68+
class EnumAttributeValidatorForStatus < EnumAttributeValidator
69+
@attribute : String
70+
@allowable_values : Array(Int32 | Int64 | Float32 | Float64 | String)
71+
72+
def initialize
73+
@attribute = "status"
74+
@allowable_values = ["available", "pending", "sold"].map { |value| to(String, value)}
6375
end
6476
end
6577

78+
6679
# Initializes the object
6780
# @param [Hash] attributes Model attributes in the form of hash
6881
def initialize(@name : String, @photo_urls : Array(String), @id : Int64? = nil, @category : Category? = nil, @tags : Array(Tag)? = nil, @status : String? = nil)
@@ -72,23 +85,29 @@ module Petstore
7285
# @return Array for valid properties with the reasons
7386
def list_invalid_properties
7487
invalid_properties = Array(String).new
88+
status_validator = EnumAttributeValidatorForStatus.new
89+
if !status_validator.valid?(@status)
90+
message = status_validator.message
91+
invalid_properties.push(message)
92+
end
93+
7594
invalid_properties
7695
end
7796

7897
# Check to see if the all the properties in the model are valid
7998
# @return true if the model is valid
8099
def valid?
81-
status_validator = EnumAttributeValidator.new("String", ["available", "pending", "sold"])
100+
status_validator = EnumAttributeValidatorForStatus.new
82101
return false unless status_validator.valid?(@status)
83102
true
84103
end
85104

86105
# Custom attribute writer method checking allowed values (enum).
87106
# @param [Object] status Object to be assigned
88107
def status=(status)
89-
validator = EnumAttributeValidator.new("String", ["available", "pending", "sold"])
108+
validator = EnumAttributeValidatorForStatus.new
90109
unless validator.valid?(status)
91-
raise ArgumentError.new("invalid value for \"status\", must be one of #{validator.allowable_values}.")
110+
raise ArgumentError.new(validator.message)
92111
end
93112
@status = status
94113
end

0 commit comments

Comments
 (0)