Skip to content

Commit

Permalink
Update modelGeneric.mustache (OpenAPITools#5378)
Browse files Browse the repository at this point in the history
* Update modelGeneric.mustache

If maxlength is specified for a property type enum it there should be .ToString() appended before length check

* ran the csharp petstore bar and updated the file

Co-authored-by: Shweta Shukla <[email protected]>
  • Loading branch information
2 people authored and MikailBag committed May 31, 2020
1 parent d1dc992 commit 0756692
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,22 +259,44 @@ this.{{name}} = {{#lambda.camelcase_param}}{{name}}{{/lambda.camelcase_param}};
{{/parent}}
{{#vars}}
{{#hasValidation}}
{{#isEnum}}
{{#maxLength}}
// {{{name}}} ({{{dataType}}}) maxLength
if(this.{{{name}}} != null && this.{{{name}}}.ToString().Length > {{maxLength}})
{
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for {{{name}}}, length must be less than {{maxLength}}.", new [] { "{{{name}}}" });
}
{{/maxLength}}
{{/isEnum}}
{{^isEnum}}
{{#maxLength}}
// {{{name}}} ({{{dataType}}}) maxLength
if(this.{{{name}}} != null && this.{{{name}}}.Length > {{maxLength}})
{
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for {{{name}}}, length must be less than {{maxLength}}.", new [] { "{{{name}}}" });
}

{{/maxLength}}
{{/isEnum}}

{{#isEnum}}
{{#minLength}}
// {{{name}}} ({{{dataType}}}) minLength
if(this.{{{name}}} != null && this.{{{name}}}.ToString().Length < {{minLength}})
{
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for {{{name}}}, length must be greater than {{minLength}}.", new [] { "{{{name}}}" });
}
{{/minLength}}
{{/isEnum}}
{{^isEnum}}
{{#minLength}}
// {{{name}}} ({{{dataType}}}) minLength
if(this.{{{name}}} != null && this.{{{name}}}.Length < {{minLength}})
{
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for {{{name}}}, length must be greater than {{minLength}}.", new [] { "{{{name}}}" });
}

{{/minLength}}
{{/isEnum}}

{{#maximum}}
// {{{name}}} ({{{dataType}}}) maximum
if(this.{{{name}}} > ({{{dataType}}}){{maximum}})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ public override int GetHashCode()
/// <returns>Validation Result</returns>
IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
{


// Integer (int) maximum
if(this.Integer > (int)100)
{
Expand All @@ -379,6 +381,8 @@ public override int GetHashCode()
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Integer, must be a value greater than or equal to 10.", new [] { "Integer" });
}



// Int32 (int) maximum
if(this.Int32 > (int)200)
{
Expand All @@ -391,6 +395,8 @@ public override int GetHashCode()
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Int32, must be a value greater than or equal to 20.", new [] { "Int32" });
}



// Number (decimal) maximum
if(this.Number > (decimal)543.2)
{
Expand All @@ -403,6 +409,8 @@ public override int GetHashCode()
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Number, must be a value greater than or equal to 32.1.", new [] { "Number" });
}



// Float (float) maximum
if(this.Float > (float)987.6)
{
Expand All @@ -415,6 +423,8 @@ public override int GetHashCode()
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Float, must be a value greater than or equal to 54.3.", new [] { "Float" });
}



// Double (double) maximum
if(this.Double > (double)123.4)
{
Expand All @@ -427,13 +437,17 @@ public override int GetHashCode()
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Double, must be a value greater than or equal to 67.8.", new [] { "Double" });
}



// String (string) pattern
Regex regexString = new Regex(@"[a-z]", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
if (false == regexString.Match(this.String).Success)
{
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for String, must match a pattern of " + regexString, new [] { "String" });
}



// Password (string) maxLength
if(this.Password != null && this.Password.Length > 64)
{
Expand All @@ -445,7 +459,7 @@ public override int GetHashCode()
{
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Password, length must be greater than 10.", new [] { "Password" });
}

yield break;
}
}
Expand Down

0 comments on commit 0756692

Please sign in to comment.