Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions JsonSchema/Index.lean
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import JsonSchema.Schema
import JsonSchema.Validation
import JsonSchema.Correctness.Index
import JsonSchema.Derive.Index
import JsonSchema.Utils.Index
29 changes: 29 additions & 0 deletions JsonSchema/Test/UtilsTest.lean
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions JsonSchema/Utils/Index.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import JsonSchema.Utils.Json
33 changes: 33 additions & 0 deletions JsonSchema/Utils/Json.lean
Original file line number Diff line number Diff line change
@@ -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
9 changes: 0 additions & 9 deletions lakefile.lean

This file was deleted.

7 changes: 7 additions & 0 deletions lakefile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name = "json-schema"
version = "0.2.0"
defaultTargets = ["JsonSchema"]

[[lean_lib]]
name = "JsonSchema"
roots = ["JsonSchema"]
Loading