forked from loafle/openapi-generator-original
[GO] Add assert constraints checks for complex types in the model template (#18654)
* [GO] Add assert constraints checks for complex types in the model template * [GO] Update samples * [GO] revert AssertRecurseInterface naming
This commit is contained in:
parent
a9b1f93d65
commit
affb6bc1f7
@ -174,6 +174,61 @@ func Assert{{classname}}Constraints(obj {{classname}}) error {
|
||||
return &ParsingError{Param: "{{name}}", Err: errors.New(errMsgMaxValueConstraint)}
|
||||
}
|
||||
{{/maximum}}
|
||||
{{#isNullable}}
|
||||
{{#isModel}}
|
||||
if obj.{{name}} != nil {
|
||||
{{/isModel}}
|
||||
{{#isArray}}
|
||||
{{#items.isModel}}
|
||||
if obj.{{name}} != nil {
|
||||
{{/items.isModel}}
|
||||
{{/isArray}}
|
||||
{{/isNullable}}
|
||||
{{#isModel}}
|
||||
{{#isNullable}} {{/isNullable}} if err := Assert{{baseType}}Constraints({{#isNullable}}*{{/isNullable}}obj.{{name}}); err != nil {
|
||||
{{#isNullable}} {{/isNullable}} return err
|
||||
{{#isNullable}} {{/isNullable}} }
|
||||
{{/isModel}}
|
||||
{{#isArray}}
|
||||
{{#items.isModel}}
|
||||
{{#isNullable}} {{/isNullable}} for _, el := range {{#isNullable}}*{{/isNullable}}obj.{{name}} {
|
||||
{{#isNullable}} {{/isNullable}} if err := Assert{{items.baseType}}Constraints(el); err != nil {
|
||||
{{#isNullable}} {{/isNullable}} return err
|
||||
{{#isNullable}} {{/isNullable}} }
|
||||
{{#isNullable}} {{/isNullable}} }
|
||||
{{/items.isModel}}
|
||||
{{^items.isModel}}
|
||||
{{#mostInnerItems.isModel}}
|
||||
{{^mostInnerItems.isPrimitiveType}}
|
||||
{{#isNullable}}
|
||||
if obj.{{name}} != nil {
|
||||
{{/isNullable}}
|
||||
{{#isNullable}} {{/isNullable}} if err := AssertRecurseInterfaceRequired({{#isNullable}}*{{/isNullable}}obj.{{name}}, Assert{{mostInnerItems.dataType}}Constraints); err != nil {
|
||||
{{#isNullable}} {{/isNullable}} return err
|
||||
{{#isNullable}} {{/isNullable}} }
|
||||
{{/mostInnerItems.isPrimitiveType}}
|
||||
{{/mostInnerItems.isModel}}
|
||||
{{/items.isModel}}
|
||||
{{/isArray}}
|
||||
{{#isNullable}}
|
||||
{{#isModel}}
|
||||
}
|
||||
{{/isModel}}
|
||||
{{#isArray}}
|
||||
{{#items.isModel}}
|
||||
}
|
||||
{{/items.isModel}}
|
||||
{{^items.isModel}}
|
||||
{{#mostInnerItems.isModel}}
|
||||
{{^mostInnerItems.isPrimitiveType}}
|
||||
{{#isNullable}}
|
||||
}
|
||||
{{/isNullable}}
|
||||
{{/mostInnerItems.isPrimitiveType}}
|
||||
{{/mostInnerItems.isModel}}
|
||||
{{/items.isModel}}
|
||||
{{/isArray}}
|
||||
{{/isNullable}}
|
||||
{{/Vars}}
|
||||
return nil
|
||||
}{{/model}}{{/models}}
|
||||
|
@ -37,5 +37,13 @@ func AssertAnObjectRequired(obj AnObject) error {
|
||||
|
||||
// AssertAnObjectConstraints checks if the values respects the defined constraints
|
||||
func AssertAnObjectConstraints(obj AnObject) error {
|
||||
if err := AssertTagConstraints(obj.Tag); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, el := range obj.Pet {
|
||||
if err := AssertPetConstraints(el); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -60,5 +60,17 @@ func AssertPetRequired(obj Pet) error {
|
||||
|
||||
// AssertPetConstraints checks if the values respects the defined constraints
|
||||
func AssertPetConstraints(obj Pet) error {
|
||||
if obj.Category != nil {
|
||||
if err := AssertCategoryConstraints(*obj.Category); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if obj.Tags != nil {
|
||||
for _, el := range *obj.Tags {
|
||||
if err := AssertTagConstraints(el); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -64,5 +64,13 @@ func AssertUserRequired(obj User) error {
|
||||
|
||||
// AssertUserConstraints checks if the values respects the defined constraints
|
||||
func AssertUserConstraints(obj User) error {
|
||||
if obj.DeepSliceModel != nil {
|
||||
if err := AssertRecurseInterfaceRequired(*obj.DeepSliceModel, AssertTagConstraints); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := AssertRecurseInterfaceRequired(obj.DeepSliceMap, AssertAnObjectConstraints); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -37,5 +37,13 @@ func AssertAnObjectRequired(obj AnObject) error {
|
||||
|
||||
// AssertAnObjectConstraints checks if the values respects the defined constraints
|
||||
func AssertAnObjectConstraints(obj AnObject) error {
|
||||
if err := AssertTagConstraints(obj.Tag); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, el := range obj.Pet {
|
||||
if err := AssertPetConstraints(el); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -60,5 +60,17 @@ func AssertPetRequired(obj Pet) error {
|
||||
|
||||
// AssertPetConstraints checks if the values respects the defined constraints
|
||||
func AssertPetConstraints(obj Pet) error {
|
||||
if obj.Category != nil {
|
||||
if err := AssertCategoryConstraints(*obj.Category); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if obj.Tags != nil {
|
||||
for _, el := range *obj.Tags {
|
||||
if err := AssertTagConstraints(el); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -64,5 +64,13 @@ func AssertUserRequired(obj User) error {
|
||||
|
||||
// AssertUserConstraints checks if the values respects the defined constraints
|
||||
func AssertUserConstraints(obj User) error {
|
||||
if obj.DeepSliceModel != nil {
|
||||
if err := AssertRecurseInterfaceRequired(*obj.DeepSliceModel, AssertTagConstraints); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := AssertRecurseInterfaceRequired(obj.DeepSliceMap, AssertAnObjectConstraints); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -37,5 +37,13 @@ func AssertAnObjectRequired(obj AnObject) error {
|
||||
|
||||
// AssertAnObjectConstraints checks if the values respects the defined constraints
|
||||
func AssertAnObjectConstraints(obj AnObject) error {
|
||||
if err := AssertTagConstraints(obj.Tag); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, el := range obj.Pet {
|
||||
if err := AssertPetConstraints(el); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -60,5 +60,17 @@ func AssertPetRequired(obj Pet) error {
|
||||
|
||||
// AssertPetConstraints checks if the values respects the defined constraints
|
||||
func AssertPetConstraints(obj Pet) error {
|
||||
if obj.Category != nil {
|
||||
if err := AssertCategoryConstraints(*obj.Category); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if obj.Tags != nil {
|
||||
for _, el := range *obj.Tags {
|
||||
if err := AssertTagConstraints(el); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -64,5 +64,13 @@ func AssertUserRequired(obj User) error {
|
||||
|
||||
// AssertUserConstraints checks if the values respects the defined constraints
|
||||
func AssertUserConstraints(obj User) error {
|
||||
if obj.DeepSliceModel != nil {
|
||||
if err := AssertRecurseInterfaceRequired(*obj.DeepSliceModel, AssertTagConstraints); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := AssertRecurseInterfaceRequired(obj.DeepSliceMap, AssertAnObjectConstraints); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user