-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update: Phone number is optional when unblocking the provider (#1246)
* Added the RequiredIfAttribute class for property validation * Made phone number optional for provider unblocking. * Moved BlockPhoneNumber field from ProviderBaseDto to the ProviderDto model. * Removed all validations in BlockPhoneNumber. * Added BlockPhoneNumber mapping ignoring
- Loading branch information
1 parent
a4f7456
commit 7aa31d1
Showing
6 changed files
with
45 additions
and
13 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
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
38 changes: 38 additions & 0 deletions
38
OutOfSchool/OutOfSchool.WebApi/Validators/RequiredIfAttribute.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,38 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace OutOfSchool.WebApi.Validators; | ||
|
||
public class RequiredIfAttribute : ValidationAttribute | ||
{ | ||
private readonly string otherProperty; | ||
private readonly bool requiredValue; | ||
|
||
public RequiredIfAttribute(string otherProperty, bool requiredValue) | ||
{ | ||
this.otherProperty = otherProperty; | ||
this.requiredValue = requiredValue; | ||
} | ||
|
||
protected override ValidationResult IsValid(object value, ValidationContext validationContext) | ||
{ | ||
var propertyInfo = validationContext.ObjectType.GetProperty(otherProperty); | ||
|
||
if (propertyInfo == null) | ||
{ | ||
return new ValidationResult($"Property {otherProperty} not found."); | ||
} | ||
|
||
var otherPropertyValue = propertyInfo.GetValue(validationContext.ObjectInstance); | ||
|
||
if ((otherPropertyValue is bool otherValue) && otherValue == requiredValue) | ||
{ | ||
if (value == null || string.IsNullOrWhiteSpace(value.ToString())) | ||
{ | ||
return new ValidationResult(ErrorMessage); | ||
} | ||
} | ||
|
||
return ValidationResult.Success; | ||
} | ||
} | ||
|