public static bool TryValidateValue(object value, ValidationContext validationContext, ICollection <ValidationResult> validationResults, IEnumerable <ValidationAttribute> validationAttributes) { if (validationContext == null) { throw new ArgumentNullException("validationContext"); } ValidationResult result; // It appears .NET makes this call before checking whether // validationAttributes is null... ValidationAttribute vattr = validationAttributes.FirstOrDefault <ValidationAttribute> (attr => attr is RequiredAttribute); if (vattr != null) { result = vattr.GetValidationResult(value, validationContext); if (result != ValidationResult.Success) { if (validationResults != null) { validationResults.Add(result); } return(false); } } if (validationAttributes == null) { return(true); } bool valid = true; foreach (ValidationAttribute attr in validationAttributes) { if (attr == null || (attr is RequiredAttribute)) { continue; } result = attr.GetValidationResult(value, validationContext); if (result != ValidationResult.Success) { valid = false; if (validationResults != null) { validationResults.Add(result); } } } return(valid); }
public override IValidationResult Execute(object target) { var result = _validationAttribute.GetValidationResult(target, CreateContext(target)); if (result != DataAnnotations.ValidationResult.Success) { return(new ValidationResult(Property, result.ErrorMessage, ValidationResultType.Field)); } else { return(ValidationResult.ValidField); } }
/// <summary> /// Tests whether a value is valid against a single <see cref="ValidationAttribute" /> using the /// <see cref="ValidationContext" />. /// </summary> /// <param name="value">The value to be tested for validity.</param> /// <param name="validationContext">Describes the property member to validate.</param> /// <param name="attribute">The validation attribute to test.</param> /// <param name="validationError"> /// The validation error that occurs during validation. Will be <c>null</c> when the return /// value is <c>true</c>. /// </param> /// <returns><c>true</c> if the value is valid.</returns> /// <exception cref="ArgumentNullException">When <paramref name="validationContext" /> is null.</exception> private static bool TryValidate(object?value, ValidationContext validationContext, ValidationAttribute attribute, [NotNullWhen(false)] out ValidationError?validationError) { Debug.Assert(validationContext != null); var validationResult = attribute.GetValidationResult(value, validationContext); if (validationResult != ValidationResult.Success) { validationError = new ValidationError(attribute, value, validationResult !); return(false); } validationError = null; return(true); }
public IEnumerable<ModelValidationError> Validate(object instance, ValidationAttribute attribute, PropertyDescriptor descriptor, NancyContext context) { var validationContext = new ValidationContext(instance, null, null) { MemberName = ((MatchAttribute)attribute).SourceProperty }; var result = attribute.GetValidationResult(instance, validationContext); if(result != null) { yield return new ModelValidationError(result.MemberNames, attribute.ErrorMessage); } yield break; }
/// <summary> /// Tests whether a value is valid against a single <see cref="ValidationAttribute"/> using the <see cref="ValidationContext"/>. /// </summary> /// <param name="value">The value to be tested for validity.</param> /// <param name="validationContext">Describes the property member to validate.</param> /// <param name="attribute">The validation attribute to test.</param> /// <param name="validationError">The validation error that occurs during validation. Will be <c>null</c> when the return value is <c>true</c>.</param> /// <returns><c>true</c> if the value is valid.</returns> /// <exception cref="ArgumentNullException">When <paramref name="validationContext"/> is null.</exception> private static bool TryValidate(object value, ValidationContext validationContext, ValidationAttribute attribute, out ValidationError validationError) { if (validationContext == null) { throw new ArgumentNullException("validationContext"); } ValidationResult validationResult = attribute.GetValidationResult(value, validationContext); if (validationResult != ValidationResult.Success) { validationError = new ValidationError(attribute, value, validationResult); return(false); } validationError = null; return(true); }
/// <summary> /// Validates the given instance. /// </summary> /// <param name="instance">The instance that should be validated.</param> /// <param name="attribute">The <see cref="ValidationAttribute"/> that should be handled.</param> /// <param name="descriptor">A <see cref="PropertyDescriptor"/> instance for the property that is being validated.</param> /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="ModelValidationRule"/> instances.</returns> public virtual IEnumerable<ModelValidationError> Validate(object instance, ValidationAttribute attribute, PropertyDescriptor descriptor) { var context = new ValidationContext(instance, null, null) { MemberName = descriptor == null ? null : descriptor.Name }; if(descriptor != null) { instance = descriptor.GetValue(instance); } var result = attribute.GetValidationResult(instance, context); if (result != null) { yield return new ModelValidationError(result.MemberNames, string.Join(" ", result.MemberNames.Select(attribute.FormatErrorMessage))); } }
private bool validate(ValidationAttribute v, object value) { return v.GetValidationResult(value, new ValidationContext(ValidationContext, null, null)) == ValidationResult.Success; }
/// <summary> /// Tests whether a value is valid against a single <see cref="ValidationAttribute"/> using the <see cref="ValidationContext"/>. /// </summary> /// <param name="value">The value to be tested for validity.</param> /// <param name="validationContext">Describes the property member to validate.</param> /// <param name="attribute">The validation attribute to test.</param> /// <param name="validationError">The validation error that occurs during validation. Will be <c>null</c> when the return value is <c>true</c>.</param> /// <returns><c>true</c> if the value is valid.</returns> /// <exception cref="ArgumentNullException">When <paramref name="validationContext"/> is null.</exception> private static bool TryValidate(object value, ValidationContext validationContext, ValidationAttribute attribute, out ValidationError validationError) { if (validationContext == null) { throw new ArgumentNullException("validationContext"); } ValidationResult validationResult = attribute.GetValidationResult(value, validationContext); if (validationResult != ValidationResult.Success) { validationError = new ValidationError(attribute, value, validationResult); return false; } validationError = null; return true; }
/// <summary> /// Validates the given instance. /// </summary> /// <param name="instance">The instance that should be validated.</param> /// <param name="attribute">The <see cref="ValidationAttribute"/> that should be handled.</param> /// <param name="descriptor">A <see cref="PropertyDescriptor"/> instance for the property that is being validated.</param> /// <param name="context">The <see cref="NancyContext"/> of the current request.</param> /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="ModelValidationRule"/> instances.</returns> public virtual IEnumerable<ModelValidationError> Validate(object instance, ValidationAttribute attribute, PropertyDescriptor descriptor, NancyContext context) { var validationContext = new ValidationContext(instance, null, null) { MemberName = descriptor == null ? null : descriptor.Name }; // When running on Mono the Display attribute is not auto populated so for now we do it ourselves if (IsRunningOnMono) { var displayName = this.GetDisplayNameForMember(instance, validationContext.MemberName); if (!string.IsNullOrEmpty(displayName)) { validationContext.DisplayName = displayName; } } if (descriptor != null) { // Display(Name) will auto populate the context, while DisplayName() needs to be manually set if (validationContext.MemberName == validationContext.DisplayName && !string.IsNullOrEmpty(descriptor.DisplayName)) { validationContext.DisplayName = descriptor.DisplayName; } instance = descriptor.GetValue(instance); } var result = attribute.GetValidationResult(instance, validationContext); if (result != null) { yield return this.GetValidationError(result, validationContext, attribute); } }
private static string GetFinalMemberName(ValidationAttribute attrib, object contextModel, object mamberValue, string givenMemberName, string givenDisplayName) { return attrib.GetValidationResult(mamberValue, new ValidationContext(contextModel) { MemberName = givenMemberName, DisplayName = givenDisplayName })?.MemberNames.Single(); }