From 956b2e3dc6c41fdd8be3c2b1a8b2e4a4bd12abd2 Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Wed, 27 Nov 2024 17:05:46 +0000 Subject: [PATCH] Fix: org party regime validation --- CHANGELOG.md | 4 ++++ org/party.go | 10 ++++++++++ org/party_test.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 559558eb..3c4bc25e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/org/party.go b/org/party.go index 867b7af1..8f89d364 100644 --- a/org/party.go +++ b/org/party.go @@ -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), @@ -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{ diff --git a/org/party_test.go b/org/party_test.go index bffbf250..9c40ee70 100644 --- a/org/party_test.go +++ b/org/party_test.go @@ -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.).).") + }) +}