Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;

namespace D2L.CodeStyle.Annotations.Contract;

Expand All @@ -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
) {
Comment on lines +13 to +15

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image

Worth it?

RegexPattern = regexPattern;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
/// <summary>Specifies the syntax used in a string.</summary>
[AttributeUsage( AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false )]
internal
sealed class StringSyntaxAttribute : Attribute {

/// <summary>Initializes the <see cref="StringSyntaxAttribute"/> with the identifier of the syntax used.</summary>
/// <param name="syntax">The syntax identifier.</param>
public StringSyntaxAttribute( string syntax ) {
Syntax = syntax;
Arguments = [];
}

/// <summary>Gets the identifier of the syntax used.</summary>
public string Syntax { get; }

/// <summary>Optional arguments associated with the specific syntax employed.</summary>
public object?[] Arguments { get; }

/// <summary>The syntax identifier for strings containing composite formats for string formatting.</summary>
public const string CompositeFormat = nameof( CompositeFormat );

/// <summary>The syntax identifier for strings containing date format specifiers.</summary>
public const string DateOnlyFormat = nameof( DateOnlyFormat );

/// <summary>The syntax identifier for strings containing date and time format specifiers.</summary>
public const string DateTimeFormat = nameof( DateTimeFormat );

/// <summary>The syntax identifier for strings containing <see cref="Enum"/> format specifiers.</summary>
public const string EnumFormat = nameof( EnumFormat );

/// <summary>The syntax identifier for strings containing <see cref="Guid"/> format specifiers.</summary>
public const string GuidFormat = nameof( GuidFormat );

/// <summary>The syntax identifier for strings containing JavaScript Object Notation (JSON).</summary>
public const string Json = nameof( Json );

/// <summary>The syntax identifier for strings containing numeric format specifiers.</summary>
public const string NumericFormat = nameof( NumericFormat );

/// <summary>The syntax identifier for strings containing regular expressions.</summary>
public const string Regex = nameof( Regex );

/// <summary>The syntax identifier for strings containing time format specifiers.</summary>
public const string TimeOnlyFormat = nameof( TimeOnlyFormat );

/// <summary>The syntax identifier for strings containing <see cref="TimeSpan"/> format specifiers.</summary>
public const string TimeSpanFormat = nameof( TimeSpanFormat );

/// <summary>The syntax identifier for strings containing URIs.</summary>
public const string Uri = nameof( Uri );

/// <summary>The syntax identifier for strings containing XML.</summary>
public const string Xml = nameof( Xml );
}
}
Loading