Skip to content

Commit c0c928a

Browse files
committed
Fix #7 - validation of EDN list input
1 parent a6d3afb commit c0c928a

File tree

5 files changed

+43
-23
lines changed

5 files changed

+43
-23
lines changed

CHANGELOG.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

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

5+
## 0.3.3 - 2022-01-05
6+
7+
- Fixed issue when EDN input to be validated contains lists
8+
- Updated Java Library
9+
510
## 0.3.2 - 2021-09-15
611

712
- Eliminated reflective function calls
@@ -98,13 +103,13 @@ This release adds usage of $ref pointing to resources in the classpath.
98103
### Added
99104
- API to prepare JSON Schema for reuse
100105
(better performance when using same Schema to validate multiple JSON documents)
101-
106+
102107
## 0.1.4 - 2019-04-29
103108

104109
### Changed
105110
- Updated Java library
106111
- Changed to Apache License, Version 2.0
107-
112+
108113
## 0.1.3 - 2019-03-12
109114

110115
### Changed
@@ -126,6 +131,10 @@ This release adds usage of $ref pointing to resources in the classpath.
126131
### Added
127132
- Initial public release
128133

134+
[0.3.3]: https://github.com/luposlip/json-schema/compare/0.3.2...0.3.3
135+
[0.3.2]: https://github.com/luposlip/json-schema/compare/0.3.1...0.3.2
136+
[0.3.1]: https://github.com/luposlip/json-schema/compare/0.3.0...0.3.1
137+
[0.3.0]: https://github.com/luposlip/json-schema/compare/0.2.9...0.3.0
129138
[0.2.9]: https://github.com/luposlip/json-schema/compare/0.2.8...0.2.9
130139
[0.2.8]: https://github.com/luposlip/json-schema/compare/0.2.7...0.2.8
131140
[0.2.7]: https://github.com/luposlip/json-schema/compare/0.2.6...0.2.7
@@ -145,4 +154,3 @@ This release adds usage of $ref pointing to resources in the classpath.
145154
[0.1.3]: https://github.com/luposlip/json-schema/compare/0.1.2...0.1.3
146155
[0.1.2]: https://github.com/luposlip/json-schema/compare/0.1.1...0.1.2
147156
[0.1.1]: https://github.com/luposlip/json-schema/compare/0.1.0...0.1.1
148-

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Clojure JSON Schema Validator & Generator
44

55
```clojure
6-
[luposlip/json-schema "0.3.2"]
6+
[luposlip/json-schema "0.3.3"]
77
```
88

99
A Clojure library for:
@@ -90,7 +90,7 @@ This will generate the following schema:
9090

