Skip to content

Commit

Permalink
Fix: org party regime validation
Browse files Browse the repository at this point in the history
  • Loading branch information
samlown committed Nov 27, 2024
1 parent eb4879b commit 956b2e3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
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.).).")
})
}

0 comments on commit 956b2e3

Please sign in to comment.