forked from loafle/openapi-generator-original
add methods to validate the ruby object
This commit is contained in:
@@ -42,7 +42,7 @@ public class CodegenProperty {
|
||||
public Map<String, Object> allowableValues;
|
||||
public CodegenProperty items;
|
||||
public Map<String, Object> vendorExtensions;
|
||||
public Boolean needValidation; // true if pattern, maximum, etc are set (only used in the mustache template)
|
||||
public Boolean hasValidation; // true if pattern, maximum, etc are set (only used in the mustache template)
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
|
||||
@@ -1106,7 +1106,7 @@ public class DefaultCodegen {
|
||||
|
||||
// check if any validation rule defined
|
||||
if (property.minimum != null || property.maximum != null || property.exclusiveMinimum != null || property.exclusiveMaximum != null)
|
||||
property.needValidation = true;
|
||||
property.hasValidation = true;
|
||||
|
||||
// legacy support
|
||||
Map<String, Object> allowableValues = new HashMap<String, Object>();
|
||||
@@ -1129,7 +1129,7 @@ public class DefaultCodegen {
|
||||
|
||||
// check if any validation rule defined
|
||||
if (property.pattern != null || property.minLength != null || property.maxLength != null)
|
||||
property.needValidation = true;
|
||||
property.hasValidation = true;
|
||||
|
||||
property.isString = true;
|
||||
if (sp.getEnum() != null) {
|
||||
|
||||
@@ -39,12 +39,109 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}}
|
||||
|
||||
{{#vars}}
|
||||
if attributes[:'{{{baseName}}}']
|
||||
{{#isContainer}}if (value = attributes[:'{{{baseName}}}']).is_a?(Array)
|
||||
{{#isContainer}}
|
||||
if (value = attributes[:'{{{baseName}}}']).is_a?(Array)
|
||||
self.{{{name}}} = value
|
||||
end{{/isContainer}}{{^isContainer}}self.{{{name}}} = attributes[:'{{{baseName}}}']{{/isContainer}}{{#defaultValue}}
|
||||
end
|
||||
{{/isContainer}}
|
||||
{{^isContainer}}
|
||||
self.{{{name}}} = attributes[:'{{{baseName}}}']
|
||||
{{/isContainer}}
|
||||
{{#defaultValue}}
|
||||
else
|
||||
self.{{{name}}} = {{{defaultValue}}}{{/defaultValue}}
|
||||
self.{{{name}}} = {{{defaultValue}}}
|
||||
{{/defaultValue}}
|
||||
end
|
||||
|
||||
{{/vars}}
|
||||
end
|
||||
|
||||
# Show invalid properties with the reasons. Usually used together with valid?
|
||||
# @return Array for valid properies with the reasons
|
||||
def list_invalid_properties
|
||||
invalid_properties = Array.new
|
||||
{{#isEnum}}
|
||||
allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}]
|
||||
if {{{name}}} && !allowed_values.include?({{{name}}})
|
||||
invalid_properties.push("invalid value for '{{{name}}}', must be one of #{allowed_values}.")
|
||||
end
|
||||
|
||||
{{/isEnum}}
|
||||
{{#hasValidation}}
|
||||
if {{{name}}}.nil?
|
||||
fail ArgumentError, "{{{name}}} cannot be nil"
|
||||
end
|
||||
|
||||
{{#minLength}}
|
||||
if {{{name}}}.to_s.length > {{{maxLength}}}
|
||||
invalid_properties.push("invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}.")
|
||||
end
|
||||
|
||||
{{/minLength}}
|
||||
{{#maxLength}}
|
||||
if {{{name}}}.to_s.length < {{{minLength}}}
|
||||
invalid_properties.push("invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}.")
|
||||
end
|
||||
|
||||
{{/maxLength}}
|
||||
{{#maximum}}
|
||||
if {{{name}}} > {{{maximum}}}
|
||||
invalid_properties.push("invalid value for '{{{name}}}', must be smaller than or equal to {{{maximum}}}.")
|
||||
end
|
||||
|
||||
{{/maximum}}
|
||||
{{#minimum}}
|
||||
if {{{name}}} < {{{minimum}}}
|
||||
invalid_properties.push("invalid value for '{{{name}}}', must be greater than or equal to {{{minimum}}}.")
|
||||
end
|
||||
|
||||
{{/minimum}}
|
||||
{{/hasValidation}}
|
||||
return invalid_properties
|
||||
end
|
||||
|
||||
# Check to see if the all the properties in the model are valid
|
||||
# @return true if the model is valid
|
||||
def valid?
|
||||
{{#vars}}
|
||||
{{#required}}
|
||||
if @{{{name}}}.nil?
|
||||
return false
|
||||
end
|
||||
|
||||
{{/required}}
|
||||
{{#isEnum}}
|
||||
allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}]
|
||||
if {{{name}}} && !allowed_values.include?({{{name}}})
|
||||
return false
|
||||
end
|
||||
{{/isEnum}}
|
||||
{{#hasValidation}}
|
||||
{{#minLength}}
|
||||
if {{{name}}}.to_s.length > {{{maxLength}}}
|
||||
return false
|
||||
end
|
||||
|
||||
{{/minLength}}
|
||||
{{#maxLength}}
|
||||
if {{{name}}}.to_s.length < {{{minLength}}}
|
||||
return false
|
||||
end
|
||||
|
||||
{{/maxLength}}
|
||||
{{#maximum}}
|
||||
if {{{name}}} > {{{maximum}}}
|
||||
return false
|
||||
end
|
||||
|
||||
{{/maximum}}
|
||||
{{#minimum}}
|
||||
if {{{name}}} < {{{minimum}}}
|
||||
return false
|
||||
end
|
||||
|
||||
{{/minimum}}
|
||||
{{/hasValidation}}
|
||||
{{/vars}}
|
||||
end
|
||||
|
||||
@@ -55,14 +152,14 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}}
|
||||
def {{{name}}}=({{{name}}})
|
||||
allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}]
|
||||
if {{{name}}} && !allowed_values.include?({{{name}}})
|
||||
fail ArgumentError, "invalid value for '{{{name}}}', must be one of #{allowed_values}"
|
||||
fail ArgumentError, "invalid value for '{{{name}}}', must be one of #{allowed_values}."
|
||||
end
|
||||
@{{{name}}} = {{{name}}}
|
||||
end
|
||||
|
||||
{{/isEnum}}
|
||||
{{^isEnum}}
|
||||
{{#needValidation}}
|
||||
{{#hasValidation}}
|
||||
# Custom attribute writer method with validation
|
||||
# @param [Object] {{{name}}} Value to be assigned
|
||||
def {{{name}}}=({{{name}}})
|
||||
@@ -72,32 +169,32 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}}
|
||||
|
||||
{{#minLength}}
|
||||
if {{{name}}}.to_s.length > {{{maxLength}}}
|
||||
fail ArgumentError, "invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}"
|
||||
fail ArgumentError, "invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}."
|
||||
end
|
||||
|
||||
{{/minLength}}
|
||||
{{#maxLength}}
|
||||
if {{{name}}}.to_s.length < {{{minLength}}}
|
||||
fail ArgumentError, "invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}"
|
||||
fail ArgumentError, "invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}."
|
||||
end
|
||||
|
||||
{{/maxLength}}
|
||||
{{#maximum}}
|
||||
if {{{name}}} > {{{maximum}}}
|
||||
fail ArgumentError, "invalid value for '{{{name}}}', must be smaller than or equal to {{{maximum}}}"
|
||||
fail ArgumentError, "invalid value for '{{{name}}}', must be smaller than or equal to {{{maximum}}}."
|
||||
end
|
||||
|
||||
{{/maximum}}
|
||||
{{#minimum}}
|
||||
if {{{name}}} < {{{minimum}}}
|
||||
fail ArgumentError, "invalid value for '{{{name}}}', must be greater than or equal to {{{minimum}}}"
|
||||
fail ArgumentError, "invalid value for '{{{name}}}', must be greater than or equal to {{{minimum}}}."
|
||||
end
|
||||
|
||||
{{/minimum}}
|
||||
@{{{name}}} = {{{name}}}
|
||||
end
|
||||
|
||||
{{/needValidation}}
|
||||
{{/hasValidation}}
|
||||
{{/isEnum}}
|
||||
{{/vars}}
|
||||
# Checks equality by comparing each attribute.
|
||||
|
||||
Reference in New Issue
Block a user