@@ -1136,6 +1136,129 @@ def test_not_fail():
11361136 errors = validator .validate_instance (instance )
11371137 assert any ("should not validate against 'not' schema" in err for err in errors )
11381138
1139+ # -------------------------------------------------------------------
1140+ # Choice Type Tests
1141+ # -------------------------------------------------------------------
1142+
1143+ def test_choice_tagged_valid ():
1144+ schema = {
1145+ "$schema" : "https://json-structure.org/meta/core/v0/#" ,
1146+ "$id" : "dummy" ,
1147+ "name" : "MyChoice" ,
1148+ "type" : "choice" ,
1149+ "choices" : {
1150+ "a" : {"type" : "string" },
1151+ "b" : {"type" : "number" }
1152+ }
1153+ }
1154+ instance = {"a" : "hello" }
1155+ validator = JSONStructureInstanceValidator (schema )
1156+ errors = validator .validate_instance (instance )
1157+ assert errors == []
1158+
1159+
1160+ def test_choice_tagged_invalid_multiple_properties ():
1161+ schema = {
1162+ "$schema" : "https://json-structure.org/meta/core/v0/#" ,
1163+ "$id" : "dummy" ,
1164+ "name" : "MyChoice" ,
1165+ "type" : "choice" ,
1166+ "choices" : {
1167+ "a" : {"type" : "string" },
1168+ "b" : {"type" : "number" }
1169+ }
1170+ }
1171+ instance = {"a" : "x" , "b" : 1 }
1172+ validator = JSONStructureInstanceValidator (schema )
1173+ errors = validator .validate_instance (instance )
1174+ assert any ("must have a single property" in err for err in errors )
1175+
1176+
1177+ def test_choice_tagged_invalid_key ():
1178+ schema = {
1179+ "$schema" : "https://json-structure.org/meta/core/v0/#" ,
1180+ "$id" : "dummy" ,
1181+ "name" : "MyChoice" ,
1182+ "type" : "choice" ,
1183+ "choices" : {
1184+ "a" : {"type" : "string" },
1185+ "b" : {"type" : "number" }
1186+ }
1187+ }
1188+ instance = {"c" : "oops" }
1189+ validator = JSONStructureInstanceValidator (schema )
1190+ errors = validator .validate_instance (instance )
1191+ assert any ("not one of choices" in err for err in errors )
1192+
1193+
1194+ def test_choice_inline_valid ():
1195+ schema = {
1196+ "$schema" : "https://json-structure.org/meta/core/v0/#" ,
1197+ "$id" : "dummy" ,
1198+ "name" : "InlineChoice" ,
1199+ "type" : "choice" ,
1200+ "$extends" : "#/definitions/Base" ,
1201+ "selector" : "type" ,
1202+ "choices" : {
1203+ "X" : {"$ref" : "#/definitions/X" },
1204+ "Y" : {"$ref" : "#/definitions/Y" }
1205+ },
1206+ "definitions" : {
1207+ "Base" : {
1208+ "name" : "Base" ,
1209+ "abstract" : True ,
1210+ "type" : "object" ,
1211+ "properties" : {"common" : {"type" : "string" }}
1212+ },
1213+ "X" : {
1214+ "type" : "object" ,
1215+ "$extends" : "#/definitions/Base" ,
1216+ "properties" : {"x" : {"type" : "number" }}
1217+ },
1218+ "Y" : {
1219+ "type" : "object" ,
1220+ "$extends" : "#/definitions/Base" ,
1221+ "properties" : {"y" : {"type" : "boolean" }}
1222+ }
1223+ }
1224+ }
1225+ instance = {"type" : "X" , "x" : 123 }
1226+ validator = JSONStructureInstanceValidator (schema )
1227+ errors = validator .validate_instance (instance )
1228+ assert errors == []
1229+
1230+
1231+ def test_choice_inline_missing_selector ():
1232+ schema = {
1233+ "$schema" : "https://json-structure.org/meta/core/v0/#" ,
1234+ "$id" : "dummy" ,
1235+ "name" : "InlineChoice" ,
1236+ "type" : "choice" ,
1237+ "$extends" : "#/definitions/Base" ,
1238+ "choices" : {"A" : {"type" : "string" }},
1239+ "definitions" : {"Base" : {"name" : "Base" , "abstract" : True , "type" : "object" , "properties" : {}}}
1240+ }
1241+ instance = {"A" : "value" }
1242+ validator = JSONStructureInstanceValidator (schema )
1243+ errors = validator .validate_instance (instance )
1244+ assert any ("missing 'selector'" in err for err in errors )
1245+
1246+
1247+ def test_choice_inline_invalid_selector_value ():
1248+ schema = {
1249+ "$schema" : "https://json-structure.org/meta/core/v0/#" ,
1250+ "$id" : "dummy" ,
1251+ "name" : "InlineChoice" ,
1252+ "type" : "choice" ,
1253+ "$extends" : "#/definitions/Base" ,
1254+ "selector" : "kind" ,
1255+ "choices" : {"A" : {"type" : "string" }},
1256+ "definitions" : {"Base" : {"name" : "Base" , "abstract" : True , "type" : "object" , "properties" : {}}}
1257+ }
1258+ instance = {"kind" : "B" , "B" : "oops" }
1259+ validator = JSONStructureInstanceValidator (schema )
1260+ errors = validator .validate_instance (instance )
1261+ assert any ("not one of choices" in err for err in errors )
11391262
11401263# -------------------------------------------------------------------
11411264# End of tests.
0 commit comments