From 1db9e6854c242e1dea591d0bf5ee150cdf55f6ba Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Mon, 22 Dec 2025 22:41:40 +0530 Subject: [PATCH] docs: add practical examples for oneOf, anyOf, and allOf --- pages/learn/json-schema-examples.md | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/pages/learn/json-schema-examples.md b/pages/learn/json-schema-examples.md index d775d0fdc..0896cd109 100644 --- a/pages/learn/json-schema-examples.md +++ b/pages/learn/json-schema-examples.md @@ -22,6 +22,44 @@ In this page, you will find examples illustrating different use cases to help yo - [Movie](#movie) - [User profile](#user-profile) +- [Choosing between oneOf, anyOf, and allOf](#choosing-between-oneof-anyof-and-allof) +## Choosing between oneOf, anyOf, and allOf + +When combining schemas, `oneOf`, `anyOf`, and `allOf` behave differently. +The examples below show common real-world scenarios and pitfalls. + +### anyOf — at least one schema must match + +```json +{ + "anyOf": [ + { "type": "string" }, + { "type": "number" } + ] +} +``` + +### oneOf — exactly one schema must match + +```json +{ + "oneOf": [ + { "type": "number", "minimum": 0 }, + { "type": "number", "maximum": 100 } + ] +} +``` + +### allOf — all schemas must match + +```json +{ + "allOf": [ + { "type": "string" }, + { "minLength": 5 } + ] +} +``` ## Address