Skip to content

Commit

Permalink
Commit version 0.4.3 (infer enhancements)
Browse files Browse the repository at this point in the history
  • Loading branch information
luposlip committed Feb 20, 2024
1 parent 3ee9585 commit 0eec0e1
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 20 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).

## 0.4.3 - 2024-02-20

### Enhanced

- When infer* receives a lazy seq, it acts as if it's individual documents. This
makes great sense when you want to generate a schema for eg. a folder
containing data files.
- Better error messages when finding `nil` values (suggest `:nullable true`)
- Update Java library

## 0.4.2 - 2023-10-23

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Clojure JSON Schema Validator & Generator

```clojure
[luposlip/json-schema "0.4.2"]
[luposlip/json-schema "0.4.3"]
```

A Clojure library for:
Expand Down
4 changes: 2 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
(defproject luposlip/json-schema "0.4.2"
(defproject luposlip/json-schema "0.4.3"
:description "Clojure library for JSON Schema validation and generation - Draft-07 compatible"
:url "https://github.com/luposlip/json-schema"
:license {:name "Apache License, Version 2.0"
:url "https://www.apache.org/licenses/LICENSE-2.0"}
:dependencies [[org.clojure/clojure "1.11.1"]
[cheshire "5.12.0"]
[com.github.erosb/everit-json-schema "1.14.3"]]
[com.github.erosb/everit-json-schema "1.14.4"]]
:global-vars {*warn-on-reflection* true}
:repl-options {:init-ns json-schema.core}
:profiles {:dev {:resource-paths ["test/resources"]}})
30 changes: 22 additions & 8 deletions src/json_schema/infer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
Params:
optional - keys that shouldn't be required
additional-props - if additional properties are allowed in schema objects
Optional params:
title - schema title
description - schema description
Expand Down Expand Up @@ -134,6 +134,7 @@
apply
(partial infer (unroot params))
data)})
(seq? data) (trampoline apply (partial infer params) data)
:else
(merge sch
(data-type
Expand All @@ -151,13 +152,24 @@
If a single document is passed, infer-strict is used directly.
Multiple documents can be passed either via apply:
(apply (partial infer params) vector-of-inputs
Or if you have a (lazy) seq of things, infer directly:
(infer params lazy-inputs)
Example:
(infer {:additional-props false
:nullable true}
(map slurp list-of-filenames))
Params:
title - schema title
description - schema description
uri - schema uri
schema - continue building on schema
optional - keys that shouldn't be required
nullable - optionality by nullability"
title - schema title
description - schema description
uri - schema uri
schema - continue building on schema
optional - keys that shouldn't be required
additional-props - don't allow props not in schema
nullable - optionality by nullability"
[params & docs]
(if (= 1 (count docs))
(infer-strict params (first docs))
Expand Down Expand Up @@ -192,11 +204,13 @@
:maxLength 20}
(associative? data) (trampoline recur-fn data)
(and (nil? data) nullable) {:type #{:null}}
(nil? data) (throw (ex-info "Found null value - do you need to set :nullable true?"
{:data data
:params params}))
:else (throw (ex-info "Not yet supporting data-type" {:data data
:params params}))))

(defn infer->json
"A helper function that returns inferred schema as JSON"
[params data]
(->> data (infer-strict params) json/encode))

54 changes: 45 additions & 9 deletions test/json_schema/infer_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
(:import clojure.lang.ExceptionInfo))

(deftest infer-map

(is (= {:$schema "http://json-schema.org/draft-07/schema#"
:title "ent-1"
:title "ent-1"
:type #{:object}
:additionalProperties false
:properties {"thing" {:type #{:integer}}}
:required #{"thing"}}
(t/infer-strict {:title "ent-1"} {:thing 1})))

(is (= {:$schema "http://json-schema.org/draft-07/schema#"
:title "ent-1"
:title "ent-1"
:type #{:object}
:additionalProperties false
:properties {"thing" {:type #{:object}
Expand All @@ -26,7 +26,7 @@
(t/infer-strict {:title "ent-1"} {:thing {:quantity 1}})))

(is (= {:$schema "http://json-schema.org/draft-07/schema#"
:title "ent-1"
:title "ent-1"
:type #{:object}
:additionalProperties false
:properties {"thing" {:type #{:object}
Expand All @@ -35,9 +35,9 @@
:required #{"quantity"}}}
:required #{"thing"}}
(t/infer-strict {:title "ent-1"} {:thing {:quantity 1.1}})))

(is (= {:$schema "http://json-schema.org/draft-07/schema#"
:title "ent-1"
:title "ent-1"
:type #{:object}
:additionalProperties false
:properties {"thing" {:type #{:object}
Expand All @@ -48,7 +48,7 @@
(t/infer-strict {:title "ent-1"} {:thing {:quantity "11.111,11"}})))

(is (= {:$schema "http://json-schema.org/draft-07/schema#"
:title "ent-1"
:title "ent-1"
:type #{:object}
:additionalProperties false
:properties {"things" {:type #{:array}
Expand All @@ -60,7 +60,7 @@
(t/infer-strict {:title "ent-1"} {:things [{:quantity 1} {:quantity 2}]})))

(is (= {:$schema "http://json-schema.org/draft-07/schema#"
:title "ent-1"
:title "ent-1"
:type #{:object}
:additionalProperties false
:properties {"thing" {:type #{:object}
Expand Down Expand Up @@ -91,7 +91,7 @@
:items {:type #{:string}}}
(t/infer-strict {:title "ent-1"} ["hej"])))

(is (= {:$schema "http://json-schema.org/draft-07/schema#",
(is (= {:$schema "http://json-schema.org/draft-07/schema#",
:title "ent-1",
:type #{:array},
:items
Expand Down Expand Up @@ -232,3 +232,39 @@
more-data (assoc data :more-data {:hey "you!"})]
(is (thrown? ExceptionInfo (v/validate schema more-data)))
(is (= more-data (v/validate more-schema more-data)))))

(deftest infer-seq
(let [res {:$schema "http://json-schema.org/draft-07/schema#",
:title "ent-1",
:type #{:object},
:additionalProperties false,
:properties {"quantity" {:type #{:integer}}},
:required #{"quantity"}}]
(is (= res
(t/infer {:title "ent-1" :additional-props false :nullable true}
(take 1000 (repeat {:quantity 1})))))
(is (= res
(t/infer-strict {:title "ent-1" :additional-props false :nullable true}
(take 1000 (repeat {:quantity 1})))))
(is (= res
(apply (partial t/infer {:title "ent-1" :additional-props false :nullable true})
(take 1000 (repeat {:quantity 1})))))))

(deftest infer-nil
(is (thrown-with-msg? clojure.lang.ExceptionInfo
#"\:nullable true"
(t/infer {:title "ent-1" :additional-props false}
{:quantity 1
:parent nil})))

(is (= {:$schema "http://json-schema.org/draft-07/schema#",
:title "ent-1",
:type #{:object},
:additionalProperties false,
:properties
{"quantity" {:type #{:integer}}, "parent" {:type #{:null}}},
:required #{"parent" "quantity"}}
(t/infer {:title "ent-1" :additional-props false
:nullable true}
{:quantity 1
:parent nil}))))

0 comments on commit 0eec0e1

Please sign in to comment.