Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core/src/main/java/org/everit/json/schema/ConstSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.everit.json.schema.EnumSchema.toJavaValue;

import org.everit.json.schema.internal.JSONPrinter;

public class ConstSchema extends Schema {

public static class ConstSchemaBuilder extends Schema.Builder<ConstSchema> {
Expand Down Expand Up @@ -29,6 +31,11 @@ protected ConstSchema(ConstSchemaBuilder builder) {
this.permittedValue = toJavaValue(builder.permittedValue);
}

@Override void describePropertiesTo(JSONPrinter writer) {
writer.key("const");
writer.value(this.permittedValue);
}

@Override void accept(Visitor visitor) {
visitor.visitConstSchema(this);
}
Expand Down
22 changes: 22 additions & 0 deletions core/src/test/java/org/everit/json/schema/ConstSchemaTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.everit.json.schema;

import static org.everit.json.schema.TestSupport.loadAsV6;
import static org.junit.Assert.assertEquals;

import org.json.JSONArray;
import org.json.JSONObject;
Expand Down Expand Up @@ -56,6 +57,27 @@ public void successWithObject() {
testSuccess(new JSONObject("{\"a\":\"b\", \"b\":\"a\"}"));
}

@Test
public void toStringTest() {
ConstSchema subject = ConstSchema.builder().permittedValue(true).build();
String actual = subject.toString();
assertEquals("{\"const\":true}", actual);
}

@Test
public void toStringWithNull() {
ConstSchema subject = ConstSchema.builder().permittedValue(null).build();
String actual = subject.toString();
assertEquals("{\"const\":null}", actual);
}

@Test
public void toStringWithObject() {
ConstSchema subject = ConstSchema.builder().permittedValue(new JSONObject("{\"a\":2}")).build();
String actual = subject.toString();
assertEquals("{\"const\":{\"a\":2}}", actual);
}

@Test
public void failureWithObject() {
testFailure(new JSONObject("{}"), new JSONObject("{\"a\":null}"));
Expand Down