Skip to content

Commit ad063b1

Browse files
committed
Refactor and optimize code
1 parent 7e2d8fd commit ad063b1

27 files changed

+416
-493
lines changed

JsonSchema/JsonSchema.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Version>1.1.0</Version>
99
<PackageVersion>1.1.0</PackageVersion>
1010
<AssemblyVersion>1.1.0</AssemblyVersion>
11-
<PackageTags>JsonSchema;Schema;Validation;Json</PackageTags>
11+
<PackageTags>JsonSchema;Schema;Json;Validation;Assert;Test</PackageTags>
1212
<Copyright>Copyright © Relogic Labs. All rights reserved.</Copyright>
1313
<NeutralLanguage>en</NeutralLanguage>
1414
<AssemblyName>RelogicLabs.JsonSchema</AssemblyName>

JsonSchema/RelogicLabs/JsonSchema/Functions/CoreFunctions1.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public bool Length(JString target, JInteger length)
1616
if(_length != length) return FailWith(new JsonSchemaException(
1717
new ErrorDetail(SLEN01, "Invalid string length"),
1818
new ExpectedDetail(Function, $"length {length}"),
19-
new ActualDetail(target, $"found {_length} for \"{target}\"")));
19+
new ActualDetail(target, $"found {_length} for {target}")));
2020
return true;
2121
}
2222

@@ -26,7 +26,7 @@ public bool Length(JArray target, JInteger length)
2626
if(_length != length) return FailWith(new JsonSchemaException(
2727
new ErrorDetail(ALEN01, "Invalid array length"),
2828
new ExpectedDetail(Function, $"length {length}"),
29-
new ActualDetail(target, $"found {_length} for {target.ToOutline()}")));
29+
new ActualDetail(target, $"found {_length} for {target.GetOutline()}")));
3030
return true;
3131
}
3232

@@ -36,7 +36,7 @@ public bool Length(JObject target, JInteger length)
3636
if(_length != length) return FailWith(new JsonSchemaException(
3737
new ErrorDetail(OLEN01, "Invalid object size or length"),
3838
new ExpectedDetail(Function, $"length {length}"),
39-
new ActualDetail(target, $"found {_length} for {target.ToOutline()}")));
39+
new ActualDetail(target, $"found {_length} for {target.GetOutline()}")));
4040
return true;
4141
}
4242

@@ -45,12 +45,12 @@ public bool Length(JString target, JInteger minimum, JInteger maximum)
4545
var length = target.Value.Length;
4646
if(length < minimum)
4747
return FailWith(new JsonSchemaException(new ErrorDetail(SLEN02,
48-
$"String \"{target.ToOutline()}\" length is outside of range"),
48+
$"String {target.GetOutline()} length is outside of range"),
4949
new ExpectedDetail(Function, $"length in range [{minimum}, {maximum}]"),
5050
new ActualDetail(target, $"found {length} that is less than {minimum}")));
5151
if(length > maximum)
5252
return FailWith(new JsonSchemaException(new ErrorDetail(SLEN03,
53-
$"String {target.ToOutline()} length is outside of range"),
53+
$"String {target.GetOutline()} length is outside of range"),
5454
new ExpectedDetail(Function, $"length in range [{minimum}, {maximum}]"),
5555
new ActualDetail(target, $"found {length} that is greater than {maximum}")));
5656
return true;
@@ -61,7 +61,7 @@ public bool Length(JString target, JInteger minimum, JUndefined undefined)
6161
var length = target.Value.Length;
6262
if(length < minimum)
6363
return FailWith(new JsonSchemaException(new ErrorDetail(SLEN04,
64-
$"String \"{target.ToOutline()}\" length is outside of range"),
64+
$"String {target.GetOutline()} length is outside of range"),
6565
new ExpectedDetail(Function, $"length in range [{minimum}, {undefined}]"),
6666
new ActualDetail(target, $"found {length} that is less than {minimum}")));
6767
return true;
@@ -72,7 +72,7 @@ public bool Length(JString target, JUndefined undefined, JInteger maximum)
7272
var length = target.Value.Length;
7373
if(length > maximum)
7474
return FailWith(new JsonSchemaException(new ErrorDetail(SLEN05,
75-
$"String \"{target.ToOutline()}\" length is outside of range"),
75+
$"String {target.GetOutline()} length is outside of range"),
7676
new ExpectedDetail(Function, $"length in range [{undefined}, {maximum}]"),
7777
new ActualDetail(target, $"found {length} that is greater than {maximum}")));
7878
return true;
@@ -83,12 +83,12 @@ public bool Length(JArray target, JInteger minimum, JInteger maximum)
8383
var length = target.Elements.Count;
8484
if(length < minimum)
8585
return FailWith(new JsonSchemaException(new ErrorDetail(ALEN02,
86-
$"Array {target.ToOutline()} length is outside of range"),
86+
$"Array {target.GetOutline()} length is outside of range"),
8787
new ExpectedDetail(Function, $"length in range [{minimum}, {maximum}]"),
8888
new ActualDetail(target, $"found {length} that is less than {minimum}")));
8989
if(length > maximum)
9090
return FailWith(new JsonSchemaException(new ErrorDetail(ALEN03,
91-
$"Array {target.ToOutline()} length is outside of range"),
91+
$"Array {target.GetOutline()} length is outside of range"),
9292
new ExpectedDetail(Function, $"length in range [{minimum}, {maximum}]"),
9393
new ActualDetail(target, $"found {length} that is greater than {maximum}")));
9494
return true;
@@ -99,7 +99,7 @@ public bool Length(JArray target, JInteger minimum, JUndefined undefined)
9999
var length = target.Elements.Count;
100100
if(length < minimum)
101101
return FailWith(new JsonSchemaException(new ErrorDetail(ALEN04,
102-
$"Array {target.ToOutline()} length is outside of range"),
102+
$"Array {target.GetOutline()} length is outside of range"),
103103
new ExpectedDetail(Function, $"length in range [{minimum}, {undefined}]"),
104104
new ActualDetail(target, $"found {length} that is less than {minimum}")));
105105
return true;
@@ -110,7 +110,7 @@ public bool Length(JArray target, JUndefined undefined, JInteger maximum)
110110
var length = target.Elements.Count;
111111
if(length > maximum)
112112
return FailWith(new JsonSchemaException(new ErrorDetail(ALEN05,
113-
$"Array {target.ToOutline()} length is outside of range"),
113+
$"Array {target.GetOutline()} length is outside of range"),
114114
new ExpectedDetail(Function, $"length in range [{undefined}, {maximum}]"),
115115
new ActualDetail(target, $"found {length} that is greater than {maximum}")));
116116
return true;
@@ -121,12 +121,12 @@ public bool Length(JObject target, JInteger minimum, JInteger maximum)
121121
var length = target.Properties.Count;
122122
if(length < minimum)
123123
return FailWith(new JsonSchemaException(new ErrorDetail(OLEN02,
124-
$"Object {target.ToOutline()} size or length is outside of range"),
124+
$"Object {target.GetOutline()} size or length is outside of range"),
125125
new ExpectedDetail(Function, $"length in range [{minimum}, {maximum}]"),
126126
new ActualDetail(target, $"found {length} that is less than {minimum}")));
127127
if(length > maximum)
128128
return FailWith(new JsonSchemaException(new ErrorDetail(OLEN03,
129-
$"Object {target.ToOutline()} size or length is outside of range"),
129+
$"Object {target.GetOutline()} size or length is outside of range"),
130130
new ExpectedDetail(Function, $"length in range [{minimum}, {maximum}]"),
131131
new ActualDetail(target, $"found {length} that is greater than {maximum}")));
132132
return true;
@@ -137,7 +137,7 @@ public bool Length(JObject target, JInteger minimum, JUndefined undefined)
137137
var length = target.Properties.Count;
138138
if(length < minimum)
139139
return FailWith(new JsonSchemaException(new ErrorDetail(OLEN04,
140-
$"Object {target.ToOutline()} size or length is outside of range"),
140+
$"Object {target.GetOutline()} size or length is outside of range"),
141141
new ExpectedDetail(Function, $"length in range [{minimum}, {undefined}]"),
142142
new ActualDetail(target, $"found {length} that is less than {minimum}")));
143143
return true;
@@ -148,7 +148,7 @@ public bool Length(JObject target, JUndefined undefined, JInteger maximum)
148148
var length = target.Properties.Count;
149149
if(length > maximum)
150150
return FailWith(new JsonSchemaException(new ErrorDetail(OLEN05,
151-
$"Object {target.ToOutline()} size or length is outside of range"),
151+
$"Object {target.GetOutline()} size or length is outside of range"),
152152
new ExpectedDetail(Function, $"length in range [{undefined}, {maximum}]"),
153153
new ActualDetail(target, $"found {length} that is greater than {maximum}")));
154154
return true;

JsonSchema/RelogicLabs/JsonSchema/Functions/CoreFunctions2.cs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,28 @@ public partial class CoreFunctions
1111
public bool Enum(JString target, params JString[] items)
1212
{
1313
if(!items.Contains(target))
14-
return FailWith(new JsonSchemaException(new ErrorDetail(ENUM01,
15-
"String is not in enum list"),
16-
new ExpectedDetail(Function, $"string in list {items.Select(s => s.ToJson())
17-
.ToString(", ", "[", "]")}"),
18-
new ActualDetail(target, $"string {target} is not found in list")));
14+
return FailWith(new JsonSchemaException(
15+
new ErrorDetail(ENUM01, "String is not in enum list"),
16+
new ExpectedDetail(Function, $"string in list {items.ToString(", ", "[", "]")}"),
17+
new ActualDetail(target, $"string {target.GetOutline()} is not found in list")));
1918
return true;
2019
}
2120

