public static void ValidateAnnotations(this ValidatorResult result, object entity) { if (entity == null) { return; } var ctx = new ValidationContext(entity); var type = entity.GetType(); var props = type.GetProperties(); foreach (var prop in props) { var value = prop.GetValue(entity, null); ctx.MemberName = prop.Name; var validationList = new List <ValidationResult>(); Validator.TryValidateProperty(value, ctx, validationList); foreach (var validation in validationList) { result.AddError(validation.ErrorMessage); } } }
protected override void DefaultValidations(ValidatorResult result, User entity) { base.DefaultValidations(result, entity); if (Service.GetAll(x => x.Login.ToLower() == entity.Login.ToLower() && x.Id != entity.Id).Any()) { result.AddError($"There is already a user with this Login: {entity.Login}"); } }
protected virtual void DefaultValidations(ValidatorResult result, T entity) { AnnotationsValidations(result, entity); if (entity == null) { result.AddError("Entity can not be null!"); } }