|
| 1 | +using ExpressValidator.Extensions; |
| 2 | +using FluentValidation; |
| 3 | +using FluentValidation.Results; |
| 4 | +using System; |
| 5 | + |
| 6 | +namespace ExpressValidator.QuickValidation |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Provides methods for quick validation. |
| 10 | + /// </summary> |
| 11 | + public static class QuickValidator |
| 12 | + { |
| 13 | + private const string FALLBACK_PROP_NAME = "Input"; |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// Validates the given object instance using <paramref name="action"/>. |
| 17 | + /// </summary> |
| 18 | + /// <typeparam name="T">The type of the object to validate.</typeparam> |
| 19 | + /// <param name="obj">The object to validate.</param> |
| 20 | + /// <param name="action">Action to add validators.</param> |
| 21 | + /// <param name="propName">The name of the property if the validation fails. |
| 22 | + /// If <see langword="null"/>, "Input" will be used.</param> |
| 23 | + /// <param name="onSuccessValidation">Specifies a method to execute when validation succeeds.</param> |
| 24 | + /// <returns></returns> |
| 25 | + public static ValidationResult Validate<T>(T obj, Action<IRuleBuilderOptions<T, T>> action, string propName, Action<T> onSuccessValidation = null) |
| 26 | + { |
| 27 | + return ValidateInner<T>(obj, action, propName ?? FALLBACK_PROP_NAME, onSuccessValidation); |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Validates the given object instance using <paramref name="action"/>. |
| 32 | + /// </summary> |
| 33 | + /// <typeparam name="T">The type of the object to validate.</typeparam> |
| 34 | + /// <param name="obj">The object to validate.</param> |
| 35 | + /// <param name="action">Action to add validators.</param> |
| 36 | + /// <param name="mode"><see cref="PropertyNameMode"/>. |
| 37 | + /// If <see cref="PropertyNameMode.Default"/>, "Input" will be used.</param> |
| 38 | + /// <param name="onSuccessValidation">Specifies a method to execute when validation succeeds.</param> |
| 39 | + /// <returns></returns> |
| 40 | + public static ValidationResult Validate<T>(T obj, Action<IRuleBuilderOptions<T, T>> action, PropertyNameMode mode = PropertyNameMode.Default, Action<T> onSuccessValidation = null) |
| 41 | + { |
| 42 | + return ValidateInner<T>(obj, action, GetPropName<T>(mode), onSuccessValidation); |
| 43 | + } |
| 44 | + |
| 45 | + private static ValidationResult ValidateInner<T>(T obj, Action<IRuleBuilderOptions<T, T>> action, string propName, Action<T> onSuccessValidation = null) |
| 46 | + { |
| 47 | + var eb = new ExpressValidatorBuilder<Unit>(); |
| 48 | + return eb.AddFunc((_) => obj, |
| 49 | + propName, |
| 50 | + onSuccessValidation) |
| 51 | + .WithValidation(action) |
| 52 | + .BuildAndValidate(Unit.Default); |
| 53 | + } |
| 54 | + |
| 55 | + private static string GetPropName<T>(PropertyNameMode mode) => mode == PropertyNameMode.Default ? FALLBACK_PROP_NAME : typeof(T).Name; |
| 56 | + } |
| 57 | +} |
0 commit comments