| Severity | Error |
| Reported by | Metalama.Extensions.Architecture |
| Message | The string '<Pattern>' is not a valid regular expression: <ErrorMessage> |
Cause
The DerivedTypesMustRespectRegexNamingConventionAttribute aspect enforces a naming convention on all types derived from the target type. Its pattern is a .NET regular expression.
Before it validates the derived types, the aspect parses the pattern. If parsing fails, Metalama reports LAMA0902 on the target type and skips the aspect, so the naming convention isn't enforced. The fabric extension methods DerivedTypesMustRespectRegexNamingConvention and MustRespectRegexNamingConvention validate their pattern in the same way. In the message, <Pattern> is the pattern and <ErrorMessage> is the error returned by the .NET regular expression parser.
A frequent cause is passing a star pattern, such as *Service, which is the syntax of DerivedTypesMustRespectNamingConventionAttribute. In a regular expression, * must follow the expression it repeats.
In the following example, the [DerivedTypesMustRespectRegexNamingConvention] attribute receives the star pattern *Service.
1using Metalama.Extensions.Architecture.Aspects;
2
3namespace Doc.LAMA0902.Error;
4
5// Error: '*Service' is a star pattern, not a valid regular expression.
6[DerivedTypesMustRespectRegexNamingConvention( "*Service" )]
Error LAMA0902: The string '*Service' is not a valid regular expression: Invalid pattern '*Service' at offset 1. Quantifier '*' following nothing.
7public abstract class ServiceBase { }
8
9internal class OrderService : ServiceBase { }
10
Resolution
Do one of the following:
- Fix the regular expression, using the error given in the message. For instance, write
^.*Service$instead of*Service, as in the following example. - If a star pattern is enough, use the [DerivedTypesMustRespectNamingConvention] attribute instead, where
*matches any sequence of characters.
1using Metalama.Extensions.Architecture.Aspects;
2
3namespace Doc.LAMA0902.Fixed;
4
5// Fixed: the pattern is a valid regular expression.
6[DerivedTypesMustRespectRegexNamingConvention( "^.*Service$" )]
7public abstract class ServiceBase { }
8
9internal class OrderService : ServiceBase { }
10