Пример #1
0
        /// <summary>
        /// Validates a username, throwing a <see cref="ValidationErrorException"/>
        /// if any errors are found.  By default the validator checks that the
        /// format contains only the characters permitted by the  <see cref="UsernameOptions"/>
        /// configuration settings, as well as checking for uniquness.
        /// </summary>
        public static async Task ValidateAsync(this IUsernameValidator usernameValidator, IUsernameValidationContext context)
        {
            var result = await usernameValidator.GetErrorsAsync(context);

            if (result.Any())
            {
                throw new ValidationErrorException(result.First());
            }
        }
        public async Task <ValidationQueryResult> ExecuteAsync(ValidateUsernameQuery query, IExecutionContext executionContext)
        {
            var userArea         = _userAreaDefinitionRepository.GetRequiredByCode(query.UserAreaCode);
            var formattingResult = _userDataFormatter.FormatUsername(userArea, query.Username);

            var context = new UsernameValidationContext()
            {
                Username         = formattingResult,
                ExecutionContext = executionContext,
                PropertyName     = nameof(query.Username),
                UserAreaCode     = userArea.UserAreaCode,
                UserId           = query.UserId
            };

            var errors = await _usernameValidator.GetErrorsAsync(context);

            var result = new ValidationQueryResult(errors.FirstOrDefault());

            return(result);
        }