-
-
Notifications
You must be signed in to change notification settings - Fork 538
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/RicoSuter/NJsonSchema
- Loading branch information
Showing
16 changed files
with
163 additions
and
13 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
25 changes: 25 additions & 0 deletions
25
src/NJsonSchema.CodeGeneration.CSharp.Tests/References/Animal.json
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,25 @@ | ||
{ | ||
"title": "Animal", | ||
"type": "object", | ||
"x-abstract": true, | ||
"discriminator": "discriminator", | ||
|
||
"definitions": { | ||
"Cat": { | ||
"type": "object", | ||
"allOf": [ | ||
{ | ||
"$ref": "Animal.json" | ||
} | ||
] | ||
}, | ||
"PersianCat": { | ||
"type": "object", | ||
"allOf": [ | ||
{ | ||
"$ref": "#/definitions/Cat" | ||
} | ||
] | ||
} | ||
} | ||
} |
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
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
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
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,46 @@ | ||
using System; | ||
using System.Linq; | ||
using Newtonsoft.Json.Linq; | ||
using NJsonSchema.Validation; | ||
using Xunit; | ||
|
||
namespace NJsonSchema.Tests.Validation | ||
{ | ||
public class FormatUuidTests | ||
{ | ||
[Fact] | ||
public void When_format_uuid_incorrect_then_validation_succeeds() | ||
{ | ||
//// Arrange | ||
var schema = new JsonSchema(); | ||
schema.Type = JsonObjectType.String; | ||
schema.Format = JsonFormatStrings.Uuid; | ||
|
||
var token = new JValue("test"); | ||
|
||
//// Act | ||
var errors = schema.Validate(token); | ||
|
||
//// Assert | ||
Assert.Equal(ValidationErrorKind.UuidExpected, errors.First().Kind); | ||
} | ||
|
||
[Fact] | ||
public void When_format_uuid_correct_then_validation_succeeds() | ||
{ | ||
//// Arrange | ||
var schema = new JsonSchema(); | ||
schema.Type = JsonObjectType.String; | ||
schema.Format = JsonFormatStrings.Uuid; | ||
|
||
var uuid = Guid.NewGuid().ToString(); | ||
var token = new JValue(uuid); | ||
|
||
//// Act | ||
var errors = schema.Validate(token); | ||
|
||
//// Assert | ||
Assert.Empty(errors); | ||
} | ||
} | ||
} |
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
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
34 changes: 34 additions & 0 deletions
34
src/NJsonSchema/Validation/FormatValidators/UuidFormatValidator.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,34 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="UuidFormatValidator.cs" company="NJsonSchema"> | ||
// Copyright (c) Rico Suter. All rights reserved. | ||
// </copyright> | ||
// <license>https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md</license> | ||
// <author>Rico Suter, [email protected]</author> | ||
//----------------------------------------------------------------------- | ||
|
||
using Newtonsoft.Json.Linq; | ||
using System; | ||
|
||
namespace NJsonSchema.Validation.FormatValidators | ||
{ | ||
/// <summary>Validator for "Uuid" format.</summary> | ||
public class UuidFormatValidator : IFormatValidator | ||
{ | ||
/// <summary>Gets the format attribute's value.</summary> | ||
#pragma warning disable CS0618 // Type or member is obsolete | ||
public string Format { get; } = JsonFormatStrings.Uuid; | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
|
||
/// <summary>Gets the kind of error produced by validator.</summary> | ||
public ValidationErrorKind ValidationErrorKind { get; } = ValidationErrorKind.UuidExpected; | ||
|
||
/// <summary>Validates format of given value.</summary> | ||
/// <param name="value">String value.</param> | ||
/// <param name="tokenType">Type of token holding the value.</param> | ||
/// <returns>True if value is correct for given format, False - if not.</returns> | ||
public bool IsValid(string value, JTokenType tokenType) | ||
{ | ||
return Guid.TryParse(value, out Guid guid); | ||
} | ||
} | ||
} |
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