Skip to content

Commit

Permalink
Added uuid format for schema validation (#1325)
Browse files Browse the repository at this point in the history
- Created new uuid format validator
- Added testing for format
  • Loading branch information
martin-shields-sage authored Feb 24, 2021
1 parent e83515b commit 8f245db
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
46 changes: 46 additions & 0 deletions src/NJsonSchema.Tests/Validation/FormatUuidTests.cs
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 src/NJsonSchema/Validation/FormatValidators/UuidFormatValidator.cs
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);
}
}
}
3 changes: 2 additions & 1 deletion src/NJsonSchema/Validation/JsonSchemaValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public class JsonSchemaValidator
new TimeSpanFormatValidator(),
new UriFormatValidator(),
new ByteFormatValidator(),
new Base64FormatValidator()
new Base64FormatValidator(),
new UuidFormatValidator()
};

private readonly IDictionary<string, IFormatValidator[]> _formatValidatorsMap;
Expand Down
5 changes: 4 additions & 1 deletion src/NJsonSchema/Validation/ValidationErrorKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ public enum ValidationErrorKind
Base64Expected,

/// <summary>No type of the types does validate (check error details in <see cref="MultiTypeValidationError"/>). </summary>
NoTypeValidates
NoTypeValidates,

/// <summary>A valid UUID is expected. </summary>
UuidExpected,
}
}

0 comments on commit 8f245db

Please sign in to comment.