rj_schema 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fb2182b7542e9cddebeff0ff48ebb1ab8a7bf1b6469c1d8e6d4fd4ae2fa5c427
4
- data.tar.gz: 773e1dc3ca67f29f37fe466d9cb07e56ebf400394bb6f294642cc917283b6cd4
3
+ metadata.gz: 63c4400e2d664fec17f2c334e8a73d1ecb016fdc741770445a2da3dc11fa2663
4
+ data.tar.gz: c053fcfb64541aa5cc5117b10607e07e2bc358a81cf3b6d05da7f1d1623b2165
5
5
  SHA512:
6
- metadata.gz: 4057db9510a3eb7276b7d4d408b6ac74c6806290fad5fae52581987ca1e5be6c23efe5885f1c773a509260c5d45c627e0a93c942a5fc4e862d514df20944c259
7
- data.tar.gz: f4e4d2e501f4e5d1c882fadd192e14514425153d7bb75554879dc79b9c66d317785591b512d7daaa44b0d47169fd010ebd4bb212b2aad11a7590f448080aa4fe
6
+ metadata.gz: c54993fbfdfe94fc4b9d4fb2bfde3608aaaeb61fd3cc944dc5cbe72b3a5d1bf29418e93816e883993ce9750e454d6bf426fa72daabd3055758de65b0dbb1e34f
7
+ data.tar.gz: 6c3295339208e03a8d4ddf3c1d4f81c39ae90514dc1c41c240c7395d534520a482c26922632f9f1204e3f4c56f46918af4802f61232e43ad09219a0a103c4ca4
@@ -10,16 +10,6 @@
10
10
 
11
11
  #include <unordered_map>
12
12
  #include <sstream>
13
- #include <ruby.h>
14
- #include <ruby/version.h>
15
-
16
- #if (RUBY_API_VERSION_MAJOR > 2 || (RUBY_API_VERSION_MAJOR == 2 && RUBY_API_VERSION_MINOR >= 7)) && !defined(TRUFFLERUBY)
17
- namespace std {
18
- static inline void* ruby_nonempty_memcpy(void *dest, const void *src, size_t n) {
19
- return ::ruby_nonempty_memcpy(dest, src, n);
20
- }
21
- };
22
- #endif
23
13
 
24
14
  #define RAPIDJSON_SCHEMA_USE_INTERNALREGEX 0
25
15
  #define RAPIDJSON_SCHEMA_USE_STDREGEX 1
@@ -30,6 +20,11 @@ namespace std {
30
20
  #include <rapidjson/error/error.h>
31
21
  #include <rapidjson/error/en.h>
32
22
 
23
+ #include <ruby.h>
24
+ #include <ruby/version.h>
25
+
26
+ #include <cstdio>
27
+
33
28
  typedef rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator> ErrorType;
34
29
  typedef std::unordered_map<std::string, VALUE> SchemaInput;
35
30
  typedef std::unordered_map<ID, rapidjson::SchemaDocument> SchemaCollection;
@@ -263,6 +258,55 @@ VALUE perform_validation(VALUE self, VALUE schema_arg, VALUE document_arg, VALUE
263
258
  }
264
259
  }
265
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
+
266
310
  extern "C" VALUE validator_validate(int argc, VALUE* argv, VALUE self) {
267
311
  VALUE schema_arg, document_arg, opts;
268
312
 
@@ -287,6 +331,10 @@ extern "C" VALUE validator_valid(VALUE self, VALUE schema_arg, VALUE document_ar
287
331
  return perform_validation(self, schema_arg, document_arg, Qfalse, Qfalse, Qfalse);
288
332
  }
289
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
+
290
338
  extern "C" void validator_free(SchemaManager* schema_manager) {
291
339
  delete schema_manager;
292
340
  }
@@ -337,4 +385,5 @@ extern "C" void Init_rj_schema(void) {
337
385
  rb_define_method(cValidator, "initialize", reinterpret_cast<VALUE(*)(...)>(validator_initialize), -1);
338
386
  rb_define_method(cValidator, "validate", reinterpret_cast<VALUE(*)(...)>(validator_validate), -1);
339
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);
340
389
  }
data/lib/rj_schema.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'json'
2
2
 
3
3
  class RjSchema
4
- VERSION = '1.0.3'
4
+ VERSION = '1.0.5'
5
5
  end
6
6
 
7
7
  require 'rj_schema/rj_schema'
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.3
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: 2021-09-23 00:00:00.000000000 Z
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.2.22
751
+ rubygems_version: 3.3.26
752
752
  signing_key:
753
753
  specification_version: 4
754
754
  summary: JSON schema validation with RapidJSON