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
2 changes: 1 addition & 1 deletion docs/extending.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ if (!OrderNumberRequirements.HasOrderPrefix(instance.OrderNumber))

## Requirement Rule Requirements

- The member selector must be a simple member access, such as `x => x.OrderNumber`.
- The member selector must be a simple member path, such as `x => x.OrderNumber` or `x => x.Profile.Email`.
- The requirement must be a static method group.
- The requirement must return `bool`.
- The requirement must have exactly one parameter.
Expand Down
10 changes: 10 additions & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ public void Define(ValidationRules<CreateUser> rules)

Built-in rules are generated as direct C# checks.

Member selectors can target direct members or nested member paths:

```csharp
rules.Required(x => x.Email);
rules.Required(x => x.Profile.Email);
```

Nested selectors report dotted member paths, such as `Profile.Email`.
Intermediate null values are handled as missing nested values.

## Required

Requires the value to be present. For strings, whitespace is treated as missing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal sealed class DefineMethodAnalyzer
{
var defineMethod = declaration.Members
.OfType<MethodDeclarationSyntax>()
.FirstOrDefault(IsDefineMethod);
.FirstOrDefault(method => IsDefineMethod(semanticModel, method, commandType, validationRules));

if (defineMethod == null)
{
Expand Down Expand Up @@ -58,19 +58,71 @@ internal sealed class DefineMethodAnalyzer
rules);
}

private static bool IsDefineMethod(MethodDeclarationSyntax method)
private static bool IsDefineMethod(
SemanticModel semanticModel,
MethodDeclarationSyntax method,
INamedTypeSymbol commandType,
INamedTypeSymbol validationRules)
{
if (method.Identifier.ValueText != "Define")
{
return false;
}

return HasSingleParameter(method);
if (!HasSingleParameter(method))
{
return false;
}

if (!ReturnsVoid(semanticModel, method))
{
return false;
}

return HasValidationRulesParameter(semanticModel, method, commandType, validationRules);
}

private static bool HasSingleParameter(MethodDeclarationSyntax method)
{
return method.ParameterList.Parameters.Count == 1;
}

private static bool ReturnsVoid(SemanticModel semanticModel, MethodDeclarationSyntax method)
{
var symbol = semanticModel.GetDeclaredSymbol(method);
if (!(symbol is IMethodSymbol methodSymbol))
{
return false;
}

return methodSymbol.ReturnsVoid;
}

private static bool HasValidationRulesParameter(
SemanticModel semanticModel,
MethodDeclarationSyntax method,
INamedTypeSymbol commandType,
INamedTypeSymbol validationRules)
{
var parameter = method.ParameterList.Parameters[0];
var type = semanticModel.GetTypeInfo(parameter.Type!).Type;

if (!(type is INamedTypeSymbol namedType))
{
return false;
}

if (!SymbolEqualityComparer.Default.Equals(namedType.OriginalDefinition, validationRules))
{
return false;
}

if (namedType.TypeArguments.Length != 1)
{
return false;
}

return SymbolEqualityComparer.Default.Equals(namedType.TypeArguments[0], commandType);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace TinyValidations.SourceGen.Analysis.Members
namespace TinyValidations.SourceGen.Analysis.Rules
{
internal sealed class AnalyzedMemberAccess
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace TinyValidations.SourceGen.Analysis.Members
namespace TinyValidations.SourceGen.Analysis.Rules
{
internal sealed class MemberAccessAnalyzer
{
Expand All @@ -25,7 +25,7 @@ internal sealed class MemberAccessAnalyzer
}

var path = string.Join(".", members);
return new AnalyzedMemberAccess(path, "instance." + path);
return new AnalyzedMemberAccess(path, CreateAccess(members));
}

private static string GetParameterName(LambdaExpressionSyntax lambda)
Expand Down Expand Up @@ -86,5 +86,15 @@ private static bool IsOriginalParameter(ExpressionSyntax? expression, string par

return identifier.Identifier.ValueText == parameterName;
}

private static string CreateAccess(List<string> members)
{
if (members.Count == 1)
{
return "instance." + members[0];
}

return "instance." + string.Join("?.", members);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using TinyValidations.SourceGen.Analysis.Members;
using TinyValidations.SourceGen.Model;
using TinyValidations.SourceGen.Validation;

Expand Down
Loading
Loading