@@ -518,7 +518,7 @@ describe("Error messages", async () => {
518518 expect ( result . errors ) . to . eql ( [ {
519519 schemaLocation : "https://example.com/main#/enum" ,
520520 instanceLocation : "#" ,
521- message : localization . getEnumErrorMessage ( { variant : "suggestion ", instanceValue : "rwd ", suggestion : "red" } )
521+ message : localization . getEnumErrorMessage ( { allowedValues : [ "red ", "green ", "blue" ] } , "rwd" )
522522 } ] ) ;
523523 } ) ;
524524
@@ -856,7 +856,7 @@ describe("Error messages", async () => {
856856 {
857857 schemaLocation : "https://example.com/main#/anyOf" ,
858858 instanceLocation : "#" ,
859- message : localization . getTypeErrorMessage ( [ "string" , "number" ] , "boolean" )
859+ message : localization . getEnumErrorMessage ( { allowedTypes : [ "string" , "number" ] } , false )
860860 }
861861 ] ) ;
862862 } ) ;
@@ -989,7 +989,7 @@ describe("Error messages", async () => {
989989 const instance = {
990990 type : "d" ,
991991 banana : "yellow" ,
992- box : 10
992+ box : ""
993993 } ;
994994
995995 /** @type OutputFormat */
@@ -1163,6 +1163,171 @@ describe("Error messages", async () => {
11631163 ] ) ;
11641164 } ) ;
11651165
1166+ test ( "anyOf with enums provides a 'did you mean' suggestion" , async ( ) => {
1167+ registerSchema ( {
1168+ $schema : "https://json-schema.org/draft/2020-12/schema" ,
1169+ anyOf : [
1170+ { enum : [ "apple" , "orange" , "banana" ] } ,
1171+ { enum : [ 100 , 200 , 300 ] }
1172+ ]
1173+ } , schemaUri ) ;
1174+
1175+ // The instance is a typo but is clearly intended to be "apple".
1176+ const instance = "aple" ;
1177+
1178+ /** @type OutputFormat */
1179+ const output = {
1180+ valid : false ,
1181+ errors : [
1182+ {
1183+ absoluteKeywordLocation : "https://example.com/main#/anyOf/0/enum" ,
1184+ instanceLocation : "#"
1185+ } ,
1186+ {
1187+ absoluteKeywordLocation : "https://example.com/main#/anyOf/1/enum" ,
1188+ instanceLocation : "#"
1189+ } ,
1190+ {
1191+ absoluteKeywordLocation : "https://example.com/main#/anyOf" ,
1192+ instanceLocation : "#"
1193+ }
1194+ ]
1195+ } ;
1196+
1197+ const result = await betterJsonSchemaErrors ( output , schemaUri , instance ) ;
1198+
1199+ expect ( result . errors ) . to . eql ( [
1200+ {
1201+ schemaLocation : "https://example.com/main#/anyOf" ,
1202+ instanceLocation : "#" ,
1203+ message : localization . getEnumErrorMessage ( { allowedValues : [ "apple" ] } , "aple" )
1204+ }
1205+ ] ) ;
1206+ } ) ;
1207+
1208+ test ( "anyOf with const" , async ( ) => {
1209+ registerSchema ( {
1210+ $schema : "https://json-schema.org/draft/2020-12/schema" ,
1211+ anyOf : [
1212+ { const : "a" } ,
1213+ { const : 1 }
1214+ ]
1215+ } , schemaUri ) ;
1216+
1217+ const instance = 12 ;
1218+
1219+ /** @type OutputFormat */
1220+ const output = {
1221+ valid : false ,
1222+ errors : [
1223+ {
1224+ absoluteKeywordLocation : "https://example.com/main#/anyOf/0/const" ,
1225+ instanceLocation : "#"
1226+ } ,
1227+ {
1228+ absoluteKeywordLocation : "https://example.com/main#/anyOf/1/const" ,
1229+ instanceLocation : "#"
1230+ } ,
1231+ {
1232+ absoluteKeywordLocation : "https://example.com/main#/anyOf" ,
1233+ instanceLocation : "#"
1234+ }
1235+ ]
1236+ } ;
1237+
1238+ const result = await betterJsonSchemaErrors ( output , schemaUri , instance ) ;
1239+
1240+ expect ( result . errors ) . to . eql ( [
1241+ {
1242+ schemaLocation : "https://example.com/main#/anyOf" ,
1243+ instanceLocation : "#" ,
1244+ message : localization . getEnumErrorMessage ( { allowedValues : [ "a" , 1 ] } , 12 )
1245+ }
1246+ ] ) ;
1247+ } ) ;
1248+
1249+ test ( "anyOf with const and enum" , async ( ) => {
1250+ registerSchema ( {
1251+ $schema : "https://json-schema.org/draft/2020-12/schema" ,
1252+ anyOf : [
1253+ { enum : [ "a" , "b" , "c" ] } ,
1254+ { const : 1 }
1255+ ]
1256+ } , schemaUri ) ;
1257+
1258+ const instance = 12 ;
1259+
1260+ /** @type OutputFormat */
1261+ const output = {
1262+ valid : false ,
1263+ errors : [
1264+ {
1265+ absoluteKeywordLocation : "https://example.com/main#/anyOf/0/enum" ,
1266+ instanceLocation : "#"
1267+ } ,
1268+ {
1269+ absoluteKeywordLocation : "https://example.com/main#/anyOf/1/const" ,
1270+ instanceLocation : "#"
1271+ } ,
1272+ {
1273+ absoluteKeywordLocation : "https://example.com/main#/anyOf" ,
1274+ instanceLocation : "#"
1275+ }
1276+ ]
1277+ } ;
1278+
1279+ const result = await betterJsonSchemaErrors ( output , schemaUri , instance ) ;
1280+
1281+ expect ( result . errors ) . to . eql ( [
1282+ {
1283+ schemaLocation : "https://example.com/main#/anyOf" ,
1284+ instanceLocation : "#" ,
1285+ message : localization . getEnumErrorMessage ( { allowedValues : [ "a" , "b" , "c" , 1 ] } , 12 )
1286+ }
1287+ ] ) ;
1288+ } ) ;
1289+
1290+ test ( "anyOf with enum and type" , async ( ) => {
1291+ registerSchema ( {
1292+ $schema : "https://json-schema.org/draft/2020-12/schema" ,
1293+ anyOf : [
1294+ { enum : [ "a" , "b" , "c" ] } ,
1295+ { type : "number" }
1296+ ]
1297+ } , schemaUri ) ;
1298+
1299+ const instance = false ;
1300+
1301+ /** @type OutputFormat */
1302+ const output = {
1303+ valid : false ,
1304+ errors : [
1305+ {
1306+ absoluteKeywordLocation : "https://example.com/main#/anyOf/0/enum" ,
1307+ instanceLocation : "#"
1308+ } ,
1309+ {
1310+ absoluteKeywordLocation : "https://example.com/main#/anyOf/1/type" ,
1311+ instanceLocation : "#"
1312+ } ,
1313+ {
1314+ absoluteKeywordLocation : "https://example.com/main#/anyOf" ,
1315+ instanceLocation : "#"
1316+ }
1317+ ]
1318+ } ;
1319+
1320+ const result = await betterJsonSchemaErrors ( output , schemaUri , instance ) ;
1321+
1322+ expect ( result . errors ) . to . eql ( [
1323+ {
1324+ schemaLocation : "https://example.com/main#/anyOf" ,
1325+ instanceLocation : "#" ,
1326+ message : localization . getEnumErrorMessage ( { allowedValues : [ "a" , "b" , "c" ] , allowedTypes : [ "number" ] } , false )
1327+ }
1328+ ] ) ;
1329+ } ) ;
1330+
11661331 test ( "normalized output for a failing 'contains' keyword" , async ( ) => {
11671332 registerSchema ( {
11681333 $schema : "https://json-schema.org/draft/2020-12/schema" ,
0 commit comments