You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
1.1 KiB

  1. using Common.Models.ModelValidation;
  2. using Common.Models.RequestResponse;
  3. using System.Collections.Generic;
  4. using System.ComponentModel.DataAnnotations;
  5. using System.Linq;
  6. namespace Common.Utility
  7. {
  8. public class CustomModelValidaton
  9. {
  10. public TPResponse IsModelValid<T>(T model) where T : class
  11. {
  12. var jm = new ValidationContext(model, serviceProvider: null, items: null);
  13. var results = new List<ValidationResult>();
  14. bool IsValid = Validator.TryValidateObject(model, jm, results);
  15. if (!IsValid)
  16. {
  17. var errorList = results.Select(x => new FieldsValidation()
  18. {
  19. Name = x.MemberNames.First(),
  20. Message = x.ErrorMessage
  21. }).ToList();
  22. TPResponse tPResponse = new TPResponse()
  23. {
  24. ResponseCode = "104",
  25. Msg = "Model Validation Failed !",
  26. Data = errorList
  27. };
  28. return tPResponse;
  29. }
  30. return null;
  31. }
  32. }
  33. }