private void WriteStringLength(ILWriter writer, PropertyInfo property, StringLengthAttribute attribute) { var local = writer.DeclareLocal <string>(); if (property.PropertyType == typeof(string)) { writer.LoadFirstParameter(); writer.GetPropertyValue(property); writer.SetLocal(local); writer.LoadLocal(local); writer.LoadNull(); var notNullBlock = writer.IfEqualThen(); var isNullMessage = CommonResults.PropertyCannotBeNull.WithValues(property.ReflectedType.Name, property.Name); WriteFailureResult(writer, ValidationKind.StringLength, isNullMessage); writer.Return(); writer.MarkLabel(notNullBlock); var lengthValue = writer.DeclareLocal <int>(); var length = KnownMetadata.Properties.String_Length; writer.LoadLocal(local); writer.GetPropertyValue(length); writer.SetLocal(lengthValue); var minValue = attribute.Minimum; writer.LoadLocal(lengthValue); writer.LoadInt32((int)minValue); var longerThanMinBlock = writer.IfLessThan(); var tooShortMessage = CommonResults.StringPropertyIsTooShort.WithValues(property.ReflectedType.Name, property.Name, minValue); WriteFailureResult(writer, ValidationKind.StringLength, tooShortMessage); writer.Return(); writer.MarkLabel(longerThanMinBlock); var maxValue = attribute.Maximum; //The maximum value might not be set, if so then just ignore it if (maxValue > 0) { writer.LoadLocal(lengthValue); writer.LoadInt32((int)maxValue); var shorterThanMaxBlock = writer.IfGreaterThan(); var tooLongMessage = CommonResults.StringPropertyIsTooLong.WithValues(property.ReflectedType.Name, property.Name, maxValue); WriteFailureResult(writer, ValidationKind.StringLength, tooLongMessage); writer.Return(); writer.MarkLabel(shorterThanMaxBlock); } } else { WriteNotApplicableResult(writer, ValidationKind.StringLength); writer.Return(); } }
private void WriteIntegerRange(ILWriter writer, PropertyInfo property, IntegerRangeAttribute integerRangeAttribute) { var validationKind = ValidationKind.IntegerRange; var signedSet = new HashSet <Type>() { typeof(sbyte), typeof(short), typeof(int), typeof(long) }; var unsignedSet = new HashSet <Type>() { typeof(byte), typeof(ushort), typeof(uint), typeof(ulong) }; LocalBuilder local; if (signedSet.Contains(property.PropertyType)) { local = writer.DeclareLocal <long>(); } else if (unsignedSet.Contains(property.PropertyType)) { local = writer.DeclareLocal <ulong>(); } else { WriteNotApplicableResult(writer, validationKind); writer.Return(); return; } writer.LoadFirstParameter(); writer.GetPropertyValue(property); writer.Cast(property.PropertyType, local.LocalType); writer.SetLocal(local); if (integerRangeAttribute.HasMinimum && integerRangeAttribute.HasMaximum) { if (integerRangeAttribute.Minimum > integerRangeAttribute.Maximum) { var message = CommonErrors.IntegerRangeValidationHasMinGreaterThanMax.WithValues(property.ReflectedType.Name, property.Name); throw new IncorrectValidationAttributeException(typeof(IntegerRangeAttribute), message); } } if (integerRangeAttribute.HasMinimum) { writer.LoadLocal(local); writer.LoadInt64(integerRangeAttribute.Minimum); var isGreaterThanOrEqual = writer.IfLessThan(); WriteFailureResult(writer, validationKind, CommonResults.IntegerPropertyIsTooLow.WithValues(property.ReflectedType.Name, property.Name, integerRangeAttribute.Minimum)); writer.Return(); writer.MarkLabel(isGreaterThanOrEqual); } if (integerRangeAttribute.HasMaximum) { writer.LoadLocal(local); writer.LoadInt64(integerRangeAttribute.Maximum); var isLessThanOrEqual = writer.IfGreaterThan(); WriteFailureResult(writer, validationKind, CommonResults.IntegerPropertyIsTooHigh.WithValues(property.ReflectedType.Name, property.Name, integerRangeAttribute.Maximum)); writer.Return(); writer.MarkLabel(isLessThanOrEqual); } }