-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
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
37 changes: 37 additions & 0 deletions
37
lib/Logitar.Identity.Core/Validators/IdentifierValidator.cs
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,37 @@ | ||
using FluentValidation; | ||
using FluentValidation.Validators; | ||
|
||
namespace Logitar.Identity.Core.Validators; | ||
|
||
/// <summary> | ||
/// The validator used to enforce that a string is an identifier. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the object being validated.</typeparam> | ||
public class IdentifierValidator<T> : IPropertyValidator<T, string> | ||
{ | ||
/// <summary> | ||
/// Gets the name of the validator. | ||
/// </summary> | ||
public string Name { get; } = "IdentifierValidator"; | ||
|
||
/// <summary> | ||
/// Returns the default error message template for this validator, when not overridden. | ||
/// </summary> | ||
/// <param name="errorCode">The error code.</param> | ||
/// <returns>The default error message template.</returns> | ||
public string GetDefaultMessageTemplate(string errorCode) | ||
{ | ||
return "'{PropertyName}' may only contain letters, digits and underscores (_), and must not start with a digit."; | ||
} | ||
|
||
/// <summary> | ||
/// Validates a specific property value. | ||
/// </summary> | ||
/// <param name="context">The validation context.</param> | ||
/// <param name="value">The value to validate.</param> | ||
/// <returns>True if the value is valid, or false otherwise.</returns> | ||
public bool IsValid(ValidationContext<T> context, string value) | ||
{ | ||
return string.IsNullOrEmpty(value) || (!char.IsDigit(value.First()) && value.All(c => char.IsLetterOrDigit(c) || c == '_')); | ||
} | ||
} |