ktsu.StrongStrings 1.4.2-pre.1

Prefix Reserved
This is a prerelease version of ktsu.StrongStrings.
There is a newer version of this package available.
See the version list below for details.
dotnet add package ktsu.StrongStrings --version 1.4.2-pre.1
                    
NuGet\Install-Package ktsu.StrongStrings -Version 1.4.2-pre.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ktsu.StrongStrings" Version="1.4.2-pre.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ktsu.StrongStrings" Version="1.4.2-pre.1" />
                    
Directory.Packages.props
<PackageReference Include="ktsu.StrongStrings" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ktsu.StrongStrings --version 1.4.2-pre.1
                    
#r "nuget: ktsu.StrongStrings, 1.4.2-pre.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=ktsu.StrongStrings&version=1.4.2-pre.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=ktsu.StrongStrings&version=1.4.2-pre.1&prerelease
                    
Install as a Cake Tool

ktsu.StrongStrings

A transparent wrapper around strings that provides strong typing, compile-time feedback, and runtime validation.

License NuGet NuGet Downloads Build Status GitHub Stars

Introduction

ktsu.StrongStrings is a .NET library that provides a way to create strongly-typed wrappers around standard strings. It gives you the benefits of strong typing, compile-time feedback, and runtime validation while maintaining full compatibility with string operations. The intention is to provide usage context and validation to naked strings in a similar way that Enums do for integers.

Features

  • Strong Typing: Create distinct types for different string usages (e.g., EmailAddress, UserId, Url)
  • Compile-Time Safety: Prevent mixing of semantically different strings
  • Runtime Validation: Apply custom validation rules that throw exceptions for invalid data
  • Transparent Usage: Works with existing string APIs through implicit conversion
  • String Operation Support: Full access to standard string methods and properties
  • Immutability: Implemented as records for value semantics and immutability

Installation

Package Manager Console

Install-Package ktsu.StrongStrings

.NET CLI

dotnet add package ktsu.StrongStrings

Package Reference

<PackageReference Include="ktsu.StrongStrings" Version="x.y.z" />

Usage Examples

Basic Example

using ktsu.StrongStrings;

// Create a strong type by deriving a record class from StrongStringAbstract<TDerived>:
public record class MyStrongString : StrongStringAbstract<MyStrongString> { }

public class MyDemoClass
{
    public MyStrongString Stronk { get; set; } = new();

    public static void Demo(MyStrongString inStrongString, string inSystemString, out MyStrongString outStrongString, out string outSystemString)
    {
        // You can implicitly cast down to a System.String
        outSystemString = inStrongString;

        // You must explicitly cast up to a StrongString
        outStrongString = (MyStrongString)inSystemString;

        //You can provide a StrongString to a method that expects a System.String
        Path.Combine(inStrongString, inSystemString);

        // You can use the .WeakString property or the .ToString() method to get the value of the underlying System.String
        outSystemString = inStrongString.WeakString;
        outSystemString = inStrongString.ToString();

        // You can not implicitly cast up to a StrongString
        // outStrongString = inSystemString; // This will not compile

        // You can not cast from one StrongString to another
        // OtherStrongString other = inStrongString; // This will not compile
        // OtherStrongString other = (OtherStrongString)inStrongString; // This will not compile either
    }
}

Validation Example

public abstract class StartsWithHttp : IValidator
{
    public static bool IsValid(AnyStrongString? strongString)
    {
        ArgumentNullException.ThrowIfNull(strongString);

        return strongString.StartsWith("http", StringComparison.InvariantCultureIgnoreCase);
    }
}

public abstract class EndsWithDotCom : IValidator
{
    public static bool IsValid(AnyStrongString? strongString)
    {
        ArgumentNullException.ThrowIfNull(strongString);

        return strongString.EndsWith(".com", StringComparison.InvariantCultureIgnoreCase);
    }
}

public record class MyValidatedString : StrongStringAbstract<MyValidatedString, StartsWithHttp, EndsWithDotCom> { }

// This will work:
MyValidatedString valid = (MyValidatedString)"http://example.com";

// This will throw a FormatException:
MyValidatedString invalid = (MyValidatedString)"ftp://example.net";

Advanced Usage

Creating Domain-Specific String Types

using ktsu.StrongStrings;

// Define validators
public abstract class ValidEmail : IValidator
{
    private static readonly Regex EmailRegex = new Regex(@"^[^@\s]+@[^@\s]+\.[^@\s]+$");
    
