Skip to content

Commit 73c8c0f

Browse files
committed
Add new validation functions
1 parent 37e1afb commit 73c8c0f

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

JsonSchema/RelogicLabs/JsonSchema/Functions/CoreFunctions2.cs

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public bool Enum(JString target, params JString[] items)
1717
new ActualDetail(target, $"string {target.GetOutline()} is not found in list")));
1818
return true;
1919
}
20-
20+
2121
public bool Enum(JNumber target, params JNumber[] items)
2222
{
2323
if(!items.Contains(target))
@@ -28,6 +28,64 @@ public bool Enum(JNumber target, params JNumber[] items)
2828
return true;
2929
}
3030

31+
public bool Minimum(JNumber target, JNumber minimum)
32+
{
33+
if(target.Compare(minimum) < 0)
34+
return FailWith(new JsonSchemaException(
35+
new ErrorDetail(MINI01, "Number is less than provided minimum"),
36+
new ExpectedDetail(Function, $"a number greater than or equal to {minimum}"),
37+
new ActualDetail(target, $"number {target} is less than {minimum}")));
38+
return true;
39+
}
40+
41+
public bool Minimum(JNumber target, JNumber minimum, JBoolean exclusive)
42+
{
43+
if(target.Compare(minimum) < 0)
44+
return FailWith(new JsonSchemaException(
45+
new ErrorDetail(MINI02, "Number is less than provided minimum"),
46+
new ExpectedDetail(Function, $"a number {RelationTo()} {minimum}"),
47+
new ActualDetail(target, $"number {target} is less than {minimum}")));
48+
if(exclusive && target.Compare(minimum) == 0)
49+
return FailWith(new JsonSchemaException(
50+
new ErrorDetail(MINI03, "Number is equal to provided minimum"),
51+
new ExpectedDetail(Function, $"a number {RelationTo()} {minimum}"),
52+
new ActualDetail(target, $"number {target} is equal to {minimum}")));
53+
return true;
54+
55+
string RelationTo() => exclusive
56+
? "greater than"
57+
: "greater than or equal to";
58+
}
59+
60+
public bool Maximum(JNumber target, JNumber maximum)
61+
{
62+
if(target.Compare(maximum) > 0)
63+
return FailWith(new JsonSchemaException(
64+
new ErrorDetail(MAXI01, "Number is greater than provided maximum"),
65+
new ExpectedDetail(Function, $"a number less than or equal {maximum}"),
66+
new ActualDetail(target, $"number {target} is greater than {maximum}")));
67+
return true;
68+
}
69+
70+
public bool Maximum(JNumber target, JNumber maximum, JBoolean exclusive)
71+
{
72+
if(target.Compare(maximum) > 0)
73+
return FailWith(new JsonSchemaException(
74+
new ErrorDetail(MAXI02, "Number is greater than provided maximum"),
75+
new ExpectedDetail(Function, $"a number {RelationTo()} {maximum}"),
76+
new ActualDetail(target, $"number {target} is greater than {maximum}")));
77+
if(exclusive && target.Compare(maximum) == 0)
78+
return FailWith(new JsonSchemaException(
79+
new ErrorDetail(MAXI03, "Number is equal to provided maximum"),
80+
new ExpectedDetail(Function, $"a number {RelationTo()} {maximum}"),
81+
new ActualDetail(target, $"number {target} is equal to {maximum}")));
82+
return true;
83+
84+
string RelationTo() => exclusive
85+
? "less than"
86+
: "less than or equal to";
87+
}
88+
3189
public bool Positive(JNumber target)
3290
{
3391
if(target.Compare(0) <= 0)
@@ -47,7 +105,7 @@ public bool Negative(JNumber target)
47105
new ActualDetail(target, $"number {target} is greater than or equal to zero")));
48106
return true;
49107
}
50-
108+
51109
public bool Range(JNumber target, JNumber minimum, JNumber maximum)
52110
{
53111
if(target.Compare(minimum) < 0)
@@ -62,7 +120,7 @@ public bool Range(JNumber target, JNumber minimum, JNumber maximum)
62120
new ActualDetail(target, $"number {target} is greater than {maximum}")));
63121
return true;
64122
}
65-
123+
66124
public bool Range(JNumber target, JNumber minimum, JUndefined undefined)
67125
{
68126
if(target.Compare(minimum) < 0)
@@ -72,7 +130,7 @@ public bool Range(JNumber target, JNumber minimum, JUndefined undefined)
72130
new ActualDetail(target, $"number {target} is less than {minimum}")));
73131
return true;
74132
}
75-
133+
76134
public bool Range(JNumber target, JUndefined undefined, JNumber maximum)
77135
{
78136
if(target.Compare(maximum) > 0)
@@ -82,7 +140,7 @@ public bool Range(JNumber target, JUndefined undefined, JNumber maximum)
82140
new ActualDetail(target, $"number {target} is greater than {maximum}")));
83141
return true;
84142
}
85-
143+
86144
public bool Nonempty(JString target)
87145
{
88146
var _length = target.Value.Length;
@@ -92,7 +150,7 @@ public bool Nonempty(JString target)
92150
new ActualDetail(target, "found empty string")));
93151
return true;
94152
}
95-
153+
96154
public bool Nonempty(JArray target)
97155
{
98156
var _length = target.Elements.Count;
@@ -102,7 +160,7 @@ public bool Nonempty(JArray target)
102160
new ActualDetail(target, "found empty array")));
103161
return true;
104162
}
105-
163+
106164
public bool Nonempty(JObject target)
107165
{
108166
var _length = target.Properties.Count;

JsonSchema/RelogicLabs/JsonSchema/Message/ErrorCode.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ internal static class ErrorCode
9999
public const string JLEX01 = "JLEX01";
100100
public const string JPRS01 = "JPRS01";
101101
public const string KEYS01 = "KEYS01";
102+
public const string MAXI01 = "MAXI01";
103+
public const string MAXI02 = "MAXI02";
104+
public const string MAXI03 = "MAXI03";
105+
public const string MINI01 = "MINI01";
106+
public const string MINI02 = "MINI02";
107+
public const string MINI03 = "MINI03";
102108
public const string NEGI01 = "NEGI01";
103109
public const string NEMT01 = "NEMT01";
104110
public const string NEMT02 = "NEMT02";

0 commit comments

Comments
 (0)