using JT808.Protocol.Enums; using JT808.Protocol.Exceptions; using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace JT808.Protocol.Extensions { /// /// 验证长度扩展方法 /// public static partial class JT808ValidationExtensions { /// /// 验证字符串长度 /// /// /// /// public static string ValiString(this string value,in string fieldName, in int fixedLength) { vali(value.Length, fieldName, fixedLength); return value; } /// /// 验证字符串最大长度 /// /// /// /// public static string ValiMaxString(this string value, in string fieldName, in int maxLength) { if (length > maxLength) { throw new JT808Exception(JT808ErrorCode.VailLength, $"{fieldName}:{length}>max length[{maxLength}]"); } return value; } /// /// 验证数组长度 /// /// /// /// public static byte[] ValiBytes(this byte[] value,in string fieldName, in int fixedLength) { vali(value.Length, fieldName, fixedLength); return value; } /// /// 验证集合长度 /// /// /// /// public static IEnumerable ValiList(this IEnumerable value, in string fieldName, in int fixedLength) { vali(value.Count(), fieldName, fixedLength); return value; } /// /// 验证 /// /// /// /// private static void vali(in int length,in string fieldName, in int fixedLength) { if (length > fixedLength) { throw new JT808Exception(JT808ErrorCode.VailLength, $"{fieldName}:{length}>fixed[{fixedLength}]"); } else if (length < fixedLength) { throw new JT808Exception(JT808ErrorCode.NotEnoughLength, $"{fieldName}:{length}