From 332b7e6291f62a51c293473abac853462dae7af9 Mon Sep 17 00:00:00 2001 From: DavidMazarro Date: Wed, 11 Mar 2026 09:39:13 +0100 Subject: [PATCH 1/3] feat: add JSON utils functions --- JsonSchema/Index.lean | 1 + JsonSchema/Test/UtilsTest.lean | 29 ++++++++++++++++ JsonSchema/Utils/Index.lean | 2 ++ JsonSchema/Utils/Json.lean | 33 +++++++++++++++++++ JsonSchema/Utils/JsonTheorems.lean | 53 ++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+) create mode 100644 JsonSchema/Test/UtilsTest.lean create mode 100644 JsonSchema/Utils/Index.lean create mode 100644 JsonSchema/Utils/Json.lean create mode 100644 JsonSchema/Utils/JsonTheorems.lean diff --git a/JsonSchema/Index.lean b/JsonSchema/Index.lean index 36bf53c..a5ad6a5 100644 --- a/JsonSchema/Index.lean +++ b/JsonSchema/Index.lean @@ -2,3 +2,4 @@ import JsonSchema.Schema import JsonSchema.Validation import JsonSchema.Correctness.Index import JsonSchema.Derive.Index +import JsonSchema.Utils.Index diff --git a/JsonSchema/Test/UtilsTest.lean b/JsonSchema/Test/UtilsTest.lean new file mode 100644 index 0000000..c8c73d1 --- /dev/null +++ b/JsonSchema/Test/UtilsTest.lean @@ -0,0 +1,29 @@ +import JsonSchema.Utils.Json + +/-! Tests for JsonSchema.Utils -/ + +open JsonSchema.Utils +open Lean (Json) + +-- ============================================================================ +-- schemaIsTopLevelArray +-- ============================================================================ + +#guard schemaIsTopLevelArray (.mkObj [("type", "array"), ("items", .mkObj [("type", "string")])]) == true +#guard schemaIsTopLevelArray (.mkObj [("type", "object")]) == false +#guard schemaIsTopLevelArray (.mkObj [("type", "string")]) == false + +-- ============================================================================ +-- wrapArraySchema / unwrapArrayResult +-- ============================================================================ + +-- Wrapped schema is no longer a top-level array +#guard schemaIsTopLevelArray (wrapArraySchema (.mkObj [("type", "array")])) == false + +-- Round-trip: wrap then unwrap preserves original data +private def testArrayResult : Json := .arr #[.str "a", .str "b"] +private def testWrapped : Json := .mkObj [("items", testArrayResult)] +#guard unwrapArrayResult testWrapped == testArrayResult + +-- unwrapArrayResult on non-wrapped input returns input unchanged +#guard unwrapArrayResult (.str "hello") == .str "hello" diff --git a/JsonSchema/Utils/Index.lean b/JsonSchema/Utils/Index.lean new file mode 100644 index 0000000..ffacd7a --- /dev/null +++ b/JsonSchema/Utils/Index.lean @@ -0,0 +1,2 @@ +import JsonSchema.Utils.Json +import JsonSchema.Utils.JsonTheorems diff --git a/JsonSchema/Utils/Json.lean b/JsonSchema/Utils/Json.lean new file mode 100644 index 0000000..0632d20 --- /dev/null +++ b/JsonSchema/Utils/Json.lean @@ -0,0 +1,33 @@ +import Lean.Data.Json + +/-! +# JSON Schema Utilities + +Generic helpers for manipulating JSON Schema documents as `Lean.Json` values. +-/ + +namespace JsonSchema.Utils + +open Lean (Json) + +/-- Returns true when the top-level schema describes an array type. -/ +def schemaIsTopLevelArray (schema : Json) : Bool := + (schema.getObjValAs? String "type" |>.toOption |>.getD "") == "array" + +/-- Wraps a top-level array schema in an object with a single "items" property. + Useful when a consumer requires an object schema at the top level. + Use `unwrapArrayResult` to extract the inner value from the response. -/ +def wrapArraySchema (schema : Json) : Json := + .mkObj [ + ("type", "object"), + ("properties", .mkObj [("items", schema)]), + ("required", .arr #[.str "items"]) + ] + +/-- Extracts the "items" field from a response produced with a wrapped + array schema (see `wrapArraySchema`). Returns the input unchanged + if no "items" field is found. -/ +def unwrapArrayResult (json : Json) : Json := + json.getObjVal? "items" |>.toOption |>.getD json + +end JsonSchema.Utils diff --git a/JsonSchema/Utils/JsonTheorems.lean b/JsonSchema/Utils/JsonTheorems.lean new file mode 100644 index 0000000..8cec9ab --- /dev/null +++ b/JsonSchema/Utils/JsonTheorems.lean @@ -0,0 +1,53 @@ +import JsonSchema.Utils.Json +import Std.Data.TreeMap.Raw.Lemmas +import Std.Data.TreeMap.Raw.WF + +/-! +# JSON Schema Utility Theorems + +Correctness theorems for the JSON helper functions in `JsonSchema.Utils`. +-/ + +namespace JsonSchema.Utils + +open Lean (Json) + +-- ---- Bridge lemmas ---- +-- Json.mkObj builds a TreeMap.Raw internally, so proving anything about +-- getObjVal? requires reasoning through TreeMap insert/lookup. These private +-- lemmas isolate that plumbing so the public theorems stay clean. + +-- Bridge lemma: singleton mkObj lookup always succeeds. +private theorem bridge_getObjVal_singleton (k : String) (v : Json) : + (Json.mkObj [(k, v)]).getObjVal? k = .ok v := by + simp only [Json.mkObj, Json.getObjVal?] + have : (∅ : Std.TreeMap.Raw String Json compare).WF := Std.TreeMap.Raw.WF.empty + simp [this, pure, Except.pure] + +-- Bridge lemma: "type" lookup in a 3-key mkObj returns the first entry. +private theorem bridge_getObjVal_type (v props req : Json) : + (Json.mkObj [("type", v), ("properties", props), ("required", req)]).getObjVal? "type" + = .ok v := by + simp only [Json.mkObj, Json.getObjVal?] + rw [Std.TreeMap.Raw.ofList_cons] + have hwf : ((∅ : Std.TreeMap.Raw String Json compare).insert "type" v).WF := + Std.TreeMap.Raw.WF.empty.insert + simp only [Std.TreeMap.Raw.get?_eq_getElem?] + rw [Std.TreeMap.Raw.getElem?_insertMany_list_of_contains_eq_false hwf + (by simp only [List.map]; decide)] + have hempty : (∅ : Std.TreeMap.Raw String Json compare).WF := Std.TreeMap.Raw.WF.empty + simp [hempty, pure, Except.pure] + +/-- Wrapping always produces a non-array top-level schema. -/ +theorem wrapArraySchema_not_topLevelArray (schema : Json) : + schemaIsTopLevelArray (wrapArraySchema schema) = false := by + simp only [schemaIsTopLevelArray, wrapArraySchema, Json.getObjValAs?, Json.getObjValD, + bridge_getObjVal_type] + decide + +/-- Unwrapping extracts the "items" field from an object that has one. -/ +theorem unwrapArrayResult_mkObj_items (v : Json) : + unwrapArrayResult (Json.mkObj [("items", v)]) = v := by + simp only [unwrapArrayResult, bridge_getObjVal_singleton, Except.toOption, Option.getD] + +end JsonSchema.Utils From 52a6e92e05caf87e930bade1f763309a59f1cb83 Mon Sep 17 00:00:00 2001 From: DavidMazarro Date: Wed, 11 Mar 2026 09:53:47 +0100 Subject: [PATCH 2/3] build: migrated lakefile to TOML, bumped minor version --- lakefile.lean | 9 --------- lakefile.toml | 7 +++++++ 2 files changed, 7 insertions(+), 9 deletions(-) delete mode 100644 lakefile.lean create mode 100644 lakefile.toml diff --git a/lakefile.lean b/lakefile.lean deleted file mode 100644 index 764805b..0000000 --- a/lakefile.lean +++ /dev/null @@ -1,9 +0,0 @@ -import Lake -open Lake DSL - -package «json-schema» where - version := v!"0.1.0" - -@[default_target] -lean_lib JsonSchema where - roots := #[`JsonSchema] diff --git a/lakefile.toml b/lakefile.toml new file mode 100644 index 0000000..7662a6f --- /dev/null +++ b/lakefile.toml @@ -0,0 +1,7 @@ +name = "json-schema" +version = "0.2.0" +defaultTargets = ["JsonSchema"] + +[[lean_lib]] +name = "JsonSchema" +roots = ["JsonSchema"] From 974629c7c6f8ed8a4056d5ab02c315eeef503c0a Mon Sep 17 00:00:00 2001 From: DavidMazarro Date: Wed, 11 Mar 2026 10:02:35 +0100 Subject: [PATCH 3/3] chore: remove the theorems, not adding much value --- JsonSchema/Utils/Index.lean | 1 - JsonSchema/Utils/JsonTheorems.lean | 53 ------------------------------ 2 files changed, 54 deletions(-) delete mode 100644 JsonSchema/Utils/JsonTheorems.lean diff --git a/JsonSchema/Utils/Index.lean b/JsonSchema/Utils/Index.lean index ffacd7a..7f9072b 100644 --- a/JsonSchema/Utils/Index.lean +++ b/JsonSchema/Utils/Index.lean @@ -1,2 +1 @@ import JsonSchema.Utils.Json -import JsonSchema.Utils.JsonTheorems diff --git a/JsonSchema/Utils/JsonTheorems.lean b/JsonSchema/Utils/JsonTheorems.lean deleted file mode 100644 index 8cec9ab..0000000 --- a/JsonSchema/Utils/JsonTheorems.lean +++ /dev/null @@ -1,53 +0,0 @@ -import JsonSchema.Utils.Json -import Std.Data.TreeMap.Raw.Lemmas -import Std.Data.TreeMap.Raw.WF - -/-! -# JSON Schema Utility Theorems - -Correctness theorems for the JSON helper functions in `JsonSchema.Utils`. --/ - -namespace JsonSchema.Utils - -open Lean (Json) - --- ---- Bridge lemmas ---- --- Json.mkObj builds a TreeMap.Raw internally, so proving anything about --- getObjVal? requires reasoning through TreeMap insert/lookup. These private --- lemmas isolate that plumbing so the public theorems stay clean. - --- Bridge lemma: singleton mkObj lookup always succeeds. -private theorem bridge_getObjVal_singleton (k : String) (v : Json) : - (Json.mkObj [(k, v)]).getObjVal? k = .ok v := by - simp only [Json.mkObj, Json.getObjVal?] - have : (∅ : Std.TreeMap.Raw String Json compare).WF := Std.TreeMap.Raw.WF.empty - simp [this, pure, Except.pure] - --- Bridge lemma: "type" lookup in a 3-key mkObj returns the first entry. -private theorem bridge_getObjVal_type (v props req : Json) : - (Json.mkObj [("type", v), ("properties", props), ("required", req)]).getObjVal? "type" - = .ok v := by - simp only [Json.mkObj, Json.getObjVal?] - rw [Std.TreeMap.Raw.ofList_cons] - have hwf : ((∅ : Std.TreeMap.Raw String Json compare).insert "type" v).WF := - Std.TreeMap.Raw.WF.empty.insert - simp only [Std.TreeMap.Raw.get?_eq_getElem?] - rw [Std.TreeMap.Raw.getElem?_insertMany_list_of_contains_eq_false hwf - (by simp only [List.map]; decide)] - have hempty : (∅ : Std.TreeMap.Raw String Json compare).WF := Std.TreeMap.Raw.WF.empty - simp [hempty, pure, Except.pure] - -/-- Wrapping always produces a non-array top-level schema. -/ -theorem wrapArraySchema_not_topLevelArray (schema : Json) : - schemaIsTopLevelArray (wrapArraySchema schema) = false := by - simp only [schemaIsTopLevelArray, wrapArraySchema, Json.getObjValAs?, Json.getObjValD, - bridge_getObjVal_type] - decide - -/-- Unwrapping extracts the "items" field from an object that has one. -/ -theorem unwrapArrayResult_mkObj_items (v : Json) : - unwrapArrayResult (Json.mkObj [("items", v)]) = v := by - simp only [unwrapArrayResult, bridge_getObjVal_singleton, Except.toOption, Option.getD] - -end JsonSchema.Utils