C
C#15mo ago
schwartzmj

❔ Questions on validating columns and types (updating a single column in a table via REST API)

I'm new to .NET and C#. I'm trying to implement a somewhat more difficult endpoint where the user provides a ColumnName and a Value. I've been able to implement checking if the ColumnName is a valid column in the table, but I'm having difficulties converting the Value to the type that the column (model property) expects, and if successful (its valid), proceeding.
2 Replies
schwartzmj
schwartzmj15mo ago
using System.ComponentModel.DataAnnotations;

namespace CCC_api.Attributes;

public class HasColumnValidatorAttribute : ValidationAttribute
{
public HasColumnValidatorAttribute(Type entityType)
: base("Value must match an existing column.")
{
EntityType = entityType;
}

public Type EntityType { get; set; }

protected override ValidationResult? IsValid(
object? value,
ValidationContext validationContext)
{
var strValue = value as string;
if (!string.IsNullOrEmpty(strValue)
&& EntityType.GetProperties()
.Any(p => p.Name == strValue))
return ValidationResult.Success;

return new ValidationResult(ErrorMessage);
}
}
using System.ComponentModel.DataAnnotations;

namespace CCC_api.Attributes;

public class HasColumnValidatorAttribute : ValidationAttribute
{
public HasColumnValidatorAttribute(Type entityType)
: base("Value must match an existing column.")
{
EntityType = entityType;
}

public Type EntityType { get; set; }

protected override ValidationResult? IsValid(
object? value,
ValidationContext validationContext)
{
var strValue = value as string;
if (!string.IsNullOrEmpty(strValue)
&& EntityType.GetProperties()
.Any(p => p.Name == strValue))
return ValidationResult.Success;

return new ValidationResult(ErrorMessage);
}
}
This is my column validator that appears to work Maybe there's a better way to go about this? Now that I think about it, would it probably be better to just make a DTO for this and set each updatable property to nullable and .NET should take care of it? Technically it gives the option to update multiple columns at once then, and it'll maybe be easier to signify which columns I don't want to be updated (like ID) I am curious just for my learning purposes how I would implement what I mentioned first though
using System.ComponentModel.DataAnnotations;

namespace CCC_api.Attributes;

public class HasCorrectValueTypeForColumnValidatorAttribute : ValidationAttribute
{
public HasCorrectValueTypeForColumnValidatorAttribute(Type entityType, dynamic value, string columnName)
: base("Value must match the type of the Column.")
{
EntityType = entityType;
Value = value;
ColumnName = columnName;
}

public Type EntityType { get; set; }
public dynamic Value { get; set; }
public string ColumnName { get; set; }

protected override ValidationResult? IsValid(
object? value,
ValidationContext validationContext)
{
var strValue = value as string;
if (!string.IsNullOrEmpty(strValue)
&& EntityType.GetProperties()
.Any(p =>
{
var columnValid = p.Name == ColumnName;
if (!columnValid) return false;
// var typeValid = p.PropertyType == Value.GetType();
var converted = Convert.ChangeType(Value, p.PropertyType);
var typeValid = converted != null;
return columnValid && typeValid;
}))
return ValidationResult.Success;

return new ValidationResult(ErrorMessage);
}
}
using System.ComponentModel.DataAnnotations;

namespace CCC_api.Attributes;

public class HasCorrectValueTypeForColumnValidatorAttribute : ValidationAttribute
{
public HasCorrectValueTypeForColumnValidatorAttribute(Type entityType, dynamic value, string columnName)
: base("Value must match the type of the Column.")
{
EntityType = entityType;
Value = value;
ColumnName = columnName;
}

public Type EntityType { get; set; }
public dynamic Value { get; set; }
public string ColumnName { get; set; }

protected override ValidationResult? IsValid(
object? value,
ValidationContext validationContext)
{
var strValue = value as string;
if (!string.IsNullOrEmpty(strValue)
&& EntityType.GetProperties()
.Any(p =>
{
var columnValid = p.Name == ColumnName;
if (!columnValid) return false;
// var typeValid = p.PropertyType == Value.GetType();
var converted = Convert.ChangeType(Value, p.PropertyType);
var typeValid = converted != null;
return columnValid && typeValid;
}))
return ValidationResult.Success;

return new ValidationResult(ErrorMessage);
}
}
I know this is blatantly wrong for probably more than a few reasons, but that's what I was hacking on
Accord
Accord15mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.