Skip to content

Commit 52cfa4c

Browse files
committed
initial implementation of StringSchema#describePropertiesTo()
1 parent c6a0e22 commit 52cfa4c

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

core/src/main/java/org/everit/json/schema/StringSchema.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.everit.json.schema;
1717

18+
import org.everit.json.schema.internal.JSONPrinter;
19+
1820
import java.util.*;
1921
import java.util.regex.Pattern;
2022

@@ -203,4 +205,14 @@ public int hashCode() {
203205
protected boolean canEqual(Object other) {
204206
return other instanceof StringSchema;
205207
}
208+
209+
@Override
210+
void describePropertiesTo(JSONPrinter writer) {
211+
if (requiresString) {
212+
writer.key("type").value("string");
213+
}
214+
writer.ifPresent("minLength", minLength);
215+
writer.ifPresent("maxLength", maxLength);
216+
writer.ifPresent("pattern", pattern);
217+
}
206218
}

core/src/test/java/org/everit/json/schema/StringSchemaTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@
1717

1818
import nl.jqno.equalsverifier.EqualsVerifier;
1919
import nl.jqno.equalsverifier.Warning;
20+
import org.everit.json.schema.loader.SchemaLoader;
21+
import org.json.JSONObject;
2022
import org.junit.Assert;
2123
import org.junit.Test;
2224

2325
import java.util.Optional;
2426

27+
import static org.junit.Assert.assertTrue;
28+
2529
public class StringSchemaTest {
2630

2731
@Test
@@ -110,4 +114,19 @@ public void equalsVerifier() {
110114
.suppress(Warning.STRICT_INHERITANCE)
111115
.verify();
112116
}
117+
118+
@Test
119+
public void toStringTest() {
120+
JSONObject rawSchemaJson = ResourceLoader.DEFAULT.readObj("tostring/stringschema.json");
121+
String actual = SchemaLoader.load(rawSchemaJson).toString();
122+
assertTrue(ObjectComparator.deepEquals(rawSchemaJson, new JSONObject(actual)));
123+
}
124+
125+
@Test
126+
public void toStringNoExplicitType() {
127+
JSONObject rawSchemaJson = ResourceLoader.DEFAULT.readObj("tostring/stringschema.json");
128+
rawSchemaJson.remove("type");
129+
String actual = SchemaLoader.load(rawSchemaJson).toString();
130+
assertTrue(ObjectComparator.deepEquals(rawSchemaJson, new JSONObject(actual)));
131+
}
113132
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type" : "string",
3+
"minLength": 0,
4+
"maxLength": 10,
5+
"pattern": "a.b*c"
6+
}

0 commit comments

Comments
 (0)