Skip to content

Commit 59325dc

Browse files
committed
add support for validating vns format json through request
1 parent b356b17 commit 59325dc

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

vns-cpp/schema.hpp

+26
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,32 @@ const std::string VNS_SCHEMA = R"(
5353
},
5454
"type": "array"
5555
},
56+
"events": {
57+
"items": {
58+
"properties": {
59+
"target": {
60+
"type": "string"
61+
},
62+
"type": {
63+
"type": "string"
64+
},
65+
"value": {
66+
"type": [
67+
"string",
68+
"number",
69+
"boolean"
70+
]
71+
}
72+
},
73+
"required": [
74+
"target",
75+
"type",
76+
"value"
77+
],
78+
"type": "object"
79+
},
80+
"type": "array"
81+
},
5682
"narrator": {
5783
"type": "string"
5884
},

vns-python/test_server.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import json
2+
from typing import Any
23

34
import requests
45

6+
# url for the site to make api call
7+
api_site: str = "https://api.vns.wiki"
8+
59
# url for getting vns schema
6-
schema_url: str = "https://api.vns.wiki/get/schema"
10+
schema_url: str = f"{api_site}/get/schema"
711

812
# send request
913
schema_response: requests.Response = requests.get(schema_url)
@@ -78,13 +82,30 @@
7882
"""
7983

8084
# url for compiling raw vns script
81-
compiler_url: str = "https://api.vns.wiki/post/compile"
85+
compiler_url: str = f"{api_site}/post/compile"
8286

8387
# send request
8488
compiler_response: requests.Response = requests.post(
8589
compiler_url, json={"data": raw_vns_content}
8690
)
8791

92+
# the json data get from compiling the raw vns script
93+
json_compiled: dict[str, Any] = json.loads(compiler_response.text)
94+
8895
# show result
8996
print("\nVNS online service responded compiler request with:")
90-
print(json.loads(compiler_response.text))
97+
print(json_compiled)
98+
99+
# url for validating compiled vns json file
100+
validation_url: str = f"{api_site}/post/validate"
101+
102+
# send request
103+
validation_response: requests.Response = requests.post(
104+
validation_url, json=json_compiled
105+
)
106+
107+
# obtain validation result
108+
validation_result: dict[str, bool] = json.loads(validation_response.text)
109+
110+
# assert the result is True
111+
assert validation_result.get("result", False) is True

vns-server/server.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "extern/vns-cpp/compiler.hpp"
33
#include "extern/vns-cpp/schema.hpp"
44
#include "extern/vns-cpp/naming.hpp"
5+
#include "extern/vns-cpp/validator.hpp"
56

67
// HTTP
78
int main(const int argc, char **argv)
@@ -50,7 +51,8 @@ int main(const int argc, char **argv)
5051
Naming::update_database(req_j["namings"].get<std::unordered_map<std::string, std::vector<std::string>>>());
5152

5253
// compile raw vns data
53-
const std::string compiled = Compiler::load_as_string(req_j["data"].get<std::string>());
54+
const std::string compiled = req_j.contains("data") ? Compiler::load_as_string(req_j["data"].get<std::string>())
55+
: std::string();
5456

5557
if (DEBUG)
5658
std::cout << "result: " << compiled << std::endl;
@@ -59,6 +61,16 @@ int main(const int argc, char **argv)
5961
res.set_content(compiled, "application/json");
6062
});
6163

64+
// Define a route for handling vns format json validation request
65+
svr.Post("/post/validate", [](const httplib::Request &req, httplib::Response &res) {
66+
// Respond with result
67+
const nlohmann::json response = {
68+
{"result", !req.body.empty() && Validator::validate(nlohmann::json::parse(req.body))}};
69+
70+
// return result
71+
res.set_content(response.dump(), "application/json");
72+
});
73+
6274
// Define a route for handling get schema request
6375
svr.Get("/get/schema", [](const httplib::Request &, httplib::Response &res) {
6476
res.set_content(VNS_SCHEMA, "application/json");

0 commit comments

Comments
 (0)