Skip to content

Commit 6232503

Browse files
authored
Merge pull request #343 from invopop/org-name-fix-validation
Org.Name: require either given or surname
2 parents b16f4c6 + 7f6376c commit 6232503

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
66

77
## [Unreleased]
88

9+
### Changed
10+
11+
- `org.Name`: either given **or** surname are required, as opposed to both at the same time.
12+
913
### Added
1014

1115
- `head`: validation rule to check for the presence of stamps

bill/discounts.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ import (
1313

1414
// LineDiscount represents an amount deducted from the line, and will be
1515
// applied before taxes.
16-
// TODO: use UNTDID 5189 code list
1716
type LineDiscount struct {
1817
// Percentage if fixed amount not applied
1918
Percent *num.Percentage `json:"percent,omitempty" jsonschema:"title=Percent"`
2019
// Fixed discount amount to apply (calculated if percent present).
21-
Amount num.Amount `json:"amount" jsonschema:"title=Value" jsonschema_extras:"calculated=true"`
20+
Amount num.Amount `json:"amount" jsonschema:"title=Amount" jsonschema_extras:"calculated=true"`
2221
// Reason code.
2322
Code string `json:"code,omitempty" jsonschema:"title=Code"`
2423
// Text description as to why the discount was applied
2524
Reason string `json:"reason,omitempty" jsonschema:"title=Reason"`
25+
26+
// TODO: support UNTDID 5189 codes
2627
}
2728

2829
// Validate checks the line discount's fields.

org/name.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ type Name struct {
1919
// Additional prefix to add to name, like Mrs. or Mr.
2020
Prefix string `json:"prefix,omitempty" jsonschema:"title=Prefix"`
2121
// Person's given or first name
22-
Given string `json:"given" jsonschema:"title=Given"`
22+
Given string `json:"given,omitempty" jsonschema:"title=Given"`
2323
// Middle names or initials
2424
Middle string `json:"middle,omitempty" jsonschema:"title=Middle"`
2525
// Second or Family name.
26-
Surname string `json:"surname" jsonschema:"title=Surname"`
26+
Surname string `json:"surname,omitempty" jsonschema:"title=Surname"`
2727
// Additional second of family name.
2828
Surname2 string `json:"surname2,omitempty" jsonschema:"title=Second Surname"`
2929
// Titles to include after the name.
@@ -41,8 +41,16 @@ func (n *Name) Validate() error {
4141
func (n *Name) ValidateWithContext(ctx context.Context) error {
4242
return tax.ValidateStructWithRegime(ctx, n,
4343
validation.Field(&n.UUID),
44-
validation.Field(&n.Given, validation.Required),
45-
validation.Field(&n.Surname, validation.Required),
44+
validation.Field(&n.Given,
45+
validation.When(n.Surname == "",
46+
validation.Required,
47+
),
48+
),
49+
validation.Field(&n.Surname,
50+
validation.When(n.Given == "",
51+
validation.Required,
52+
),
53+
),
4654
validation.Field(&n.Meta),
4755
)
4856
}

org/name_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/invopop/gobl/org"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestNameValidation(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
n *org.Name
14+
err string
15+
}{
16+
{
17+
name: "empty",
18+
n: &org.Name{},
19+
20+
err: "given: cannot be blank; surname: cannot be blank.",
21+
},
22+
{
23+
name: "given",
24+
n: &org.Name{
25+
Given: "John",
26+
},
27+
},
28+
{
29+
name: "surname",
30+
n: &org.Name{
31+
Surname: "Doe",
32+
},
33+
},
34+
{
35+
name: "both",
36+
n: &org.Name{
37+
Given: "John",
38+
Surname: "Doe",
39+
},
40+
},
41+
}
42+
43+
for _, tt := range tests {
44+
t.Run(tt.name, func(t *testing.T) {
45+
err := tt.n.Validate()
46+
if tt.err == "" {
47+
require.NoError(t, err)
48+
return
49+
}
50+
require.ErrorContains(t, err, tt.err)
51+
})
52+
}
53+
}

0 commit comments

Comments
 (0)