Analyzer for compile-time parameter validation with the power of regex.
This project has a dependency to Emik.Morsels, if you are building this project, refer to its README first.
[StringSyntax(StringSyntaxAttribute.Regex)]
const string ValidatorQuery = @"^\d{2}$";
[GeneratedRegex(ValidatorQuery)]
partial Regex Validator();
byte? Test([Emik.Match(ValidatorQuery)] string x)
{
if (!Validator().IsMatch(out var capture)) // OK
return null;
if (!Validator().IsMatch(out _, out var captureThatDoesNotExist)) // Error
return null;
if (!new Regex(x).IsMatch(out _)) // Warning: Not a constant
return null;
return byte.Parse(capture);
}
string TestTyped(X x) => x.Value;
record X(string Value)
{
public static implicit operator X([Match(@"^\w{3}$")] string value) => new(value);
}
Test("12"); // OK
Test("34"); // OK
TestTyped("foo"); // OK
TestTyped("bar"); // OK
Test("1"); // Error
Test("foobar"); // Error
TestTyped("12"); // Error
TestTyped("food"); // Error
Test(bool.FalseString); // Warning: Not a constant
TestTyped(bool.TrueString); // Warning: Not a constant
Id | Title |
---|---|
EAM001 | Argument fails regex test |
EAM002 | Non-constant argument may fail regex test |
EAM003 | Argument regex test took too long |
EAM004 | Capture group doesn't exist |
EAM005 | Missing out declaration for capture group |
EAM006 | Non-constant Regex may have wrong number of capture groups |
Issues and pull requests are welcome to help this repository be the best it can be.
This repository falls under the MPL-2 license.