Skip to content

Example.DataValidator

IzayoiJiichan edited this page Sep 9, 2024 · 6 revisions

Examples

Basic

Model Class

using System;
using System.ComponentModel.DataAnnotations;

public class User
{
    [Display(Name = "ID")]
    [Required]
    public int? Id { get; set; }

    [Required]
    [StringLength(10)]
    public string? Name { get; set; }

    [Required]
    [Range(0, 200)]
    public byte? Age { get; set; }
}

To create your own attribute, create a new class that inherits from ValidationAttribute class.

Validation

using Izayoi.Data.Validation;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

public class Example()
{
    private readonly IDataValidator dataValidator = new DataValidator();

    public void Method()
    {
        var user = new User
        {
            Id = null,
            Name = "1234567890a",
            Age = 201,
        };

        bool isValid = dataValidator.TryValidateResults(user, out List<ValidationResult> validationResults);

        // isValid: false

        // validationResult[0]
        //   ErrorMessage: "The ID field is required."
        // validationResult[1]
        //   ErrorMessage: "The field Name must be a string with a maximum length of 10."
        // validationResult[2]
        //   ErrorMessage: "The field Age must be between 0 and 200."
    }
}

Localization

Supports defined CultureInfo.

Resoureces

DataAnnotations.resx

name value
RangeAttribute_ValidationError The field {0} must be between {1} and {2}.
StringLengthAttribute_ValidationError The field {0} must be a string with a maximum length of {1}.
RequiredAttribute_ValidationError The {0} field is required.

Models.resx

name value
User_Age Age
User_Id User ID
User_Name Username

Model Class (Resource)

using Resources;
using System;
using System.ComponentModel.DataAnnotations;

public class User
{
    [Display(Name = nameof(Models.User_Id), ResourceType = typeof(Models))]
    [Required(ErrorMessageResourceName = nameof(DataAnnotations.RequiredAttribute_ValidationError), ErrorMessageResourceType = typeof(DataAnnotations))]
    public int? Id { get; set; }

    [Display(Name = nameof(Models.User_Name), ResourceType = typeof(Models))]
    [Required(ErrorMessageResourceName = nameof(DataAnnotations.RequiredAttribute_ValidationError), ErrorMessageResourceType = typeof(DataAnnotations))]
    [StringLength(10, ErrorMessageResourceName = nameof(DataAnnotations.StringLengthAttribute_ValidationError), ErrorMessageResourceType = typeof(DataAnnotations))]
    public string? Name { get; set; }

    [Display(Name = nameof(Models.User_Age), ResourceType = typeof(Models))]
    [Required(ErrorMessageResourceName = nameof(DataAnnotations.RequiredAttribute_ValidationError), ErrorMessageResourceType = typeof(DataAnnotations))]
    [Range(0, 200, ErrorMessageResourceName = nameof(DataAnnotations.RangeAttribute_ValidationError), ErrorMessageResourceType = typeof(DataAnnotations))]
    public byte? Age { get; set; }
}

Validation (Resource)

using Izayoi.Data.Validation;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

public class Example()
{
    private readonly IDataValidator dataValidator = new DataValidator();

    public void Method()
    {
        var user = new User
        {
            Id = null,
            Name = "1234567890a",
            Age = 201,
        };

        bool isValid = dataValidator.TryValidateResults(user, out List<ValidationResult> validationResults);

        // isValid: false

        // validationResult[0]
        //   ErrorMessage: "The User ID field is required."
        // validationResult[1]
        //   ErrorMessage: "The field Username must be a string with a maximum length of 10."
        // validationResult[2]
        //   ErrorMessage: "The field Age must be between 0 and 200."
    }
}
Clone this wiki locally