public Task <object?> InvokeAsync(QueryContext context, CancellationToken cancellationToken) { try { DataAnnotationsValidator.Validate(context.Query, context.ScopedServices); } catch (ValidationException ex) { throw ServiceErrorException.From(ex); } return(_next(context, cancellationToken)); }
public void RespectsNullableRefTypeAnnotation() { var command = new GetUserQuery { }; var validationEx = Assert.Throws <ValidationException>(() => DataAnnotationsValidator.Validate(command)); Assert.IsType <RequiredAttribute>(validationEx.ValidationAttribute); Assert.Equal(new[] { nameof(command.Identifier) }, validationEx.ValidationResult.MemberNames); var serviceErrorEx = ServiceErrorException.From(validationEx); Assert.Equal(ServiceErrorCode.ParamNotSpecified, serviceErrorEx.ErrorCode); Assert.Equal(new[] { nameof(command.Identifier) }, serviceErrorEx.Args); }
public void ChecksNestedProperty() { var command = new GetUserQuery { Identifier = new UserIdentifier.Name { } }; var validationEx = Assert.Throws <ValidationException>(() => DataAnnotationsValidator.Validate(command)); Assert.IsType <RequiredAttribute>(validationEx.ValidationAttribute); Assert.Equal(new[] { nameof(UserIdentifier.Name.Value) }, validationEx.ValidationResult.MemberNames); var serviceErrorEx = ServiceErrorException.From(validationEx); Assert.Equal(ServiceErrorCode.ParamNotSpecified, serviceErrorEx.ErrorCode); Assert.Equal(new[] { nameof(command.Identifier) + "." + nameof(UserIdentifier.Name.Value) }, serviceErrorEx.Args); }
public void RespectsIValidatableObjectImplementation() { var command = new ApproveUserCommand { UserName = "******", Verify = true, VerificationToken = null }; var validationEx = Assert.Throws <ValidationException>(() => DataAnnotationsValidator.Validate(command)); Assert.IsType <ExtendedValidationResult>(validationEx.ValidationResult); Assert.IsType <RequiredAttribute>(validationEx.ValidationAttribute); Assert.Equal(new[] { nameof(command.VerificationToken) }, validationEx.ValidationResult.MemberNames); var serviceErrorEx = ServiceErrorException.From(validationEx); Assert.Equal(ServiceErrorCode.ParamNotSpecified, serviceErrorEx.ErrorCode); Assert.Equal(new[] { nameof(command.VerificationToken) }, serviceErrorEx.Args); }