Skip to content

Commit 8e5b3ef

Browse files
committed
Extracted all limits out to their own file
Right now all limit attributes are defined as nested classes of `Limit`. We've already done this for `Format` classes. Moving them out to their own classes reduces bulk and makes it easier to find the class you need.
1 parent 57639a1 commit 8e5b3ef

File tree

16 files changed

+206
-127
lines changed

16 files changed

+206
-127
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55

66
## [Unreleased]
77

8+
### Changed
9+
- All limit classes are now stored in their own files in 'json-schema/attributes/limits'
10+
811
### Fixed
912
- Corrected the draft6 schema id to `http://json-schema.org/draft/schema#`
1013
- Rescue URI error when initializing a data string that contains a colon

lib/json-schema/attributes/limit.rb

Lines changed: 0 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -48,132 +48,5 @@ def self.limit_name
4848
raise NotImplementedError
4949
end
5050
end
51-
52-
class MinLengthAttribute < LimitAttribute
53-
def self.acceptable_type
54-
String
55-
end
56-
57-
def self.limit_name
58-
'minLength'
59-
end
60-
61-
def self.error_message(schema)
62-
"was not of a minimum string length of #{limit(schema)}"
63-
end
64-
65-
def self.value(data)
66-
data.length
67-
end
68-
end
69-
70-
class MaxLengthAttribute < MinLengthAttribute
71-
def self.limit_name
72-
'maxLength'
73-
end
74-
75-
def self.error_message(schema)
76-
"was not of a maximum string length of #{limit(schema)}"
77-
end
78-
end
79-
80-
class MinItemsAttribute < LimitAttribute
81-
def self.acceptable_type
82-
Array
83-
end
84-
85-
def self.value(data)
86-
data.length
87-
end
88-
89-
def self.limit_name
90-
'minItems'
91-
end
92-
93-
def self.error_message(schema)
94-
"did not contain a minimum number of items #{limit(schema)}"
95-
end
96-
end
97-
98-
class MaxItemsAttribute < MinItemsAttribute
99-
def self.limit_name
100-
'maxItems'
101-
end
102-
103-
def self.error_message(schema)
104-
"had more items than the allowed #{limit(schema)}"
105-
end
106-
end
107-
108-
class MinPropertiesAttribute < LimitAttribute
109-
def self.acceptable_type
110-
Hash
111-
end
112-
113-
def self.value(data)
114-
data.size
115-
end
116-
117-
def self.limit_name
118-
'minProperties'
119-
end
120-
121-
def self.error_message(schema)
122-
"did not contain a minimum number of properties #{limit(schema)}"
123-
end
124-
end
125-
126-
class MaxPropertiesAttribute < MinPropertiesAttribute
127-
def self.limit_name
128-
'maxProperties'
129-
end
130-
131-
def self.error_message(schema)
132-
"had more properties than the allowed #{limit(schema)}"
133-
end
134-
end
135-
136-
class NumericLimitAttribute < LimitAttribute
137-
def self.acceptable_type
138-
Numeric
139-
end
140-
141-
def self.error_message(schema)
142-
exclusivity = exclusive?(schema) ? 'exclusively' : 'inclusively'
143-
format("did not have a %s value of %s, %s", limit_name, limit(schema), exclusivity)
144-
end
145-
end
146-
147-
class MaximumAttribute < NumericLimitAttribute
148-
def self.limit_name
149-
'maximum'
150-
end
151-
152-
def self.exclusive?(schema)
153-
schema['exclusiveMaximum']
154-
end
155-
end
156-
157-
class MaximumInclusiveAttribute < MaximumAttribute
158-
def self.exclusive?(schema)
159-
schema['maximumCanEqual'] == false
160-
end
161-
end
162-
163-
class MinimumAttribute < NumericLimitAttribute
164-
def self.limit_name
165-
'minimum'
166-
end
167-
168-
def self.exclusive?(schema)
169-
schema['exclusiveMinimum']
170-
end
171-
end
172-
173-
class MinimumInclusiveAttribute < MinimumAttribute
174-
def self.exclusive?(schema)
175-
schema['minimumCanEqual'] == false
176-
end
177-
end
17851
end
17952
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'json-schema/attributes/limit'
2+
3+
module JSON
4+
class Schema
5+
class ItemsLimitAttribute < LimitAttribute
6+
def self.acceptable_type
7+
Array
8+
end
9+
10+
def self.value(data)
11+
data.length
12+
end
13+
end
14+
end
15+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'json-schema/attributes/limit'
2+
3+
module JSON
4+
class Schema
5+
class LengthLimitAttribute < LimitAttribute
6+
def self.acceptable_type
7+
String
8+
end
9+
10+
def self.value(data)
11+
data.length
12+
end
13+
end
14+
end
15+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'json-schema/attributes/limits/items'
2+
3+
module JSON
4+
class Schema
5+
class MaxItemsAttribute < ItemsLimitAttribute
6+
def self.limit_name
7+
'maxItems'
8+
end
9+
10+
def self.error_message(schema)
11+
"had more items than the allowed #{limit(schema)}"
12+
end
13+
end
14+
end
15+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'json-schema/attributes/limits/length'
2+
3+
module JSON
4+
class Schema
5+
class MaxLengthAttribute < LengthLimitAttribute
6+
def self.limit_name
7+
'maxLength'
8+
end
9+
10+
def self.error_message(schema)
11+
"was not of a maximum string length of #{limit(schema)}"
12+
end
13+
end
14+
end
15+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'json-schema/attributes/limits/properties'
2+
3+
module JSON
4+
class Schema
5+
class MaxPropertiesAttribute < PropertiesLimitAttribute
6+
def self.limit_name
7+
'maxProperties'
8+
end
9+
10+
def self.error_message(schema)
11+
"had more properties than the allowed #{limit(schema)}"
12+
end
13+
end
14+
end
15+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'json-schema/attributes/limits/numeric'
2+
3+
module JSON
4+
class Schema
5+
class MaximumAttribute < NumericLimitAttribute
6+
def self.limit_name
7+
'maximum'
8+
end
9+
10+
def self.exclusive?(schema)
11+
schema['exclusiveMaximum']
12+
end
13+
end
14+
end
15+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'json-schema/attributes/limits/maximum'
2+
3+
module JSON
4+
class Schema
5+
class MaximumInclusiveAttribute < MaximumAttribute
6+
def self.exclusive?(schema)
7+
schema['maximumCanEqual'] == false
8+
end
9+
end
10+
end
11+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'json-schema/attributes/limits/items'
2+
3+
module JSON
4+
class Schema
5+
class MinItemsAttribute < ItemsLimitAttribute
6+
def self.limit_name
7+
'minItems'
8+
end
9+
10+
def self.error_message(schema)
11+
"did not contain a minimum number of items #{limit(schema)}"
12+
end
13+
end
14+
end
15+
end

0 commit comments

Comments
 (0)