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..7f9072b --- /dev/null +++ b/JsonSchema/Utils/Index.lean @@ -0,0 +1 @@ +import JsonSchema.Utils.Json 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/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"]