forked from loafle/openapi-generator-original
[go-server] Feat: add required assertions to models (#10068)
* Add RequiredError * Add IsZeroValue helper * Add AssertRequired method to all models * Add AssertRequired call for body param * Regenerate files * Add DisallowUnknownFields * Regenerate samples * Use hasRequired in model to remove unnecessary code * Revert disallowUnknownFields * Use isAdditionalPropertiesTrue for disallowing unknown fields * Updated samples * Fix indent * Add require checks for nested slices * Add new tests * Regenerate samples * Regenerate samples after merging
This commit is contained in:
@@ -26,3 +26,38 @@ type Pet struct {
|
||||
// Deprecated
|
||||
Status string `json:"status,omitempty"`
|
||||
}
|
||||
|
||||
// AssertPetRequired checks if the required fields are not zero-ed
|
||||
func AssertPetRequired(obj Pet) error {
|
||||
elements := map[string]interface{}{
|
||||
"name": obj.Name,
|
||||
"photoUrls": obj.PhotoUrls,
|
||||
}
|
||||
for name, el := range elements {
|
||||
if isZero := IsZeroValue(el); isZero {
|
||||
return &RequiredError{Field: name}
|
||||
}
|
||||
}
|
||||
|
||||
if err := AssertCategoryRequired(obj.Category); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, el := range obj.Tags {
|
||||
if err := AssertTagRequired(el); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AssertRecursePetRequired recursively checks if required fields are not zero-ed in a nested slice.
|
||||
// Accepts only nested slice of Pet (e.g. [][]Pet), otherwise ErrTypeAssertionError is thrown.
|
||||
func AssertRecursePetRequired(objSlice interface{}) error {
|
||||
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
|
||||
aPet, ok := obj.(Pet)
|
||||
if !ok {
|
||||
return ErrTypeAssertionError
|
||||
}
|
||||
return AssertPetRequired(aPet)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user