/// <summary> /// CheckFactsAndDimensionsMatch /// </summary> /// <param name="totalCells"></param> /// <param name="spec"></param> /// <param name="language"></param> /// <param name="messageFormatter"></param> /// <returns></returns> private bool CheckFactsAndDimensionsMatch(int totalCells, Matrix.Specification spec, string language, MessageFormatter messageFormatter) { try { var cubeSize = 0; var statisticCount = spec.Statistic.Count; var periodCount = spec.Frequency.Period.Count; var variableCombinations = 1; foreach (var classification in spec.Classification) { variableCombinations = variableCombinations * classification.Variable.Count; } cubeSize = variableCombinations * periodCount * statisticCount; if (cubeSize != totalCells) { //TODO: do we need translation here? messageFormatter.AppendArgument("Language", language); messageFormatter.AppendArgument("Expected", cubeSize); messageFormatter.AppendArgument("Found", totalCells); return(false); } return(true); } catch (Exception e) { Log.Instance.ErrorFormat("Error @ CheckFactsAndDimensionsMatch for language = {0}", language); Log.Instance.Error(e); return(false); } }
public override IEnumerable <ModelClientValidationRule> GetClientValidationRules() { if (!ShouldGenerateClientSideRules()) { yield break; } string comparisonValuePlaceHolder = "ComparisonValue"; // Convert validator NotEqualValidator notEqualValidator = (NotEqualValidator)Validator; // Initialize client rule ModelClientValidationRule rule = new ModelClientValidationRule(); rule.ValidationType = "notequal"; MessageFormatter formatter = new MessageFormatter() .AppendPropertyName(Metadata.DisplayName ?? Rule.PropertyName); if (notEqualValidator.MemberToCompare != null) { // Append comparision member to message formatter.AppendArgument( comparisonValuePlaceHolder, notEqualValidator.MemberToCompare.GetDisplayName()); // Append '*.' to the name of field for prefix merging rule.ValidationParameters["field"] = string.Format("*.{0}", notEqualValidator.MemberToCompare.Name); } else if (notEqualValidator.ValueToCompare != null) { string valueToCompare = null; if (notEqualValidator.ValueToCompare is DateTime) { // If value is of type DateTime convert it to DateTime // and format as yyyy-MM-dd to be able to parse it at client side // easily using jQuery. DateTime dateValueToCompare = (DateTime)notEqualValidator.ValueToCompare; valueToCompare = dateValueToCompare.ToString("yyyy-MM-dd"); } else { valueToCompare = notEqualValidator.ValueToCompare.ToString(); } // Append comparision value to message formatter.AppendArgument(comparisonValuePlaceHolder, valueToCompare); // Set value to compare rule.ValidationParameters["value"] = valueToCompare; } // Set error message of rule rule.ErrorMessage = formatter.BuildMessage(Validator.ErrorMessageSource.GetString(null)); yield return(rule); }
public void Adds_value_to_message() { string result = formatter .AppendArgument("foo", "bar") .BuildMessage("{foo}"); result.ShouldEqual("bar"); }
public override IEnumerable <ValidationFailure> Validate(PropertyValidatorContext context) { var results = _innerValidator.Validate(context); if (results.Any()) { if (string.IsNullOrWhiteSpace(_message)) { _onFailureSimple((T)context.Instance, context); } else { var messageFormatter = new MessageFormatter(); messageFormatter.AppendPropertyName(context.PropertyName); messageFormatter.AppendArgument("PropertyValue", context.PropertyValue); try { messageFormatter.AppendAdditionalArguments( _funcs.Select(func => func((T)context.Instance, (TProperty)context.PropertyValue)).ToArray()); } catch (Exception ex) { Debug.WriteLine(ex); } var msg = messageFormatter.BuildMessage(_message); _onFailureComplex((T)context.Instance, context, msg); } } return(results); }
/// <summary> /// CheckIsAValidSource /// </summary> /// <param name="source"></param> /// <param name="messageFormatter"></param> /// <returns></returns> private bool CheckIsAValidSource(string source, MessageFormatter messageFormatter) { try { if (!Matrix.SourceIsSupported(ado, source)) { //TODO: do we need translation here? messageFormatter.AppendArgument("Source", source); return(false); } return(true); } catch (Exception e) { Log.Instance.ErrorFormat("Error @ CheckIsAValidSource for id = {0}", source); Log.Instance.Error(e); return(false); } }
public override IEnumerable<ModelClientValidationRule> GetClientValidationRules() { var formatter = new MessageFormatter().AppendPropertyName(propertyDescription); string message = LengthValidator.ErrorMessageSource.GetString(); if(LengthValidator.ErrorMessageSource.ResourceType == typeof(Messages)) { // If we're using the default resources then the mesage for length errors will have two parts, eg: // '{PropertyName}' must be between {MinLength} and {MaxLength} characters. You entered {TotalLength} characters. // We can't include the "TotalLength" part of the message because this information isn't available at the time the message is constructed. // Instead, we'll just strip this off by finding the index of the period that separates the two parts of the message. message = message.Substring(0, message.IndexOf(".") + 1); // also append the min/max bits formatter.AppendArgument("MinLength", LengthValidator.Min) .AppendArgument("MaxLength", LengthValidator.Max); } message = formatter.BuildMessage(message); return new[] { new ModelClientValidationStringLengthRule(message, LengthValidator.Min, LengthValidator.Max) }; }