rj_schema 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/rj_schema/rj_schema.cpp +56 -0
- data/lib/rj_schema.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 63c4400e2d664fec17f2c334e8a73d1ecb016fdc741770445a2da3dc11fa2663
|
4
|
+
data.tar.gz: c053fcfb64541aa5cc5117b10607e07e2bc358a81cf3b6d05da7f1d1623b2165
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c54993fbfdfe94fc4b9d4fb2bfde3608aaaeb61fd3cc944dc5cbe72b3a5d1bf29418e93816e883993ce9750e454d6bf426fa72daabd3055758de65b0dbb1e34f
|
7
|
+
data.tar.gz: 6c3295339208e03a8d4ddf3c1d4f81c39ae90514dc1c41c240c7395d534520a482c26922632f9f1204e3f4c56f46918af4802f61232e43ad09219a0a103c4ca4
|
data/ext/rj_schema/rj_schema.cpp
CHANGED
@@ -23,6 +23,8 @@
|
|
23
23
|
#include <ruby.h>
|
24
24
|
#include <ruby/version.h>
|
25
25
|
|
26
|
+
#include <cstdio>
|
27
|
+
|
26
28
|
typedef rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator> ErrorType;
|
27
29
|
typedef std::unordered_map<std::string, VALUE> SchemaInput;
|
28
30
|
typedef std::unordered_map<ID, rapidjson::SchemaDocument> SchemaCollection;
|
@@ -256,6 +258,55 @@ VALUE perform_validation(VALUE self, VALUE schema_arg, VALUE document_arg, VALUE
|
|
256
258
|
}
|
257
259
|
}
|
258
260
|
|
261
|
+
VALUE perform_sax_validation(VALUE self, VALUE schema_arg, VALUE file_arg) {
|
262
|
+
SchemaManager* schema_manager;
|
263
|
+
Data_Get_Struct(self, SchemaManager, schema_manager);
|
264
|
+
|
265
|
+
auto validate = [&file_arg](const rapidjson::SchemaDocument& schema) -> VALUE {
|
266
|
+
rapidjson::SchemaValidator validator(schema);
|
267
|
+
char buffer[4096];
|
268
|
+
FILE* fp = std::fopen(StringValuePtr(file_arg), "r");
|
269
|
+
|
270
|
+
if (!fp) {
|
271
|
+
rb_raise(rb_eArgError, "file not found: %s", StringValuePtr(file_arg));
|
272
|
+
return -1;
|
273
|
+
}
|
274
|
+
|
275
|
+
rapidjson::Reader reader;
|
276
|
+
rapidjson::FileReadStream is(fp, buffer, sizeof(buffer));
|
277
|
+
|
278
|
+
if (!reader.Parse(is, validator)) {
|
279
|
+
if (validator.IsValid()) {
|
280
|
+
std::fclose(fp);
|
281
|
+
return Qtrue;
|
282
|
+
} else {
|
283
|
+
std::fclose(fp);
|
284
|
+
return Qfalse;
|
285
|
+
}
|
286
|
+
}
|
287
|
+
|
288
|
+
std::fclose(fp);
|
289
|
+
return Qtrue;
|
290
|
+
};
|
291
|
+
|
292
|
+
if (SYMBOL_P(schema_arg)) {
|
293
|
+
auto it = schema_manager->collection.find(SYM2ID(schema_arg));
|
294
|
+
if (it == schema_manager->collection.end())
|
295
|
+
rb_raise(rb_eArgError, "schema not found: %s", rb_id2name(SYM2ID(schema_arg)));
|
296
|
+
return validate(it->second);
|
297
|
+
}
|
298
|
+
else {
|
299
|
+
auto schema = rapidjson::SchemaDocument(
|
300
|
+
parse_document(schema_arg),
|
301
|
+
0,
|
302
|
+
0,
|
303
|
+
&schema_manager->provider
|
304
|
+
);
|
305
|
+
return validate(schema);
|
306
|
+
}
|
307
|
+
}
|
308
|
+
|
309
|
+
|
259
310
|
extern "C" VALUE validator_validate(int argc, VALUE* argv, VALUE self) {
|
260
311
|
VALUE schema_arg, document_arg, opts;
|
261
312
|
|
@@ -280,6 +331,10 @@ extern "C" VALUE validator_valid(VALUE self, VALUE schema_arg, VALUE document_ar
|
|
280
331
|
return perform_validation(self, schema_arg, document_arg, Qfalse, Qfalse, Qfalse);
|
281
332
|
}
|
282
333
|
|
334
|
+
extern "C" VALUE validator_sax_valid(VALUE self, VALUE schema_arg, VALUE file_arg) {
|
335
|
+
return perform_sax_validation(self, schema_arg, file_arg);
|
336
|
+
}
|
337
|
+
|
283
338
|
extern "C" void validator_free(SchemaManager* schema_manager) {
|
284
339
|
delete schema_manager;
|
285
340
|
}
|
@@ -330,4 +385,5 @@ extern "C" void Init_rj_schema(void) {
|
|
330
385
|
rb_define_method(cValidator, "initialize", reinterpret_cast<VALUE(*)(...)>(validator_initialize), -1);
|
331
386
|
rb_define_method(cValidator, "validate", reinterpret_cast<VALUE(*)(...)>(validator_validate), -1);
|
332
387
|
rb_define_method(cValidator, "valid?", reinterpret_cast<VALUE(*)(...)>(validator_valid), 2);
|
388
|
+
rb_define_method(cValidator, "sax_valid?", reinterpret_cast<VALUE(*)(...)>(validator_sax_valid), 2);
|
333
389
|
}
|
data/lib/rj_schema.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rj_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christian Semmler
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-02-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake-compiler
|
@@ -748,7 +748,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
748
748
|
- !ruby/object:Gem::Version
|
749
749
|
version: '0'
|
750
750
|
requirements: []
|
751
|
-
rubygems_version: 3.
|
751
|
+
rubygems_version: 3.3.26
|
752
752
|
signing_key:
|
753
753
|
specification_version: 4
|
754
754
|
summary: JSON schema validation with RapidJSON
|