From f472bcca1ee79b0447a69438b4eedc8d14040996 Mon Sep 17 00:00:00 2001 From: OpSocket Date: Fri, 19 Aug 2022 17:51:48 -0400 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=93=9A=EF=B8=8F=20raise=20error=20whe?= =?UTF-8?q?n=20trying=20to=20change=20a=20read=20only=20value?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/json-validator.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/json-validator.cpp b/src/json-validator.cpp index 3ffd087..89a28cf 100644 --- a/src/json-validator.cpp +++ b/src/json-validator.cpp @@ -457,6 +457,7 @@ class type_schema : public schema std::vector> type_; std::pair enum_, const_; std::vector> logic_; + bool read_only_ = false; static std::shared_ptr make(json &schema, json::value_t type, @@ -507,6 +508,13 @@ class type_schema : public schema else_->validate(ptr, instance, patch, e); } } + + // enforce default value if any read only values + if(read_only_) { + auto rod_value = default_value(ptr, instance, e); + if(rod_value != nullptr && instance != rod_value) + e.error(ptr, instance, "key is read-only"); + } } public: @@ -562,6 +570,12 @@ public: sch.erase(attr); } + attr = sch.find("readOnly"); + if (attr != sch.end()) { + read_only_ = attr.value().get(); + sch.erase(attr); + } + for (auto &key : known_keywords) sch.erase(key); @@ -1327,7 +1341,7 @@ class throwing_error_handler : public error_handler { void error(const json::json_pointer &ptr, const json &instance, const std::string &message) override { - throw std::invalid_argument(std::string("At ") + ptr.to_string() + " of " + instance.dump() + " - " + message + "\n"); + throw std::invalid_argument(std::string("At \"") + ptr.to_string() + "\" with a value of " + instance.dump() + " - " + message + "\n"); } }; @@ -1402,3 +1416,4 @@ json json_validator::validate(const json &instance, error_handler &err, const js } // namespace json_schema } // namespace nlohmann + From 3d7feb112566c5eb9e8eb43441b761acb7adc5f0 Mon Sep 17 00:00:00 2001 From: opsocket Date: Fri, 19 Aug 2022 18:25:23 -0400 Subject: [PATCH 2/4] =?UTF-8?q?=E2=9C=85=20test=20read=20only=20values?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/CMakeLists.txt | 4 ++++ test/read-only-values.cpp | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 test/read-only-values.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index fd1c309..23f3ca9 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -73,3 +73,7 @@ add_test(NAME issue-149-entry-selection COMMAND issue-149-entry-selection) add_executable(issue-189-default-values issue-189-default-values.cpp) target_link_libraries(issue-189-default-values nlohmann_json_schema_validator) add_test(NAME issue-189-default-values COMMAND issue-189-default-values) + +add_executable(issue-212-read-only-values read-only-values.cpp) +target_link_libraries(issue-212-read-only-values nlohmann_json_schema_validator) +add_test(NAME issue-212-read-only-values COMMAND issue-212-read-only-values) diff --git a/test/read-only-values.cpp b/test/read-only-values.cpp new file mode 100644 index 0000000..0adeb7c --- /dev/null +++ b/test/read-only-values.cpp @@ -0,0 +1,23 @@ +#include + +using nlohmann::json; +using nlohmann::json_schema::json_validator; + +static const json read_only_schema = R"({ + "type": "object", + "properties": { + "debug": { + "type": "boolean", + "default": false, + "readOnly": true + } + } +})"; + +int main() { + json_validator validator(read_only_schema); + validator.validate(R"({ + "debug": true + })"); + return 0; +} \ No newline at end of file From 72256c3368aedd1314bcee43c6a50f58f7d6deda Mon Sep 17 00:00:00 2001 From: opsocket Date: Fri, 19 Aug 2022 18:33:24 -0400 Subject: [PATCH 3/4] =?UTF-8?q?=E2=9C=85=20fix=20test=20for=20read=20only?= =?UTF-8?q?=20values?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/read-only-values.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/test/read-only-values.cpp b/test/read-only-values.cpp index 0adeb7c..225c5f4 100644 --- a/test/read-only-values.cpp +++ b/test/read-only-values.cpp @@ -16,8 +16,13 @@ static const json read_only_schema = R"({ int main() { json_validator validator(read_only_schema); - validator.validate(R"({ - "debug": true - })"); - return 0; + try { + validator.validate(R"({ + "debug": true + })"); + } catch (const std::exception&e ) { + return EXIT_SUCCESS; + } + + return EXIT_FAILURE; } \ No newline at end of file From d224111591e4f8481980cc6486c2f7563e8c981c Mon Sep 17 00:00:00 2001 From: opsocket Date: Fri, 19 Aug 2022 19:37:37 -0400 Subject: [PATCH 4/4] =?UTF-8?q?=E2=9C=85=20fix=20test=20for=20read=20only?= =?UTF-8?q?=20values?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/read-only-values.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/read-only-values.cpp b/test/read-only-values.cpp index 225c5f4..4f3c28c 100644 --- a/test/read-only-values.cpp +++ b/test/read-only-values.cpp @@ -12,17 +12,17 @@ static const json read_only_schema = R"({ "readOnly": true } } -})"; +})"_json; int main() { json_validator validator(read_only_schema); try { validator.validate(R"({ "debug": true - })"); + })"_json); } catch (const std::exception&e ) { return EXIT_SUCCESS; } return EXIT_FAILURE; -} \ No newline at end of file +}