I'm testing my API and I'm not able to figure out how this thing works.
I need the Validator to fail when the pattern [z-a0-9-] is matched against all kinds of input, both valid and invalid.
it doesn't seem to work like i think it should work.
my test code:
[Theory]
[InlineData("62c1a17e6b09e88d43cbb087-transactionreport")]
[InlineData("test-json-20220619324234")]
[InlineData("62c1a17e6b09e88d43cbb087")]
[InlineData("test")]
[InlineData("-json-")]
[InlineData("FileName")]
[InlineData("20220619324234")]
[InlineData("2022--06193--24234")]
public async Task ValidateCreateFileRequest_ReturnTrue(string name)
{
//Arrange
var request = new CreateFileRequest { Name = name };
//Act
var result = _validator.TestValidate(request);
//Assert
result.ShouldNotHaveValidationErrorFor(m => m.Name);
}
validator code:
public CreateFileRequestValidator(IConfiguration configuration)
{
RuleFor(x => x.Name).NotEmpty()
.NotNull()
.MinimumLength(3)
.Matches(configuration["Validations:FileNamePattern"]).WithMessage("File name can contain only lowercase characters, digits and hyphens");
}
All these tests pass but one test ("FileName") should NOT pass because it doesn't match the pattern.
it seem like it returns valid when the first match is found. how do i tell the validation to keep matching until it fails?
CodePudding user response:
I think your pattern is not correct. try this.
^[a-z0-9-]
I tried this with a sample app and it fails for "FileName" but passed for all other string you mention in your question.
CodePudding user response:
the pattern is "^[a-z0-9-] $"