    public static bool IsValid(AnyStrongString? strongString)
    {
        ArgumentNullException.ThrowIfNull(strongString);
        return EmailRegex.IsMatch(strongString.ToString());
    }
}

// Create domain-specific types
public record class EmailAddress : StrongStringAbstract<EmailAddress, ValidEmail> { }
public record class Username : StrongStringAbstract<Username> { }
public record class UserId : StrongStringAbstract<UserId> { }

// Use in a service
public class UserService
{
    public User GetUser(UserId id) { /* ... */ }
    
    public void SendEmail(EmailAddress to, string subject) { /* ... */ }
    
    // Type safety ensures you can't pass a Username where UserId is expected
}

Modifying Strong Strings

// Strong strings are immutable, but you can create new ones with modifications
public record class Path : StrongStringAbstract<Path> { }

Path basePath = (Path)"/home/user";

// Add prefixes or suffixes
Path documentsPath = basePath.WithSuffix("/documents");  // "/home/user/documents"
Path etcPath = (Path)"/etc".WithSuffix("/config");       // "/etc/config"

// Combine with standard string methods
Path combined = (Path)(basePath + "/downloads");         // "/home/user/downloads"

API Reference

StrongStringAbstract<TDerived>

The base class for creating strongly-typed strings.

Properties
Name Type Description
WeakString string The underlying string value
Length int Length of the string
Methods
Name Return Type Description
IsEmpty() bool Checks if the string is empty
IsValid() bool Validates the string against defined validators
WithPrefix(string) TDerived Creates a new instance with the specified prefix
WithSuffix(string) TDerived Creates a new instance with the specified suffix
* * All standard string methods are available

IValidator Interface

Interface for implementing custom validation rules.

Methods
Name Return Type Description
IsValid(AnyStrongString?) bool Static method that validates a string

Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Please make sure to update tests as appropriate.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on ktsu.StrongStrings:

Package Downloads
ktsu.Extensions

A collection of useful extension methods for .NET types including strings, collections, dictionaries, enumerables, and reflection.

ktsu.AppDataStorage

Application data management library using JSON serialization to save and load data in the user's app data folder.

ktsu.StrongPaths

A library that provides strong typing for common filesystem paths providing compile time feedback and runtime validation.

ktsu.CredentialCache

CredentialCache

ktsu.GitIntegration

Git Integration

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.4.3-pre.17 117 5/20/2025
1.4.3-pre.15 77 5/17/2025
1.4.3-pre.14 125 5/16/2025
1.4.3-pre.13 190 5/15/2025
1.4.3-pre.12 193 5/14/2025
1.4.3-pre.11 194 5/13/2025
1.4.3-pre.10 224 5/12/2025
1.4.3-pre.9 165 5/11/2025
1.4.3-pre.8 103 5/10/2025
1.4.3-pre.7 47 5/9/2025
1.4.3-pre.6 123 5/8/2025
1.4.3-pre.5 118 5/7/2025
1.4.3-pre.4 113 5/6/2025
1.4.3-pre.3 115 5/5/2025
1.4.3-pre.2 111 5/4/2025
1.4.3-pre.1 115 5/4/2025
1.4.2 2,897 5/4/2025
1.4.2-pre.1 55 4/26/2025
1.4.1 396 4/26/2025
1.4.1-pre.1 118 4/4/2025
1.4.0 2,790 3/30/2025
1.3.1-pre.2 70 3/29/2025
1.3.1-pre.1 456 3/25/2025
1.3.0 4,947 2/17/2025
1.2.27-pre.3 74 2/6/2025
1.2.27-pre.2 60 2/5/2025
1.2.27-pre.1 67 2/5/2025
1.2.26 6,754 12/28/2024
1.2.26-pre.26 62 2/4/2025
1.2.26-pre.25 65 2/3/2025
1.2.26-pre.24 64 2/2/2025
1.2.26-pre.23 61 1/31/2025
1.2.26-pre.22 54 1/29/2025
1.2.26-pre.21 57 1/27/2025
1.2.26-pre.20 67 1/25/2025
1.2.26-pre.19 61 1/23/2025
1.2.26-pre.18 56 1/21/2025
1.2.26-pre.17 56 1/20/2025
1.2.26-pre.16 60 1/19/2025
1.2.26-pre.15 48 1/17/2025
1.2.26-pre.14 48 1/15/2025
1.2.26-pre.13 51 1/13/2025
1.2.26-pre.12 56 1/11/2025
1.2.26-pre.11 56 1/10/2025
1.2.26-pre.10 56 1/10/2025
1.2.26-pre.9 48 1/8/2025
1.2.26-pre.8 61 1/7/2025
1.2.26-pre.7 61 1/6/2025
1.2.26-pre.6 79 1/4/2025
1.2.26-pre.5 61 1/3/2025
1.2.26-pre.4 62 1/3/2025
1.2.26-pre.3 74 1/3/2025
1.2.26-pre.2 71 1/1/2025
1.2.26-pre.1 66 12/27/2024
1.2.25 1,827 12/26/2024
1.2.24 96 12/26/2024
1.2.23 96 12/26/2024
1.2.22 93 12/26/2024
1.2.21 90 12/26/2024
1.2.20 112 12/26/2024
1.2.19 105 12/26/2024
1.2.18 2,269 12/23/2024
1.2.17 98 12/23/2024
1.2.16 101 12/23/2024
1.2.15 1,963 12/4/2024
1.2.14 577 12/2/2024
1.2.13 584 11/30/2024
1.2.12 102 11/30/2024
1.2.11 709 11/20/2024
1.2.10 1,142 11/13/2024
1.2.9 921 11/1/2024
1.2.8 1,641 10/4/2024
1.2.7 903 9/19/2024
1.2.6 336 9/19/2024
1.2.5 265 9/19/2024
1.2.4 122 9/18/2024
1.2.3 122 9/18/2024
1.2.2 228 9/18/2024
1.2.1 425 9/18/2024
1.2.0 551 9/18/2024
1.1.1 431 9/14/2024
1.1.0 115 9/14/2024

