Skip to content

Commit a7480cb

Browse files
committed
more tests
1 parent d8ca22a commit a7480cb

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

json-java21-schema/src/test/java/io/github/simbo1905/json/schema/JsonSchemaTypeAndEnumTest.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,44 @@
66

77
class JsonSchemaTypeAndEnumTest extends JsonSchemaLoggingConfig {
88

9+
@Test
10+
void testEnum_strict_noTypeCoercion_edgeCases() {
11+
// Heterogeneous enum must compare with strict JSON equality (no string/number/boolean coercion)
12+
final var schemaJson = """
13+
{
14+
"enum": ["1", 1, true, false, 0, null, {"a":1}, [1]]
15+
}
16+
""";
17+
18+
final var schema = JsonSchema.compile(Json.parse(schemaJson));
19+
20+
// ✅ Exact matches (should PASS)
21+
assertThat(schema.validate(Json.parse("\"1\"")).valid()).isTrue(); // string "1"
22+
assertThat(schema.validate(Json.parse("1")).valid()).isTrue(); // number 1
23+
assertThat(schema.validate(Json.parse("true")).valid()).isTrue(); // boolean true
24+
assertThat(schema.validate(Json.parse("false")).valid()).isTrue(); // boolean false
25+
assertThat(schema.validate(Json.parse("0")).valid()).isTrue(); // number 0
26+
assertThat(schema.validate(Json.parse("null")).valid()).isTrue(); // null
27+
assertThat(schema.validate(Json.parse("{\"a\":1}")).valid()).isTrue(); // object
28+
assertThat(schema.validate(Json.parse("[1]")).valid()).isTrue(); // array
29+
30+
// ❌ Look-alikes (should FAIL — ensure no coercion)
31+
assertThat(schema.validate(Json.parse("\"true\"")).valid()).isFalse(); // string "true" ≠ true
32+
assertThat(schema.validate(Json.parse("\"false\"")).valid()).isFalse(); // string "false" ≠ false
33+
assertThat(schema.validate(Json.parse("\"0\"")).valid()).isFalse(); // string "0" ≠ 0 (already covered positive for "1")
34+
assertThat(schema.validate(Json.parse("0.0")).valid()).isFalse(); // 0.0 ≠ 0 if enum stores exact numeric identity
35+
assertThat(schema.validate(Json.parse("1.0")).valid()).isFalse(); // 1.0 ≠ 1 if equality is strict (no coercion)
36+
assertThat(schema.validate(Json.parse("false")).valid()).isTrue(); // sanity: false is in enum (contrast with failures above)
37+
38+
// ❌ Structural near-misses
39+
assertThat(schema.validate(Json.parse("{\"a\":2}")).valid()).isFalse(); // object value differs
40+
assertThat(schema.validate(Json.parse("[1,2]")).valid()).isFalse(); // array contents differ
41+
42+
// Optional: key order should not matter for object equality (document your intended policy).
43+
// If your validator treats {"a":1} equal regardless of key order, this should PASS.
44+
assertThat(schema.validate(Json.parse("{\"a\":1}")).valid()).isTrue();
45+
}
46+
947
@Test
1048
void testTypeArray_anyOfSemantics() {
1149
String schemaJson = """
@@ -250,4 +288,4 @@ void testConst_array() {
250288
// Invalid - missing element
251289
assertThat(schema.validate(Json.parse("[1, 2]")).valid()).isFalse();
252290
}
253-
}
291+
}

0 commit comments

Comments
 (0)