Cross-resource validation

Is it possible to validate resource against values from another? I’m modeling API where some objects are nested within other objects, and we want to make them separate resources. The only catch is that type of the subresource has to match type of its parent resource. Is it possible to enforce something like that during validation?

You can implement a resource validator by fulfilling the resource.ResourceWithValidateConfig interface.

The ValidateConfig() method has access to the entire configuration and can make whatever comparisons necessary.

Alternatively, a normal attribute validator (applied as part of the schema attributes) also has access to the whole configuration, so you can pass them a path.Expression which spells out either:

  • the absolute path to the other attribute
  • the relative path from this attribute to the other attribute

So, the validator running on attribute “A” can pull out the value(s) of attribute(s) “B” based on the supplied path. See the AlsoRequires attribute validator for an example of one attribute’s validator peeking at another attribute’s value.