Skip to content

Commit 56367df

Browse files
committed
Add support for .NET 6 and C# 10
1 parent e5bd6dd commit 56367df

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+587
-330
lines changed

JsonSchema/JsonSchema.csproj

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
<PropertyGroup>
44
<PackageId>RelogicLabs.JsonSchema</PackageId>
5-
<Description>A simplified, concise, intuitive, and extensible JSON Schema</Description>
5+
<Description>New JSON Schema prioritizes simplicity, conciseness, readability, and efficiency.
6+
It offers precise JSON document definition through ample functionalities and extensibility
7+
to meet the diverse web service requirements.</Description>
68
<Authors>Relogic Labs</Authors>
79
<Company>Relogic Labs</Company>
8-
<Version>1.5.0</Version>
9-
<PackageVersion>1.5.0</PackageVersion>
10-
<AssemblyVersion>1.5.0</AssemblyVersion>
10+
<Version>1.6.0</Version>
11+
<PackageVersion>1.6.0</PackageVersion>
12+
<AssemblyVersion>1.6.0</AssemblyVersion>
1113
<PackageTags>JsonSchema;Schema;Json;Validation;Assert;Test</PackageTags>
1214
<Copyright>Copyright © Relogic Labs. All rights reserved.</Copyright>
1315
<NeutralLanguage>en</NeutralLanguage>
1416
<AssemblyName>RelogicLabs.JsonSchema</AssemblyName>
15-
<TargetFramework>net7.0</TargetFramework>
17+
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
1618
<ImplicitUsings>enable</ImplicitUsings>
1719
<Nullable>enable</Nullable>
1820
<RootNamespace>RelogicLabs</RootNamespace>

JsonSchema/RelogicLabs/JsonSchema/Collections/IIndexMap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ public interface IIndexMap<TK, TV> : IList<TV>
77
IEnumerable<TK> Keys { get; }
88
IEnumerable<TV> Values { get; }
99
bool TryGetValue(TK key, out TV? value);
10-
void MakeReadOnly();
10+
IIndexMap<TK, TV> AsReadOnly();
1111
}

JsonSchema/RelogicLabs/JsonSchema/Collections/IndexHashMap.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections;
2+
using System.Collections.ObjectModel;
23

34
namespace RelogicLabs.JsonSchema.Collections;
45

@@ -10,8 +11,8 @@ public class IndexHashMap<TK, TV> : IIndexMap<TK, TV>
1011

1112
public IndexHashMap(IEnumerable<TV> source)
1213
{
13-
_list = source.ToList().AsReadOnly();
14-
_dictionary = _list.ToDictionary(e => e.GetKey(), e => e).AsReadOnly();
14+
_list = source.ToList();
15+
_dictionary = _list.ToDictionary(e => e.GetKey(), e => e);
1516
}
1617

1718
public IEnumerator<TV> GetEnumerator() => _list.GetEnumerator();
@@ -68,9 +69,10 @@ public TV this[int index]
6869
public bool TryGetValue(TK key, out TV? value)
6970
=> _dictionary.TryGetValue(key, out value);
7071

71-
public void MakeReadOnly()
72+
public IIndexMap<TK, TV> AsReadOnly()
7273
{
73-
_list = _list.AsReadOnly();
74-
_dictionary = _dictionary.AsReadOnly();
74+
_list = new ReadOnlyCollection<TV>(_list);
75+
_dictionary = new ReadOnlyDictionary<TK, TV>(_dictionary);
76+
return this;
7577
}
7678
}

JsonSchema/RelogicLabs/JsonSchema/Functions/CoreFunctions3.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ namespace RelogicLabs.JsonSchema.Functions;
1010

