Skip to content

Commit 1c83b62

Browse files
committed
adding FormatValidator interface with no-op and stub default implementation, added Format enum with forName() lookup method
1 parent c1f3fde commit 1c83b62

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.everit.json.schema;
2+
3+
import java.util.Arrays;
4+
5+
public enum Format {
6+
7+
DATE_TIME {
8+
9+
@Override
10+
public String toString() {
11+
return "date-time";
12+
}
13+
14+
},
15+
EMAIL {
16+
17+
@Override
18+
public String toString() {
19+
return "email";
20+
}
21+
22+
},
23+
HOSTNAME {
24+
25+
@Override
26+
public String toString() {
27+
return "hostname";
28+
}
29+
30+
},
31+
IPV4 {
32+
33+
@Override
34+
public String toString() {
35+
return "ipv4";
36+
}
37+
38+
},
39+
IPV6 {
40+
41+
@Override
42+
public String toString() {
43+
return "ipv6";
44+
}
45+
46+
},
47+
URI {
48+
49+
@Override
50+
public String toString() {
51+
return "uri";
52+
}
53+
54+
};
55+
56+
public static Format forName(final String name) {
57+
return Arrays.stream(Format.values())
58+
.filter(fmt -> fmt.toString().equals(name))
59+
.findFirst()
60+
.orElseThrow(() -> new IllegalArgumentException("format [" + name + "] is not supported"));
61+
}
62+
63+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.everit.json.schema;
2+
3+
import org.everit.json.schema.internal.DefaultFormatValidator;
4+
5+
@FunctionalInterface
6+
public interface FormatValidator {
7+
8+
/**
9+
* No-operation implementation (never throws {@link ValidationException}).
10+
*/
11+
public static final FormatValidator NONE = (subject, format) -> {
12+
};
13+
14+
public static FormatValidator DEFAULT = new DefaultFormatValidator();
15+
16+
void validate(String subject, Format format);
17+
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.everit.json.schema.internal;
2+
3+
import org.everit.json.schema.Format;
4+
import org.everit.json.schema.FormatValidator;
5+
6+
public class DefaultFormatValidator implements FormatValidator {
7+
8+
@Override
9+
public void validate(String subject, Format format) {
10+
// TODO Auto-generated method stub
11+
12+
}
13+
14+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.everit.json.schema;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.junit.runners.Parameterized;
10+
import org.junit.runners.Parameterized.Parameters;
11+
12+
@RunWith(Parameterized.class)
13+
public class FormatTest {
14+
15+
@Parameters
16+
public static List<Object[]> params() {
17+
return Arrays.asList(
18+
new Object[] { Format.DATE_TIME, "date-time" },
19+
new Object[] { Format.EMAIL, "email" },
20+
new Object[] { Format.HOSTNAME, "hostname" },
21+
new Object[] { Format.IPV6, "ipv6" },
22+
new Object[] { Format.IPV4, "ipv4" },
23+
new Object[] { Format.URI, "uri" }
24+
);
25+
}
26+
27+
private final Format format;
28+
29+
private final String formatName;
30+
31+
public FormatTest(final Format format, final String formatName) {
32+
this.format = format;
33+
this.formatName = formatName;
34+
}
35+
36+
@Test
37+
public void check() {
38+
Assert.assertEquals(format, Format.forName(formatName));
39+
}
40+
41+
@Test(expected = IllegalArgumentException.class)
42+
public void nonexistent() {
43+
Format.forName("nonexistent");
44+
}
45+
46+
}

core/src/test/java/org/everit/json/schema/loader/SchemaLoaderTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.json.JSONTokener;
3838
import org.junit.Assert;
3939
import org.junit.BeforeClass;
40+
import org.junit.Ignore;
4041
import org.junit.Test;
4142
import org.mockito.Mockito;
4243

@@ -352,6 +353,12 @@ public void stringSchema() {
352353
Assert.assertEquals(3, actual.getMaxLength().intValue());
353354
}
354355

356+
@Test
357+
@Ignore
358+
public void stringSchemaWithFormat() {
359+
StringSchema actual = (StringSchema) SchemaLoader.load(get("stringSchemaWithFormat"));
360+
}
361+
355362
@Test
356363
public void tupleSchema() {
357364
ArraySchema actual = (ArraySchema) SchemaLoader.load(get("tupleSchema"));

0 commit comments

Comments
 (0)