Beispiel #1
0
        public string GetString(object context)
        {
            if (context is PropertyValidatorContext pvc)
            {
                // For backwards compatibility, only pass in the PropertyValidatorContext if the string source implements IContextAwareStringSource
                // otherwise fall back to old behaviour of passing the instance.
                object contextForInner = _inner is IContextAwareStringSource ? pvc : pvc.Instance;

                //Apply the message format args to the MessageFormatter. Workflow looks like this:
                // -> PropertyValidator calls GetString
                // -> GetString adds to MessageFormmater
                // -> PropertyValidator calls MessageFormatter.Build passing in the string already appended.

                if (_customFormatArgs != null &&
                    _customFormatArgs.Count > 0)
                {
                    var additionalArguments = _customFormatArgs.Select(func => func(pvc.Instance, pvc.PropertyValue)).ToArray();
                    pvc.MessageFormatter.AppendAdditionalArguments(additionalArguments);
                }

                return(_inner.GetString(contextForInner));
            }

            return(_inner.GetString(context));
        }
Beispiel #2
0
        /// <summary>
        /// Creates an error validation result for this validator.
        /// </summary>
        /// <param name="context">The validator context</param>
        /// <returns>Returns an error validation result.</returns>
        protected virtual ValidationFailure CreateValidationError(PropertyValidatorContext context)
        {
            var messageBuilderContext = new MessageBuilderContext(context, errorSource, this);

            var error = context.Rule.MessageBuilder != null
                                ? context.Rule.MessageBuilder(messageBuilderContext)
                                : messageBuilderContext.GetDefaultMessage();

            var failure = new ValidationFailure(context.PropertyName, error, context.PropertyValue);

            failure.FormattedMessageArguments         = context.MessageFormatter.AdditionalArguments;
            failure.FormattedMessagePlaceholderValues = context.MessageFormatter.PlaceholderValues;
            failure.ResourceName = errorSource.ResourceName;
            failure.ErrorCode    = (errorCodeSource != null)
                                ? errorCodeSource.GetString(context.Instance)
                                : ValidatorOptions.ErrorCodeResolver(this);

            if (CustomStateProvider != null)
            {
                failure.CustomState = CustomStateProvider(context);
            }

            failure.Severity = Severity;
            return(failure);
        }
        string BuildErrorMessage(PropertyValidatorContext context)
        {
            // For backwards compatibility, only pass in the PropertyValidatorContext if the string source implements IContextAwareStringSource
            // otherwise fall back to old behaviour of passing the instance.
            object stringSourceContext = errorSource is IContextAwareStringSource ? context : context.Instance;

            string error = context.MessageFormatter.BuildMessage(errorSource.GetString(stringSourceContext));

            return(error);
        }
        string BuildErrorMessage(PropertyValidatorContext context)
        {
            context.MessageFormatter.AppendAdditionalArguments(
                customFormatArgs.Select(func => func(context.Instance, context.PropertyValue)).ToArray()
                );

            string error = context.MessageFormatter.BuildMessage(errorSource.GetString());

            return(error);
        }
        public string GetString()
        {
            var messagePattern = new StringBuilder(_patternStringSource.GetString());

            foreach (var messageArgument in _arguments)
            {
                messagePattern = messagePattern.Replace($"{{{messageArgument.Key}}}", messageArgument.Value.GetString());
            }

            return(messagePattern.ToString());
        }
