diff --git a/src/D2L.CodeStyle.Annotations/Contract/PatternStringAttribute.cs b/src/D2L.CodeStyle.Annotations/Contract/PatternStringAttribute.cs index ffd8be9a..fe8314b2 100644 --- a/src/D2L.CodeStyle.Annotations/Contract/PatternStringAttribute.cs +++ b/src/D2L.CodeStyle.Annotations/Contract/PatternStringAttribute.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; namespace D2L.CodeStyle.Annotations.Contract; @@ -9,7 +10,9 @@ namespace D2L.CodeStyle.Annotations.Contract; [AttributeUsage( AttributeTargets.Parameter, AllowMultiple = true )] public sealed class PatternStringAttribute : Attribute { - public PatternStringAttribute( string regexPattern ) { + public PatternStringAttribute( + [StringSyntax( StringSyntaxAttribute.Regex )] string regexPattern + ) { RegexPattern = regexPattern; } diff --git a/src/D2L.CodeStyle.Annotations/_InternalPolyfills/StringSyntaxAttribute.cs b/src/D2L.CodeStyle.Annotations/_InternalPolyfills/StringSyntaxAttribute.cs new file mode 100644 index 00000000..f0f0316a --- /dev/null +++ b/src/D2L.CodeStyle.Annotations/_InternalPolyfills/StringSyntaxAttribute.cs @@ -0,0 +1,61 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// Adapted from https://github.com/dotnet/dotnet/blob/b0f34d51fccc69fd334253924abd8d6853fad7aa/src/runtime/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/StringSyntaxAttribute.cs + +namespace System.Diagnostics.CodeAnalysis { + /// Specifies the syntax used in a string. + [AttributeUsage( AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false )] + internal + sealed class StringSyntaxAttribute : Attribute { + + /// Initializes the with the identifier of the syntax used. + /// The syntax identifier. + public StringSyntaxAttribute( string syntax ) { + Syntax = syntax; + Arguments = []; + } + + /// Gets the identifier of the syntax used. + public string Syntax { get; } + + /// Optional arguments associated with the specific syntax employed. + public object?[] Arguments { get; } + + /// The syntax identifier for strings containing composite formats for string formatting. + public const string CompositeFormat = nameof( CompositeFormat ); + + /// The syntax identifier for strings containing date format specifiers. + public const string DateOnlyFormat = nameof( DateOnlyFormat ); + + /// The syntax identifier for strings containing date and time format specifiers. + public const string DateTimeFormat = nameof( DateTimeFormat ); + + /// The syntax identifier for strings containing format specifiers. + public const string EnumFormat = nameof( EnumFormat ); + + /// The syntax identifier for strings containing format specifiers. + public const string GuidFormat = nameof( GuidFormat ); + + /// The syntax identifier for strings containing JavaScript Object Notation (JSON). + public const string Json = nameof( Json ); + + /// The syntax identifier for strings containing numeric format specifiers. + public const string NumericFormat = nameof( NumericFormat ); + + /// The syntax identifier for strings containing regular expressions. + public const string Regex = nameof( Regex ); + + /// The syntax identifier for strings containing time format specifiers. + public const string TimeOnlyFormat = nameof( TimeOnlyFormat ); + + /// The syntax identifier for strings containing format specifiers. + public const string TimeSpanFormat = nameof( TimeSpanFormat ); + + /// The syntax identifier for strings containing URIs. + public const string Uri = nameof( Uri ); + + /// The syntax identifier for strings containing XML. + public const string Xml = nameof( Xml ); + } +}