Skip to content

Commit

Permalink
regex valitation for dictionary values (#1773)
Browse files Browse the repository at this point in the history
  • Loading branch information
olydis authored and fearthecowboy committed Feb 1, 2017
1 parent 84a1cb8 commit 5788a5f
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/generator/AutoRest.CSharp/ClientModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public static string ValidateType(this IModelType type, IChild scope, string val

if (constraints != null && constraints.Any())
{
AppendConstraintValidations(valueReference, constraints, sb, (type as PrimaryType)?.KnownFormat ?? KnownFormat.none);
AppendConstraintValidations(valueReference, constraints, sb, type);
}

if (sequence != null && sequence.ShouldValidateChain())
Expand Down Expand Up @@ -401,12 +401,12 @@ private static string ToLiteral(string input)
}
}

private static void AppendConstraintValidations(string valueReference, Dictionary<Constraint, string> constraints, IndentedStringBuilder sb, KnownFormat format)
private static void AppendConstraintValidations(string valueReference, Dictionary<Constraint, string> constraints, IndentedStringBuilder sb, IModelType type)
{
foreach (var constraint in constraints.Keys)
{
string constraintCheck;
string constraintValue = (format == KnownFormat.@char) ?$"'{constraints[constraint]}'" : constraints[constraint];
string constraintValue = ((type as PrimaryType)?.KnownFormat == KnownFormat.@char) ?$"'{constraints[constraint]}'" : constraints[constraint];
switch (constraint)
{
case Constraint.ExclusiveMaximum:
Expand Down Expand Up @@ -438,7 +438,14 @@ private static void AppendConstraintValidations(string valueReference, Dictionar
break;
case Constraint.Pattern:
constraintValue = ToLiteral(constraintValue);
constraintCheck = $"!System.Text.RegularExpressions.Regex.IsMatch({valueReference}, {constraintValue})";
if (type is DictionaryType)
{
constraintCheck = $"!System.Linq.Enumerable.All({valueReference}.Values, value => System.Text.RegularExpressions.Regex.IsMatch(value, {constraintValue}))";
}
else
{
constraintCheck = $"!System.Text.RegularExpressions.Regex.IsMatch({valueReference}, {constraintValue})";
}
break;
case Constraint.UniqueItems:
if ("true".EqualsIgnoreCase(constraints[constraint]))
Expand Down

0 comments on commit 5788a5f

Please sign in to comment.