9191
```clojure
9292
{:$schema "http://json-schema.org/draft-07/schema#"
93-
:title "ent-1"
93+
:title "ent-1"
9494
:type :object
9595
:additionalProperties false
9696
:properties {"things" {:type :array
@@ -183,14 +183,14 @@ To the contributors:
183183
184184
## Copyright & License
185185
186-
Copyright (C) 2020-2021 Henrik Mohr
186+
Copyright (C) 2020-2022 Henrik Mohr
187187
188188
Licensed under the Apache License, Version 2.0 (the "License");
189189
you may not use this file except in compliance with the License.
190190
You may obtain a copy of the License at
191191
192192
http://www.apache.org/licenses/LICENSE-2.0
193-
193+
194194
Unless required by applicable law or agreed to in writing, software
195195
distributed under the License is distributed on an "AS IS" BASIS,
196196
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

project.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
(defproject luposlip/json-schema "0.3.2"
1+
(defproject luposlip/json-schema "0.3.3"
22
:description "Clojure library for JSON Schema validation and generation - Draft-07 compatible"
33
:url "https://github.com/luposlip/json-schema"
44
:license {:name "Apache License, Version 2.0"
55
:url "https://www.apache.org/licenses/LICENSE-2.0"}
66
:repositories {"jitpack" {:url "https://jitpack.io"}}
77
:dependencies [[org.clojure/clojure "1.10.3"]
88
[cheshire "5.10.1"]
9-
[com.github.everit-org.json-schema/org.everit.json.schema "1.13.0"]]
9+
[com.github.everit-org.json-schema/org.everit.json.schema "1.14.0"]]
1010
:global-vars {*warn-on-reflection* true}
1111
:repl-options {:init-ns json-schema.core}
1212
:profiles {:dev {:resource-paths ["test/resources"]}})

src/json_schema/core.clj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
"Prepares JSON instance based on input string, map or vector"
2121
[input]
2222
(if (or (associative? input)
23+
(sequential? input)
2324
(and (string? input)
2425
(#{\{ \[} (first input))))
25-
(let [json-tokener ^JSONTokener(prepare-tokener input)]
26-
(if (or (vector? input)
27-
(= \[ (first input)))
26+
(let [json-tokener ^JSONTokener (prepare-tokener input)]
27+
(if (or (sequential? input)
28+
(= \[ (first input)))
2829
(JSONArray. json-tokener)
2930
(JSONObject. json-tokener)))
3031
(throw (ex-info "Unsupported JSON input" {:input input}))))
@@ -112,7 +113,7 @@
112113
(try
113114
(.validate ^Schema schema (prepare-json json))
114115
json
115-
(catch ValidationException e
116+
(catch ValidationException e
116117
(let [errors (into [] (.getAllMessages ^ValidationException e))
117118
c (count errors)
118119
[n s] (if (> c 1)

test/json_schema/core_test.clj

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,26 @@
1616

1717
(testing "Numbers are not valid input"
1818
(is (thrown-with-msg? Exception #"Unsupported Schema input"
19-
(json/validate 1 2)))
19+
(json/validate 1 2)))
2020
(is (thrown-with-msg? Exception #"Unsupported Schema input"
2121
(json/validate 1 "2")))
2222
(is (thrown-with-msg? Exception #"Unsupported Schema input"
2323
(json/validate "1" 2))))
24-
24+
2525
(testing "Schema has to be a map"
2626
(is (thrown-with-msg? Exception #"Unsupported Schema input"
2727
(json/validate "1" "2"))))
28-
28+
2929
(testing "JSON has to be a map or an array"
3030
(is (thrown-with-msg? Exception #"Unsupported JSON input"
3131
(json/validate schema "1"))))
32-
32+
3333
(testing "ID has to be a number"
3434
(is (thrown? RuntimeException (json/validate schema "{\"id\" : \"1\"}"))))
35-
35+
3636
(testing "Valid input as JSON string"
3737
(let [data "{\"id\": 1}"] (is (= data (json/validate schema data)))))
38-
38+
3939
(testing "Valid input as EDN"
4040
(let [data {:id 1}] (is (= data (json/validate schema data)))))
4141

@@ -75,9 +75,9 @@
7575
schema (json/prepare-schema json-schema-edn)
7676
json-edn-valid {:id 0.001}
7777
json-edn-invalid {:id 0}]
78-
78+
7979
(testing "valid input VALIDATES"
80-
(is (= json-edn-valid (json/validate schema json-edn-valid))))
80+
(is (= json-edn-valid (json/validate schema json-edn-valid))))
8181

8282
(testing "valid input does NOT validate"
8383
(is (thrown?
@@ -116,9 +116,9 @@
116116
json-str-valid "[{\"some\":\"data\"}]"
117117
json-edn-valid [{:some "data"}]
118118
json-edn-invalid {:some "data"}]
119-
119+
120120
(testing "valid JSON string input VALIDATES"
121-
(is (= json-str-valid (json/validate schema json-str-valid))))
121+
(is (= json-str-valid (json/validate schema json-str-valid))))
122122

123123
(testing "valid EDN input VALIDATES"
124124
(is (= json-edn-valid (json/validate schema json-edn-valid))))
@@ -161,3 +161,14 @@
161161
Exception
162162
#"JSON Validation error"
163163
(json/validate schema invalid-input)))))))))
164+
165+
(deftest validate-list
166+
(testing "Validate EDN list as JSON array"
167+
(let [schema (json/prepare-schema {:$schema "http://json-schema.org/draft-04/schema"
168+
:type "array"})]
169+
170+
(testing "Valid input as EDN vector"
171+
(is (= ["a" "b"] (json/validate schema ["a" "b"]))))
172+
173+
(testing "Valid input as EDN list"
174+
(is (= '("a" "b") (json/validate schema '("a" "b"))))))))

0 commit comments

Comments
 (0)