Skip to content

Add DirectoryName type and improve path validation semantics#3

Merged
matt-edmondson merged 1 commit intomainfrom
refactor-relative-path-constraints
Jan 6, 2026
Merged

Add DirectoryName type and improve path validation semantics#3
matt-edmondson merged 1 commit intomainfrom
refactor-relative-path-constraints

Conversation

@matt-edmondson
Copy link
Contributor

Introduce DirectoryName as a new semantic type for single directory name components (without path separators), providing better type safety and clarity when working with path segments.

New types:

  • DirectoryName: Semantic string for single directory names
  • IDirectoryName: Marker interface for directory name types
  • IsValidDirectoryNameAttribute: Validates directory names (no path separators, only valid filename characters)

Enhanced path operators:

  • AbsoluteDirectoryPath / DirectoryName → AbsoluteDirectoryPath
  • DirectoryPath / DirectoryName → DirectoryPath

Type corrections:

  • RelativeDirectoryPath.Name now returns DirectoryName (was FileName) More semantically correct for representing a directory's name

Validation improvements:

  • Replace IsFileNameAttribute with IsValidFileNameAttribute
  • Remove redundant validation attributes from RelativeDirectoryPath and RelativeFilePath (IsDirectoryPath, IsFilePath)
  • Focus on format-only validation without file system checks

This change provides clearer semantics: use DirectoryName for single components like "src" or "bin", and RelativeDirectoryPath for multi-segment paths like "../folder" or ".\src\app".

Introduce DirectoryName as a new semantic type for single directory name components (without path separators), providing better type safety and clarity when working with path segments.

New types:
- DirectoryName: Semantic string for single directory names
- IDirectoryName: Marker interface for directory name types
- IsValidDirectoryNameAttribute: Validates directory names (no path separators, only valid filename characters)

Enhanced path operators:
- AbsoluteDirectoryPath / DirectoryName → AbsoluteDirectoryPath
- DirectoryPath / DirectoryName → DirectoryPath

Type corrections:
- RelativeDirectoryPath.Name now returns DirectoryName (was FileName) More semantically correct for representing a directory's name

Validation improvements:
- Replace IsFileNameAttribute with IsValidFileNameAttribute
- Remove redundant validation attributes from RelativeDirectoryPath and RelativeFilePath (IsDirectoryPath, IsFilePath)
- Focus on format-only validation without file system checks

This change provides clearer semantics: use DirectoryName for single components like "src" or "bin", and RelativeDirectoryPath for multi-segment paths like "../folder" or ".\src\app".
@sonarqubecloud
Copy link

sonarqubecloud bot commented Jan 5, 2026

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

[SuppressMessage("Usage", "CA2225:Operator overloads have named alternates", Justification = "Path combination is the semantic meaning, not mathematical division")]
public static DirectoryPath operator /(DirectoryPath left, DirectoryName right)
{
#if NET6_0_OR_GREATER

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These guards exist here but not in the / overload within AbsoluteDirectoryPath.

Either this is not required here, or it needs to be added to the other location?

@matt-edmondson matt-edmondson merged commit 0292967 into main Jan 6, 2026
4 of 6 checks passed
ArgumentNullExceptionPolyfill.ThrowIfNull(right);
#endif

string combinedPath = Path.Combine(left.WeakString, right.WeakString);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this use PooledStringBuilder like the other function, or should the other function not use it? (or is both valid)

public void IsValidDirectoryNameAttribute_EmptyDirectoryName_ShouldPass()
{
// Arrange
TestDirectoryName emptyName = TestDirectoryName.Create<TestDirectoryName>("");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't an empty folder name be invalid?

[TestMethod]
public void IsValidDirectoryNameAttribute_DirectoryNameWithColon_ShouldFail()
{
// Arrange & Act & Assert - colon is invalid in directory names (except drive letters)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test for drive letters?

Assert.IsTrue(specialCharsName.IsValid());
}

[TestMethod]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests seem mostly duplicated in DirectoryNameTests. Since this file is PathValidation should it be testing the path combination (which it probably already is)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants