-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented NCCO param for creating a call
- Loading branch information
BiBi
committed
Apr 11, 2019
1 parent
835c0cf
commit 4915afc
Showing
4 changed files
with
108 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Reflection; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Nexmo.Api.Helpers | ||
{ | ||
public class RequiredIfAttribute : ValidationAttribute | ||
{ | ||
protected RequiredAttribute _innerAttribute; | ||
public string DependentProperty { get; set; } | ||
public object TargetValue { get; set; } | ||
|
||
public RequiredIfAttribute(string dependentProperty, object targetValue) | ||
{ | ||
_innerAttribute = new RequiredAttribute(); | ||
DependentProperty = dependentProperty; | ||
TargetValue = targetValue; | ||
} | ||
protected override ValidationResult IsValid(object value, ValidationContext validationContext) | ||
{ | ||
var containerType = validationContext.ObjectInstance.GetType(); | ||
var field = containerType.GetProperty(DependentProperty); | ||
|
||
if (field != null) | ||
{ | ||
if (field != null) | ||
{ | ||
// get the value of the dependent property | ||
var dependentValue = field.GetValue(validationContext.ObjectInstance, null); | ||
// trim spaces of dependent value | ||
if (dependentValue != null && dependentValue is string) | ||
{ | ||
dependentValue = (dependentValue as string).Trim(); | ||
|
||
if ((dependentValue as string).Length == 0) | ||
{ | ||
dependentValue = null; | ||
} | ||
} | ||
|
||
// compare the value against the target value | ||
if ((dependentValue == null && TargetValue == null) || | ||
(dependentValue != null && (TargetValue.Equals("*") || dependentValue.Equals(TargetValue)))) | ||
{ | ||
// match => means we should try validating this field | ||
if (!_innerAttribute.IsValid(value)) | ||
// validation failed - return an error | ||
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName), new[] { validationContext.MemberName }); | ||
} | ||
|
||
} | ||
} | ||
|
||
return ValidationResult.Success; | ||
} | ||
} | ||
} |
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