Skip to content

Commit e828fe9

Browse files
committed
Add and update validation functions
1 parent 7469d2d commit e828fe9

File tree

5 files changed

+59
-47
lines changed

5 files changed

+59
-47
lines changed

JsonSchema/JsonSchema.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
<AssemblyName>RelogicLabs.JsonSchema</AssemblyName>
99
<Authors>Relogic Labs</Authors>
1010
<Company>Relogic Labs</Company>
11-
<Version>1.9.0</Version>
12-
<PackageVersion>1.9.0</PackageVersion>
13-
<AssemblyVersion>1.9.0</AssemblyVersion>
11+
<Version>1.10.0</Version>
12+
<PackageVersion>1.10.0</PackageVersion>
13+
<AssemblyVersion>1.10.0</AssemblyVersion>
1414
<PackageTags>JsonSchema;Schema;Json;Validation;Assert;Test</PackageTags>
1515
<Copyright>Copyright © Relogic Labs. All rights reserved.</Copyright>
1616
<NeutralLanguage>en</NeutralLanguage>
17-
<TargetFrameworks>net5.0;net6.0;net7.0</TargetFrameworks>
17+
<TargetFrameworks>net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
1818
<ImplicitUsings>enable</ImplicitUsings>
1919
<Nullable>enable</Nullable>
2020
<RootNamespace>RelogicLabs</RootNamespace>

JsonSchema/RelogicLabs/JsonSchema/Functions/CoreFunctions2.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,26 @@ public bool Negative(JNumber target)
106106
return true;
107107
}
108108

109+
public bool Positive(JNumber target, JNumber reference)
110+
{
111+
if(target.Compare(reference) < 0)
112+
return FailWith(new JsonSchemaException(
113+
new ErrorDetail(POSI02, "Number is not positive from reference"),
114+
new ExpectedDetail(Function, $"a positive number from {reference}"),
115+
new ActualDetail(target, $"number {target} is less than reference")));
116+
return true;
117+
}
118+
119+
public bool Negative(JNumber target, JNumber reference)
120+
{
121+
if(target.Compare(reference) > 0)
122+
return FailWith(new JsonSchemaException(
123+
new ErrorDetail(NEGI02, "Number is not negative from reference"),
124+
new ExpectedDetail(Function, $"a negative number from {reference}"),
125+
new ActualDetail(target, $"number {target} is greater than reference")));
126+
return true;
127+
}
128+
109129
public bool Range(JNumber target, JNumber minimum, JNumber maximum)
110130
{
111131
if(target.Compare(minimum) < 0)

JsonSchema/RelogicLabs/JsonSchema/Functions/CoreFunctions4.cs

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -53,60 +53,50 @@ public bool Range(JDateTime target, JString start, JString end)
5353
var _end = GetDateTime(target.GetDateTimeParser(), end);
5454
if(ReferenceEquals(_end, null)) return false;
5555
if(target.DateTime.Compare(_start.DateTime) < 0)
56-
{
57-
var type = target.DateTime.Type;
58-
var code = type == DATE_TYPE ? DRNG01 : DRNG02;
59-
return FailWith(new JsonSchemaException(
60-
new ErrorDetail(code, $"{type} is earlier than start {type}"),
61-
new ExpectedDetail(_start, $"a {type} from or after {_start}"),
62-
new ActualDetail(target, $"found {target} which is before start {type}")
63-
));
64-
}
56+
return FailOnStartDate(target, _start, GetErrorCode(target, DRNG01, DRNG02));
6557
if(target.DateTime.Compare(_end.DateTime) > 0)
66-
{
67-
var type = target.DateTime.Type;
68-
var code = type == DATE_TYPE ? DRNG03 : DRNG04;
69-
return FailWith(new JsonSchemaException(
70-
new ErrorDetail(code, $"{type} is later than end {type}"),
71-
new ExpectedDetail(_end, $"a {type} until or before {_end}"),
72-
new ActualDetail(target, $"found {target} which is after end {type}")
73-
));
74-
}
58+
return FailOnEndDate(target, _end, GetErrorCode(target, DRNG03, DRNG04));
7559
return true;
7660
}
7761

