/// <summary> /// Verification for VerifiableObjectContext /// </summary> /// <param name="context"></param> /// <returns></returns> internal override CorrectVerifyVal Valid(VerifiableObjectContext context) { var value = GetValueFrom(context); if (!IsActivate(context, value)) { return(CorrectVerifyVal.Ignore); } var verifyVal = CreateVerifyVal(); if (context is null) { UpdateVal(verifyVal, value); } else { var regex = _regexFunc((T)context.Instance); if (regex is null || value is null || !regex.IsMatch((string)value)) { UpdateVal(verifyVal, value, regex?.ToString()); } } return(verifyVal); }
/// <summary> /// Verification for VerifiableObjectContext /// </summary> /// <param name="context"></param> /// <returns></returns> internal override CorrectVerifyVal Valid(VerifiableObjectContext context) { var value = GetValueFrom(context); if (!IsActivate(context, value)) { return(CorrectVerifyVal.Ignore); } var verifyVal = CreateVerifyVal(); if (ContainsMember(context) && Types.IsCollectionType(VerifiableMember.MemberType) && value is ICollection collection) { if (!IsValidImpl(collection, _func)) { UpdateVal(verifyVal, value); } } else { UpdateVal(verifyVal, value, $"The type is not a collection or an array, and an exception occurs when using {TokenName}."); } return(verifyVal); }
/// <summary> /// Verify the entire entity <br /> /// 验证入口 /// </summary> /// <param name="context"></param> /// <returns></returns> /// <exception cref="ArgumentNullException"></exception> public virtual VerifyResult VerifyViaContext(VerifiableObjectContext context) { if (context is null) { throw new ArgumentNullException(nameof(context)); } return(VerifyImpl(context)); }
public static VerifyResult Verify(VerifiableObjectContext context) { if (context is null) { throw new ArgumentNullException(nameof(context)); } return(Verify(context.Instance)); }
/// <summary> /// Determine whether this verifiable token can be verified against the given VerifiableObjectContext. /// </summary> /// <param name="context"></param> /// <returns></returns> protected bool ContainsMember(VerifiableObjectContext context) { if (context is null) { return(false); } return(context.ContainsMember(VerifiableMember.MemberName)); }
protected override VerifyResult VerifyImpl(VerifiableObjectContext context) { var valueContext = context?.GetValue("Name"); if (valueContext is null) { return(_options.ReturnNullReferenceOrSuccess()); } if (valueContext.Value is string { Length : 32 })
/// <summary> /// Is activate, This method is only applicable to 'Verify'. /// </summary> /// <param name="context"></param> /// <param name="value"></param> /// <returns></returns> protected bool IsActivate(VerifiableObjectContext context, object value) { if (WithActivationConditions && ActivationConditions3 is not null) { return(ActivationConditions3.Invoke(context.Instance, value)); } if (WithActivationConditions && ActivationConditions2 is not null) { return(ActivationConditions2.Invoke(value)); } return(true); }
/// <summary> /// Verification for VerifiableObjectContext /// </summary> /// <param name="context"></param> /// <returns></returns> internal override CorrectVerifyVal Valid(VerifiableObjectContext context) { var value = GetValueFrom(context); if (!IsActivate(context, value)) { return(CorrectVerifyVal.Ignore); } var verifyVal = CreateVerifyVal(); if (!IsValidImpl(value)) { UpdateVal(verifyVal, value); } return(verifyVal); }
/// <summary> /// Verification for VerifiableObjectContext /// </summary> /// <param name="context"></param> /// <returns></returns> internal override CorrectVerifyVal Valid(VerifiableObjectContext context) { var value = GetValueFrom(context); if (!IsActivate(context, value)) { return(CorrectVerifyVal.Ignore); } var verifyVal = CreateVerifyVal(); if (!IsValidImpl(value, out var result)) { verifyVal.NameOfExecutedRule = result?.OperationName ?? Name; UpdateVal(verifyVal, value, result?.ErrorMessage); } return(verifyVal); }
public static VerifyResult Verify(IValidator validator, VerifiableObjectContext context, Type typeOfValidator) { #if NET452 var ret = validator.Validate(context.Instance); #else var ctx = FluentValidationContextFactory.Resolve(context); var ret = validator.Validate(ctx); #endif if (ret.IsValid) { return(VerifyResult.Success); } var failures = ret.Errors.ConvertToVerifyFailures(typeOfValidator); return(new VerifyResult(failures) { NameOfExecutedRules = ret.RuleSetsExecuted.Copy() }); }
/// <summary> /// Verification for VerifiableObjectContext /// </summary> /// <param name="context"></param> /// <returns></returns> internal override CorrectVerifyVal Valid(VerifiableObjectContext context) { var value = GetValueFrom(context); if (!IsActivate(context, value)) { return(CorrectVerifyVal.Ignore); } var verifyVal = CreateVerifyVal(); var valueToCompare = _valueToCompareFunc is null ? _valueToCompare : _valueToCompareFunc.Invoke(); if (!IsValidImpl(value, valueToCompare, _typeOfValueToCompare, out var message)) { UpdateVal(verifyVal, value, message); } return(verifyVal); }
protected override VerifyResult VerifyImpl(VerifiableObjectContext context) { if (context is null) { return(_options.ReturnNullReferenceOrSuccess()); } List <VerifyResult> results = new(); var values = context.GetValues(); foreach (var value in values) { // 如果 Value 为 String,对其进行验证 if (value.Value is string str) { var attr = value.GetAttributes <LengthShould16Attribute>().FirstOrDefault(); if (attr is null) { continue; } if (str.Length != 16) { results.Add(new VerifyResult(new VerifyFailure(value.MemberName, "Length should 16.", str))); } } // 否则,如果 Value 不是基础类型(即 Value 为引用类型、结构等),对其进一步解析并验证 else if (!value.BasicTypeState()) { results.Add(VerifyImpl(value.ConvertToObjectContext())); } } return(VerifyResult.MakeTogether(results)); }
/// <summary> /// ObjectContext Verify /// </summary> /// <param name="context"></param> /// <returns></returns> public VerifyResult StrongVerify(VerifiableObjectContext context) => EmailValidator.Instance.VerifyViaContext(context);
/// <summary> /// ObjectContext Verify /// </summary> /// <param name="context"></param> /// <returns></returns> public VerifyResult StrongVerify(VerifiableObjectContext context) => ChinaIdNumberValidator.Instance.VerifyViaContext(context);
public static IValidationContext Resolve(VerifiableObjectContext context) { return(Resolve(context.Type, context.Instance)); }
protected override VerifyResult VerifyImpl(VerifiableObjectContext context) { return(FluentValidationCore.Verify(_validatorImpl, context, typeof(TValidator))); }
/// <summary> /// Verification for VerifiableObjectContext /// </summary> /// <param name="context"></param> /// <returns></returns> internal abstract CorrectVerifyVal Valid(VerifiableObjectContext context);
/// <summary> /// Get value from VerifiableObjectContext /// </summary> /// <param name="context"></param> /// <returns></returns> protected object GetValueFrom(VerifiableObjectContext context) { var memberContext = context?.GetValue(VerifiableMember.MemberName); return(memberContext?.GetValue()); }
public static VerifyResult ValidViaCustomValidators(VerifiableObjectContext context, IEnumerable <CustomValidator> validators) { return(VerifyResult.MakeTogether(validators.Select(validator => validator.VerifyViaContext(context)).ToList())); }
protected abstract VerifyResult VerifyImpl(VerifiableObjectContext context);
public VerifyResult Verify(VerifiableObjectContext context) { return(VerifyResult.Success); }
protected override VerifyResult VerifyImpl(VerifiableObjectContext context) { return(VerifyResult.Success); }