本文整理汇总了C#中FluentValidation.Validators.PropertyValidatorContext类的典型用法代码示例。如果您正苦于以下问题:C# PropertyValidatorContext类的具体用法?C# PropertyValidatorContext怎么用?C# PropertyValidatorContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PropertyValidatorContext类属于FluentValidation.Validators命名空间,在下文中一共展示了PropertyValidatorContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetComparisonValue
private IComparable GetComparisonValue(PropertyValidatorContext context) {
if(valueToCompareFunc != null) {
return (IComparable)valueToCompareFunc(context.Instance);
}
return (IComparable)ValueToCompare;
}
示例2: IsValid
protected override bool IsValid(PropertyValidatorContext context)
{
if (context.PropertyValue != null)
return (bool)context.PropertyValue;
return false;
}
示例3: IsValid
protected override bool IsValid(PropertyValidatorContext context)
{
if (context.PropertyValue == null) return true;
bool fail = false;
string stringTarget = Convert.ToString(context.PropertyValue);
switch (typeName)
{
case ValidatableTypes.number:
re = new Regex(@"^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$");
break;
case ValidatableTypes.email:
re = new Regex(@"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$");
break;
case ValidatableTypes.date:
DateTime d;
fail = !DateTime.TryParse(stringTarget, out d);
break;
case ValidatableTypes.dateISO:
re = new Regex(@"^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$");
break;
case ValidatableTypes.digits:
re = new Regex(@"^\d+$");
break;
case ValidatableTypes.url:
re = new Regex(@"^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$");
break;
}
if (re != null)
fail = !re.IsMatch(stringTarget);
return !fail;
}
示例4: IsValid
protected override bool IsValid(PropertyValidatorContext context) {
var value = context.PropertyValue as string;
if (String.IsNullOrEmpty(value))
return false;
return value.Length == 24;
}
示例5: Validate
public override IEnumerable<ValidationFailure> Validate(PropertyValidatorContext context)
{
if (context.Rule.Member == null) {
throw new InvalidOperationException(string.Format("Nested validators can only be used with Member Expressions."));
}
var collection = context.PropertyValue as IEnumerable;
if (collection == null) {
yield break;
}
int count = 0;
foreach (var element in collection) {
if(element == null) {
// If an element in the validator is null then we want to skip it to prevent NullReferenceExceptions in the child validator.
// We still need to update the counter to ensure the indexes are correct.
count++;
continue;
}
var newContext = context.ParentContext.CloneForChildValidator(element);
newContext.PropertyChain.Add(context.Rule.Member);
newContext.PropertyChain.AddIndexer(count++);
var results = childValidator.Validate(newContext).Errors;
foreach (var result in results) {
yield return result;
}
}
}
示例6: IsValid
protected override bool IsValid(PropertyValidatorContext context) {
if (context.PropertyValue == null) return true;
var min = Min;
var max = Max;
if (MaxFunc != null && MinFunc != null)
{
max = MaxFunc(context.Instance);
min = MinFunc(context.Instance);
}
int length = context.PropertyValue.ToString().Length;
if (length < min || (length > max && max != -1)) {
context.MessageFormatter
.AppendArgument("MinLength", min)
.AppendArgument("MaxLength", max)
.AppendArgument("TotalLength", length);
return false;
}
return true;
}
示例7: IsValid
protected override bool IsValid(PropertyValidatorContext context) {
var value = context.PropertyValue as string;
if (value == null) {
return true;
}
value = value.Replace("-", "").Replace(" ", "");
int checksum = 0;
bool evenDigit = false;
// http://www.beachnet.com/~hstiles/cardtype.html
foreach (char digit in value.ToCharArray().Reverse()) {
if (!char.IsDigit(digit)) {
return false;
}
int digitValue = (digit - '0') * (evenDigit ? 2 : 1);
evenDigit = !evenDigit;
while (digitValue > 0) {
checksum += digitValue % 10;
digitValue /= 10;
}
}
return (checksum % 10) == 0;
}
示例8: IsValid
protected override bool IsValid(PropertyValidatorContext context)
{
if (context.PropertyValue != null && !regex.IsMatch((string)context.PropertyValue)) {
return false;
}
return true;
}
示例9: GetComparisonValue
private object GetComparisonValue(PropertyValidatorContext context) {
if(func != null) {
return func(context.Instance);
}
return ValueToCompare;
}
示例10: CreateNewValidationContextForChildValidator
protected ValidationContext CreateNewValidationContextForChildValidator(object instanceToValidate, PropertyValidatorContext propertyValidatorContext, IValidatorSelector validatorSelector)
{
var propertyChain = new PropertyChain(propertyValidatorContext.PropertyChain);
propertyChain.Add(propertyValidatorContext.Member);
return new ValidationContext(instanceToValidate, propertyChain, validatorSelector);
}
示例11: IsValid
protected override bool IsValid(PropertyValidatorContext context)
{
var ccValue = context.PropertyValue as string;
if (String.IsNullOrWhiteSpace(ccValue))
return false;
ccValue = ccValue.Replace(" ", "");
ccValue = ccValue.Replace("-", "");
int checksum = 0;
bool evenDigit = false;
//http://www.beachnet.com/~hstiles/cardtype.html
foreach (char digit in ccValue.Reverse())
{
if (!Char.IsDigit(digit))
return false;
int digitValue = (digit - '0') * (evenDigit ? 2 : 1);
evenDigit = !evenDigit;
while (digitValue > 0)
{
checksum += digitValue % 10;
digitValue /= 10;
}
}
return (checksum % 10) == 0;
}
示例12: IsValid
protected override bool IsValid(PropertyValidatorContext context) {
if (!predicate(context.Instance, context.PropertyValue, context)) {
return false;
}
return true;
}
示例13: IsValid
protected override bool IsValid(PropertyValidatorContext context) {
#if PORTABLE40
return IsValidAsync(context, new CancellationToken()).Result;
#else
return Task.Run(() => IsValidAsync(context, new CancellationToken())).GetAwaiter().GetResult();
#endif
}
示例14: IsValid
protected override bool IsValid(PropertyValidatorContext context)
{
if (context.PropertyValue == null) {
return false;
}
return true;
}
示例15: IsValid
protected override bool IsValid(PropertyValidatorContext context)
{
if (context.PropertyValue == null) return false;
if (OsInfo.IsNotWindows) return true;
if (!_runtimeInfo.IsWindowsService) return true;
var path = context.PropertyValue.ToString();
if (!DriveRegex.IsMatch(path)) return true;
var drives = _diskProvider.GetDrives();
foreach (var drive in drives)
{
if (path.StartsWith(drive.Name, StringComparison.InvariantCultureIgnoreCase))
{
if (drive.DriveType == DriveType.Network)
{
return false;
}
}
}
return true;
}