Skip to content

Commit a9dee02

Browse files
committed
Update error codes and messages
1 parent 5548a73 commit a9dee02

File tree

7 files changed

+75
-42
lines changed

7 files changed

+75
-42
lines changed

JsonSchema/RelogicLabs/JsonSchema/Message/ActualDetail.cs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,39 @@ namespace RelogicLabs.JsonSchema.Message;
88
public class ActualDetail : ContextDetail
99
{
1010
[SetsRequiredMembers]
11-
public ActualDetail(Context context, string message)
11+
public ActualDetail(Context context, string message)
1212
: base(context, message) { }
13-
13+
1414
[SetsRequiredMembers]
15-
public ActualDetail(JNode node, string message)
15+
public ActualDetail(JNode node, string message)
1616
: base(node, message) { }
1717

1818
internal static ActualDetail AsValueMismatch(JNode node)
1919
=> new(node, node.GetOutline().Affix("found "));
20-
21-
internal static ActualDetail AsPropertyNotFound(JNode node, JProperty property)
22-
=> new(node, $"not found property key \"{property.Key}\"");
23-
20+
21+
internal static ActualDetail AsPropertyNotFound(JNode node, JProperty property)
22+
=> new(node, $"not found property key '{property.Key}'");
23+
2424
internal static ActualDetail AsUndefinedProperty(JProperty property)
2525
=> new(property, $"property found {{{property.GetOutline()}}}");
26-
27-
internal static ActualDetail AsDataTypeMismatch(JNode node)
26+
27+
internal static ActualDetail AsDataTypeMismatch(JNode node)
2828
=> new(node, $"found {GetTypeName(node)} inferred by {node.GetOutline()}");
2929

3030
internal static ActualDetail AsArrayElementNotFound(JArray array, int index)
3131
=> new(array, "not found");
32-
33-
internal static ActualDetail AsInvalidFunction(JNode node)
34-
=> new(node, $"applied on non-composite type {GetTypeName(node)}");
35-
36-
internal static ActualDetail AsInvalidDataType(JNode node)
32+
33+
internal static ActualDetail AsInvalidFunction(JNode node)
3734
=> new(node, $"applied on non-composite type {GetTypeName(node)}");
3835

36+
internal static ActualDetail AsInvalidNestedDataType(JNode node)
37+
=> new(node, $"found non-composite type {GetTypeName(node)}");
38+
39+
internal static ActualDetail AsDataTypeArgumentFailed(JNode node)
40+
=> new(node, $"found invalid value {node.GetOutline()}");
41+
3942
internal static ActualDetail AsPropertyOrderMismatch(JNode node)
4043
=> node is JProperty property
41-
? new(property, $"key \"{property.Key}\" is found at position")
42-
: new(node, "key not found at position");
44+
? new(property, $"key '{property.Key}' is found at current position")
45+
: new(node, "key not found at current position");
4346
}

JsonSchema/RelogicLabs/JsonSchema/Message/ErrorCode.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ internal static class ErrorCode
3232
public const string DEFI01 = "DEFI01";
3333
public const string DEFI02 = "DEFI02";
3434
public const string DEFI03 = "DEFI03";
35+
public const string DEFI04 = "DEFI04";
3536
public const string DERA01 = "DERA01";
3637
public const string DERA02 = "DERA02";
3738
public const string DFRC01 = "DFRC01";
@@ -71,6 +72,7 @@ internal static class ErrorCode
7172
public const string DTYP04 = "DTYP04";
7273
public const string DTYP05 = "DTYP05";
7374
public const string DTYP06 = "DTYP06";
75+
public const string DTYP07 = "DTYP07";
7476
public const string DUBL01 = "DUBL01";
7577
public const string DUTC01 = "DUTC01";
7678
public const string DUTC02 = "DUTC02";

JsonSchema/RelogicLabs/JsonSchema/Message/ErrorDetail.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@ namespace RelogicLabs.JsonSchema.Message;
44

