Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: org party regime validation #434

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## [Unreleased]

### Fixed

- `org`: Party validation now working with regimes when defined.

## [v0.206.0] - 2024-11-26

### Added
Expand Down
10 changes: 10 additions & 0 deletions org/party.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ func (p *Party) Validate() error {

// ValidateWithContext is used to check the party's data meets minimum expectations.
func (p *Party) ValidateWithContext(ctx context.Context) error {
ctx = p.ValidationContext(ctx)
return tax.ValidateStructWithContext(ctx, p,
validation.Field(&p.Regime),
validation.Field(&p.Name),
validation.Field(&p.TaxID),
validation.Field(&p.Identities),
Expand All @@ -109,6 +111,14 @@ func (p *Party) ValidateWithContext(ctx context.Context) error {
)
}

// ValidationContext returns a context with the regime's validation rules.
func (p *Party) ValidationContext(ctx context.Context) context.Context {
if r := p.RegimeDef(); r != nil {
ctx = r.WithContext(ctx)
}
return ctx
}

// JSONSchemaExtend adds extra details to the schema.
func (Party) JSONSchemaExtend(js *jsonschema.Schema) {
js.Extras = map[string]any{
Expand Down
33 changes: 33 additions & 0 deletions org/party_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,36 @@ func TestPartyAddressNill(t *testing.T) {
party.Normalize(nil)
assert.NoError(t, party.Validate())
}

func TestPartyValidation(t *testing.T) {
t.Run("with regime", func(t *testing.T) {
party := org.Party{
Regime: tax.WithRegime("DE"),
Name: "Invopop",
Identities: []*org.Identity{
{
Key: "de-tax-number",
Code: "123 456 78901",
},
},
}
require.NoError(t, party.Calculate())
assert.NoError(t, party.Validate())
assert.Equal(t, "DE", party.GetRegime().String())
})
t.Run("with regime and bad code", func(t *testing.T) {
party := org.Party{
Regime: tax.WithRegime("DE"),
Name: "Invopop",
Identities: []*org.Identity{
{
Key: "de-tax-number",
Code: "1231312423432422",
},
},
}
require.NoError(t, party.Calculate())
err := party.Validate()
assert.ErrorContains(t, err, "identities: (0: (code: must be in a valid format.).).")
})
}