Skip to content

Commit

Permalink
chore: add AvroValidationRule for names and symbols (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualBean authored Oct 25, 2024
1 parent 59fef05 commit f7d96d8
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/LEGO.AsyncAPI/Resource.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/LEGO.AsyncAPI/Resource.resx
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,10 @@
<data name="Validation_MustBeAbsoluteUrl" xml:space="preserve">
<value>The field '{0}' in '{1}' object MUST be an absolute uri.</value>
</data>
<data name="Validation_NameMustMatchRegularExpr" xml:space="preserve">
<value>'{0}' MUST match the regular expression '{1}'.</value>
</data>
<data name="Validation_SymbolsMustMatchRegularExpression" xml:space="preserve">
<value>Symbols MUST match the regular expression '{1}'.</value>
</data>
</root>
8 changes: 8 additions & 0 deletions src/LEGO.AsyncAPI/Services/AsyncApiVisitorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public virtual void Visit(AsyncApiDocument doc)
{
}

public virtual void Visit(AsyncApiJsonSchemaPayload jsonPayload)
{
}

public virtual void Visit(AsyncApiAvroSchemaPayload avroPayload)
{
}

public virtual void Visit(IDictionary<string, AsyncApiAny> anys)
{
}
Expand Down
11 changes: 10 additions & 1 deletion src/LEGO.AsyncAPI/Services/AsyncApiWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ internal void Walk(AsyncApiParameter parameter, bool isComponent = false)
this.Walk(parameter as IAsyncApiExtensible);
}

internal void Walk(AsyncApiAvroSchemaPayload payload)
{
this.visitor.Visit(payload);
}

internal void Walk(AsyncApiSchema schema, bool isComponent = false)
{
if (schema == null || this.ProcessAsReference(schema, isComponent))
Expand Down Expand Up @@ -539,7 +544,11 @@ internal void Walk(AsyncApiMessage message, bool isComponent = false)
this.Walk(AsyncApiConstants.Payload, () => this.Walk((AsyncApiSchema)payload));
}

// #ToFix Add walking for avro.
if (message.Payload is AsyncApiAvroSchemaPayload avroPayload)
{
this.Walk(AsyncApiConstants.Payload, () => this.Walk(avroPayload));
}

this.Walk(AsyncApiConstants.CorrelationId, () => this.Walk(message.CorrelationId));
this.Walk(AsyncApiConstants.Tags, () => this.Walk(message.Tags));
this.Walk(AsyncApiConstants.Examples, () => this.Walk(message.Examples));
Expand Down
4 changes: 4 additions & 0 deletions src/LEGO.AsyncAPI/Validation/AsyncApiValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ public void AddWarning(AsyncApiValidatorWarning warning)

public override void Visit(IMessageBinding item) => this.Validate(item);

public override void Visit(AsyncApiAvroSchemaPayload item) => this.Validate(item);

public override void Visit(AsyncApiJsonSchemaPayload item) => this.Validate(item);

/// <summary>
/// Execute validation rules against an <see cref="IAsyncApiExtensible"/>.
/// </summary>
Expand Down
60 changes: 60 additions & 0 deletions src/LEGO.AsyncAPI/Validation/Rules/AsyncApiAvroRules.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) The LEGO Group. All rights reserved.

namespace LEGO.AsyncAPI.Validation.Rules
{
using System.Linq;
using System.Text.RegularExpressions;
using LEGO.AsyncAPI.Models;
using LEGO.AsyncAPI.Validations;

[AsyncApiRule]
public static class AsyncApiAvroRules
{
/// <summary>
/// The key regex.
/// </summary>
public static Regex NameRegex = new Regex(@"^[A-Za-z_][A-Za-z0-9_]*$");

public static ValidationRule<AsyncApiAvroSchemaPayload> NameRegularExpression =>
new ValidationRule<AsyncApiAvroSchemaPayload>(
(context, avroPayload) =>
{
string name = null;
context.Enter("name");
if (avroPayload.TryGetAs<AvroRecord>(out var record))
{
name = record.Name;
}

if (avroPayload.TryGetAs<AvroEnum>(out var @enum))
{
name = @enum.Name;
if (@enum.Symbols.Any(symbol => !NameRegex.IsMatch(symbol)))
{
context.CreateError(
"SymbolsRegularExpression",
string.Format(Resource.Validation_SymbolsMustMatchRegularExpression, NameRegex.ToString()));
}
}

if (avroPayload.TryGetAs<AvroFixed>(out var @fixed))
{
name = @fixed.Name;
}

if (name == null)
{
return;
}

if (!NameRegex.IsMatch(record.Name))
{
context.CreateError(
nameof(NameRegex),
string.Format(Resource.Validation_NameMustMatchRegularExpr, name, NameRegex.ToString()));
}

context.Exit();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void DefaultRuleSet_PropertyReturnsTheCorrectRules()
Assert.IsNotEmpty(rules);

// Update the number if you add new default rule(s).
Assert.AreEqual(17, rules.Count);
Assert.AreEqual(18, rules.Count);
}
}
}

0 comments on commit f7d96d8

Please sign in to comment.