2221
public bool Enum(JNumber target, params JNumber[] items)
2322
{
2423
if(!items.Contains(target))
25-
return FailWith(new JsonSchemaException(new ErrorDetail(ENUM02,
26-
"Number is not in enum list"),
27-
new ExpectedDetail(Function, $"number in list {items.Select(s => s.ToJson())
28-
.ToString(", ", "[", "]")}"),
24+
return FailWith(new JsonSchemaException(
25+
new ErrorDetail(ENUM02, "Number is not in enum list"),
26+
new ExpectedDetail(Function, $"number in list {items.ToString(", ", "[", "]")}"),
2927
new ActualDetail(target, $"number {target} is not found in list")));
3028
return true;
3129
}
3230

3331
public bool Positive(JNumber target)
3432
{
3533
if(target.Compare(0) <= 0)
36-
return FailWith(new JsonSchemaException(new ErrorDetail(POSI01,
37-
"Number is not positive"),
34+
return FailWith(new JsonSchemaException(
35+
new ErrorDetail(POSI01, "Number is not positive"),
3836
new ExpectedDetail(Function, "a positive number"),
3937
new ActualDetail(target, $"number {target} is less than or equal to zero")));
4038
return true;
@@ -43,8 +41,8 @@ public bool Positive(JNumber target)
4341
public bool Negative(JNumber target)
4442
{
4543
if(target.Compare(0) >= 0)
46-
return FailWith(new JsonSchemaException(new ErrorDetail(NEGI01,
47-
"Number is not negative"),
44+
return FailWith(new JsonSchemaException(
45+
new ErrorDetail(NEGI01, "Number is not negative"),
4846
new ExpectedDetail(Function, "a negative number"),
4947
new ActualDetail(target, $"number {target} is greater than or equal to zero")));
5048
return true;
@@ -53,13 +51,13 @@ public bool Negative(JNumber target)
5351
public bool Range(JNumber target, JNumber minimum, JNumber maximum)
5452
{
5553
if(target.Compare(minimum) < 0)
56-
return FailWith(new JsonSchemaException(new ErrorDetail(RANG01,
57-
"Number is outside of range"),
54+
return FailWith(new JsonSchemaException(
55+
new ErrorDetail(RANG01, "Number is outside of range"),
5856
new ExpectedDetail(Function, $"number in range [{minimum}, {maximum}]"),
5957
new ActualDetail(target, $"number {target} is less than {minimum}")));
6058
if(target.Compare(maximum) > 0)
61-
return FailWith(new JsonSchemaException(new ErrorDetail(RANG02,
62-
$"Number is outside of range"),
59+
return FailWith(new JsonSchemaException(
60+
new ErrorDetail(RANG02, "Number is outside of range"),
6361
new ExpectedDetail(Function, $"number in range [{minimum}, {maximum}]"),
6462
new ActualDetail(target, $"number {target} is greater than {maximum}")));
6563
return true;
@@ -68,8 +66,8 @@ public bool Range(JNumber target, JNumber minimum, JNumber maximum)
6866
public bool Range(JNumber target, JNumber minimum, JUndefined undefined)
6967
{
7068
if(target.Compare(minimum) < 0)
71-
return FailWith(new JsonSchemaException(new ErrorDetail(RANG03,
72-
"Number is outside of range"),
69+
return FailWith(new JsonSchemaException(
70+
new ErrorDetail(RANG03, "Number is outside of range"),
7371
new ExpectedDetail(Function, $"number in range [{minimum}, {undefined}]"),
7472
new ActualDetail(target, $"number {target} is less than {minimum}")));
7573
return true;
@@ -78,8 +76,8 @@ public bool Range(JNumber target, JNumber minimum, JUndefined undefined)
7876
public bool Range(JNumber target, JUndefined undefined, JNumber maximum)
7977
{
8078
if(target.Compare(maximum) > 0)
81-
return FailWith(new JsonSchemaException(new ErrorDetail(RANG04,
82-
"Number is outside of range"),
79+
return FailWith(new JsonSchemaException(
80+
new ErrorDetail(RANG04, "Number is outside of range"),
8381
new ExpectedDetail(Function, $"number in range [{undefined}, {maximum}]"),
8482
new ActualDetail(target, $"number {target} is greater than {maximum}")));
8583
return true;

0 commit comments

Comments
 (0)