Skip to content

Commit 09096f4

Browse files
authored
Adds constant builder (#103)
## Description Adds a `.constant(JSONValue)` modifier ```swift @JSONSchemaBuilder var sample: some JSONSchemaComponent { JSONString() .constant("red") } ``` ## Type of Change - [ ] Bug fix - [x] New feature - [ ] Breaking change - [ ] Documentation update ## Additional Notes Add any other context or screenshots about the pull request here.
1 parent fccff85 commit 09096f4

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import JSONSchema
2+
3+
extension JSONSchemaComponent {
4+
public func constant(_ value: JSONValue) -> Self {
5+
var copy = self
6+
copy.schemaValue[Keywords.Constant.name] = value
7+
return copy
8+
}
9+
}

Tests/JSONSchemaBuilderTests/JSONEnumTests.swift renamed to Tests/JSONSchemaBuilderTests/JSONModifierTests.swift

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import JSONSchema
2+
import JSONSchemaBuilder
23
import Testing
34

4-
@testable import JSONSchemaBuilder
5-
65
struct JSONEnumTests {
76
@Test func singleValue() {
87
@JSONSchemaBuilder var sample: some JSONSchemaComponent {
@@ -75,3 +74,32 @@ struct JSONEnumTests {
7574
#expect(sample.schemaValue == .object(expected))
7675
}
7776
}
77+
78+
struct JSONConstantTests {
79+
@Test func constantOnly() {
80+
@JSONSchemaBuilder var sample: some JSONSchemaComponent {
81+
JSONAnyValue()
82+
.constant("red")
83+
}
84+
85+
let expected: [String: JSONValue] = [
86+
"const": "red"
87+
]
88+
89+
#expect(sample.schemaValue == .object(expected))
90+
}
91+
92+
@Test func string() {
93+
@JSONSchemaBuilder var sample: some JSONSchemaComponent {
94+
JSONString()
95+
.constant("red")
96+
}
97+
98+
let expected: [String: JSONValue] = [
99+
"type": "string",
100+
"const": "red",
101+
]
102+
103+
#expect(sample.schemaValue == .object(expected))
104+
}
105+
}

0 commit comments

Comments
 (0)