Show / Hide Table of Contents

Class DataAnnotationValidator

Implements methods to validate property values based on their data annotations.

Inheritance
System.Object
DataAnnotationValidator
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: Sartorius.SAF.Validation
Assembly: Sartorius.SAF.dll
Syntax
public static class DataAnnotationValidator
Examples

All examples validate the following class.

public class Customer : ObservableObject
{
    private String firstName;
    private String lastName;

    [Required]
    [StringLength(8)]
    public String FirstName
    {
        get => this.firstName;
        set
        {
            this.firstName = value;
            SetValue(nameof(FirstName), ref this.firstName, value);
        }
    }

    [Required]
    [StringLength(12)]
    public String LastName
    {
        get => this.lastName;
        set
        {
            this.lastName = value;
            SetValue(nameof(LastName), ref this.lastName, value);
        }
    }
}

This example illustrates how to the Validate(Object) method to validate all properties of the given instance and get a list of IError as result. This method is obsolete.

        var customer = new Customer {FirstName = "Bob", LastName = "Lorem Ipsum"};

        // Validate the entire customer instance
        var errors = DataAnnotationValidator.Validate((Object) customer);
        if (errors.Any())
        {
            // show the errors
        }

This example illustrates how to use the Validate<TInstance>(TInstance) method to validate all properties of the given instance and get a list of System.ComponentModel.DataAnnotations.ValidationResult as result.

        var customer = new Customer {FirstName = "Bob", LastName = "Lorem Ipsum"};

        // Validate the entire customer instance
        var errors = DataAnnotationValidator.Validate(customer);
        if (errors.Any())
        {
            // show the errors
        }

This example illustrates how to use the Validate<TValue>(Expression<Func<TValue>>, TValue) method to validate the given property value.

        var customer = new Customer {FirstName = "Bob"};

        // Validate the first name property.
        var validationResult =
            DataAnnotationValidator.Validate(() => customer.FirstName, customer.FirstName);
        if (validationResult != ValidationResult.Success)
        {
            // show error
        }

This example illustrates how to use the Validate<TValue>(PropertyInfo, TValue) method to validate the given property value.

        var customer = new Customer {FirstName = "Bob"};

        var propertyInfo = customer.GetType().GetProperty("FirstName");

        // Validate the first name property.
        var validationResult =
            DataAnnotationValidator.Validate(propertyInfo, customer.FirstName);
        if (validationResult != ValidationResult.Success)
        {
            // show error
        }

This example illustrates how to use the Validate<TValue>(Expression<Func<TValue>>, TValue, ValidationContext) method to validate the given property value.

        var customer = new Customer {FirstName = "Bob"};

        var validationContext = new ValidationContext(customer) {MemberName = "FirstName", DisplayName = "First name"};

        // Validate the first name property.
        var validationResult =
            DataAnnotationValidator.Validate(() => customer.FirstName, customer.FirstName, validationContext);
        if (validationResult != ValidationResult.Success)
        {
            // show error
        }

This example illustrates how to use the Validate<TValue>(PropertyInfo, TValue, ValidationContext) method to validate the given property value.

        var customer = new Customer {FirstName = "Bob"};

        var propertyInfo = customer.GetType().GetProperty("FirstName");

        var validationContext = new ValidationContext(customer) {MemberName = "FirstName", DisplayName = "First name"};

        // Validate the first name property.
        var validationResult =
            DataAnnotationValidator.Validate(propertyInfo, customer.FirstName, validationContext);
        if (validationResult != ValidationResult.Success)
        {
            // show error
        }

Methods

View Source

Validate(Object)

Validates all properties of the given instance.

Declaration
[Obsolete("This method created obsolete PropertyErrors.")]
public static List<IError> Validate(object instance)
Parameters
Type Name Description
System.Object instance

The object instance that is validated.

Returns
Type Description
System.Collections.Generic.List<IError>

A list of IError.

View Source

Validate<TInstance>(TInstance)

Validates all property values of the given instance.

Declaration
public static IEnumerable<ValidationResult> Validate<TInstance>(TInstance instance)
Parameters
Type Name Description
TInstance instance

