Skip to content

Commit

Permalink
Merge pull request #6 from relogiclabs/develop
Browse files Browse the repository at this point in the history
Update Data Type Validation
  • Loading branch information
zhossain-info authored Oct 25, 2023
2 parents cecb26a + ad6a762 commit f04a968
Show file tree
Hide file tree
Showing 33 changed files with 573 additions and 325 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public void When_JsonNotArray_ExceptionThrown()
{
var schema = "#array";
var json = "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_JsonNotArrayInObject_ExceptionThrown()
{
Expand All @@ -38,14 +38,14 @@ public void When_JsonNotArrayInObject_ExceptionThrown()
"key3": 100000
}
""";

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_JsonNotArrayInArray_ExceptionThrown()
{
Expand All @@ -57,14 +57,14 @@ public void When_JsonNotArrayInArray_ExceptionThrown()
"""
[{}, "value1", 10.5]
""";

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_NestedJsonNotArrayInArray_ExceptionThrown()
{
Expand All @@ -76,14 +76,14 @@ public void When_NestedJsonNotArrayInArray_ExceptionThrown()
"""
[true, "value1", false]
""";

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_NestedJsonNotArrayInObject_ExceptionThrown()
{
Expand All @@ -99,44 +99,44 @@ public void When_NestedJsonNotArrayInObject_ExceptionThrown()
"key3": "value1"
}
""";

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_ElementsWithWrongArray_ExceptionThrown()
{
var schema = "@elements(10, 20, 30, 40) #array";
var json = "[5, 10, 15, 20, 25]";

JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(ELEM01, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_NestedElementsWithWrongArrayInArray_ExceptionThrown()
{
var schema = "@elements*(5, 10) #array";
var json = "[[5, 10], [], [5, 10, 15, 20]]";

JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(ELEM01, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_EnumWithWrongValueInArray_ExceptionThrown()
{
var schema =
var schema =
"""
[
@enum(5, 10, 15),
Expand All @@ -145,27 +145,27 @@ public void When_EnumWithWrongValueInArray_ExceptionThrown()
] #array
""";
var json = """[11, 102, "efg"]""";

JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(ENUM02, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_InvalidJsonInArray_ExceptionThrown()
{
var schema = "#array";
var json = "[,,]";

//JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonParserException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(JPRS01, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_EmptyArrayInObject_ExceptionThrown()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ public void When_JsonNotBoolean_ExceptionThrown()
{
var schema = "#boolean";
var json = "5";

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_JsonValueNotEqualForBoolean_ExceptionThrown()
{
var schema = "true #boolean";
var json = "false";

JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(BOOL01, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_JsonNotBooleanInObject_ExceptionThrown()
{
Expand All @@ -51,14 +51,14 @@ public void When_JsonNotBooleanInObject_ExceptionThrown()
"key3": true
}
""";

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_JsonNotBooleanInArray_ExceptionThrown()
{
Expand All @@ -70,14 +70,14 @@ public void When_JsonNotBooleanInArray_ExceptionThrown()
"""
[[], 11.5, "false"]
""";

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_NestedJsonNotBooleanInArray_ExceptionThrown()
{
Expand All @@ -89,14 +89,14 @@ public void When_NestedJsonNotBooleanInArray_ExceptionThrown()
"""
["true", {}, [true]]
""";

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_NestedJsonNotBooleanInObject_ExceptionThrown()
{
Expand All @@ -112,7 +112,7 @@ public void When_NestedJsonNotBooleanInObject_ExceptionThrown()
"key3": 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
@@ -0,0 +1,143 @@
using RelogicLabs.JsonSchema.Exceptions;
using static RelogicLabs.JsonSchema.Message.ErrorCode;

namespace RelogicLabs.JsonSchema.Tests.Negative;

[TestClass]
public class DataTypeTests
{
[TestMethod]
public void When_JsonWithWrongMainDataType_ExceptionThrown()
{
var schema =
"""
#string* #array
""";
var json =
"""
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_JsonWithWrongNestedDataType_ExceptionThrown()
{
var schema =
"""
#string* #array
""";
var json =
"""
[10, 20]
""";

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_NestedTypeWithNonCompositeJson_ExceptionThrown()
{
var schema =
"""
#string*
""";
var json =
"""
10
""";

JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(DTYP03, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_UndefinedDataTypeArgument_ExceptionThrown()
{
var schema =
"""
#array($undefined)
""";
var json =
"""
[10, 20]
""";

JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<DefinitionNotFoundException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(DEFI03, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_UndefinedNestedDataTypeArgument_ExceptionThrown()
{
var schema =
"""
#integer*($undefined) #array
""";
var json =
"""
[10, 20]
""";

JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<DefinitionNotFoundException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(DEFI04, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_DataTypeArgumentWithValidationFailed_ExceptionThrown()
{
var schema =
"""
%define $test: {"k1": #string}
%schema: #object($test)
""";
var json =
"""
{"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_NestedDataTypeArgumentWithValidationFailed_ExceptionThrown()
{
var schema =
"""
%define $test: {"k1": #string}
%schema: #object*($test) #array
""";
var json =
"""
[{"k1": 10}]
""";

JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(DTYP04, exception.Code);
Console.WriteLine(exception);
}
}
Loading

0 comments on commit f04a968

Please sign in to comment.