StringMate
offers a collection of useful classes designed for working with the C# string
type.
StringValidation
is a static class which contains all the validation methods.- Since it is a Static class, garbage collection won't be an issue.
- To install, run
dotnet add package StringMate
or from Nuget
Example Usage:
using StringMate.Validators;
using StringMate.Helpers;
var resultOfTimeValidation = StringValidation.Is12HourTime("06:10 PM", includeSecond: false);
if (resultOfTimeValidation) Console.WriteLine("Yee! Validated time");
var constructedDateFormat = new DateFormatBuilder()
.AddDayWithLeadingZero()
.AddMonthWithLeadingZero()
.AddYearWithFourDigit()
.AddHyphenDelimiter()
.Build();
var resultOfDateValidation = StringValidation.IsDate("10-11-2014", dateFormat: constructedDateFormat);
if (resultOfDateValidation) Console.WriteLine("Yee! Validated date");
Method | Signature | Description |
---|---|---|
IsDate |
bool IsDate(string text, string dateFormat) |
Validates date. Supports - or / as delimiter. Use the DateFormatBuilder helper class to construct the dateFormat parameter. IDateFormatBuilder interface is also included for loose coupling and dependency injection purpose. |
Is12HourTime |
bool Is12HourTime(string text, bool includeSecond) |
Validates 12-hour time format. |
Is24HourTime |
bool Is24HourTime(string text, bool includeSecond) |
Validates 24-hour time format. |
IsStrongPassword |
IsStrongPassword(string text, int minLength = 8, int minLowercase = 1, int minUppercase = 1, int minNumbers = 1, int minSymbols = 1, int minUniqueChars = 0) |
Validates whether the input string represents a strong password. Default parameters are added for convenience. |
IsHash |
bool IsHash(string text, HashingAlgorithm algorithm) |
Validates hash based on the specified hashing algorithm.HashingAlgorithm is an enum type. |
IsSlug |
bool IsSlug(string text) |
Validates slug. |
IsMongoId |
bool IsMongoId(string text) |
Validates MongoDB ObjectId. |
IsUuid |
bool IsUuid(string text, UuidVersion version) |
Validates UUID based on the specified version. UuidVersion is an enum type. |
IsJwt |
bool IsJwt(string text) |
Validates JSON Web Token (JWT). |
IsBase64 |
bool IsBase64(string text, bool checkUrlSafety) |
Validates Base64-encoded string, with URL safety check. |
IsEnumMember |
bool IsEnumMember<TEnum>(string text) |
Validates enum member of the given enum generic type parameter. |
IsHexadecimal |
bool IsHexadecimal(string text) |
Validates hexadecimal number. |
Attribute | Data Annotation | Description |
---|---|---|
HexadecimalAttribute |
[Hexadecimal] |
Validates property value of hexadecimal number. |
JsonWebTokenAttribute |
[JsonWebToken] |
Validates property value of JSON Web Token (JWT). |
MongoIdAttribute |
[MongoId] |
Validates property value of MongoDB ObjectId. |
SlugAttribute |
[Slug] |
Validates property value of slug. |
HashAttribute |
[Hash(HashingAlgorithm algorithm)] |
Validates property value of hash based on the specified hashing algorithm. |
StrongPasswordAttribute |
[StrongPassword] or [StrongPassword(int minLength = 8, int minLowercase = 1, int minUppercase = 1, int minNumbers = 1, int minSymbols = 1, int minUniqueChars = 0) |
Validates property value of strong password. Default parameters are added for convenience. |
DateAttribute |
[Date] or [Date(string dateFormat)] |
Validates property value of date. Supports - or / as delimiter. Use the DateFormatBuilder helper class to construct the dateFormat parameter. IDateFormatBuilder interface is also included for loose coupling and dependency injection purpose. |
TimeOf12HourAttribute |
[TimeOf12Hour(bool includeSecond)] |
Validates property value of 12-hour time format. |
TimeOf24HourAttribute |
[TimeOf24Hour(bool includeSecond)] |
Validates property value of 24-hour time format. |
UuidAttribute |
[Uuid(UuidVersion version)] or [Uuid] |
Validates property value of UUID. Optionally specifies the UUID version for validation. |