Skip to content

Commit 9d77c26

Browse files
committed
Merge pull request #12 from relogiclabs/develop
Update Nested Data Type Validation
2 parents ae4c4a7 + 5082095 commit 9d77c26

File tree

191 files changed

+3502
-3797
lines changed

Some content is hidden

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

191 files changed

+3502
-3797
lines changed

JsonSchema.Tests/JsonSchema.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<Description>This project contains test cases for JsonSchema project.</Description>
66
<Authors>Relogic Labs</Authors>
77
<Company>Relogic Labs</Company>
8-
<Version>1.10.0</Version>
9-
<AssemblyVersion>1.10.0</AssemblyVersion>
8+
<Version>1.12.0</Version>
9+
<AssemblyVersion>1.12.0</AssemblyVersion>
1010
<Copyright>Copyright © Relogic Labs. All rights reserved.</Copyright>
1111
<NeutralLanguage>en</NeutralLanguage>
1212
<TargetFrameworks>net5.0;net6.0;net7.0;net8.0</TargetFrameworks>

JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/Negative/ExternalFunctions.cs renamed to JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/External/ExternalErrorTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
using RelogicLabs.JsonSchema.Tree;
33
using RelogicLabs.JsonSchema.Types;
44

5-
namespace RelogicLabs.JsonSchema.Tests.Negative;
5+
// Functions for negative (error) test cases
6+
namespace RelogicLabs.JsonSchema.Tests.External;
67