55
public class ErrorDetail
66
{
7-
internal const string InvalidNestedDataType = "Invalid nested data type operation";
7+
internal const string ValidationFailed = "Validation failed";
8+
internal const string ValueMismatch = "Value mismatch";
9+
internal const string DataTypeMismatch = "Data type mismatch";
10+
internal const string InvalidNestedDataType = "Invalid nested data type";
11+
internal const string DataTypeArgumentFailed = "Data type argument failed";
812
internal const string InvalidNestedFunction = "Invalid nested function operation";
913
internal const string PropertyNotFound = "Mandatory property not found";
1014
internal const string ArrayElementNotFound = "Mandatory array element not found";
1115
internal const string UndefinedPropertyFound = "Undefined property found";
1216
internal const string PropertyKeyMismatch = "Property key mismatch";
1317
internal const string PropertyValueMismatch = "Property value mismatch";
14-
internal const string DataTypeMismatch = "Data type mismatch";
15-
internal const string ValidationFailed = "Validation Failed";
16-
internal const string ValueMismatch = "Value mismatch";
1718
internal const string PropertyOrderMismatch = "Property order mismatch";
1819

1920
public required string Code { get; init; }
2021
public required string Message { get; init; }
21-
22+
2223
[SetsRequiredMembers]
2324
public ErrorDetail(string code, string message)
2425
{
Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
11
using System.Diagnostics.CodeAnalysis;
22
using RelogicLabs.JsonSchema.Tree;
33
using RelogicLabs.JsonSchema.Types;
4-
using RelogicLabs.JsonSchema.Utilities;
54

65
namespace RelogicLabs.JsonSchema.Message;
76

87
public class ExpectedDetail : ContextDetail
98
{
109
[SetsRequiredMembers]
11-
public ExpectedDetail(Context context, string message)
10+
public ExpectedDetail(Context context, string message)
1211
: base(context, message) { }
1312

1413
[SetsRequiredMembers]
15-
public ExpectedDetail(JNode node, string message)
14+
public ExpectedDetail(JNode node, string message)
1615
: base(node, message) { }
1716

18-
internal static ExpectedDetail AsArrayElementNotFound(JNode node, int index)
19-
=> new(node, $"element at {index} ({node.GetOutline()})");
20-
17+
internal static ExpectedDetail AsArrayElementNotFound(JNode node, int index)
18+
=> new(node, $"element at {index} '{node.GetOutline()}'");
19+
2120
internal static ExpectedDetail AsValueMismatch(JNode node)
2221
=> new(node, $"value {node.GetOutline()}");
23-
22+
2423
internal static ExpectedDetail AsUndefinedProperty(JObject @object, JProperty property)
25-
=> new(@object, $"no property with key {property.Key.Quote()}");
26-
27-
internal static ExpectedDetail AsPropertyNotFound(JProperty property)
28-
=> new(property, $"property ({property.GetOutline()})");
24+
=> new(@object, $"no property with key '{property.Key}'");
25+
26+
internal static ExpectedDetail AsPropertyNotFound(JProperty property)
27+
=> new(property, $"property '{property.GetOutline()}'");
2928

30-
internal static ExpectedDetail AsDataTypeMismatch(JNode node)
29+
internal static ExpectedDetail AsDataTypeMismatch(JNode node)
3130
=> new(node, $"{GetTypeName(node)} inferred by {node.GetOutline()}");
32-
33-
internal static ExpectedDetail AsDataTypeMismatch(JDataType dataType)
31+
32+
internal static ExpectedDetail AsDataTypeMismatch(JDataType dataType)
3433
=> new(dataType, $"data type {dataType.ToString(true)}");
3534

36-
internal static ExpectedDetail AsInvalidFunction(JFunction function)
35+
internal static ExpectedDetail AsInvalidFunction(JFunction function)
3736
=> new(function, "applying on composite type");
3837

39-
internal static ExpectedDetail AsInvalidDataType(JDataType dataType)
40-
=> new(dataType, "applying on composite type");
38+
internal static ExpectedDetail AsInvalidNestedDataType(JDataType dataType)
39+
=> new(dataType, "composite data type");
40+
41+
internal static ExpectedDetail AsDataTypeArgumentFailed(JDataType dataType)
42+
=> new(dataType, $"a valid value for '{dataType.Alias}'");
4143

42-
internal static ExpectedDetail AsPropertyOrderMismatch(JProperty property)
43-
=> new(property, $"property with key {property.Key.Quote()} at position");
44+
internal static ExpectedDetail AsPropertyOrderMismatch(JProperty property)
45+
=> new(property, $"property with key '{property.Key}' at current position");
4446
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using static RelogicLabs.JsonSchema.Message.ErrorCode;
2+
3+
namespace RelogicLabs.JsonSchema.Message;
4+
5+
internal class MatchReport
6+
{
7+
public static readonly MatchReport Success = new();
8+
public static readonly MatchReport TypeError = new(DTYP04, DTYP06);
9+
public static readonly MatchReport ArgumentError = new(DTYP05, DTYP07);
10+
public static readonly MatchReport AliasError = new(DEFI03, DEFI04);
11+
12+
private readonly string _code1;
13+
private readonly string _code2;
14+
15+
private MatchReport(string code1, string code2)
16+
{
17+
_code1 = code1;
18+
_code2 = code2;
19+
}
20+
21+
private MatchReport() : this(string.Empty, string.Empty) { }
22+
23+
public string GetCode(bool nested)
24+
=> nested ? _code2 : _code1;
25+
}

JsonSchema/RelogicLabs/JsonSchema/Types/JNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public virtual string GetOutline()
5959
{
6060
if(node is T other) return other;
6161
FailWith(new JsonSchemaException(
62-
new ErrorDetail(DTYP03, DataTypeMismatch),
62+
new ErrorDetail(DTYP02, DataTypeMismatch),
6363
ExpectedDetail.AsDataTypeMismatch(this),
6464
ActualDetail.AsDataTypeMismatch(node)));
6565
return default;

JsonSchema/RelogicLabs/JsonSchema/Utilities/DebugUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ internal static void Print(Exception exception)
5050
if(!DebugPrint) return;
5151
Console.Error.WriteLine("[DEBUG] Print of exception: " + exception);
5252
}
53-
}
53+
}

0 commit comments

Comments
 (0)