diff --git a/src/NJsonSchema.Tests/Validation/FormatUuidTests.cs b/src/NJsonSchema.Tests/Validation/FormatUuidTests.cs new file mode 100644 index 000000000..ae88bc9da --- /dev/null +++ b/src/NJsonSchema.Tests/Validation/FormatUuidTests.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/src/NJsonSchema/Validation/FormatValidators/UuidFormatValidator.cs b/src/NJsonSchema/Validation/FormatValidators/UuidFormatValidator.cs new file mode 100644 index 000000000..f8ddf4da9 --- /dev/null +++ b/src/NJsonSchema/Validation/FormatValidators/UuidFormatValidator.cs @@ -0,0 +1,34 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Rico Suter. All rights reserved. +// +// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md +// Rico Suter, mail@rsuter.com +//----------------------------------------------------------------------- + +using Newtonsoft.Json.Linq; +using System; + +namespace NJsonSchema.Validation.FormatValidators +{ + /// Validator for "Uuid" format. + public class UuidFormatValidator : IFormatValidator + { + /// Gets the format attribute's value. + #pragma warning disable CS0618 // Type or member is obsolete + public string Format { get; } = JsonFormatStrings.Uuid; + #pragma warning restore CS0618 // Type or member is obsolete + + /// Gets the kind of error produced by validator. + public ValidationErrorKind ValidationErrorKind { get; } = ValidationErrorKind.UuidExpected; + + /// Validates format of given value. + /// String value. + /// Type of token holding the value. + /// True if value is correct for given format, False - if not. + public bool IsValid(string value, JTokenType tokenType) + { + return Guid.TryParse(value, out Guid guid); + } + } +} diff --git a/src/NJsonSchema/Validation/JsonSchemaValidator.cs b/src/NJsonSchema/Validation/JsonSchemaValidator.cs index 0154add03..ab96bd35e 100644 --- a/src/NJsonSchema/Validation/JsonSchemaValidator.cs +++ b/src/NJsonSchema/Validation/JsonSchemaValidator.cs @@ -33,7 +33,8 @@ public class JsonSchemaValidator new TimeSpanFormatValidator(), new UriFormatValidator(), new ByteFormatValidator(), - new Base64FormatValidator() + new Base64FormatValidator(), + new UuidFormatValidator() }; private readonly IDictionary _formatValidatorsMap; diff --git a/src/NJsonSchema/Validation/ValidationErrorKind.cs b/src/NJsonSchema/Validation/ValidationErrorKind.cs index 84d5d08ee..ffbff6a5c 100644 --- a/src/NJsonSchema/Validation/ValidationErrorKind.cs +++ b/src/NJsonSchema/Validation/ValidationErrorKind.cs @@ -141,6 +141,9 @@ public enum ValidationErrorKind Base64Expected, /// No type of the types does validate (check error details in ). - NoTypeValidates + NoTypeValidates, + + /// A valid UUID is expected. + UuidExpected, } } \ No newline at end of file