78
public class ExternalFunctions1
89
{

JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/Positive/ExternalFunctions.cs renamed to JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/External/ExternalFunctions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
using RelogicLabs.JsonSchema.Types;
66
using RelogicLabs.JsonSchema.Utilities;
77

8-
namespace RelogicLabs.JsonSchema.Tests.Positive;
8+
// Functions for positive (valid) test cases
9+
namespace RelogicLabs.JsonSchema.Tests.External;
910

1011
public class ExternalFunctions : FunctionBase
1112
{

JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/Negative/AggregatedTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using RelogicLabs.JsonSchema.Exceptions;
22
using static RelogicLabs.JsonSchema.Message.ErrorCode;
3-
using static RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions;
3+
using static RelogicLabs.JsonSchema.Tests.External.ExternalFunctions;
44

55
namespace RelogicLabs.JsonSchema.Tests.Negative;
66

@@ -74,7 +74,7 @@ public void When_ExtendedAggregatedTestWithInvalidAccess_ExceptionThrown()
7474
var schema = """
7575
%title: "Extended User Profile Dashboard API Response"
7676
%version: 2.0.0
77-
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
77+
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions,
7878
RelogicLabs.JsonSchema.Tests
7979
8080
%pragma IgnoreUndefinedProperties: true

JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/Negative/DataTypeTests.cs

Lines changed: 84 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace RelogicLabs.JsonSchema.Tests.Negative;
77
public class DataTypeTests
88
{
99
[TestMethod]
10-
public void When_JsonWithWrongMainDataType_ExceptionThrown()
10+
public void When_WrongJsonWithDirectDataType_ExceptionThrown()
1111
{
1212
var schema =
1313
"""
@@ -17,7 +17,6 @@ public void When_JsonWithWrongMainDataType_ExceptionThrown()
1717
"""
1818
10
1919
""";
20-
2120
JsonSchema.IsValid(schema, json);
2221
var exception = Assert.ThrowsException<JsonSchemaException>(
2322
() => JsonAssert.IsValid(schema, json));
@@ -26,7 +25,7 @@ public void When_JsonWithWrongMainDataType_ExceptionThrown()
2625
}
2726

2827
[TestMethod]
29-
public void When_JsonWithWrongNestedDataType_ExceptionThrown()
28+
public void When_WrongJsonWithNestedDataType_ExceptionThrown()
3029
{
3130
var schema =
3231
"""
@@ -36,7 +35,6 @@ public void When_JsonWithWrongNestedDataType_ExceptionThrown()
3635
"""
3736
[10, 20]
3837
""";
39-
4038
JsonSchema.IsValid(schema, json);
4139
var exception = Assert.ThrowsException<JsonSchemaException>(
4240
() => JsonAssert.IsValid(schema, json));
@@ -55,7 +53,6 @@ public void When_NestedTypeWithNonCompositeJson_ExceptionThrown()
5553
"""
5654
10
5755
""";
58-
5956
JsonSchema.IsValid(schema, json);
6057
var exception = Assert.ThrowsException<JsonSchemaException>(
6158
() => JsonAssert.IsValid(schema, json));
@@ -74,7 +71,6 @@ public void When_UndefinedDataTypeArgument_ExceptionThrown()
7471
"""
7572
[10, 20]
7673
""";
77-
7874
JsonSchema.IsValid(schema, json);
7975
var exception = Assert.ThrowsException<DefinitionNotFoundException>(
8076
() => JsonAssert.IsValid(schema, json));
@@ -93,7 +89,6 @@ public void When_UndefinedNestedDataTypeArgument_ExceptionThrown()
9389
"""
9490
[10, 20]
9591
""";
96-
9792
JsonSchema.IsValid(schema, json);
9893
var exception = Assert.ThrowsException<DefinitionNotFoundException>(
9994
() => JsonAssert.IsValid(schema, json));
@@ -113,7 +108,6 @@ public void When_DataTypeArgumentWithValidationFailed_ExceptionThrown()
113108
"""
114109
{"k1": 10}
115110
""";
116-
117111
JsonSchema.IsValid(schema, json);
118112
var exception = Assert.ThrowsException<JsonSchemaException>(
119113
() => JsonAssert.IsValid(schema, json));
@@ -133,7 +127,89 @@ public void When_NestedDataTypeArgumentWithValidationFailed_ExceptionThrown()
133127
"""
134128
[{"k1": 10}]
135129
""";
130+
JsonSchema.IsValid(schema, json);
131+
var exception = Assert.ThrowsException<JsonSchemaException>(
132+
() => JsonAssert.IsValid(schema, json));
133+
Assert.AreEqual(DTYP04, exception.Code);
134+
Console.WriteLine(exception);
135+
}
136+
137+
[TestMethod]
138+
public void When_MultipleNestedDataTypeWithWrongValueInObject_ExceptionThrown()
139+
{
140+
var schema =
141+
"""
142+
{
143+
"key1": #date* #time* #null* #array,
144+
"key2": #string* #null* #array,
145+
"key3": #integer* #float* #array
146+
}
147+
""";
148+
var json =
149+
"""
150+
{
151+
"key1": ["2021-08-01", "2021-08-01T15:50:30.300Z", "test"],
152+
"key2": ["test", null, "text", 10],
153+
"key3": [false, true, null, "text"]
154+
}
155+
""";
156+
JsonSchema.IsValid(schema, json);
157+
var exception = Assert.ThrowsException<JsonSchemaException>(
158+
() => JsonAssert.IsValid(schema, json));
159+
Assert.AreEqual(DTYP06, exception.Code);
160+
Console.WriteLine(exception);
161+
}
162+
163+
[TestMethod]
164+
public void When_DataTypeExceptionCountInObject_ExceptionThrown() {
165+
var schema =
166+
"""
167+
{
168+
"key1": #boolean* #integer #string #array,
169+
"key2": #boolean* #integer #string,
170+
"key3": #boolean* #integer #string #array
171+
}
172+
""";
173+
var json =
174+
"""
175+
{
176+
"key1": [10, "test", "2021-08-01"],
177+
"key2": [10, "test", "2021-08-01"],
178+
"key3": []
179+
}
180+
""";
181+
var jsonSchema = new JsonSchema(schema);
182+
if(!jsonSchema.IsValid(json)) jsonSchema.WriteError();
183+
Assert.AreEqual(8, jsonSchema.Exceptions.Count);
184+
var exception = Assert.ThrowsException<JsonSchemaException>(
185+
() => JsonAssert.IsValid(schema, json));
186+
Assert.AreEqual(DTYP06, exception.Code);
187+
Console.WriteLine(exception);
188+
}
136189

190+
[TestMethod]
191+
public void When_MultipleDataTypeWithWrongValueInObject_ExceptionThrown()
192+
{
193+
var schema =
194+
"""
195+
{
196+
"key1": #date #time #null,
197+
"key2": #array #object #null,
198+
"key3": #integer #float,
199+
"key4": #integer #date #null,
200+
"key5": #number #string #null
201+
}
202+
""";
203+
var json =
204+
"""
205+
{
206+
"key1": "2021-08-01",
207+
"key2": null,
208+
"key3": 100,
209+
"key4": "test",
210+
"key5": false
211+
}
212+
""";
137213
JsonSchema.IsValid(schema, json);
138214
var exception = Assert.ThrowsException<JsonSchemaException>(
139215
() => JsonAssert.IsValid(schema, json));

JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/Negative/FunctionTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void When_ExternalIncludeNotInheritBaseClass_ExceptionThrown()
3131
{
3232
var schema =
3333
"""
34-
%include: RelogicLabs.JsonSchema.Tests.Negative.ExternalFunctions1,
34+
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions1,
3535
RelogicLabs.JsonSchema.Tests
3636
%schema: @odd #integer
3737
""";
@@ -49,7 +49,7 @@ public void When_ExternalIncludeNotExisting_ExceptionThrown()
4949
{
5050
var schema =
5151
"""
52-
%include: RelogicLabs.JsonSchema.Tests.Negative.NotExisting,
52+
%include: RelogicLabs.JsonSchema.Tests.External.NotExisting,
5353
RelogicLabs.JsonSchema.Tests
5454
%schema: @odd #integer
5555
""";
@@ -67,9 +67,9 @@ public void When_ExternalIncludeDuplicationOccurred_ExceptionThrown()
6767
{
6868
var schema =
6969
"""
70-
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
70+
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions,
7171
RelogicLabs.JsonSchema.Tests
72-
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
72+
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions,
7373
RelogicLabs.JsonSchema.Tests
7474
%schema: @odd #integer
7575
""";
@@ -87,7 +87,7 @@ public void When_ExternalIncludeInstantiationNotCompleted_ExceptionThrown()
8787
{
8888
var schema =
8989
"""
90-
%include: RelogicLabs.JsonSchema.Tests.Negative.ExternalFunctions5,
90+
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions5,
9191
RelogicLabs.JsonSchema.Tests
9292
%schema: @odd #integer
9393
""";
@@ -105,7 +105,7 @@ public void When_ExternalFunctionWrongReturnType_ExceptionThrown()
105105
{
106106
var schema =
107107
"""
108-
%include: RelogicLabs.JsonSchema.Tests.Negative.ExternalFunctions2,
108+
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions2,
109109
RelogicLabs.JsonSchema.Tests
110110
%schema: @odd #integer
111111
""";
@@ -123,7 +123,7 @@ public void When_ExternalFunctionWrongParameterNumber_ExceptionThrown()
123123
{
124124
var schema =
125125
"""
126-
%include: RelogicLabs.JsonSchema.Tests.Negative.ExternalFunctions3,
126+
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions3,
127127
RelogicLabs.JsonSchema.Tests
128128
%schema: @odd #integer
129129
""";
@@ -141,7 +141,7 @@ public void When_ExternalFunctionNotExists_ExceptionThrown()
141141
{
142142
var schema =
143143
"""
144-
%include: RelogicLabs.JsonSchema.Tests.Negative.ExternalFunctions4,
144+
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions4,
145145
RelogicLabs.JsonSchema.Tests
146146
%schema: @notExist #integer
147147
""";
@@ -159,7 +159,7 @@ public void When_FunctionThrowArbitraryException_ExceptionThrown()
159159
{
160160
var schema =
161161
"""
162-
%include: RelogicLabs.JsonSchema.Tests.Negative.ExternalFunctions4,
162+
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions4,
163163
RelogicLabs.JsonSchema.Tests
164164
%schema: @canTest #integer
165165
""";

JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/Negative/OtherTests.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public void When_NonStaticValidMethodWithWrongJson_ExceptionThrown()
119119
[TestMethod]
120120
public void When_MandatoryValueMissingInArray_ExceptionThrown()
121121
{
122-
var schema = "[#number, #number?, #number?]";
122+
var schema = "[@range(1, 10) #number, @range(10, 100) #number?, #number?]";
123123
var json = "[]";
124124
JsonSchema.IsValid(schema, json);
125125
var exception = Assert.ThrowsException<JsonSchemaException>(
@@ -128,6 +128,18 @@ public void When_MandatoryValueMissingInArray_ExceptionThrown()
128128
Console.WriteLine(exception);
129129
}
130130

131+
[TestMethod]
132+
public void When_OptionalValidatorMisplacedInArray_ExceptionThrown()
133+
{
134+
var schema = "[#number, #number?, #number]";
135+
var json = "[10, 20]";
136+
JsonSchema.IsValid(schema, json);
137+
var exception = Assert.ThrowsException<MisplacedOptionalException>(
138+
() => JsonAssert.IsValid(schema, json));
139+
Assert.AreEqual(ARRY02, exception.Code);
140+
Console.WriteLine(exception);
141+
}
142+
131143
[TestMethod]
132144
public void When_MandatoryPropertyMissingInObject_ExceptionThrown()
133145
{

JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/Negative/ReceiverTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using RelogicLabs.JsonSchema.Exceptions;
22
using static RelogicLabs.JsonSchema.Message.ErrorCode;
3-
using static RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions;
3+
using static RelogicLabs.JsonSchema.Tests.External.ExternalFunctions;
44

55
namespace RelogicLabs.JsonSchema.Tests.Negative;
66

@@ -12,7 +12,7 @@ public void When_WrongReceiverNameInObject_ExceptionThrown()
1212
{
1313
var schema =
1414
"""
15-
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
15+
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions,
1616
RelogicLabs.JsonSchema.Tests
1717
%schema:
1818
{
@@ -39,7 +39,7 @@ public void When_NoValueReceiveInObject_ExceptionThrown()
3939
{
4040
var schema =
4141
"""
42-
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
42+
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions,
4343
RelogicLabs.JsonSchema.Tests
4444
4545
%schema:
@@ -67,7 +67,7 @@ public void When_ConditionFailedInObject_ExceptionThrown()
6767
{
6868
var schema =
6969
"""
70-
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
70+
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions,
7171
RelogicLabs.JsonSchema.Tests
7272
7373
%schema:
@@ -95,7 +95,7 @@ public void When_ConditionAllFailedInObject_ExceptionThrown()
9595
{
9696
var schema =
9797
"""
98-
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
98+
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions,
9999
RelogicLabs.JsonSchema.Tests
100100
101101
%define $numbers: @range(1, 10) #integer &relatedValues
@@ -124,7 +124,7 @@ public void When_ReceiveWrongValuesInObject_ExceptionThrown()
124124
{
125125
var schema =
126126
"""
127-
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
127+
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions,
128128
RelogicLabs.JsonSchema.Tests
129129
130130
%schema:
@@ -160,7 +160,7 @@ public void When_MultiReceiverFunctionWrongValuesInObject_ExceptionThrown()
160160
{
161161
var schema =
162162
"""
163-
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
163+
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions,
164164
RelogicLabs.JsonSchema.Tests
165165
166166
%schema:

0 commit comments

Comments
 (0)