optakt / flow-dps

Flow Data Provisioning Service
Apache License 2.0
29 stars 13 forks source link

Rosetta: Validation improvements #401

Closed Maelkum closed 3 years ago

Maelkum commented 3 years ago

Goal of this PR

This PR improves the Rosetta input requests validation code.

There were few alternatives that were considered before this. a) Checking errors as strings. Abandoned for evident reasons. b) Defining validation functions in tags. E.g.

type Request struct {
    BlockID identifier.Block `json:"block_identifier" validate:"block_validator"`
}

Then we'd define a custom validation function associated with the block_validator tag. I didn't like this is because if you rename a function/tag, the code explodes at runtime.

c) Using the regular required, gt and similar tags. I didn't like this approach because the structured errors say something like - validation failed on tag "required" for field X. Then you're string-matching the struct field names and if you rename a field you lose the validation.

The code in this PR defines validators on structs/types, and they are called when found in the validated data (at whichever level).

Checklist