add validation to ruby model

This commit is contained in:
wing328
2016-04-25 17:07:42 +08:00
parent d225da082c
commit e143c6cd2f
12 changed files with 172 additions and 31 deletions

View File

@@ -42,6 +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)
@Override
public int hashCode()

View File

@@ -327,6 +327,16 @@ public class DefaultCodegen {
this.ensureUniqueParams = ensureUniqueParams;
}
/**
* Return the JSON schema pattern (http://json-schema.org/latest/json-schema-validation.html#anchor33)
*
* @param pattern the pattern (regular expression)
* @return properly-escaped pattern
*/
public String toJSONSchemaPattern(String pattern) {
return escapeText(pattern);
}
/**
* Return the file name of the Api Test
*
@@ -1094,6 +1104,10 @@ public class DefaultCodegen {
property.exclusiveMinimum = np.getExclusiveMinimum();
property.exclusiveMaximum = np.getExclusiveMaximum();
// check if any validation rule defined
if (property.minimum != null || property.maximum != null || property.exclusiveMinimum != null || property.exclusiveMaximum != null)
property.needValidation = true;
// legacy support
Map<String, Object> allowableValues = new HashMap<String, Object>();
if (np.getMinimum() != null) {
@@ -1111,7 +1125,12 @@ public class DefaultCodegen {
StringProperty sp = (StringProperty) p;
property.maxLength = sp.getMaxLength();
property.minLength = sp.getMinLength();
property.pattern = sp.getPattern();
property.pattern = toJSONSchemaPattern(sp.getPattern());
// check if any validation rule defined
if (property.pattern != null || property.minLength != null || property.maxLength != null)
property.needValidation = true;
property.isString = true;
if (sp.getEnum() != null) {
List<String> _enum = sp.getEnum();
@@ -1802,9 +1821,6 @@ public class DefaultCodegen {
if (model.complexType != null) {
imports.add(model.complexType);
}
p.maxLength = qp.getMaxLength();
p.minLength = qp.getMinLength();
p.pattern = qp.getPattern();
p.maximum = qp.getMaximum();
p.exclusiveMaximum = qp.isExclusiveMaximum();
@@ -1812,7 +1828,7 @@ public class DefaultCodegen {
p.exclusiveMinimum = qp.isExclusiveMinimum();
p.maxLength = qp.getMaxLength();
p.minLength = qp.getMinLength();
p.pattern = qp.getPattern();
p.pattern = toJSONSchemaPattern(qp.getPattern());
p.maxItems = qp.getMaxItems();
p.minItems = qp.getMinItems();
p.uniqueItems = qp.isUniqueItems();

View File

@@ -31,6 +31,7 @@ Then either install the gem locally:
```shell
gem install ./{{{gemName}}}-{{{gemVersion}}}.gem
```
(for development, run `gem install --dev ./{{{gemName}}}-{{{gemVersion}}}.gem` to install the development dependencies)
or publish the gem to a gem hosting service, e.g. [RubyGems](https://rubygems.org/).

View File

@@ -47,7 +47,9 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}}
end
{{/vars}}
end
{{#vars}}{{#isEnum}}
{{#vars}}
{{#isEnum}}
# Custom attribute writer method checking allowed values (enum).
# @param [Object] {{{name}}} Object to be assigned
def {{{name}}}=({{{name}}})
@@ -57,7 +59,29 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}}
end
@{{{name}}} = {{{name}}}
end
{{/isEnum}}{{/vars}}
{{/isEnum}}
{{^isEnum}}
{{#needValidation}}
# Custom attribute writer method with validation
# @param [Object] {{{name}}} Value to be assigned
def {{{name}}}=({{{name}}})
{{#maximum}}
if {{{name}}} > {{{maximum}}}
fail "invalid value for '{{{name}}}', must be smaller than or equal to {{{maximum}}}"
end
{{/maximum}}
{{#minimum}}
if {{{name}}} < {{{minimum}}}
fail "invalid value for '{{{name}}}', must be greater than or equal to {{{minimum}}}"
end
{{/minimum}}
@{{{name}}} = {{{name}}}
end
{{/needValidation}}
{{/isEnum}}
{{/vars}}
# Checks equality by comparing each attribute.
# @param [Object] Object to be compared
def ==(o)

View File

@@ -761,22 +761,33 @@ definitions:
properties:
integer:
type: integer
maximum: 100
minimum: 10
int32:
type: integer
format: int32
maximum: 200
minimum: 20
int64:
type: integer
format: int64
number:
maximum: 543.2
minimum: 32.1
type: number
float:
type: number
format: float
maximum: 987.6
minimum: 54.3
double:
type: number
format: double
maximum: 123.4
minimum: 67.8
string:
type: string
pattern: /[az]/i
byte:
type: string
format: byte