Skip to content

WIP converting all static strings over to draft 2020-12 #343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "json-schema-spec"]
path = json-schema-spec/2020-12
url = ../../json-schema-org/json-schema-spec.git
[submodule "json-schema-spec_2019-09"]
path = json-schema-spec/2019-09
url = ../../json-schema-org/json-schema-spec.git
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.14)
cmake_minimum_required(VERSION 3.19)
# CMake version compatibility
# TODO: Remove when bumping cmake >= 3.25
if (POLICY CMP0140)
Expand Down Expand Up @@ -151,6 +151,9 @@ add_subdirectory(src)

# Enable examples

# Convert meta-schema into strings and include into build
add_subdirectory(json-schema-spec)

# Enable testings
if (JSON_VALIDATOR_BUILD_TESTS)
enable_testing()
Expand All @@ -161,7 +164,6 @@ if (JSON_VALIDATOR_BUILD_EXAMPLES)
add_subdirectory(example)
endif ()


#[==============================================================================================[
# Install or Export #
]==============================================================================================]
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

This is a C++ library for validating JSON documents based on a
[JSON Schema](http://json-schema.org/) which itself should validate with
[draft-7 of JSON Schema Validation](http://json-schema.org/schema).
[draft 2020-12 of JSON Schema Validation](http://json-schema.org/schema).

First a disclaimer: *It is work in progress and
contributions or hints or discussions are welcome.*
Expand All @@ -24,7 +24,7 @@ Although significant changes have been done for the 2nd version
(a complete rewrite) the API is compatible with the 1.0.0 release. Except for
the namespace which is now `nlohmann::json_schema`.

Version **2** supports JSON schema draft 7, whereas 1 was supporting draft 4
Version **2** supports JSON schema draft 2020-12, whereas 1 was supporting draft 4
only. Please update your schemas.

The primary change in 2 is the way a schema is used. While in version 1 the schema was
Expand Down Expand Up @@ -170,7 +170,7 @@ using nlohmann::json_schema::json_validator;
// The schema is defined based upon a string literal
static json person_schema = R"(
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft/2020-12/schema",
"title": "A person",
"properties": {
"name": {
Expand Down Expand Up @@ -314,7 +314,7 @@ using nlohmann::json_schema::json_validator;

static const json rectangle_schema = R"(
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft/2020-12/schema",
"title": "A rectangle",
"properties": {
"width": {
Expand Down Expand Up @@ -353,7 +353,7 @@ int main()

The example above will output the specified default values `{"height":10,"width":20}` to stdout.

> Note that the default value specified in a `$ref` may be overridden by the current instance location. Also note that this behavior will break draft-7, but it is compliant to newer drafts (e.g. `2019-09` or `2020-12`).
> Note that the default value specified in a `$ref` may be overridden by the current instance location. Also note that this behavior will break draft 2020-12, but it is compliant to newer drafts (e.g. `2019-09` or `2020-12`).

# Contributing

Expand Down
2 changes: 1 addition & 1 deletion example/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using nlohmann::json_schema::json_validator;
// The schema is defined based upon a string literal
static json uri_schema = R"(
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"myUri": {
Expand Down
2 changes: 1 addition & 1 deletion example/readme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using nlohmann::json_schema::json_validator;
// The schema is defined based upon a string literal
static json person_schema = R"(
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft/2020-12/schema",
"title": "A person",
"properties": {
"name": {
Expand Down
1 change: 1 addition & 0 deletions json-schema-spec/2019-09
Submodule 2019-09 added at c8eb3d
1 change: 1 addition & 0 deletions json-schema-spec/2020-12
Submodule 2020-12 added at 0d2e45
44 changes: 44 additions & 0 deletions json-schema-spec/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#[==============================================================================================[
# Meta-schema Generation #
]==============================================================================================]

function (addSchemaFileToBuild META_SCHEMA_PATH)
file(READ ${META_SCHEMA_PATH} FILE_CONTENT)
string(JSON META_SCHEMA_ID GET ${FILE_CONTENT} "$id")
string(REPLACE "(" "-^-(" FILE_CONTENT "${FILE_CONTENT}") # work around the reality that there may be '(' within strings
string(REPLACE ")" ")-^-" FILE_CONTENT "${FILE_CONTENT}") # work around the reality that there may be ')' within strings
#string(REPLACE "\n" ")\"\nR\"(" FILE_CONTENT "${FILE_CONTENT}") # break lines into separate strings so that they are not too long for VS
set(SOURCES ${SOURCES} PARENT_SCOPE)
file (APPEND ${SCHEMA_CPP_FILE_NAME} " {\"${META_SCHEMA_ID}\",\nR\"(")
file (APPEND ${SCHEMA_CPP_FILE_NAME} ${FILE_CONTENT})
file (APPEND ${SCHEMA_CPP_FILE_NAME} ")\"_json},\n")
endfunction()

# this logic is such that the schema header file will only be generated once; until a "clean"
set(SCHEMA_BASE_FILE_NAME builtin_schema_map)
set(SCHEMA_BASE_FILE_PATH ${CMAKE_CURRENT_BINARY_DIR}/${SCHEMA_BASE_FILE_NAME})
set(SCHEMA_HEADER_FILE_NAME ${SCHEMA_BASE_FILE_PATH}.h)
set(SCHEMA_CPP_FILE_NAME ${SCHEMA_BASE_FILE_PATH}.cpp)
if (EXISTS ${SCHEMA_HEADER_FILE_NAME})
message("Not re-generating meta schema source files")
else()
file (WRITE ${SCHEMA_HEADER_FILE_NAME} "#include <map>\n#include <string>\n#include <nlohmann/json.hpp>\nnamespace nlohmann {\n namespace json_schema {\n const extern std::map<std::string,json> builtin_schema_map;\n }\n}\n")
file (WRITE ${SCHEMA_CPP_FILE_NAME} "#include \"${SCHEMA_BASE_FILE_NAME}.h\"\nnamespace nlohmann {\n namespace json_schema {\n const std::map<std::string,json> builtin_schema_map = {\n")
file (GLOB META_SCHEMA_PATHS
2019-09/meta/*.json
2020-12/meta/*.json
)
LIST(APPEND META_SCHEMA_PATHS
2019-09/schema.json
2020-12/schema.json
)
message("META_SCHEMA_PATHS = ${META_SCHEMA_PATHS}")
foreach(ext_json_path ${META_SCHEMA_PATHS})
addSchemaFileToBuild(${ext_json_path} APPEND) # subsequent invocations of addSchemaFileToBuild(), use APPEND
endforeach()
file (APPEND ${SCHEMA_CPP_FILE_NAME} " };\n }\n}\n")
endif()

target_sources(nlohmann_json_schema_validator PRIVATE ${SCHEMA_CPP_FILE_NAME})
target_include_directories(nlohmann_json_schema_validator PRIVATE ${CMAKE_BINARY_DIR}/json-schema-spec)

11 changes: 11 additions & 0 deletions json-schema-spec/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# JSON Schema Spec

This directory contains several dialects of the JSON, also known
as meta-schema.
This is done using git submodules, so that the upstream version
can be tracked and updated easily.

The `2019-09` dialect is not really supported, nor is it intended
to be.
However, it is included here as an example of how multiple dialects
will be supported, looking forward to the next release.
168 changes: 0 additions & 168 deletions schema

This file was deleted.

1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
target_sources(nlohmann_json_schema_validator PRIVATE
smtp-address-validator.cpp
json-schema-draft7.json.cpp
json-uri.cpp
json-validator.cpp
json-patch.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/json-patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace
// with fixes
const nlohmann::json patch_schema = R"patch({
"title": "JSON schema for JSONPatch files",
"$schema": "http://json-schema.org/draft-04/schema#",
"$schema": "http://json-schema.org/draft/2020-12/schema",
"type": "array",

"items": {
Expand Down
Loading