Skip to content

Commit

Permalink
Feature: adresse.Ortsteil (#234)
Browse files Browse the repository at this point in the history
* Link lead to 404 and there's no v10 branch in the playground validator repo, so switch to the package documentation instead.

* Add Ortsteil to Adresse.

* Don't hardcode enum value in test as it may change. Also it's been already the wrong one.
  • Loading branch information
GodsBoss authored Aug 2, 2023
1 parent ba54ce2 commit fb0e30b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion com/adresse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type Adresse struct {
Postleitzahl string `json:"postleitzahl,omitempty" example:"82031" validate:"numeric,required"` // Postleitzahl is the zip code, mandatory
Ort string `json:"ort,omitempty" example:"Grünwald" validate:"alphaunicode,required"` // Ort is the city name, mandatory
Ortsteil string `json:"ortsteil,omitempty" example:"Geiselgasteig"` // Ortsteil is the city part, optional
Strasse string `json:"strasse,omitempty" example:"Nördliche Münchner Straße 27A"` // Strasse is the street name
Hausnummer string `json:"hausnummer,omitempty" example:"27A"` // Hausnummer is the house number including suffixes like "a" or "-1"
Postfach string `json:"postfach,omitempty"` // Postfach is a post box
Expand All @@ -20,7 +21,7 @@ type Adresse struct {
// AdresseStructLevelValidation does a cross check on a Adresse object
func AdresseStructLevelValidation(sl validator.StructLevel) {
// ToDo: use required_without/required_if instead of own validator
// see https://github.com/go-playground/validator/v10/search?q=required_without
// see https://pkg.go.dev/github.com/go-playground/validator/v10#hdr-Required_Without
address := sl.Current().Interface().(Adresse)
if (len(address.Strasse) == 0 && len(address.Postfach) == 0) || (len(address.Strasse) > 0 && len(address.Postfach) > 0) {
sl.ReportError(address.Strasse, "Strasse", "Postfach", "StrasseXORPostfach", "")
Expand Down
9 changes: 6 additions & 3 deletions com/adresse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com_test

import (
"encoding/json"
"github.com/hochfrequenz/go-bo4e/internal"
"strconv"
"strings"

"github.com/hochfrequenz/go-bo4e/internal"

"github.com/corbym/gocrest/is"
"github.com/corbym/gocrest/then"
"github.com/go-playground/validator/v10"
Expand All @@ -17,14 +19,15 @@ func (s *Suite) Test_Address_Deserialization() {
var adresse = com.Adresse{
Postleitzahl: "82031",
Ort: "Grünwald",
Ortsteil: "Geiselgasteig",
Strasse: "Nördlicher Münchner Straße",
Hausnummer: "27A",
Landescode: internal.Ptr(landescode.DE),
}
serializedAdresse, err := json.Marshal(adresse)
jsonString := string(serializedAdresse)
then.AssertThat(s.T(), strings.Contains(jsonString, "DE"), is.True()) // stringified enum
then.AssertThat(s.T(), strings.Contains(jsonString, "61"), is.False()) // no "61" for DE
then.AssertThat(s.T(), strings.Contains(jsonString, "DE"), is.True()) // stringified enum
then.AssertThat(s.T(), strings.Contains(jsonString, strconv.Itoa(int(landescode.DE))), is.False()) // no raw int enum value
then.AssertThat(s.T(), err, is.Nil())
then.AssertThat(s.T(), serializedAdresse, is.Not(is.NilArray[byte]()))
var deserializedAdresse com.Adresse
Expand Down

0 comments on commit fb0e30b

Please sign in to comment.