The instance that is validated.

Returns
Type Description
System.Collections.Generic.IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult>

A list of System.ComponentModel.DataAnnotations.ValidationResult.

Type Parameters
Name Description
TInstance

The instance type.

Exceptions
Type Condition
System.ArgumentNullException

instance is null.

View Source

Validate<TValue>(Expression<Func<TValue>>, TValue)

Validates the given value via the System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(System.Object) method.

Declaration
public static ValidationResult Validate<TValue>(Expression<Func<TValue>> propertyExpression, TValue value)
Parameters
Type Name Description
System.Linq.Expressions.Expression<System.Func<TValue>> propertyExpression

The expression that defines the property to validate.

TValue value

The property value to validate.

Returns
Type Description
System.ComponentModel.DataAnnotations.ValidationResult

The first validation error as System.ComponentModel.DataAnnotations.ValidationResult or System.ComponentModel.DataAnnotations.ValidationResult.Success if validation succeeds.

Type Parameters
Name Description
TValue

The property value type.

Exceptions
Type Condition
System.ArgumentNullException

propertyExpression is null.

View Source

Validate<TValue>(Expression<Func<TValue>>, TValue, ValidationContext)

Validates the given value via the System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(System.Object, System.ComponentModel.DataAnnotations.ValidationContext) method.

Declaration
public static ValidationResult Validate<TValue>(Expression<Func<TValue>> propertyExpression, TValue value, ValidationContext validationContext)
Parameters
Type Name Description
System.Linq.Expressions.Expression<System.Func<TValue>> propertyExpression

The expression that defines the property to validate.

TValue value

The property value to validate.

System.ComponentModel.DataAnnotations.ValidationContext validationContext

The System.ComponentModel.DataAnnotations.ValidationContext.

Returns
Type Description
System.ComponentModel.DataAnnotations.ValidationResult

The first validation error as System.ComponentModel.DataAnnotations.ValidationResult or System.ComponentModel.DataAnnotations.ValidationResult.Success if validation succeeds.

Type Parameters
Name Description
TValue

The property value type.

Exceptions
Type Condition
System.ArgumentNullException

propertyExpression is null or validationContext is null.

View Source

Validate<TValue>(PropertyInfo, TValue)

Validates the given value via the System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(System.Object) method.

Declaration
public static ValidationResult Validate<TValue>(PropertyInfo propertyInfo, TValue value)
Parameters
Type Name Description
System.Reflection.PropertyInfo propertyInfo

The System.Reflection.PropertyInfo that defines the property to validate.

TValue value

The property value to validate.

Returns
Type Description
System.ComponentModel.DataAnnotations.ValidationResult

The first validation error as System.ComponentModel.DataAnnotations.ValidationResult or System.ComponentModel.DataAnnotations.ValidationResult.Success if validation succeeds.

Type Parameters
Name Description
TValue

The property value type.

Exceptions
Type Condition
System.ArgumentNullException

propertyInfo is null.

View Source

Validate<TValue>(PropertyInfo, TValue, ValidationContext)

Validates the given value via the System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(System.Object, System.ComponentModel.DataAnnotations.ValidationContext) method.

Declaration
public static ValidationResult Validate<TValue>(PropertyInfo propertyInfo, TValue value, ValidationContext validationContext)
Parameters
Type Name Description
System.Reflection.PropertyInfo propertyInfo

The System.Reflection.PropertyInfo that defines the property to validate.

TValue value

The property value to validate.

System.ComponentModel.DataAnnotations.ValidationContext validationContext

The System.ComponentModel.DataAnnotations.ValidationContext.

Returns
Type Description
System.ComponentModel.DataAnnotations.ValidationResult

The first validation error as System.ComponentModel.DataAnnotations.ValidationResult or System.ComponentModel.DataAnnotations.ValidationResult.Success if validation succeeds.

Type Parameters
Name Description
TValue

The property value type.

Exceptions
Type Condition
System.ArgumentNullException

propertyInfo is null or validationContext is null.

  • View Source
Back to top Generated by DocFX