-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from relogiclabs/develop
Update Data Type Validation
- Loading branch information
Showing
33 changed files
with
573 additions
and
325 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/Negative/DataTypeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.