-
Notifications
You must be signed in to change notification settings - Fork 2
Added Pattern attribute support for string domain primitives. #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3f22569
Added Pattern attribute support for string domain primitives.
0c66c3f
Update src/AltaSoft.DomainPrimitives/PatternAttribute.cs
temonk 3536b4f
Update src/AltaSoft.DomainPrimitives.Generator/Helpers/MethodGenerato…
temonk f51dadc
Copilot suggestions
a82093a
Update src/AltaSoft.DomainPrimitives/InvalidDomainValueException.cs
temonk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| using System; | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace AltaSoft.DomainPrimitives; | ||
|
|
||
| /// <summary> | ||
| /// Specifies a regex pattern that is always emitted to the OpenAPI schema and can optionally be used for runtime validation. | ||
| /// By default, the pattern is not validated at runtime; validation occurs only when explicitly requested via <see cref="Validate"/>. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Class)] | ||
| public class PatternAttribute : Attribute | ||
| { | ||
| /// <summary> | ||
| /// Gets the regex pattern used in the generated OpenAPI schema and, when enabled, for runtime validation. | ||
| /// </summary> | ||
| public string Pattern { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets a value indicating whether the <see cref="Pattern"/> should also be enforced via runtime validation. | ||
| /// </summary> | ||
| public bool Validate { get; } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of <see cref="PatternAttribute"/>. | ||
| /// </summary> | ||
| /// <param name="pattern"> | ||
| /// The regex pattern that will always be included in the OpenAPI schema and may also be used for runtime validation. | ||
| /// </param> | ||
| /// <param name="validate"> | ||
| /// A value indicating whether runtime validation should be performed using <paramref name="pattern"/>. Defaults to <see langword="false"/> | ||
| /// to avoid incurring runtime validation overhead unless explicitly requested. | ||
| /// </param> | ||
|
|
||
| public PatternAttribute([StringSyntax(StringSyntaxAttribute.Regex)] string pattern, bool validate = false) | ||
| { | ||
| Pattern = pattern; | ||
| Validate = validate; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
tests/AltaSoft.DomainPrimitives.Generator.Tests/PrimitivesWithPatternAttributeTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| namespace AltaSoft.DomainPrimitives.Generator.Tests; | ||
|
|
||
| public class PrimitivesWithPatternAttributeTests | ||
| { | ||
| [Theory] | ||
| [InlineData("[A-Z]{3}")] | ||
| [InlineData("[A-Z]{10}")] | ||
| [InlineData("\\d+")] | ||
| [InlineData("\\w+")] | ||
| [InlineData("\\s+")] | ||
| [InlineData("[A-Z]{3}\\d{2}")] | ||
| [InlineData("[A-Z]{3}-\\d{3}")] | ||
| [InlineData("^\\d{4}-\\d{2}-\\d{2}$")] | ||
| [InlineData("^\\w+@\\w+\\.\\w+$")] | ||
| [InlineData("^\\d{3}-\\d{2}-\\d{4}$")] | ||
| public void Pattern_Should_Compile(string pattern) | ||
| { | ||
| var source = $$""" | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using AltaSoft.DomainPrimitives; | ||
|
|
||
| namespace AltaSoft.DomainPrimitives; | ||
|
|
||
| /// <summary> | ||
| /// A string domain primitive with both length and pattern validation attributes, as well as a custom validation method. | ||
| /// </summary> | ||
| [StringLength(1, 100)] | ||
| [Pattern(@"{{pattern}}, true")] | ||
| internal partial class StringWithLengthAndPattern : IDomainValue<string> | ||
temonk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| /// <inheritdoc/> | ||
| public static PrimitiveValidationResult Validate(string value) | ||
| { | ||
| return PrimitiveValidationResult.Ok; | ||
| } | ||
| } | ||
|
|
||
| """; | ||
|
|
||
| TestHelper.Compile(source, (_, sources, _) => Assert.Equal(4, sources.Count)); | ||
|
|
||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.