Skip to content

Commit

Permalink
Adding validation for Rate extension definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
samlown committed Dec 5, 2023
1 parent 0eb5c7c commit 5e31e7a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
17 changes: 10 additions & 7 deletions tax/regime.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,9 @@ func ValidateStructWithRegime(ctx context.Context, obj interface{}, fields ...*v
return ValidateInRegime(ctx, obj)
}

// Validate ensures the Category's contents are correct.
func (c *Category) Validate() error {
err := validation.ValidateStruct(c,
// ValidateWithContext ensures the Category's contents are correct.
func (c *Category) ValidateWithContext(ctx context.Context) error {
err := validation.ValidateStructWithContext(ctx, c,
validation.Field(&c.Code, validation.Required),
validation.Field(&c.Name, validation.Required),
validation.Field(&c.Title, validation.Required),
Expand Down Expand Up @@ -507,17 +507,20 @@ func (s *Source) Validate() error {
)
}

// Validate checks that our tax definition is valid. This is only really
// ValidateWithContext checks that our tax definition is valid. This is only really
// meant to be used when testing new regional tax definitions.
func (r *Rate) Validate() error {
err := validation.ValidateStruct(r,
func (r *Rate) ValidateWithContext(ctx context.Context) error {
reg := ctx.Value(KeyRegime).(*Regime)
err := validation.ValidateStructWithContext(ctx, r,
validation.Field(&r.Key, validation.Required),
validation.Field(&r.Name, validation.Required),
validation.Field(&r.Values,
validation.When(r.Exempt, validation.Nil),
validation.By(checkRateValuesOrder),
),
validation.Field(&r.Extensions),
validation.Field(&r.Extensions,
validation.Each(InKeyDefs(reg.Extensions)),
),
validation.Field(&r.Map),
validation.Field(&r.Meta),
)
Expand Down
3 changes: 2 additions & 1 deletion tax/scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ func (s *Scenario) hasTags(docTags []cbc.Key) bool {
return false
}

// Validate checks the scenario for errors.
// ValidateWithContext checks the scenario for errors, using the regime in the context
// to validate the list of tags.
func (s *Scenario) ValidateWithContext(ctx context.Context) error {
r := ctx.Value(KeyRegime).(*Regime)
err := validation.ValidateStructWithContext(ctx, s,
Expand Down

0 comments on commit 5e31e7a

Please sign in to comment.