Skip to content

Commit 7fb35c8

Browse files
committed
feat: adjust date time property suffix rule (#1536)
Signed-off-by: Tronje Krop <[email protected]>
1 parent 301b550 commit 7fb35c8

File tree

3 files changed

+43
-57
lines changed

3 files changed

+43
-57
lines changed

server/zally-ruleset-zalando/src/main/kotlin/org/zalando/zally/ruleset/zalando/DateTimePropertiesSuffixRule.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import org.zalando.zally.rule.api.Violation
1313
ruleSet = ZalandoRuleSet::class,
1414
id = "235",
1515
severity = Severity.SHOULD,
16-
title = "Name date/time properties using the \"_at\" suffix"
16+
title = "Name date/time properties using the common suffix"
1717
)
1818
class DateTimePropertiesSuffixRule(rulesConfig: Config) {
1919

@@ -22,7 +22,7 @@ class DateTimePropertiesSuffixRule(rulesConfig: Config) {
2222
.map { Regex(it) }
2323
.toSet()
2424

25-
private val propertyFormats = setOf("date", "date-time")
25+
private val propertyFormats = setOf("date", "time", "date-time")
2626

2727
@Check(severity = Severity.SHOULD)
2828
fun validate(context: Context): List<Violation> {

server/zally-ruleset-zalando/src/main/resources/reference.conf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,11 @@ ProprietaryHeadersRule {
216216

217217
DateTimePropertiesSuffixRule {
218218
patterns: [
219-
".*_at"
219+
".*_at$",
220+
"(.*_)*time(_.*)*",
221+
"(.*_)*timestamp(_.*)*",
222+
"(.*_)*date(_.*)*",
223+
"(.*_)*day(_.*)*",
220224
]
221225
}
222226

server/zally-ruleset-zalando/src/test/kotlin/org/zalando/zally/ruleset/zalando/DateTimePropertiesSuffixRuleTest.kt

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
package org.zalando.zally.ruleset.zalando
44

5-
import com.typesafe.config.ConfigValueFactory
65
import org.assertj.core.api.Assertions.assertThat
76
import org.intellij.lang.annotations.Language
87
import org.junit.jupiter.api.Test
@@ -14,7 +13,7 @@ class DateTimePropertiesSuffixRuleTest {
1413
private val rule = DateTimePropertiesSuffixRule(rulesConfig)
1514

1615
@Test
17-
fun `rule should pass with correct 'date-time' fields`() {
16+
fun `should pass with correct format and suffix`() {
1817
@Language("YAML")
1918
val content = """
2019
openapi: '3.0.1'
@@ -28,44 +27,18 @@ class DateTimePropertiesSuffixRuleTest {
2827
created_at:
2928
type: string
3029
format: date-time
31-
modified_at:
30+
modified_time:
3231
type: string
33-
format: date-time
34-
occurred_at:
32+
format: time
33+
occurred_timestamp:
3534
type: string
3635
format: date-time
37-
returned_at:
38-
type: string
39-
format: date-time
40-
""".trimIndent()
41-
val violations = rule.validate(DefaultContextFactory().getOpenApiContext(content))
42-
assertThat(violations).isEmpty()
43-
}
44-
45-
@Test
46-
fun `rule should pass with correct 'date' fields`() {
47-
@Language("YAML")
48-
val content = """
49-
openapi: '3.0.1'
50-
info:
51-
title: Test API
52-
version: 1.0.0
53-
components:
54-
schemas:
55-
Car:
56-
properties:
57-
created_at:
36+
returned_date:
5837
type: string
5938
format: date
60-
modified_at:
61-
type: string
62-
format: date
63-
occurred_at:
39+
delivered_day:
6440
type: string
65-
format: date
66-
returned_at:
67-
type: string
68-
format: date
41+
format: date
6942
""".trimIndent()
7043
val violations = rule.validate(DefaultContextFactory().getOpenApiContext(content))
7144
assertThat(violations).isEmpty()
@@ -84,20 +57,23 @@ class DateTimePropertiesSuffixRuleTest {
8457
Car:
8558
properties:
8659
created:
87-
type: string
60+
type: string
61+
modified:
62+
type: enum
63+
enum: [ "yes", "no" ]
8864
occurred:
8965
type: string
9066
returned:
9167
type: string
92-
modified:
68+
delivered:
9369
type: int
9470
""".trimIndent()
9571
val violations = rule.validate(DefaultContextFactory().getOpenApiContext(content))
9672
assertThat(violations).isEmpty()
9773
}
9874

9975
@Test
100-
fun `rule should fail to validate schema`() {
76+
fun `should fail on suffix _at and succeed on infix patterns`() {
10177
@Language("YAML")
10278
val content = """
10379
openapi: '3.0.1'
@@ -108,30 +84,30 @@ class DateTimePropertiesSuffixRuleTest {
10884
schemas:
10985
Car:
11086
properties:
111-
created:
87+
created_at_suffix:
11288
type: string
11389
format: date-time
114-
occurred:
90+
modified_time_suffix:
11591
type: string
116-
format: date
117-
returned:
92+
format: time
93+
occurred_timestamp_suffix:
11894
type: string
11995
format: date-time
120-
modified:
96+
returned_date_suffix:
97+
type: string
98+
format: date
99+
delivered_day_suffix:
121100
type: string
122101
format: date
123102
""".trimIndent()
124103
val violations = rule.validate(DefaultContextFactory().getOpenApiContext(content))
125104
assertThat(violations.map { it.description }).containsExactly(
126-
rule.generateMessage("created", "string", "date-time"),
127-
rule.generateMessage("occurred", "string", "date"),
128-
rule.generateMessage("returned", "string", "date-time"),
129-
rule.generateMessage("modified", "string", "date")
105+
rule.generateMessage("created_at_suffix", "string", "date-time")
130106
)
131107
}
132108

133109
@Test
134-
fun `rule should support different patterns`() {
110+
fun `should fail on prefix _at and succeed on prefix patterns`() {
135111
@Language("YAML")
136112
val content = """
137113
openapi: '3.0.1'
@@ -142,19 +118,25 @@ class DateTimePropertiesSuffixRuleTest {
142118
schemas:
143119
Car:
144120
properties:
145-
created:
121+
at_created:
146122
type: string
147123
format: date-time
148-
modified:
124+
time_modified:
125+
type: string
126+
format: time
127+
timestamp_occurred:
128+
type: string
129+
format: date-time
130+
date_returned:
131+
type: string
132+
format: date
133+
day_delivered:
149134
type: string
150135
format: date
151136
""".trimIndent()
152-
val newConfig = rulesConfig.withValue("DateTimePropertiesSuffixRule/patterns", ConfigValueFactory.fromIterable(listOf("was_.*")))
153-
val customRule = DateTimePropertiesSuffixRule(newConfig)
154-
val violations = customRule.validate(DefaultContextFactory().getOpenApiContext(content))
137+
val violations = rule.validate(DefaultContextFactory().getOpenApiContext(content))
155138
assertThat(violations.map { it.description }).containsExactly(
156-
customRule.generateMessage("created", "string", "date-time"),
157-
customRule.generateMessage("modified", "string", "date")
139+
rule.generateMessage("at_created", "string", "date-time")
158140
)
159141
}
160142
}

0 commit comments

Comments
 (0)