-
-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added uuid format for schema validation (#1325)
- Created new uuid format validator - Added testing for format
- Loading branch information
1 parent
e83515b
commit 8f245db
Showing
4 changed files
with
86 additions
and
2 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
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); | ||
} | ||
} | ||
} |
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, mail@rsuter.com</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