VOOZH about

URL: https://thenewstack.io/json-schema-keywords-allof-and-anyof-in-opa-type-checker/

⇱ JSON Schema Keywords AllOf and AnyOf in OPA Type Checker - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2021-09-29 08:20:29
JSON Schema Keywords AllOf and AnyOf in OPA Type Checker
contributed,sponsor-ibm-cloud,sponsored,sponsored-post-contributed,
Cloud Native Ecosystem

JSON Schema Keywords AllOf and AnyOf in OPA Type Checker

Enhancement of the Open Policy Agent Rego type checker did not support some keywords in the JSON Schema 2019-09 release. Now it does.
Sep 29th, 2021 8:20am by Jing Chen and Julia Friedman
👁 Featued image for: JSON Schema Keywords AllOf and AnyOf in OPA Type Checker
Featured image via Pixabay
IBM Cloud sponsored this post.
Jing Chen
Jing is a junior majoring in computer science and located in New York. She is excited to learn more about research in the hybrid cloud space and is passionate to always learn more in the tech domain.
Open Policy Agent (OPA) is an open source engine that unifies policy enforcement across the cloud native stack. This project includes a recent enhancement to its Rego type checker that uses JSON schemas as input during evaluation of policy. This enhancement is achieved through the “opa eval” command and the -s flag to include a schema in the input. The JSON schemas that are provided as input during policy evaluation essentially provide a blueprint for the policy being written, and this allows for easier and more immediate type checking. This provides more precise and informative error messages. Previous blog posts introduced the type checker with a few limitations, as it does not provide support for many keywords available in the JSON Schema 2019-09 release. For example, this type checker did not provide support for “allOf” and “anyOf” keywords.

allOf vs. anyOf — What’s the Difference?

To understand the meaning of anyOf and allOf, consider entering your mailing address to purchase something online. After entering your address line, you can either specify it with state, city and ZIP code or with country and postcode. For those who are familiar with JSON Schema language, it might look something like this:
{
 "$schema": "http://json-schema.org/draft-04/schema#",
 "type": "object",
 "title": "My schema",
 "properties": {
 "Address": { "type": "string" },
 },
 "required": [ "Address" ],
 "anyOf": [
 {
 "allOf":[
 {
 "type": "object",
 "properties": {
 "State": { "type": "string" },
 "City": { "type": "string" },
 "ZipCode": { "type": "string" }
 },
 },
 {
 "required": [ “State”, “City”, "ZipCode" ]
 }
 ]
 },
 {
 "allOf":[
 {
 "type": "object",
 "properties": {
 "County": { "type": "string" },
 "PostCode": { "type": "string" }
 },
 },
 {
 "required": [ “County”, "PostCode" ]
 }
 ]
 }
 ]
}

In other words, after entering your address line, you’re prompted to enter either (state AND city AND ZIP code) or (country AND postcode) (technically, you can enter both options under “anyOf” since it’s an inclusive “or”).
Julia Friedman
Julia is a third-year student at the University of Virginia majoring in computer science and minoring in engineering business. Over the summer, she interned at IBM in the field of cloud computing.
As we can see in the two options to describe your address line, whichever one you choose, you must include all the specified information because of a sub-schema containing the “required” properties in both of the allOf listed schemas. Allowing “anyOf” then adds the flexibility of choosing different mailing address options, and allowing “allOf” ensures security in that all the required information will be there (applicable to this example only). Support was added to allow for these keywords in OPA’s type checker in the 0.32.0, so users can now include schemas that use these keywords. Let’s suppose you’re the admin of a shopping website and you want to temporarily deny Californian shoppers the ability to order because the current wildfire situation impedes shipping. Thus, you can write an API policy like the following to access user information and identify those visitors who live in California.
package shopper.mailingAddress 

deny { 
 input.States == “California” 
}

As humans, we all make mistakes, and the above policy contains a typo that you may or may not have noticed (if you have, good job!). Now with the newly implemented support for “anyOf” and “allOf”, OPA will inform you of the possible error:
input.States
 ^
 have: "States"
 want (one of): ["City" "County" "PostCode" "State" "ZipCode"]

Implementation of support for the “allOf” and “anyOf” keywords eliminated a limitation of OPA’s type checker, so now schemas that use these keywords will render more useful error messages during policy creation and evaluation. These keywords are particularly relevant because Kubernetes admission control is one of the important use cases for OPA, and many of Kubernetes’ published schemas use “allOf” and “anyOf.” With this newly implemented support for “allOf” and “anyOf,” OPA is now able to support many of these Kubernetes use cases and enhance both security and flexibility for the API policy written. You can read more about this enhancement in OPA’s documentation page.

What’s Next?

Future work relating to this project will involve implementing support for the many other keywords not currently supported by OPA’s JSON schema Rego type checker (enum, if/then/else, among others). Further, we hope to conduct user surveys with the OPA community to evaluate the effectiveness of the type checker. Let us know what you think!
Cut complexity, fuel growth. IBM automation transforms tangled IT stacks into intelligent, streamlined systems. By infusing AI into integration, observability, and identity, leaders gain agility, resilience, and confidence to scale innovation without the drag of inefficiency.
Learn More
Hear more from our sponsor

Links

    1. Documentation
    2. Examples
    3. JSON-to-JSON schema online tool
    4. JSON schema reference
Note: Other authors of this article were Mandana Vaziri, principal research staff member at IBM, and Ansu Varghese, research software engineer at IBM.
IBM Cloud is ready for any company that needs to transform with speed but can’t compromise on mission-critical workloads or security and compliance requirements to meet enterprise challenges. Cloud with confidence with the IBM Cloud.
Learn More
The latest from IBM Cloud
TRENDING STORIES
Jing is a junior majoring in computer science and located in New York. She is excited to learn more about research in the hybrid cloud space and is passionate to always learn more in the tech domain.
Read more from Jing Chen
Julia is a third-year student at the University of Virginia majoring in computer science and minoring in engineering business. Over the summer, she interned at IBM in the field of cloud computing.
Read more from Julia Friedman
IBM Cloud sponsored this post.
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.