-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add AvroValidationRule for names and symbols (#197)
- Loading branch information
1 parent
59fef05
commit f7d96d8
Showing
7 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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,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(); | ||
}); | ||
} | ||
} |
This file contains 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