Beispiel #6
0
        public string GetString()
        {
            var validatorErrorMessageSource = _propertyValidator.ErrorMessageSource;
            var globalizedErrorMessage      = _errorMessageGlobalizationService.GetErrorMessage(_propertyValidator);

            Assertion.IsTrue(
                ReferenceEquals(_propertyValidator.ErrorMessageSource, validatorErrorMessageSource),
                "ErrorMessageSource of PropertyValidator has been changed by invocation of '{0}'.GetErrorMessage (...).",
                _errorMessageGlobalizationService.GetType().FullName);

            return(globalizedErrorMessage ?? _fallbackErrorMessageSource.GetString());
        }
        public ValidationMessage(IStringSource stringSource, ValidationMessageType validationMessageType = ValidationMessageType.Error)
        {
            _stringSource = stringSource;

            ValidationMessageType = validationMessageType;
            Message = _stringSource.GetString();

            if (ValidationOptions.LanguageManager.TrackCultureChanged == true)
            {
                WeakEventManager <ILanguageManager, CultureChangedEventArgs> .AddHandler(
                    ValidationOptions.LanguageManager, nameof(ILanguageManager.CultureChanged), OnCultureChanged);
            }
        }
        string BuildErrorMessage(PropertyValidatorContext context)
        {
            // Performance: If we got no args we can skip adding nothing to the MessageFormatter.
            if (this.customFormatArgs != null &&
                this.customFormatArgs.Count > 0)
            {
                var additionalArguments = customFormatArgs.Select(func => func(context.Instance, context.PropertyValue)).ToArray();
                context.MessageFormatter.AppendAdditionalArguments(additionalArguments);
            }

            string error = context.MessageFormatter.BuildMessage(errorSource.GetString(context.Instance));

            return(error);
        }
Beispiel #9
0
        /// <summary>
        /// Creates an error validation result for this validator.
        /// </summary>
        /// <param name="context">The validator context</param>
        /// <returns>Returns an error validation result.</returns>
        protected virtual ValidationFailure CreateValidationError(PropertyValidatorContext context)
        {
            context.MessageFormatter.AppendAdditionalArguments(
                customFormatArgs.Select(func => func(context.Instance)).ToArray()
                );

            string error = context.MessageFormatter.BuildMessage(errorSource.GetString());

            var failure = new ValidationFailure(context.PropertyName, error, errorCode, context.PropertyValue);

            if (CustomStateProvider != null)
            {
                failure.CustomState = CustomStateProvider(context.Instance);
            }

            return(failure);
        }
Beispiel #10
0
        string BuildErrorMessage(PropertyValidatorContext context)
        {
            // Performance: If we got no args we can skip adding nothing to the MessageFormatter.
            if (this.customFormatArgs != null &&
                this.customFormatArgs.Count > 0)
            {
                var additionalArguments = customFormatArgs.Select(func => func(context.Instance, context.PropertyValue)).ToArray();
                context.MessageFormatter.AppendAdditionalArguments(additionalArguments);
            }


            // For backwards compatibility, only pass in the PropertyValidatorContext if the string source implements IContextAwareStringSource
            // otherwise fall back to old behaviour of passing the instance.
            object stringSourceContext = errorSource is IContextAwareStringSource ? context : context.Instance;

            string error = context.MessageFormatter.BuildMessage(errorSource.GetString(stringSourceContext));

            return(error);
        }
        /// <summary>
        /// Creates an error validation result for this validator.
        /// </summary>
        /// <param name="context">The validator context</param>
        /// <returns>Returns an error validation result.</returns>
        protected virtual ValidationFailure CreateValidationError(PropertyValidatorContext context)
        {
            Func <PropertyValidatorContext, string> errorBuilder = context.Rule.MessageBuilder ?? BuildErrorMessage;
            var error = errorBuilder(context);

            var failure = new ValidationFailure(context.PropertyName, error, context.PropertyValue);

            failure.FormattedMessageArguments         = context.MessageFormatter.AdditionalArguments;
            failure.FormattedMessagePlaceholderValues = context.MessageFormatter.PlaceholderValues;
            failure.ResourceName = errorSource.ResourceName;
            failure.ErrorCode    = (errorCodeSource != null) ? errorCodeSource.GetString() : originalErrorSource.ResourceName;

            if (CustomStateProvider != null)
            {
                failure.CustomState = CustomStateProvider(context.Instance);
            }

            return(failure);
        }
 protected static string GetValidationMessage(IStringSource source)
 {
     return(source.ResourceType == null?source.GetString(null) : null);
 }
 private void OnCultureChanged(object sender, CultureChangedEventArgs args)
 {
     Message = _stringSource.GetString();
     OnPropertyChanged(nameof(Message));
 }
Beispiel #14
0
 public void Write(IStringSource stringSource)
 {
     Console.WriteLine(stringSource.GetString().Result);
 }