Skip to content

Commit

Permalink
Fix #7 - validation of EDN list input
Browse files Browse the repository at this point in the history
  • Loading branch information
luposlip committed Jan 5, 2022
1 parent a6d3afb commit c0c928a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
14 changes: 11 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

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.3.3 - 2022-01-05

- Fixed issue when EDN input to be validated contains lists
- Updated Java Library

## 0.3.2 - 2021-09-15

- Eliminated reflective function calls
Expand Down Expand Up @@ -98,13 +103,13 @@ This release adds usage of $ref pointing to resources in the classpath.
### Added
- API to prepare JSON Schema for reuse
(better performance when using same Schema to validate multiple JSON documents)

## 0.1.4 - 2019-04-29

### Changed
- Updated Java library
- Changed to Apache License, Version 2.0

## 0.1.3 - 2019-03-12

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

[0.3.3]: https://github.com/luposlip/json-schema/compare/0.3.2...0.3.3
[0.3.2]: https://github.com/luposlip/json-schema/compare/0.3.1...0.3.2
[0.3.1]: https://github.com/luposlip/json-schema/compare/0.3.0...0.3.1
[0.3.0]: https://github.com/luposlip/json-schema/compare/0.2.9...0.3.0
[0.2.9]: https://github.com/luposlip/json-schema/compare/0.2.8...0.2.9
[0.2.8]: https://github.com/luposlip/json-schema/compare/0.2.7...0.2.8
[0.2.7]: https://github.com/luposlip/json-schema/compare/0.2.6...0.2.7
Expand All @@ -145,4 +154,3 @@ This release adds usage of $ref pointing to resources in the classpath.
[0.1.3]: https://github.com/luposlip/json-schema/compare/0.1.2...0.1.3
[0.1.2]: https://github.com/luposlip/json-schema/compare/0.1.1...0.1.2
[0.1.1]: https://github.com/luposlip/json-schema/compare/0.1.0...0.1.1

8 changes: 4 additions & 4 deletions 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.3.2"]
[luposlip/json-schema "0.3.3"]
```

A Clojure library for:
Expand Down Expand Up @@ -90,7 +90,7 @@ This will generate the following schema:

```clojure
{:$schema "http://json-schema.org/draft-07/schema#"
:title "ent-1"
:title "ent-1"
:type :object
:additionalProperties false
:properties {"things" {:type :array
Expand Down Expand Up @@ -183,14 +183,14 @@ To the contributors:
## Copyright & License
Copyright (C) 2020-2021 Henrik Mohr
Copyright (C) 2020-2022 Henrik Mohr
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down
4 changes: 2 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
(defproject luposlip/json-schema "0.3.2"
(defproject luposlip/json-schema "0.3.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"}
:repositories {"jitpack" {:url "https://jitpack.io"}}
:dependencies [[org.clojure/clojure "1.10.3"]
[cheshire "5.10.1"]
[com.github.everit-org.json-schema/org.everit.json.schema "1.13.0"]]
[com.github.everit-org.json-schema/org.everit.json.schema "1.14.0"]]
:global-vars {*warn-on-reflection* true}
:repl-options {:init-ns json-schema.core}
:profiles {:dev {:resource-paths ["test/resources"]}})
9 changes: 5 additions & 4 deletions src/json_schema/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
"Prepares JSON instance based on input string, map or vector"
[input]
(if (or (associative? input)
(sequential? input)
(and (string? input)
(#{\{ \[} (first input))))
(let [json-tokener ^JSONTokener(prepare-tokener input)]
(if (or (vector? input)
(= \[ (first input)))
(let [json-tokener ^JSONTokener (prepare-tokener input)]
(if (or (sequential? input)
(= \[ (first input)))
(JSONArray. json-tokener)
(JSONObject. json-tokener)))
(throw (ex-info "Unsupported JSON input" {:input input}))))
Expand Down Expand Up @@ -112,7 +113,7 @@
(try
(.validate ^Schema schema (prepare-json json))
json
(catch ValidationException e
(catch ValidationException e
(let [errors (into [] (.getAllMessages ^ValidationException e))
c (count errors)
[n s] (if (> c 1)
Expand Down
31 changes: 21 additions & 10 deletions test/json_schema/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@

(testing "Numbers are not valid input"
(is (thrown-with-msg? Exception #"Unsupported Schema input"
(json/validate 1 2)))
(json/validate 1 2)))
(is (thrown-with-msg? Exception #"Unsupported Schema input"
(json/validate 1 "2")))
(is (thrown-with-msg? Exception #"Unsupported Schema input"
(json/validate "1" 2))))

(testing "Schema has to be a map"
(is (thrown-with-msg? Exception #"Unsupported Schema input"
(json/validate "1" "2"))))

(testing "JSON has to be a map or an array"
(is (thrown-with-msg? Exception #"Unsupported JSON input"
(json/validate schema "1"))))

(testing "ID has to be a number"
(is (thrown? RuntimeException (json/validate schema "{\"id\" : \"1\"}"))))

(testing "Valid input as JSON string"
(let [data "{\"id\": 1}"] (is (= data (json/validate schema data)))))

(testing "Valid input as EDN"
(let [data {:id 1}] (is (= data (json/validate schema data)))))

Expand Down Expand Up @@ -75,9 +75,9 @@
schema (json/prepare-schema json-schema-edn)
json-edn-valid {:id 0.001}
json-edn-invalid {:id 0}]

(testing "valid input VALIDATES"
(is (= json-edn-valid (json/validate schema json-edn-valid))))
(is (= json-edn-valid (json/validate schema json-edn-valid))))

(testing "valid input does NOT validate"
(is (thrown?
Expand Down Expand Up @@ -116,9 +116,9 @@
json-str-valid "[{\"some\":\"data\"}]"
json-edn-valid [{:some "data"}]
json-edn-invalid {:some "data"}]

(testing "valid JSON string input VALIDATES"
(is (= json-str-valid (json/validate schema json-str-valid))))
(is (= json-str-valid (json/validate schema json-str-valid))))

(testing "valid EDN input VALIDATES"
(is (= json-edn-valid (json/validate schema json-edn-valid))))
Expand Down Expand Up @@ -161,3 +161,14 @@
Exception
#"JSON Validation error"
(json/validate schema invalid-input)))))))))

(deftest validate-list
(testing "Validate EDN list as JSON array"
(let [schema (json/prepare-schema {:$schema "http://json-schema.org/draft-04/schema"
:type "array"})]

(testing "Valid input as EDN vector"
(is (= ["a" "b"] (json/validate schema ["a" "b"]))))

(testing "Valid input as EDN list"
(is (= '("a" "b") (json/validate schema '("a" "b"))))))))

0 comments on commit c0c928a

Please sign in to comment.