DEV Community

Cover image for Dev Log 3/30: anyOf
Richard A. Allen
Richard A. Allen

Posted on

Dev Log 3/30: anyOf

Jumping straight into it with no context.

Panettone: packagist.org

Open API anyOf Reference: oneof-anyof-allof-not

Use the anyOf keyword to validate the data against any amount of the given subschemas.
That is, the data may be valid against one or more subschemas at the same time.

Like I've said before (rifles through internal notes), there's no substitute for real use cases.

Panettone is all about DTOs, but it is in the greater service of API data consumption. What good is a DTO (Data Transfer Object) if it's can't represent the data it's supposed to transfer!

And so, today, while designing/developing/crafting/over-engineering the anyOf keyword into my project, I find myself asking:

How am I going to choose which schema to represent (convert to a type), when presented with multiple options?
What's the logic, the use case?

I clearly don't know the answer because it's groundhog day and I'm so deep head-first into Open API that I've lost sight of the forest, or prairie?

The only thing that is going to provide me clarity into how to designing the consumption of anyOf, and oneOf for that matter schemas is by looking at the data coming down from The Internets.

Or, not?

First of. What does it say on the tin? What does anyOf mean?

Welp, translated into human, this basically means:

The data coming down the pipeline might be Dog or Cat, but we don't know.
So be ready for either.

For the program doing the typing, validation, and consumption. What does it mean?

The program doesn't know either which Schema is coming down until it gets it.

Schrodinger's Cat. Dog. Animal.

On the type side of the animal house, the best I can think of doing is, given a schema of both Dog and Cat, just create types for both. Because the data could be representing any of these, as in anyOf these. Wait a minute, I think I might be on to something 🀑

During the actual API consumption (run) time, let whoever the receiver know that the data coming in can be of either type.

Using anyOf in a typed context means that the data must validate to at least one of the types.

How does PHP know which type the data conforms to? Well that's what PHP union types are there for.

Because History, these were added to PHP 8.0, which according to the changelog was released 26 Nov 2020.
So no that old, but not that recent either on a 20 year old-timer scale.

Conclusion, for now...

I don't want to context switch into another (PHP) program in order to gain some clarity or context as to how my generated types are going to be consumed.

But by the logic above, as long as long as I design Panettone to generate ALL types, whenever it runs into a anyOf scenario, then the consumer of Panettone types will be Gucci. See the Italiano theme here?

Imaginary Editor's Notes:

  • Editor: how many popular references?
  • Me: Yes. The Yes is recursive.

Photo by blackieshoot on Unsplash

Top comments (0)