Skip to content

Commit

Permalink
validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Utar94 committed Dec 15, 2024
1 parent d635c5c commit 0fa409e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/Logitar.Identity.Core/Roles/Role.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,17 @@ public void RemoveCustomAttribute(string key)
/// <param name="value">The value of the custom attribute.</param>
public void SetCustomAttribute(string key, string value)
{
if (string.IsNullOrWhiteSpace(key))
if (string.IsNullOrWhiteSpace(value))
{
RemoveCustomAttribute(key);
}

key = key.Trim();
value = value.Trim();
// TODO(fpion): validate key
if (!key.IsIdentifier())
{
throw new ArgumentException("The value must be an identifier.", nameof(key));
}

if (!_customAttributes.TryGetValue(key, out string? existingValue) || existingValue != value)
{
Expand Down
11 changes: 11 additions & 0 deletions lib/Logitar.Identity.Core/ValidationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ public static IRuleBuilderOptions<T, string> DisplayName<T>(this IRuleBuilder<T,
return ruleBuilder.NotEmpty().MaximumLength(Core.DisplayName.MaximumLength);
}

/// <summary>
/// Returns a value indicating whether or not the specified value is an identifier.
/// An identifier only contains letters, digits and underscores (_) and cannot start with a digit.
/// </summary>
/// <param name="value">The input string.</param>
/// <returns>True if the value is an identifier, or false otherwise.</returns>
public static bool IsIdentifier(this string value)
{
return !string.IsNullOrEmpty(value) && !char.IsDigit(value.First()) && value.All(c => char.IsLetterOrDigit(c) || c == '_');
}

/// <summary>
/// Defines a 'unique name' validator on the current rule builder.
/// Validation will fail if the property is null, an empty string, only white-space, or its length exceeds the maximum length.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Logitar.Identity.Core.Validators;
/// The validator used to enforce a strict list of characters.
/// </summary>
/// <typeparam name="T">The type of the object being validated.</typeparam>
internal class AllowedCharactersValidator<T> : IPropertyValidator<T, string>
public class AllowedCharactersValidator<T> : IPropertyValidator<T, string>
{
/// <summary>
/// Gets the allowed characters.
Expand Down
37 changes: 37 additions & 0 deletions lib/Logitar.Identity.Core/Validators/IdentifierValidator.cs
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 == '_'));
}
}

0 comments on commit 0fa409e

Please sign in to comment.