yavsc/src/Yavsc.Abstract/Attributes/Validation/YaStringLength.cs

46 lines
1.2 KiB
C#
Raw Normal View History

2019-06-16 01:10:52 +01:00
using System;
2017-10-04 23:53:47 +02:00
namespace Yavsc.Attributes.Validation
{
public class YaStringLength: YaValidationAttribute
{
public long MinLen { get; set; } = -1;
private long maxLen;
public YaStringLength(long maxLen) : base(
2019-06-16 01:10:52 +01:00
()=>
"BadStringLength")
2017-10-04 23:53:47 +02:00
{
this.maxLen = maxLen;
}
2019-06-16 01:10:52 +01:00
private long excedent=0;
private long manquant=0;
2017-10-04 23:53:47 +02:00
public override bool IsValid(object value) {
2019-06-16 01:10:52 +01:00
2017-10-04 23:53:47 +02:00
if (value == null) {
return false;
}
2019-06-16 01:10:52 +01:00
2017-10-04 23:53:47 +02:00
string stringValue = value as string;
if (stringValue==null) return false;
if (MinLen>=0)
{
if (stringValue.Length<MinLen)
{
manquant = MinLen-stringValue.Length;
}
return false;
}
if (maxLen>=0)
{
if (stringValue.Length>maxLen) {
excedent = stringValue.Length-maxLen;
return false;
}
}
return true;
}
2019-06-16 01:10:52 +01:00
2017-10-04 23:53:47 +02:00
}
}