1111
public partial class CoreFunctions
1212
{
13+
// Based on SMTP protocol RFC 5322
14+
private static readonly Regex EmailRegex = new(
15+
"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$", RegexOptions.Compiled);
16+
17+
// Based on ITU-T E.163 and E.164 (extended)
18+
private static readonly Regex PhoneRegex = new(@"^\+?[0-9\s-()]+$", RegexOptions.Compiled);
19+
1320
public bool Elements(JArray target, params JNode[] items)
1421
{
1522
return items.Where(n => !target.Elements.Contains(n))
@@ -65,7 +72,7 @@ public bool Regex(JString target, JString pattern)
6572

6673
public bool Email(JString target)
6774
{
68-
var result = EmailRegex().IsMatch(target);
75+
var result = EmailRegex.IsMatch(target);
6976
if(!result) return FailWith(new JsonSchemaException(
7077
new ErrorDetail(EMAL01, "Invalid email address"),
7178
new ExpectedDetail(Function, "a valid email address"),
@@ -85,8 +92,8 @@ public bool Url(JString target)
8592
if(!result) return FailWith(new JsonSchemaException(
8693
new ErrorDetail(URLA02, "Invalid url address scheme"),
8794
new ExpectedDetail(Function, "HTTP or HTTPS scheme"),
88-
new ActualDetail(target, $"found {uriResult.Scheme.Quote()} from {
89-
target} that has invalid scheme")));
95+
new ActualDetail(target, $"found {uriResult.Scheme.Quote()} from {target} " +
96+
$"that has invalid scheme")));
9097
return true;
9198
}
9299

@@ -101,14 +108,14 @@ public bool Url(JString target, JString scheme)
101108
if(!result) return FailWith(new JsonSchemaException(
102109
new ErrorDetail(URLA04, "Mismatch url address scheme"),
103110
new ExpectedDetail(Function, $"scheme {scheme} for url address"),
104-
new ActualDetail(target, $"found {uriResult.Scheme.Quote()} from {
105-
target} that does not matched")));
111+
new ActualDetail(target, $"found {uriResult.Scheme.Quote()} from {target} " +
112+
$"that does not matched")));
106113
return true;
107114
}
108115

109116
public bool Phone(JString target)
110117
{
111-
bool result = PhoneRegex().IsMatch(target);
118+
bool result = PhoneRegex.IsMatch(target);
112119
if(!result) return FailWith(new JsonSchemaException(
113120
new ErrorDetail(PHON01, "Invalid phone number format"),
114121
new ExpectedDetail(Function, "a valid phone number"),
@@ -149,12 +156,4 @@ private bool DateTime(JString target, JString pattern, DateTimeType type)
149156
ex));
150157
}
151158
}
152-
153-
// Based on SMTP protocol RFC 5322
154-
[GeneratedRegex("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$", RegexOptions.Compiled)]
155-
private static partial Regex EmailRegex();
156-
157-
// Based on ITU-T E.163 and E.164 (extended)
158-
[GeneratedRegex(@"^\+?[0-9\s-()]+$", RegexOptions.Compiled)]
159-
private static partial Regex PhoneRegex();
160159
}

JsonSchema/RelogicLabs/JsonSchema/Functions/FunctionBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ namespace RelogicLabs.JsonSchema.Functions;
66

77
public abstract class FunctionBase
88
{
9-
public required RuntimeContext Runtime { get; init; }
9+
public RuntimeContext Runtime { get; }
1010
public JFunction Function { get; set; } = null!;
11-
11+
1212
protected FunctionBase(RuntimeContext runtime) => Runtime = runtime;
13-
13+
1414
protected bool FailWith(JsonSchemaException exception)
1515
=> Runtime.FailWith(exception);
1616
}

JsonSchema/RelogicLabs/JsonSchema/Message/ActualDetail.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Diagnostics.CodeAnalysis;
21
using RelogicLabs.JsonSchema.Tree;
32
using RelogicLabs.JsonSchema.Types;
43
using RelogicLabs.JsonSchema.Utilities;
@@ -7,11 +6,9 @@ namespace RelogicLabs.JsonSchema.Message;
76

87
public class ActualDetail : ContextDetail
98
{
10-
[SetsRequiredMembers]
119
public ActualDetail(Context context, string message)
1210
: base(context, message) { }
1311

14-
[SetsRequiredMembers]
1512
public ActualDetail(JNode node, string message)
1613
: base(node, message) { }
1714

JsonSchema/RelogicLabs/JsonSchema/Message/ContextDetail.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
using System.Diagnostics.CodeAnalysis;
21
using RelogicLabs.JsonSchema.Tree;
32
using RelogicLabs.JsonSchema.Types;
43

54
namespace RelogicLabs.JsonSchema.Message;
65

76
public abstract class ContextDetail
87
{
9-
public required Context Context { get; init; }
10-
public required string Message { get; init; }
11-
public required Location Location { get; init; }
8+
public Context Context { get; }
9+
public string Message { get; }
10+
public Location Location { get; }
1211

13-
[SetsRequiredMembers]
1412
protected ContextDetail(Context context, string message)
1513
{
1614
Context = context;
1715
Message = message;
1816
Location = context.GetLocation();
1917
}
2018

21-
[SetsRequiredMembers]
2219
protected ContextDetail(JNode node, string message)
2320
: this(node.Context, message) { }
2421

JsonSchema/RelogicLabs/JsonSchema/Message/ErrorDetail.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System.Diagnostics.CodeAnalysis;
2-
31
namespace RelogicLabs.JsonSchema.Message;
42

53
public class ErrorDetail
@@ -17,10 +15,9 @@ public class ErrorDetail
1715
internal const string PropertyValueMismatch = "Property value mismatch";
1816
internal const string PropertyOrderMismatch = "Property order mismatch";
1917

20-
public required string Code { get; init; }
21-
public required string Message { get; init; }
18+
public string Code { get; }
19+
public string Message { get; }
2220

23-
[SetsRequiredMembers]
2421
public ErrorDetail(string code, string message)
2522
{
2623
Code = code;

JsonSchema/RelogicLabs/JsonSchema/Message/ExpectedDetail.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
using System.Diagnostics.CodeAnalysis;
21
using RelogicLabs.JsonSchema.Tree;
32
using RelogicLabs.JsonSchema.Types;
43

54
namespace RelogicLabs.JsonSchema.Message;
65

76
public class ExpectedDetail : ContextDetail
87
{
9-
[SetsRequiredMembers]
108
public ExpectedDetail(Context context, string message)
119
: base(context, message) { }
1210

13-
[SetsRequiredMembers]
1411
public ExpectedDetail(JNode node, string message)
1512
: base(node, message) { }
1613

JsonSchema/RelogicLabs/JsonSchema/Message/MessageFormatter.cs

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,37 @@ public abstract class MessageFormatter
1212
private const string JsonBaseException = "Json Input [{0}]: {1}";
1313
private const string SchemaParseException = "Schema (Line: {0}) [{1}]: {2}";
1414
private const string JsonParseException = "Json (Line: {0}) [{1}]: {2}";
15-
15+
1616
public static readonly MessageFormatter SchemaValidation
17-
= new ValidationFormatter
18-
{
19-
Summary = "Schema (Line: {0}) Json (Line: {1}) [{2}]: {3}.",
20-
Expected = " {0} is expected",
21-
Actual = " but {0}."
22-
};
17+
= new ValidationFormatter(
18+
"Schema (Line: {0}) Json (Line: {1}) [{2}]: {3}.",
19+
" {0} is expected",
20+
" but {0}.");
21+
2322
public static readonly MessageFormatter SchemaAssertion
24-
= new AssertionFormatter
25-
{
26-
Summary = $"{{0}}: {{1}}{NewLine}",
27-
Expected = $"Expected (Schema Line: {{0}}): {{1}}{NewLine}",
28-
Actual = $"Actual (Json Line: {{0}}): {{1}}{NewLine}"
29-
};
30-
23+
= new AssertionFormatter(
24+
$"{{0}}: {{1}}{NewLine}",
25+
$"Expected (Schema Line: {{0}}): {{1}}{NewLine}",
26+
$"Actual (Json Line: {{0}}): {{1}}{NewLine}");
27+
3128
public static readonly MessageFormatter JsonAssertion
32-
= new AssertionFormatter
33-
{
34-
Summary = $"{{0}}: {{1}}{NewLine}",
35-
Expected = $"Expected (Json Line: {{0}}): {{1}}{NewLine}",
36-
Actual = $"Actual (Json Line: {{0}}): {{1}}{NewLine}"
37-
};
38-
39-
public required string Summary { get; init; }
40-
public required string Expected { get; init; }
41-
public required string Actual { get; init; }
29+
= new AssertionFormatter(
30+
$"{{0}}: {{1}}{NewLine}",
31+
$"Expected (Json Line: {{0}}): {{1}}{NewLine}",
32+
$"Actual (Json Line: {{0}}): {{1}}{NewLine}");
33+
34+
public string Summary { get; }
35+
public string Expected { get; }
36+
public string Actual { get; }
4237
public int OutlineLength { get; set; } = 200;
4338

39+
private MessageFormatter(string summary, string expected, string actual)
40+
{
41+
Summary = summary;
42+
Expected = expected;
43+
Actual = actual;
44+
}
45+
4446
public string CreateOutline(string target)
4547
{
4648
int front = 2 * OutlineLength / 3;
@@ -53,21 +55,27 @@ public string CreateOutline(string target)
5355

5456
private class ValidationFormatter : MessageFormatter
5557
{
56-
internal override string Format(ErrorDetail error, ExpectedDetail expected,
58+
public ValidationFormatter(string summary, string expected, string actual)
59+
: base(summary, expected, actual) { }
60+
61+
internal override string Format(ErrorDetail error, ExpectedDetail expected,
5762
ActualDetail actual)
5863
{
5964
return new StringBuilder()
60-
.Append(string.Format(Summary, expected.Location,
65+
.Append(string.Format(Summary, expected.Location,
6166
actual.Location, error.Code, error.Message))
6267
.Append(string.Format(Expected, expected.Message.ToUpperFirstLetter()))
6368
.Append(string.Format(Actual, actual.Message))
6469
.ToString();
6570
}
6671
}
67-
72+
6873
private class AssertionFormatter : MessageFormatter
6974
{
70-
internal override string Format(ErrorDetail error, ExpectedDetail expected,
75+
public AssertionFormatter(string summary, string expected, string actual)
76+
: base(summary, expected, actual) { }
77+
78+
internal override string Format(ErrorDetail error, ExpectedDetail expected,
7179
ActualDetail actual)
7280
{
7381
return new StringBuilder()
@@ -83,21 +91,21 @@ internal abstract string Format(ErrorDetail error, ExpectedDetail expected,
8391

8492
internal static ErrorDetail FormatForSchema(string code, string message, Context? context)
8593
=> FormatForSchema(code, message, context?.GetLocation());
86-
87-
internal static ErrorDetail FormatForSchema(string code, string message, Location? location)
94+
95+
internal static ErrorDetail FormatForSchema(string code, string message, Location? location)
8896
=> location == null ? CreateDetail(code, SchemaBaseException, message)
8997
: CreateDetail(code, SchemaParseException, message, location);
9098

9199
internal static ErrorDetail FormatForJson(string code, string message, Context? context)
92100
=> FormatForJson(code, message, context?.GetLocation());
93-
94-
internal static ErrorDetail FormatForJson(string code, string message, Location? location)
101+
102+
internal static ErrorDetail FormatForJson(string code, string message, Location? location)
95103
=> location == null ? CreateDetail(code, JsonBaseException, message)
96104
: CreateDetail(code, JsonParseException, message, location);
97105

98-
private static ErrorDetail CreateDetail(string code, string format, string message)
106+
private static ErrorDetail CreateDetail(string code, string format, string message)
99107
=> new(code, string.Format(format, code, message));
100108

101-
private static ErrorDetail CreateDetail(string code, string format, string message,
109+
private static ErrorDetail CreateDetail(string code, string format, string message,
102110
Location location) => new(code, string.Format(format, location, code, message));
103-
}
111+
}

0 commit comments

Comments
 (0)