public PropertyValidationResult ValidateProperties() { var propertyValidationResult = new PropertyValidationResult(); if (this.InputValue == null) propertyValidationResult.Add("InputValue is needed"); if (string.IsNullOrEmpty(this.SwapValue)) propertyValidationResult.Add("SwapValue is needed"); return propertyValidationResult; }
private string GetPropertyDisplayName(PropertyValidationResult propertyValidationResult) { // If you want "user readable" property names to be used in validation messages, you have to write your own logic to get them. // Otherwise, display name will be used (but it's the same as property name). switch (propertyValidationResult.ValidationContext.ObjectInstance) { case IssuedInvoicePostModel invoice: return(GetInvoicePropertyName(propertyValidationResult)); default: return(propertyValidationResult.ValidationContext.DisplayName); } }
private string GetInvoicePropertyName(PropertyValidationResult propertyValidationResult) { switch (propertyValidationResult.Name) { case nameof(IssuedInvoicePostModel.Description): return(ApplicationStrings.Label_Description); case nameof(IssuedInvoicePostModel.VariableSymbol): return(ApplicationStrings.Label_VariableSymbol); default: return(propertyValidationResult.ValidationContext.DisplayName); } }
public PropertyValidationResult ValidateProperties() { var propertyValidationResult = new PropertyValidationResult(); if (this.InputValue == null) { propertyValidationResult.Add("InputValue is needed"); } if (string.IsNullOrEmpty(this.SwapValue)) { propertyValidationResult.Add("SwapValue is needed"); } return(propertyValidationResult); }
private void ProcessPropertyValidationResult( PropertyInfo propertyInfo, string propertyNamePrefix, ValidationContext validationContext, List <AttributeValidationResult> invalidAttributes) { if (invalidAttributes.Any()) { var propertyValidationResult = new PropertyValidationResult { Name = GetPropertyName(propertyInfo, propertyNamePrefix), ValidationContext = validationContext, InvalidAttributes = invalidAttributes }; InvalidProperties.Add(propertyValidationResult.Name, propertyValidationResult); } }
/// <summary> /// Get erros if object implement IValidatableObject /// </summary> /// <typeparam name="TEntity">The typeof entity</typeparam> /// <param name="item">The item to validate</param> /// <param name="errors">A collection of current errors</param> private Dictionary <string, PropertyValidationResult> SetValidatableObjectErrors <TEntity>(TEntity item) where TEntity : class { Dictionary <string, PropertyValidationResult> errors = new Dictionary <string, PropertyValidationResult>(); if (typeof(IValidatableObject).IsAssignableFrom(typeof(TEntity))) { var validationContext = new ValidationContext(item, null, null); var validationResults = ((IValidatableObject)item).Validate(validationContext); foreach (ValidationResult result in validationResults) { if (!string.IsNullOrWhiteSpace(result.ErrorMessage) && (result.MemberNames == null || !result.MemberNames.Any())) { if (!errors.ContainsKey("Common")) { errors["Common"] = new PropertyValidationResult() { PropertyName = "Common" }; } errors["Common"].ErrorMessages.Add(result.ErrorMessage); } else { foreach (string propertyName in result.MemberNames) { if (!errors.ContainsKey(propertyName)) { errors[propertyName] = new PropertyValidationResult() { PropertyName = propertyName }; } errors[propertyName].ErrorMessages.Add(result.ErrorMessage); } } } } return(errors); }
/// <inheritdoc /> public override string GetValidationMessage(PropertyValidationResult propertyValidationResult, AttributeValidationResult attributeValidationResult) { _ = attributeValidationResult ?? throw new ArgumentNullException(nameof(attributeValidationResult)); _ = propertyValidationResult ?? throw new ArgumentNullException(nameof(propertyValidationResult)); var propertyDisplayName = GetPropertyDisplayName(propertyValidationResult); switch (attributeValidationResult.ValidationType) { case ValidationType.Required: case ValidationType.RequiredNonDefault: return(string.Format(CultureInfo.CurrentCulture, ValidationMessages.Required, propertyDisplayName)); case ValidationType.StringLength: var stringLengthAttribute = (StringLengthAttribute)attributeValidationResult.ValidationAttribute; return(string.Format(CultureInfo.CurrentCulture, ValidationMessages.StringLength, propertyDisplayName, stringLengthAttribute.MinimumLength, stringLengthAttribute.MaximumLength)); default: return(base.GetValidationMessage(propertyValidationResult, attributeValidationResult)); } }
public void ValidationResultNotifiesWhenValidationStateChanges() { var validatableObject = new ValidatableTestObject(); // Validator validates that the property is equal to the string "Inigo Montoya". var validator = PropertyValidator .For(validatableObject, o => o.SomeStringProperty) .ValidIfTrue(value => value == "Inigo Montoya", "Error occurred!"); PropertyValidationResult validationResult = null; validator.PropertyChanged += (s, e) => { if (e.PropertyName == nameof(validator.ValidationResult)) { validationResult = validator.ValidationResult; } }; Assert.IsNull(validationResult); // Precondition validatableObject.SomeStringProperty = "not empty"; Assert.AreEqual(validationResult, validator.ValidationResult); Assert.IsFalse(validationResult.IsValid); }
/// <summary> /// Returns validation messages for given property. /// </summary> /// <param name="propertyValidationResult">Result of validation property value against all its validation attributes.</param> /// <returns>Validation message.</returns> public virtual IEnumerable <string> GetValidationMessages(PropertyValidationResult propertyValidationResult) { _ = propertyValidationResult ?? throw new ArgumentNullException(nameof(propertyValidationResult)); return(propertyValidationResult.InvalidAttributes.Select(a => GetValidationMessage(propertyValidationResult, a))); }
/// <summary> /// Returns validation message for given property and validation attribute. /// Can be overriden to return custom messages based on <see cref="AttributeValidationResult.ValidationType"/> property. /// </summary> /// <param name="propertyValidationResult">Result of validation of property value against all its validation attributes.</param> /// <param name="attributeValidationResult">Result of validation of property value against single validation attribute.</param> /// <returns>Validation message.</returns> public virtual string GetValidationMessage(PropertyValidationResult propertyValidationResult, AttributeValidationResult attributeValidationResult) { _ = attributeValidationResult ?? throw new ArgumentNullException(nameof(attributeValidationResult)); return(attributeValidationResult.ValidationResult.ErrorMessage); }