62+
private static string GetErrorCode(JDateTime target, string date, string time) {
63+
return target.DateTime.Type == DATE_TYPE ? date : time;
64+
}
65+
66+
private bool FailOnStartDate(JDateTime target, JDateTime start, string code)
67+
{
68+
var type = target.DateTime.Type;
69+
return FailWith(new JsonSchemaException(
70+
new ErrorDetail(code, $"{type} is earlier than start {type}"),
71+
new ExpectedDetail(start, $"a {type} from or after {start}"),
72+
new ActualDetail(target, $"found {target} which is before start {type}")
73+
));
74+
}
75+
76+
private bool FailOnEndDate(JDateTime target, JDateTime end, string code)
77+
{
78+
var type = target.DateTime.Type;
79+
return FailWith(new JsonSchemaException(
80+
new ErrorDetail(code, $"{type} is later than end {type}"),
81+
new ExpectedDetail(end, $"a {type} until or before {end}"),
82+
new ActualDetail(target, $"found {target} which is after end {type}")
83+
));
84+
}
85+
7886
public bool Range(JDateTime target, JUndefined start, JString end)
7987
{
8088
var _end = GetDateTime(target.GetDateTimeParser(), end);
8189
if(ReferenceEquals(_end, null)) return false;
82-
if(target.DateTime.Compare(_end.DateTime) > 0)
83-
{
84-
var type = target.DateTime.Type;
85-
var code = type == DATE_TYPE ? DRNG05 : DRNG06;
86-
return FailWith(new JsonSchemaException(
87-
new ErrorDetail(code, $"{type} is later than end {type}"),
88-
new ExpectedDetail(_end, $"a {type} until or before {_end}"),
89-
new ActualDetail(target, $"found {target} which is after end {type}")
90-
));
91-
}
92-
return true;
90+
if(target.DateTime.Compare(_end.DateTime) <= 0) return true;
91+
return FailOnEndDate(target, _end, GetErrorCode(target, DRNG05, DRNG06));
9392
}
9493

9594
public bool Range(JDateTime target, JString start, JUndefined end)
9695
{
9796
var _start = GetDateTime(target.GetDateTimeParser(), start);
9897
if(ReferenceEquals(_start, null)) return false;
99-
if(target.DateTime.Compare(_start.DateTime) < 0)
100-
{
101-
var type = target.DateTime.Type;
102-
var code = type == DATE_TYPE ? DRNG07 : DRNG08;
103-
return FailWith(new JsonSchemaException(
104-
new ErrorDetail(code, $"{type} is earlier than start {type}"),
105-
new ExpectedDetail(_start, $"a {type} from or after {_start}"),
106-
new ActualDetail(target, $"found {target} which is before start {type}")
107-
));
108-
}
109-
return true;
98+
if(target.DateTime.Compare(_start.DateTime) >= 0) return true;
99+
return FailOnStartDate(target, _start, GetErrorCode(target, DRNG07, DRNG08));
110100
}
111101

112102
public bool Start(JDateTime target, JString reference)
@@ -149,7 +139,7 @@ public bool End(JDateTime target, JString reference)
149139
&& _result.DateTime.Type == parser.Type) return _result;
150140
var _dateTime = new DateTimeAgent(parser).Parse(Function, dateTime);
151141
if(ReferenceEquals(_dateTime, null)) return null;
152-
dateTime.Derived = _dateTime.Create(dateTime);
142+
dateTime.Derived = _dateTime.CreateNode(dateTime);
153143
return (JDateTime) dateTime.Derived;
154144
}
155145
}

JsonSchema/RelogicLabs/JsonSchema/Message/ErrorCode.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ internal static class ErrorCode
121121
public const string MINI02 = "MINI02";
122122
public const string MINI03 = "MINI03";
123123
public const string NEGI01 = "NEGI01";
124+
public const string NEGI02 = "NEGI02";
124125
public const string NEMT01 = "NEMT01";
125126
public const string NEMT02 = "NEMT02";
126127
public const string NEMT03 = "NEMT03";
@@ -131,6 +132,7 @@ internal static class ErrorCode
131132
public const string OLEN05 = "OLEN05";
132133
public const string PHON01 = "PHON01";
133134
public const string POSI01 = "POSI01";
135+
public const string POSI02 = "POSI02";
134136
public const string PRAG01 = "PRAG01";
135137
public const string PRAG02 = "PRAG02";
136138
public const string PRAG03 = "PRAG03";

JsonSchema/RelogicLabs/JsonSchema/Message/ExpectedDetail.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public ExpectedDetail(JNode node, string message)
1212
: base(node, message) { }
1313

1414
internal static ExpectedDetail AsArrayElementNotFound(JNode node, int index)
15-
=> new(node, $"element at {index} '{node.GetOutline()}'");
15+
=> new(node, $"'{node.GetOutline()}' element at {index}");
1616

1717
internal static ExpectedDetail AsValueMismatch(JNode node)
1818
=> new(node, $"value {node.GetOutline()}");
@@ -21,7 +21,7 @@ internal static ExpectedDetail AsUndefinedProperty(JObject @object, JProperty pr
2121
=> new(@object, $"no property with key '{property.Key}'");
2222

2323
internal static ExpectedDetail AsPropertyNotFound(JProperty property)
24-
=> new(property, $"property '{property.GetOutline()}'");
24+
=> new(property, $"property {{{property.GetOutline()}}}");
2525

2626
internal static ExpectedDetail AsDataTypeMismatch(JNode node)
2727
=> new(node, $"{GetTypeName(node)} inferred by {node.GetOutline()}");

0 commit comments

Comments
 (0)