Skip to content

Commit

Permalink
[release/8.0-preview3] Remove resource and endpoint name validation (#…
Browse files Browse the repository at this point in the history
…2093)

* [release/8.0-preview3] Remove resource and endpoint name validation

* Remove code rather than comment out
  • Loading branch information
JamesNK authored Feb 6, 2024
1 parent 62f941d commit 8840642
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 127 deletions.
36 changes: 0 additions & 36 deletions src/Aspire.Hosting/ApplicationModel/ModelName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,5 @@ internal static void ValidateName(string target, string name)
{
ArgumentNullException.ThrowIfNull(target);
ArgumentNullException.ThrowIfNull(name);

if (name.Length < 1 || name.Length > 64)
{
throw new ArgumentException($"{target} name '{name}' is invalid. Name must be between 1 and 64 characters long.", nameof(name));
}

var lastCharacterHyphen = false;
for (var i = 0; i < name.Length; i++)
{
if (name[i] == '-')
{
if (lastCharacterHyphen)
{
throw new ArgumentException($"{target} name '{name}' is invalid. Name cannot contain consecutive hyphens.", nameof(name));
}
lastCharacterHyphen = true;
}
else if (!char.IsAsciiLetterOrDigit(name[i]))
{
throw new ArgumentException($"{target} name '{name}' is invalid. Name must contain only ASCII letters, digits, and hyphens.", nameof(name));
}
else
{
lastCharacterHyphen = false;
}
}

if (!char.IsAsciiLetter(name[0]))
{
throw new ArgumentException($"{target} name '{name}' is invalid. Name must start with an ASCII letter.", nameof(name));
}

if (name[^1] == '-')
{
throw new ArgumentException($"{target} name '{name}' is invalid. Name cannot end with a hyphen.", nameof(name));
}
}
}
93 changes: 2 additions & 91 deletions tests/Aspire.Hosting.Tests/ModelNameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,97 +7,6 @@ namespace Aspire.Hosting.Tests;

public class ModelNameTests
{
[Fact]
public void ValidateName_Null_Error()
{
// Arrange & Act
var exception = Assert.Throws<ArgumentNullException>(() => ModelName.ValidateName(nameof(Resource), null!));

// Assert
Assert.Equal("Value cannot be null. (Parameter 'name')", exception.Message);
}

[Fact]
public void ValidateName_Empty_Error()
{
// Arrange & Act
var exception = Assert.Throws<ArgumentException>(() => ModelName.ValidateName(nameof(Resource), ""));

// Assert
Assert.Equal($"Resource name '' is invalid. Name must be between 1 and 64 characters long. (Parameter 'name')", exception.Message);
}

[Fact]
public void ValidateName_LongString_Error()
{
// Arrange & Act
var name = new string('a', 65);
var exception = Assert.Throws<ArgumentException>(() => ModelName.ValidateName(nameof(Resource), name));

// Assert
Assert.Equal($"Resource name '{name}' is invalid. Name must be between 1 and 64 characters long. (Parameter 'name')", exception.Message);
}

[Fact]
public void ValidateName_Whitespace_Error()
{
// Arrange & Act
var exception = Assert.Throws<ArgumentException>(() => ModelName.ValidateName(nameof(Resource), " "));

// Assert
Assert.Equal("Resource name ' ' is invalid. Name must contain only ASCII letters, digits, and hyphens. (Parameter 'name')", exception.Message);
}

[Fact]
public void ValidateName_Underscore_Error()
{
// Arrange & Act
var exception = Assert.Throws<ArgumentException>(() => ModelName.ValidateName(nameof(Resource), "test_name"));

// Assert
Assert.Equal("Resource name 'test_name' is invalid. Name must contain only ASCII letters, digits, and hyphens. (Parameter 'name')", exception.Message);
}

[Fact]
public void ValidateName_StartHyphen_Error()
{
// Arrange & Act
var exception = Assert.Throws<ArgumentException>(() => ModelName.ValidateName(nameof(Resource), "-abc"));

// Assert
Assert.Equal("Resource name '-abc' is invalid. Name must start with an ASCII letter. (Parameter 'name')", exception.Message);
}

[Fact]
public void ValidateName_ConsecutiveHyphens_Error()
{
// Arrange & Act
var exception = Assert.Throws<ArgumentException>(() => ModelName.ValidateName(nameof(Resource), "test--name"));

// Assert
Assert.Equal("Resource name 'test--name' is invalid. Name cannot contain consecutive hyphens. (Parameter 'name')", exception.Message);
}

[Fact]
public void ValidateName_StartNumber_Error()
{
// Arrange & Act
var exception = Assert.Throws<ArgumentException>(() => ModelName.ValidateName(nameof(Resource), "1abc"));

// Assert
Assert.Equal("Resource name '1abc' is invalid. Name must start with an ASCII letter. (Parameter 'name')", exception.Message);
}

[Fact]
public void ValidateName_EndHyphen_Error()
{
// Arrange & Act
var exception = Assert.Throws<ArgumentException>(() => ModelName.ValidateName(nameof(Resource), "abc-"));

// Assert
Assert.Equal("Resource name 'abc-' is invalid. Name cannot end with a hyphen. (Parameter 'name')", exception.Message);
}

[Theory]
[InlineData("a")]
[InlineData("ab")]
Expand All @@ -106,6 +15,8 @@ public void ValidateName_EndHyphen_Error()
[InlineData("abc-123")]
[InlineData("a-b-c-1-2-3")]
[InlineData("ABC")]
[InlineData("a_b")]
[InlineData("a.b")]
public void ValidateName_ValidNames_Success(string name)
{
ModelName.ValidateName(nameof(Resource), name);
Expand Down

0 comments on commit 8840642

Please sign in to comment.