Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 8 additions & 7 deletions src/vanilla/ClientModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,18 @@ public static string CheckNull(string valueReference, string executionBlock)
/// </summary>
/// <param name="type">The type to validate</param>
/// <param name="scope">A scope provider for generating variable names as necessary</param>
/// <param name="valueReference">A reference to the value being validated</param>
/// <param name="variable">A variable to validate the type.</param>
/// <param name="constraints">Constraints</param>
/// <returns>The code to validate the reference of the given type</returns>
public static string ValidateType(this IModelType type, IChild scope, string valueReference,
public static string ValidateType(this IModelType type, IChild scope, IVariable variable,
Dictionary<Constraint, string> constraints)
{
if (scope == null)
{
throw new ArgumentNullException("scope");
}

string valueReference = variable.Name;
var model = type as CompositeTypeCs;
var sequence = type as SequenceTypeCs;
var dictionary = type as DictionaryTypeCs;
Expand All @@ -346,7 +347,8 @@ public static string ValidateType(this IModelType type, IChild scope, string val
if (sequence != null && sequence.ShouldValidateChain())
{
var elementVar = scope.GetUniqueName("element");
var innerValidation = sequence.ElementType.ValidateType(scope, elementVar, null);
variable.Name = elementVar;
var innerValidation = sequence.ElementType.ValidateType(scope, variable, null);
if (!string.IsNullOrEmpty(innerValidation))
{
sb.AppendLine("foreach (var {0} in {1})", elementVar, valueReference)
Expand All @@ -358,7 +360,8 @@ public static string ValidateType(this IModelType type, IChild scope, string val
else if (dictionary != null && dictionary.ShouldValidateChain())
{
var valueVar = scope.GetUniqueName("valueElement");
var innerValidation = dictionary.ValueType.ValidateType(scope, valueVar, null);
variable.Name = valueVar;
var innerValidation = dictionary.ValueType.ValidateType(scope, variable, null);
if (!string.IsNullOrEmpty(innerValidation))
{
sb.AppendLine("foreach (var {0} in {1}.Values)", valueVar, valueReference)
Expand All @@ -368,11 +371,9 @@ public static string ValidateType(this IModelType type, IChild scope, string val
}
}

var IsRequired = sequence != null && sequence.ShouldValidateChain() && !sequence.IsNullable;

if (sb.ToString().Trim().Length > 0)
{
if (type.IsValueType() && IsRequired)
if (type.IsValueType() && !variable.IsNullable())
{
return sb.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
if(parameter.CanBeValidated && (Model.HttpMethod != HttpMethod.Patch || parameter.Location != ParameterLocation.Body))
{
@:@(parameter.ModelType.ValidateType(Model, parameter.Name, parameter.Constraints))
@:@(parameter.ModelType.ValidateType(Model, parameter, parameter.Constraints))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/vanilla/Templates/Rest/Common/ModelTemplate.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ foreach (PropertyCs property in Model.InstanceProperties.Where(p => p.IsRequired
foreach (var property in Model.InstanceProperties.Where(p => p.Constraints.Any() || !(p.ModelType is PrimaryType)))
{
anythingToValidate = true;
@:@property.ModelType.ValidateType(Model, $"this.{property.Name}", property.Constraints)
@:@property.ModelType.ValidateType(Model, property, property.Constraints)
}
if (!anythingToValidate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
}
if (parameter.CanBeValidated && (Model.HttpMethod != HttpMethod.Patch || parameter.Location != ParameterLocation.Body))
{
@:@(parameter.ModelType.ValidateType(Model, parameter.Name, parameter.Constraints))
@:@(parameter.ModelType.ValidateType(Model, parameter, parameter.Constraints))
}
}
@if (Model.HttpMethod == HttpMethod.Get)
Expand Down