-
Notifications
You must be signed in to change notification settings - Fork 3
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 #251 from NoxOrg/types-url
Types url
- Loading branch information
Showing
8 changed files
with
340 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,9 @@ | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
|
||
namespace Nox.Types.EntityFramework.Types; | ||
|
||
public class UrlConverter : ValueConverter<Url, string> | ||
{ | ||
public UrlConverter() : base(url => url.Value.AbsoluteUri, | ||
url => Url.From(url)) { } | ||
} |
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,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Nox.Types; | ||
|
||
public sealed class Url : ValueObject<System.Uri, Url> | ||
{ | ||
|
||
public static Url From(string value) | ||
{ | ||
if (!System.Uri.TryCreate(value, System.UriKind.Absolute, out var uriValue)) | ||
{ | ||
throw new TypeValidationException( | ||
new List<ValidationFailure> { | ||
new ValidationFailure("Uri", $"The string '{value}' you provided, is not a valid Uri.") }); | ||
} | ||
|
||
var newObject = new Url | ||
{ | ||
Value = uriValue | ||
}; | ||
|
||
return newObject; | ||
} | ||
|
||
internal override ValidationResult Validate() | ||
{ | ||
var result = base.Validate(); | ||
|
||
bool isValid = System.Uri.TryCreate(base.Value.ToString(), UriKind.Absolute, out var uriResult) && | ||
(uriResult.Scheme == System.Uri.UriSchemeHttps | ||
|| uriResult.Scheme == System.Uri.UriSchemeHttp | ||
|| uriResult.Scheme == System.Uri.UriSchemeMailto | ||
|| uriResult.Scheme == System.Uri.UriSchemeFtp | ||
|| uriResult.Scheme == System.Uri.UriSchemeFile); | ||
|
||
if (!isValid) | ||
{ | ||
result.Errors.Add(new ValidationFailure(nameof(Value), | ||
$"Could not create a Nox Url type; as value {Value} is not a valid Url.")); | ||
} | ||
|
||
return result; | ||
} | ||
} |
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 FluentAssertions; | ||
|
||
namespace Nox.Types.Tests.Types; | ||
|
||
public class UrlTests | ||
{ | ||
[Theory] | ||
[InlineData("https://www.google.com")] | ||
[InlineData("https://www.google.com/")] | ||
[InlineData("https://www.google.com/test")] | ||
[InlineData("https://www.google.com/test/test1?q=123")] | ||
[InlineData("https://example.org/test/test1?search=test-question#part2")] | ||
[InlineData("file://website.com/pathtofile/intro.pdf")] | ||
[InlineData("mailto:abc@iwgplc.com")] | ||
public void UrlFromUri_ShouldBe_Validated(string url) | ||
{ | ||
var expected = new System.Uri(url); | ||
var actual = Url.From(expected); | ||
actual.Value.AbsoluteUri.Should().Be(expected.AbsoluteUri); | ||
} | ||
|
||
[Theory] | ||
[InlineData("https://www.google.com/")] | ||
[InlineData("https://www.google.com/test")] | ||
[InlineData("https://www.google.com/test/test1?q=123")] | ||
[InlineData("https://example.org/test/test1?search=test-question#part2")] | ||
[InlineData("file://website.com/pathtofile/intro.pdf")] | ||
[InlineData("mailto:abc@iwgplc.com")] | ||
public void UrlFromString_ShouldBe_Validated(string expected) | ||
{ | ||
var actual = Url.From(expected); | ||
actual.Value.AbsoluteUri.Should().Be(expected); | ||
} | ||
|
||
|
||
[Theory] | ||
[InlineData("abc://www.google.com/")] | ||
public void BadUrl_Should_ThrowException(string input) | ||
{ | ||
var uri = new System.Uri(input); | ||
Action init = () => { var url = Url.From(uri); }; | ||
|
||
init.Should().Throw<TypeValidationException>(); | ||
} | ||
|
||
} |