Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Latest commit

 

History

History
59 lines (48 loc) · 1.34 KB

File metadata and controls

59 lines (48 loc) · 1.34 KB

Main page

Validation introduction

Model validation can be leverage by performing two steps. Create your Model and add a supported attribute from System.ComponentModel.DataAnnotations. Next, add the following in your AutoFac Module.

builder.UseDataAnnotationsValidation();

Currently supported attribute

  • StringLengthAttribute

Custom Model Validation

You can also validate your Model via a custom validator. You will need to implement the IModelValidation interface. An example below.

	public class MyValidation : IModelValidation
	{
		public bool CanHandle(Type type)
		{
			return type == typeof(Model4);
		}

		public bool TryAssertMemberValueIsValid(Member member, object value, out string errorCode, out string message)
		{
			if (member.Name == "DateField")
			{
				DateTime result;
				if (DateTime.TryParse(value.ToString(), out result))
				{
					if (result == DateTime.MinValue)
					{
						errorCode = "DateTimeError";
						message = "DateTime cannot be Min Value.";
						return false;
					}
				}
				else
				{
					errorCode = "DateTimeError";
					message = "DateTime is invalid.";
					return false;
				}
			}

			errorCode = null;
			message = null;
			return true;
		}
	}

Lastly, register your custom validator in your AutoFac Module.

builder.RegisterType<MyValidation>().As<IModelValidation>();