using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Primitives;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using VOL.Core.Extensions;
using VOL.Core.Filters;
using VOL.Core.Utilities;
namespace VOL.Core.ObjectActionValidator
{
public static class MethodsValidator
{
///
/// 方法上的model校验配置
///
public static Dictionary ValidatorCollection { get; } = new Dictionary();
public static void Add(this ValidatorModel validatorGroup, Expression> loginExpress = null)
{
if (!ValidatorCollection.TryAdd(validatorGroup.ToString().ToLower(),
loginExpress == null
? typeof(T).GetGenericProperties().Select(x => x.Name).ToArray()
: loginExpress.GetExpressionToArray()))
{
throw new Exception($"键{validatorGroup.ToString()}的表达式已经注册过了");
}
}
///
/// 方法上的普通参数校验配置
///
public static Dictionary ValidatorGeneralCollection { get; } = new Dictionary();
///
/// 默认校验的是string类型
///
/// 普通参数名
/// 校验错误时显示的提示名字
public static void Add(this ValidatorGeneral general, string CNName)
{
general.Add(CNName, ParamType.String, null, null);
}
public static void Add(this ValidatorGeneral general, string CNName, int? max)
{
general.Add(CNName, ParamType.String, null, max);
}
public static void Add(this ValidatorGeneral general, string CNName, ParamType type)
{
general.Add(CNName, type, null, null);
}
public static void Add(this ValidatorGeneral general, string CNName, ParamType type, int? max)
{
general.Add(CNName, type, null, max);
}
public static void Add(this ValidatorGeneral general, string CNName, int? min, int? max)
{
general.Add(CNName, ParamType.String, min, max);
}
///
///
///
/// 普通参数名
/// 校验错误时显示的提示名字
/// 参数类型
/// 最大小度(最小值)
/// 最大长度(最大值)
public static void Add(this ValidatorGeneral general, string CNName, ParamType type, int? min = null, int? max = null)
{
GeneralOptions options = new GeneralOptions(general, CNName, type, min, max);
if (!ValidatorGeneralCollection.TryAdd(general.ToString().ToLower(), options))
{
throw new Exception($"键{general.ToString()}参数配置已经注入过了");
}
}
///
/// 自定义验证
///
///
///
public static void Add(this ValidatorGeneral general, string CNName, Func