## v1.4.1 (patch)

Changes since v1.4.0:

- Update README to match standard template format ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.4.0 (minor)

Changes since v1.3.0:

- Add LICENSE template ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.3.0 (minor)

Changes since v1.2.0:

- Add automation scripts for metadata management and versioning ([@matt-edmondson](https://github.com/matt-edmondson))
- Add conditional build arguments for dotnet build command ([@matt-edmondson](https://github.com/matt-edmondson))
- Apply new editorconfig ([@matt-edmondson](https://github.com/matt-edmondson))
- Change build schedule to nightly and on push ([@matt-edmondson](https://github.com/matt-edmondson))
- Fix concurrency and run every 5 mins ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor AnyStrongString with MakeCanonical method ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor assertions and add suppress warnings ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor build argument assignment in dotnet.yml for clarity ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor MakeCanonical and As methods in AnyStrongString ([@matt-edmondson](https://github.com/matt-edmondson))
- Renamed metadata files ([@matt-edmondson](https://github.com/matt-edmondson))
- Replace LICENSE file with LICENSE.md and update copyright information ([@matt-edmondson](https://github.com/matt-edmondson))
- Suppress CA1859 warning and re-indent PreBuild target ([@matt-edmondson](https://github.com/matt-edmondson))
- Update dotnet.yml ([@matt-edmondson](https://github.com/matt-edmondson))
- Update namespace from ktsu.io.StrongStrings to ktsu.StrongStrings ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.2.26 (patch)

Changes since v1.2.25:

- Add conditional build arguments for dotnet build command ([@matt-edmondson](https://github.com/matt-edmondson))
- Renamed metadata files ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.2.21 (patch)

Changes since v1.2.20:

- Replace LICENSE file with LICENSE.md and update copyright information ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.2.16 (patch)

Changes since v1.2.15:

- Refactor assertions and add suppress warnings ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.2.13 (patch)

Changes since v1.2.12:

- Refactor MakeCanonical and As methods in AnyStrongString ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.2.12 (patch)

Changes since v1.2.11:

- Refactor AnyStrongString with MakeCanonical method ([@matt-edmondson](https://github.com/matt-edmondson))
- Suppress CA1859 warning and re-indent PreBuild target ([@matt-edmondson](https://github.com/matt-edmondson))
- Update namespace from ktsu.io.StrongStrings to ktsu.StrongStrings ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.2.7 (patch)

Changes since v1.2.6:

- Change build schedule to nightly and on push ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.2.6 (patch)

Changes since v1.2.5:

- Fix concurrency and run every 5 mins ([@matt-edmondson](https://github.com/matt-edmondson))
- Update dotnet.yml ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.2.0 (minor)

Changes since 1.1.0:

- Add an As method to convert between different types of string strings ([@matt-edmondson](https://github.com/matt-edmondson))
- Migrate ktsu.io to ktsu namespace ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.0.1 (major)

Changes since 0.0.0.0:

- Add a JsonConverter for use with System.Text.Json ([@matt-edmondson](https://github.com/matt-edmondson))
- Add builtin json convertor for System.Text.Json ([@matt-edmondson](https://github.com/matt-edmondson))
- Add github package support ([@matt-edmondson](https://github.com/matt-edmondson))
- Add more unit tests ([@matt-edmondson](https://github.com/matt-edmondson))
- Assign dependabot PRs to matt ([@matt-edmondson](https://github.com/matt-edmondson))
- Avoid double upload of symbols package ([@matt-edmondson](https://github.com/matt-edmondson))
- Bump version to 1.0.0-alpha.2 ([@matt-edmondson](https://github.com/matt-edmondson))
- Code style cleanup and add symbols and source to the nuget package ([@matt-edmondson](https://github.com/matt-edmondson))
- Correct a typo in the namespace ([@matt-edmondson](https://github.com/matt-edmondson))
- Create dependabot-merge.yml ([@matt-edmondson](https://github.com/matt-edmondson))
- Dont try to push packages when building pull requests ([@matt-edmondson](https://github.com/matt-edmondson))
- Enable dependabot and sourcelink ([@matt-edmondson](https://github.com/matt-edmondson))
- Fix a bunch of interface problems and add a unit test project ([@matt-edmondson](https://github.com/matt-edmondson))
- Fix build to properly read variables from files ([@matt-edmondson](https://github.com/matt-edmondson))
- Fix some style warnings ([@matt-edmondson](https://github.com/matt-edmondson))
- Formatted with resharper and add t4 templating for the inheritance hierarchy ([@matt-edmondson](https://github.com/matt-edmondson))
- Implement the full interface of System.String and add support for validators ([@matt-edmondson](https://github.com/matt-edmondson))
- Initial commit ([@matt-edmondson](https://github.com/matt-edmondson))
- Make code generation use csx rather than t4 so that it works with Rider ([@matt-edmondson](https://github.com/matt-edmondson))
- Migrate from .project.props to Directory.Build.props ([@matt-edmondson](https://github.com/matt-edmondson))
- Read from AUTHORS file during build ([@matt-edmondson](https://github.com/matt-edmondson))
- Read from VERSION when building ([@matt-edmondson](https://github.com/matt-edmondson))
- Read PackageDescription from DESCRIPTION file ([@matt-edmondson](https://github.com/matt-edmondson))
- Rearrange the inheritance hierarchy to be better and split out tests into categories ([@matt-edmondson](https://github.com/matt-edmondson))
- Rearrange the interface/base class to make using it as a parameter, casting to derived types, and calling statics easier ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor empty array returns in AnyStrongString ([@matt-edmondson](https://github.com/matt-edmondson))
- Remove obsolete t4 junk from project file ([@matt-edmondson](https://github.com/matt-edmondson))
- Remove old resharper settings ([@matt-edmondson](https://github.com/matt-edmondson))
- Remove suffix methods with are now in ktsu.io.Extensions, and do a cleanup of old cruft ([@matt-edmondson](https://github.com/matt-edmondson))
- Test signing ([@matt-edmondson](https://github.com/matt-edmondson))
- Update build config ([@matt-edmondson](https://github.com/matt-edmondson))
- Update build scripts ([@matt-edmondson](https://github.com/matt-edmondson))
- Update Directory.Build.props ([@matt-edmondson](https://github.com/matt-edmondson))
- Update Directory.Build.targets ([@matt-edmondson](https://github.com/matt-edmondson))
- Update dotnet.yml ([@matt-edmondson](https://github.com/matt-edmondson))
- Update github workflow to support submodules ([@matt-edmondson](https://github.com/matt-edmondson))
- Update LICENSE ([@matt-edmondson](https://github.com/matt-edmondson))
- Update nuget.config ([@matt-edmondson](https://github.com/matt-edmondson))
- Update readme and description ([@matt-edmondson](https://github.com/matt-edmondson))
- Update README.md ([@matt-edmondson](https://github.com/matt-edmondson))
- Upgrade actions ([@matt-edmondson](https://github.com/matt-edmondson))
- Use environment files for reading VERSION as set-output is deprecated ([@matt-edmondson](https://github.com/matt-edmondson))
- Use fully qualified names for templated types to avoid having to provide the namespace ([@matt-edmondson](https://github.com/matt-edmondson))
- v1.0.0-alpha.6 ([@matt-edmondson](https://github.com/matt-edmondson))