Skip to content

Commit

Permalink
Merge pull request #12 from relogiclabs/develop
Browse files Browse the repository at this point in the history
Update Nested Data Type Validation
  • Loading branch information
zhossain-info committed Dec 24, 2023
2 parents ae4c4a7 + 5082095 commit 9d77c26
Show file tree
Hide file tree
Showing 191 changed files with 3,502 additions and 3,797 deletions.
4 changes: 2 additions & 2 deletions JsonSchema.Tests/JsonSchema.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<Description>This project contains test cases for JsonSchema project.</Description>
<Authors>Relogic Labs</Authors>
<Company>Relogic Labs</Company>
<Version>1.10.0</Version>
<AssemblyVersion>1.10.0</AssemblyVersion>
<Version>1.12.0</Version>
<AssemblyVersion>1.12.0</AssemblyVersion>
<Copyright>Copyright © Relogic Labs. All rights reserved.</Copyright>
<NeutralLanguage>en</NeutralLanguage>
<TargetFrameworks>net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
using RelogicLabs.JsonSchema.Tree;
using RelogicLabs.JsonSchema.Types;

namespace RelogicLabs.JsonSchema.Tests.Negative;
// Functions for negative (error) test cases
namespace RelogicLabs.JsonSchema.Tests.External;

public class ExternalFunctions1
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
using RelogicLabs.JsonSchema.Types;
using RelogicLabs.JsonSchema.Utilities;

namespace RelogicLabs.JsonSchema.Tests.Positive;
// Functions for positive (valid) test cases
namespace RelogicLabs.JsonSchema.Tests.External;

public class ExternalFunctions : FunctionBase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using RelogicLabs.JsonSchema.Exceptions;
using static RelogicLabs.JsonSchema.Message.ErrorCode;
using static RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions;
using static RelogicLabs.JsonSchema.Tests.External.ExternalFunctions;

namespace RelogicLabs.JsonSchema.Tests.Negative;

Expand Down Expand Up @@ -74,7 +74,7 @@ public void When_ExtendedAggregatedTestWithInvalidAccess_ExceptionThrown()
var schema = """
%title: "Extended User Profile Dashboard API Response"
%version: 2.0.0
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions,
RelogicLabs.JsonSchema.Tests
%pragma IgnoreUndefinedProperties: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace RelogicLabs.JsonSchema.Tests.Negative;
public class DataTypeTests
{
[TestMethod]
public void When_JsonWithWrongMainDataType_ExceptionThrown()
public void When_WrongJsonWithDirectDataType_ExceptionThrown()
{
var schema =
"""
Expand All @@ -17,7 +17,6 @@ public void When_JsonWithWrongMainDataType_ExceptionThrown()
"""
10
""";

JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Expand All @@ -26,7 +25,7 @@ public void When_JsonWithWrongMainDataType_ExceptionThrown()
}

[TestMethod]
public void When_JsonWithWrongNestedDataType_ExceptionThrown()
public void When_WrongJsonWithNestedDataType_ExceptionThrown()
{
var schema =
"""
Expand All @@ -36,7 +35,6 @@ public void When_JsonWithWrongNestedDataType_ExceptionThrown()
"""
[10, 20]
""";

JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Expand All @@ -55,7 +53,6 @@ public void When_NestedTypeWithNonCompositeJson_ExceptionThrown()
"""
10
""";

JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Expand All @@ -74,7 +71,6 @@ public void When_UndefinedDataTypeArgument_ExceptionThrown()
"""
[10, 20]
""";

JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<DefinitionNotFoundException>(
() => JsonAssert.IsValid(schema, json));
Expand All @@ -93,7 +89,6 @@ public void When_UndefinedNestedDataTypeArgument_ExceptionThrown()
"""
[10, 20]
""";

JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<DefinitionNotFoundException>(
() => JsonAssert.IsValid(schema, json));
Expand All @@ -113,7 +108,6 @@ public void When_DataTypeArgumentWithValidationFailed_ExceptionThrown()
"""
{"k1": 10}
""";

JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Expand All @@ -133,7 +127,89 @@ public void When_NestedDataTypeArgumentWithValidationFailed_ExceptionThrown()
"""
[{"k1": 10}]
""";
JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(DTYP04, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_MultipleNestedDataTypeWithWrongValueInObject_ExceptionThrown()
{
var schema =
"""
{
"key1": #date* #time* #null* #array,
"key2": #string* #null* #array,
"key3": #integer* #float* #array
}
""";
var json =
"""
{
"key1": ["2021-08-01", "2021-08-01T15:50:30.300Z", "test"],
"key2": ["test", null, "text", 10],
"key3": [false, true, null, "text"]
}
""";
JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(DTYP06, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_DataTypeExceptionCountInObject_ExceptionThrown() {
var schema =
"""
{
"key1": #boolean* #integer #string #array,
"key2": #boolean* #integer #string,
"key3": #boolean* #integer #string #array
}
""";
var json =
"""
{
"key1": [10, "test", "2021-08-01"],
"key2": [10, "test", "2021-08-01"],
"key3": []
}
""";
var jsonSchema = new JsonSchema(schema);
if(!jsonSchema.IsValid(json)) jsonSchema.WriteError();
Assert.AreEqual(8, jsonSchema.Exceptions.Count);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(DTYP06, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_MultipleDataTypeWithWrongValueInObject_ExceptionThrown()
{
var schema =
"""
{
"key1": #date #time #null,
"key2": #array #object #null,
"key3": #integer #float,
"key4": #integer #date #null,
"key5": #number #string #null
}
""";
var json =
"""
{
"key1": "2021-08-01",
"key2": null,
"key3": 100,
"key4": "test",
"key5": false
}
""";
JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void When_ExternalIncludeNotInheritBaseClass_ExceptionThrown()
{
var schema =
"""
%include: RelogicLabs.JsonSchema.Tests.Negative.ExternalFunctions1,
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions1,
RelogicLabs.JsonSchema.Tests
%schema: @odd #integer
""";
Expand All @@ -49,7 +49,7 @@ public void When_ExternalIncludeNotExisting_ExceptionThrown()
{
var schema =
"""
%include: RelogicLabs.JsonSchema.Tests.Negative.NotExisting,
%include: RelogicLabs.JsonSchema.Tests.External.NotExisting,
RelogicLabs.JsonSchema.Tests
%schema: @odd #integer
""";
Expand All @@ -67,9 +67,9 @@ public void When_ExternalIncludeDuplicationOccurred_ExceptionThrown()
{
var schema =
"""
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions,
RelogicLabs.JsonSchema.Tests
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions,
RelogicLabs.JsonSchema.Tests
%schema: @odd #integer
""";
Expand All @@ -87,7 +87,7 @@ public void When_ExternalIncludeInstantiationNotCompleted_ExceptionThrown()
{
var schema =
"""
%include: RelogicLabs.JsonSchema.Tests.Negative.ExternalFunctions5,
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions5,
RelogicLabs.JsonSchema.Tests
%schema: @odd #integer
""";
Expand All @@ -105,7 +105,7 @@ public void When_ExternalFunctionWrongReturnType_ExceptionThrown()
{
var schema =
"""
%include: RelogicLabs.JsonSchema.Tests.Negative.ExternalFunctions2,
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions2,
RelogicLabs.JsonSchema.Tests
%schema: @odd #integer
""";
Expand All @@ -123,7 +123,7 @@ public void When_ExternalFunctionWrongParameterNumber_ExceptionThrown()
{
var schema =
"""
%include: RelogicLabs.JsonSchema.Tests.Negative.ExternalFunctions3,
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions3,
RelogicLabs.JsonSchema.Tests
%schema: @odd #integer
""";
Expand All @@ -141,7 +141,7 @@ public void When_ExternalFunctionNotExists_ExceptionThrown()
{
var schema =
"""
%include: RelogicLabs.JsonSchema.Tests.Negative.ExternalFunctions4,
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions4,
RelogicLabs.JsonSchema.Tests
%schema: @notExist #integer
""";
Expand All @@ -159,7 +159,7 @@ public void When_FunctionThrowArbitraryException_ExceptionThrown()
{
var schema =
"""
%include: RelogicLabs.JsonSchema.Tests.Negative.ExternalFunctions4,
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions4,
RelogicLabs.JsonSchema.Tests
%schema: @canTest #integer
""";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void When_NonStaticValidMethodWithWrongJson_ExceptionThrown()
[TestMethod]
public void When_MandatoryValueMissingInArray_ExceptionThrown()
{
var schema = "[#number, #number?, #number?]";
var schema = "[@range(1, 10) #number, @range(10, 100) #number?, #number?]";
var json = "[]";
JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
Expand All @@ -128,6 +128,18 @@ public void When_MandatoryValueMissingInArray_ExceptionThrown()
Console.WriteLine(exception);
}

[TestMethod]
public void When_OptionalValidatorMisplacedInArray_ExceptionThrown()
{
var schema = "[#number, #number?, #number]";
var json = "[10, 20]";
JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<MisplacedOptionalException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(ARRY02, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_MandatoryPropertyMissingInObject_ExceptionThrown()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using RelogicLabs.JsonSchema.Exceptions;
using static RelogicLabs.JsonSchema.Message.ErrorCode;
using static RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions;
using static RelogicLabs.JsonSchema.Tests.External.ExternalFunctions;

namespace RelogicLabs.JsonSchema.Tests.Negative;

Expand All @@ -12,7 +12,7 @@ public void When_WrongReceiverNameInObject_ExceptionThrown()
{
var schema =
"""
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions,
RelogicLabs.JsonSchema.Tests
%schema:
{
Expand All @@ -39,7 +39,7 @@ public void When_NoValueReceiveInObject_ExceptionThrown()
{
var schema =
"""
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions,
RelogicLabs.JsonSchema.Tests
%schema:
Expand Down Expand Up @@ -67,7 +67,7 @@ public void When_ConditionFailedInObject_ExceptionThrown()
{
var schema =
"""
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions,
RelogicLabs.JsonSchema.Tests
%schema:
Expand Down Expand Up @@ -95,7 +95,7 @@ public void When_ConditionAllFailedInObject_ExceptionThrown()
{
var schema =
"""
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions,
RelogicLabs.JsonSchema.Tests
%define $numbers: @range(1, 10) #integer &relatedValues
Expand Down Expand Up @@ -124,7 +124,7 @@ public void When_ReceiveWrongValuesInObject_ExceptionThrown()
{
var schema =
"""
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions,
RelogicLabs.JsonSchema.Tests
%schema:
Expand Down Expand Up @@ -160,7 +160,7 @@ public void When_MultiReceiverFunctionWrongValuesInObject_ExceptionThrown()
{
var schema =
"""
%include: RelogicLabs.JsonSchema.Tests.Positive.ExternalFunctions,
%include: RelogicLabs.JsonSchema.Tests.External.ExternalFunctions,
RelogicLabs.JsonSchema.Tests
%schema:
Expand Down
Loading

0 comments on commit 9d77c26

Please sign in to comment.