コード例 #1
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, this);

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

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

#pragma warning disable 618
            failure.FormattedMessageArguments = context.MessageFormatter.AdditionalArguments;
#pragma warning restore 618
            failure.FormattedMessagePlaceholderValues = context.MessageFormatter.PlaceholderValues;
#pragma warning disable 618
            failure.ErrorCode = ErrorCodeSource?.GetString(context) ?? ValidatorOptions.Global.ErrorCodeResolver(this);
#pragma warning restore 618

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

            if (SeverityProvider != null)
            {
                failure.Severity = SeverityProvider(context);
            }

            return(failure);
        }
コード例 #2
0
        protected PropertyValidator(IStringSource errorMessageSource)
        {
            if (errorMessageSource == null)
            {
                errorMessageSource = new StaticStringSource("No default error message has been specified.");
            }
            else if (errorMessageSource is LanguageStringSource l && l.ErrorCodeFunc == null)
            {
                l.ErrorCodeFunc = ctx => ErrorCodeSource?.GetString(ctx);
            }

            ErrorMessageSource = errorMessageSource;
        }