jsonschema_rs 0.62.0 → 0.63.1
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 +4 -4
- data/CHANGELOG.md +28 -1
- data/Cargo.toml +2 -2
- data/README.md +30 -0
- data/ext/jsonschema/Cargo.lock +324 -354
- data/ext/jsonschema/Cargo.toml +3 -3
- data/lib/jsonschema/version.rb +1 -1
- data/sig/jsonschema.rbs +54 -7
- data/src/json_text.rs +239 -0
- data/src/lib.rs +102 -35
- data/src/options.rs +177 -34
- data/src/ser.rs +1 -1
- metadata +3 -2
data/src/options.rs
CHANGED
|
@@ -10,15 +10,18 @@ use magnus::{
|
|
|
10
10
|
prelude::*,
|
|
11
11
|
scan_args::{get_kwargs, scan_args, KwArgs},
|
|
12
12
|
value::Opaque,
|
|
13
|
-
Error, Exception, RArray, RHash, RModule, Ruby, Symbol, TryConvert, Value,
|
|
13
|
+
DataTypeFunctions, Error, Exception, RArray, RHash, RModule, Ruby, Symbol, TryConvert, Value,
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
use jsonschema::json::{
|
|
16
|
+
use jsonschema::json::{
|
|
17
|
+
magnus_invalidate_members_cache, Json, JsonText, JsonTextNode, Magnus, RbNode,
|
|
18
|
+
};
|
|
17
19
|
|
|
18
20
|
use crate::{
|
|
21
|
+
json_text::text_node_to_ruby,
|
|
19
22
|
registry::Registry,
|
|
20
23
|
retriever::{make_retriever, DetachedRetriever, RubyRetriever},
|
|
21
|
-
ser::{map_to_ruby, value_to_ruby},
|
|
24
|
+
ser::{map_to_ruby, number_to_ruby, value_to_ruby},
|
|
22
25
|
static_id::{define_rb_intern, StaticId},
|
|
23
26
|
CUSTOM_KEYWORD_CAUSE, LAST_CALLBACK_ERROR,
|
|
24
27
|
};
|
|
@@ -59,9 +62,9 @@ define_rb_intern!(static SYM_CALL: "call");
|
|
|
59
62
|
define_rb_intern!(static SYM_NEW: "new");
|
|
60
63
|
define_rb_intern!(static SYM_VALIDATE: "validate");
|
|
61
64
|
|
|
62
|
-
pub struct ParsedOptions<'i> {
|
|
65
|
+
pub struct ParsedOptions<'i, F: Json = Magnus> {
|
|
63
66
|
pub mask: Option<String>,
|
|
64
|
-
pub options: jsonschema::ValidationOptions<'i, Arc<dyn jsonschema::Retrieve>,
|
|
67
|
+
pub options: jsonschema::ValidationOptions<'i, Arc<dyn jsonschema::Retrieve>, F>,
|
|
65
68
|
pub registry: Option<&'i jsonschema::Registry<'static>>,
|
|
66
69
|
pub retriever: Option<RubyRetriever>,
|
|
67
70
|
pub callback_roots: CallbackRoots,
|
|
@@ -371,8 +374,18 @@ struct RubyFormatChecker {
|
|
|
371
374
|
impl RubyFormatChecker {
|
|
372
375
|
fn check(&self, value: &str) -> bool {
|
|
373
376
|
let ruby = Ruby::get().expect("Ruby VM should be initialized");
|
|
374
|
-
|
|
375
|
-
|
|
377
|
+
Self::outcome(ruby.get_inner(self.proc).call((value,)))
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
fn check_number(&self, number: &serde_json::Number) -> bool {
|
|
381
|
+
let ruby = Ruby::get().expect("Ruby VM should be initialized");
|
|
382
|
+
Self::outcome(
|
|
383
|
+
number_to_ruby(&ruby, number)
|
|
384
|
+
.and_then(|number| ruby.get_inner(self.proc).call((number,))),
|
|
385
|
+
)
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
fn outcome(result: Result<bool, Error>) -> bool {
|
|
376
389
|
magnus_invalidate_members_cache();
|
|
377
390
|
match result {
|
|
378
391
|
Ok(v) => v,
|
|
@@ -386,13 +399,74 @@ impl RubyFormatChecker {
|
|
|
386
399
|
}
|
|
387
400
|
}
|
|
388
401
|
|
|
402
|
+
/// A `format` check that reads numbers, made by `JSONSchema.number_format`.
|
|
403
|
+
#[derive(magnus::TypedData)]
|
|
404
|
+
#[magnus(class = "JSONSchema::NumberFormat", free_immediately, mark)]
|
|
405
|
+
pub struct NumberFormat {
|
|
406
|
+
check: Opaque<Value>,
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
impl DataTypeFunctions for NumberFormat {
|
|
410
|
+
fn mark(&self, marker: &magnus::gc::Marker) {
|
|
411
|
+
marker.mark(self.check);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
fn number_format(ruby: &Ruby, check: Value) -> Result<NumberFormat, Error> {
|
|
416
|
+
let responds_to_call: bool = check.funcall("respond_to?", (SYM_CALL.to_symbol(),))?;
|
|
417
|
+
if !responds_to_call {
|
|
418
|
+
return Err(Error::new(
|
|
419
|
+
ruby.exception_type_error(),
|
|
420
|
+
"number_format expects a callable (Proc or Lambda)",
|
|
421
|
+
));
|
|
422
|
+
}
|
|
423
|
+
Ok(NumberFormat {
|
|
424
|
+
check: Opaque::from(check),
|
|
425
|
+
})
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/// known_formats(draft, formats: nil) -> Array[String]
|
|
429
|
+
///
|
|
430
|
+
/// The names of the formats with a check under `draft`, sorted and without duplicates: the
|
|
431
|
+
/// built-in formats and those in `formats`.
|
|
432
|
+
fn known_formats(ruby: &Ruby, args: &[Value]) -> Result<Vec<String>, Error> {
|
|
433
|
+
let parsed_args = scan_args::<(Value,), (), (), (), _, ()>(args)?;
|
|
434
|
+
let (draft,) = parsed_args.required;
|
|
435
|
+
let draft = parse_draft_symbol(ruby, draft)?;
|
|
436
|
+
let kw: KwArgs<(), (Option<Option<RHash>>,), ()> =
|
|
437
|
+
get_kwargs(parsed_args.keywords, &[], &[*KW_FORMATS])?;
|
|
438
|
+
let parsed = make_options_from_kwargs::<Magnus>(
|
|
439
|
+
ruby,
|
|
440
|
+
None,
|
|
441
|
+
None,
|
|
442
|
+
None,
|
|
443
|
+
None,
|
|
444
|
+
None,
|
|
445
|
+
None,
|
|
446
|
+
kw.optional.0.flatten(),
|
|
447
|
+
None,
|
|
448
|
+
None,
|
|
449
|
+
None,
|
|
450
|
+
None,
|
|
451
|
+
None,
|
|
452
|
+
None,
|
|
453
|
+
None,
|
|
454
|
+
)?;
|
|
455
|
+
Ok(parsed
|
|
456
|
+
.options
|
|
457
|
+
.known_formats(draft)
|
|
458
|
+
.into_iter()
|
|
459
|
+
.map(str::to_owned)
|
|
460
|
+
.collect())
|
|
461
|
+
}
|
|
462
|
+
|
|
389
463
|
/// Wrapper for a Ruby custom keyword validator factory.
|
|
390
464
|
struct RubyKeywordFactory {
|
|
391
465
|
class: Opaque<Value>,
|
|
392
466
|
}
|
|
393
467
|
|
|
394
468
|
/// Wrapper for a Ruby custom keyword validator instance.
|
|
395
|
-
struct RubyKeyword {
|
|
469
|
+
pub struct RubyKeyword {
|
|
396
470
|
instance: Opaque<Value>,
|
|
397
471
|
}
|
|
398
472
|
|
|
@@ -403,13 +477,14 @@ fn node_to_ruby(node: RbNode<'_>) -> Value {
|
|
|
403
477
|
}
|
|
404
478
|
}
|
|
405
479
|
|
|
406
|
-
impl
|
|
407
|
-
fn
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
480
|
+
impl RubyKeyword {
|
|
481
|
+
fn validate_object<'i>(
|
|
482
|
+
&self,
|
|
483
|
+
ruby: &Ruby,
|
|
484
|
+
instance: Value,
|
|
485
|
+
) -> Result<(), jsonschema::ValidationError<'i>> {
|
|
411
486
|
let keyword = ruby.get_inner(self.instance);
|
|
412
|
-
let result: Result<Value, _> = keyword.funcall("validate", (
|
|
487
|
+
let result: Result<Value, _> = keyword.funcall("validate", (instance,));
|
|
413
488
|
magnus_invalidate_members_cache();
|
|
414
489
|
match result {
|
|
415
490
|
Ok(_) => Ok(()),
|
|
@@ -423,28 +498,25 @@ impl<'i> jsonschema::Keyword<'i, Magnus> for RubyKeyword {
|
|
|
423
498
|
}
|
|
424
499
|
}
|
|
425
500
|
|
|
426
|
-
fn
|
|
427
|
-
let
|
|
428
|
-
let
|
|
429
|
-
let inst = ruby.get_inner(self.instance);
|
|
430
|
-
let result: Result<Value, _> = inst.funcall("validate", (rb_instance,));
|
|
501
|
+
fn is_valid_object(&self, ruby: &Ruby, instance: Value) -> bool {
|
|
502
|
+
let keyword = ruby.get_inner(self.instance);
|
|
503
|
+
let result: Result<Value, _> = keyword.funcall("validate", (instance,));
|
|
431
504
|
magnus_invalidate_members_cache();
|
|
432
505
|
result.is_ok()
|
|
433
506
|
}
|
|
434
507
|
|
|
435
|
-
fn
|
|
508
|
+
fn iter_errors_object<'i>(
|
|
436
509
|
&self,
|
|
437
|
-
|
|
510
|
+
ruby: &Ruby,
|
|
511
|
+
instance: Value,
|
|
438
512
|
) -> Box<dyn Iterator<Item = jsonschema::ValidationError<'i>> + 'i> {
|
|
439
|
-
let ruby = Ruby::get().expect("Ruby VM should be initialized");
|
|
440
513
|
let keyword = ruby.get_inner(self.instance);
|
|
441
514
|
// Without an `iter_errors` method, fall back to the single-error `validate` path.
|
|
442
515
|
if !keyword.respond_to("iter_errors", false).unwrap_or(false) {
|
|
443
|
-
return Box::new(self.
|
|
516
|
+
return Box::new(self.validate_object(ruby, instance).err().into_iter());
|
|
444
517
|
}
|
|
445
|
-
let rb_instance = node_to_ruby(instance);
|
|
446
518
|
let array = keyword
|
|
447
|
-
.funcall::<_, _, Value>("iter_errors", (
|
|
519
|
+
.funcall::<_, _, Value>("iter_errors", (instance,))
|
|
448
520
|
.and_then(|result| result.funcall::<_, _, RArray>("to_a", ()));
|
|
449
521
|
magnus_invalidate_members_cache();
|
|
450
522
|
let array = match array {
|
|
@@ -463,7 +535,7 @@ impl<'i> jsonschema::Keyword<'i, Magnus> for RubyKeyword {
|
|
|
463
535
|
})
|
|
464
536
|
.and_then(|index| array.entry::<Value>(index));
|
|
465
537
|
let error = match item {
|
|
466
|
-
Ok(item) => ruby_value_to_error(
|
|
538
|
+
Ok(item) => ruby_value_to_error(ruby, item),
|
|
467
539
|
Err(error) => error,
|
|
468
540
|
};
|
|
469
541
|
errors.push(custom_error_from_ruby(error));
|
|
@@ -472,8 +544,55 @@ impl<'i> jsonschema::Keyword<'i, Magnus> for RubyKeyword {
|
|
|
472
544
|
}
|
|
473
545
|
}
|
|
474
546
|
|
|
547
|
+
impl<'i> jsonschema::Keyword<'i, Magnus> for RubyKeyword {
|
|
548
|
+
fn validate(&self, instance: RbNode<'i>) -> Result<(), jsonschema::ValidationError<'i>> {
|
|
549
|
+
let ruby = Ruby::get().expect("Ruby VM should be initialized");
|
|
550
|
+
self.validate_object(&ruby, node_to_ruby(instance))
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
fn is_valid(&self, instance: RbNode<'i>) -> bool {
|
|
554
|
+
let ruby = Ruby::get().expect("Ruby VM should be initialized");
|
|
555
|
+
self.is_valid_object(&ruby, node_to_ruby(instance))
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
fn iter_errors(
|
|
559
|
+
&self,
|
|
560
|
+
instance: RbNode<'i>,
|
|
561
|
+
) -> Box<dyn Iterator<Item = jsonschema::ValidationError<'i>> + 'i> {
|
|
562
|
+
let ruby = Ruby::get().expect("Ruby VM should be initialized");
|
|
563
|
+
self.iter_errors_object(&ruby, node_to_ruby(instance))
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// The keyword class reads Ruby objects, so a node of JSON text becomes one first.
|
|
568
|
+
impl<'i> jsonschema::Keyword<'i, JsonText> for RubyKeyword {
|
|
569
|
+
fn validate(&self, instance: JsonTextNode<'i>) -> Result<(), jsonschema::ValidationError<'i>> {
|
|
570
|
+
let ruby = Ruby::get().expect("Ruby VM should be initialized");
|
|
571
|
+
match text_node_to_ruby(&ruby, &instance) {
|
|
572
|
+
Ok(object) => self.validate_object(&ruby, object),
|
|
573
|
+
Err(error) => Err(custom_error_from_ruby(error)),
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
fn is_valid(&self, instance: JsonTextNode<'i>) -> bool {
|
|
578
|
+
let ruby = Ruby::get().expect("Ruby VM should be initialized");
|
|
579
|
+
text_node_to_ruby(&ruby, &instance).is_ok_and(|object| self.is_valid_object(&ruby, object))
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
fn iter_errors(
|
|
583
|
+
&self,
|
|
584
|
+
instance: JsonTextNode<'i>,
|
|
585
|
+
) -> Box<dyn Iterator<Item = jsonschema::ValidationError<'i>> + 'i> {
|
|
586
|
+
let ruby = Ruby::get().expect("Ruby VM should be initialized");
|
|
587
|
+
match text_node_to_ruby(&ruby, &instance) {
|
|
588
|
+
Ok(object) => self.iter_errors_object(&ruby, object),
|
|
589
|
+
Err(error) => Box::new(std::iter::once(custom_error_from_ruby(error))),
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
475
594
|
// Wrap a yielded Ruby value as an `Error`, preserving exception instances so they surface as
|
|
476
|
-
// `#cause`; a non-exception
|
|
595
|
+
// `#cause`; a non-exception becomes a `RuntimeError` holding its text.
|
|
477
596
|
fn ruby_value_to_error(ruby: &Ruby, value: Value) -> Error {
|
|
478
597
|
if let Ok(exception) = Exception::try_convert(value) {
|
|
479
598
|
return Error::from(exception);
|
|
@@ -481,7 +600,13 @@ fn ruby_value_to_error(ruby: &Ruby, value: Value) -> Error {
|
|
|
481
600
|
let message = value
|
|
482
601
|
.funcall::<_, _, String>("to_s", ())
|
|
483
602
|
.unwrap_or_else(|_| "custom keyword error".to_string());
|
|
484
|
-
|
|
603
|
+
match ruby
|
|
604
|
+
.exception_runtime_error()
|
|
605
|
+
.new_instance((message.as_str(),))
|
|
606
|
+
{
|
|
607
|
+
Ok(exception) => Error::from(exception),
|
|
608
|
+
Err(error) => error,
|
|
609
|
+
}
|
|
485
610
|
}
|
|
486
611
|
|
|
487
612
|
// Build a custom `ValidationError` from a Ruby error, queuing the exception as the error's cause so
|
|
@@ -493,7 +618,7 @@ fn custom_error_from_ruby<'i>(error: Error) -> jsonschema::ValidationError<'i> {
|
|
|
493
618
|
}
|
|
494
619
|
|
|
495
620
|
#[allow(clippy::too_many_arguments)]
|
|
496
|
-
pub fn make_options_from_kwargs(
|
|
621
|
+
pub fn make_options_from_kwargs<F: Json>(
|
|
497
622
|
ruby: &Ruby,
|
|
498
623
|
draft: Option<jsonschema::Draft>,
|
|
499
624
|
validate_formats: Option<bool>,
|
|
@@ -509,7 +634,10 @@ pub fn make_options_from_kwargs(
|
|
|
509
634
|
email_options_val: Option<Value>,
|
|
510
635
|
http_options_val: Option<Value>,
|
|
511
636
|
offline: Option<bool>,
|
|
512
|
-
) -> Result<ParsedOptions<'_>, Error>
|
|
637
|
+
) -> Result<ParsedOptions<'_, F>, Error>
|
|
638
|
+
where
|
|
639
|
+
RubyKeyword: for<'i> jsonschema::Keyword<'i, F>,
|
|
640
|
+
{
|
|
513
641
|
let offline = offline.unwrap_or(false);
|
|
514
642
|
if offline && retriever_val.is_some() {
|
|
515
643
|
return Err(Error::new(
|
|
@@ -517,7 +645,7 @@ pub fn make_options_from_kwargs(
|
|
|
517
645
|
"`offline` cannot be used together with `retriever`",
|
|
518
646
|
));
|
|
519
647
|
}
|
|
520
|
-
let mut opts = jsonschema::options_for::<
|
|
648
|
+
let mut opts = jsonschema::options_for::<F>().with_retriever(DetachedRetriever(None));
|
|
521
649
|
let mut registry = None;
|
|
522
650
|
let mut retriever = None;
|
|
523
651
|
let retriever_was_provided = retriever_val.is_some();
|
|
@@ -608,7 +736,13 @@ pub fn make_options_from_kwargs(
|
|
|
608
736
|
for item in formats_hash.enumeratorize("each", ()) {
|
|
609
737
|
let pair: magnus::RArray = magnus::TryConvert::try_convert(item?)?;
|
|
610
738
|
let name: String = pair.entry(0)?;
|
|
611
|
-
let callback: Value = pair.entry(1)?;
|
|
739
|
+
let mut callback: Value = pair.entry(1)?;
|
|
740
|
+
let reads_numbers = if let Ok(number_format) = <&NumberFormat>::try_convert(callback) {
|
|
741
|
+
callback = ruby.get_inner(number_format.check);
|
|
742
|
+
true
|
|
743
|
+
} else {
|
|
744
|
+
false
|
|
745
|
+
};
|
|
612
746
|
|
|
613
747
|
let responds_to_call: bool =
|
|
614
748
|
callback.funcall("respond_to?", (SYM_CALL.to_symbol(),))?;
|
|
@@ -650,7 +784,13 @@ pub fn make_options_from_kwargs(
|
|
|
650
784
|
proc: Opaque::from(proc),
|
|
651
785
|
};
|
|
652
786
|
|
|
653
|
-
opts =
|
|
787
|
+
opts = if reads_numbers {
|
|
788
|
+
opts.with_number_format(name, move |number: &serde_json::Number| {
|
|
789
|
+
checker.check_number(number)
|
|
790
|
+
})
|
|
791
|
+
} else {
|
|
792
|
+
opts.with_format(name, move |value: &str| checker.check(value))
|
|
793
|
+
};
|
|
654
794
|
}
|
|
655
795
|
}
|
|
656
796
|
|
|
@@ -761,7 +901,7 @@ pub fn make_options_from_kwargs(
|
|
|
761
901
|
Ok(Box::new(RubyKeyword {
|
|
762
902
|
instance: opaque_inst,
|
|
763
903
|
})
|
|
764
|
-
as Box<dyn for<'i> jsonschema::Keyword<'i,
|
|
904
|
+
as Box<dyn for<'i> jsonschema::Keyword<'i, F>>)
|
|
765
905
|
}
|
|
766
906
|
Err(e) => Err(jsonschema::ValidationError::custom(format!(
|
|
767
907
|
"Failed to instantiate keyword class '{name_err}': {e}"
|
|
@@ -1166,6 +1306,9 @@ impl HttpOptions {
|
|
|
1166
1306
|
}
|
|
1167
1307
|
|
|
1168
1308
|
pub fn define_classes(ruby: &Ruby, module: &RModule) -> Result<(), Error> {
|
|
1309
|
+
module.define_class("NumberFormat", ruby.class_object())?;
|
|
1310
|
+
module.define_singleton_method("number_format", function!(number_format, 1))?;
|
|
1311
|
+
module.define_singleton_method("known_formats", function!(known_formats, -1))?;
|
|
1169
1312
|
let email_class = module.define_class("EmailOptions", ruby.class_object())?;
|
|
1170
1313
|
email_class.define_singleton_method("new", function!(EmailOptions::new_impl, -1))?;
|
|
1171
1314
|
email_class.define_method("require_tld", method!(EmailOptions::require_tld, 0))?;
|
data/src/ser.rs
CHANGED
|
@@ -800,7 +800,7 @@ pub fn value_to_ruby(ruby: &Ruby, value: &JsonValue) -> Result<Value, Error> {
|
|
|
800
800
|
}
|
|
801
801
|
|
|
802
802
|
#[inline]
|
|
803
|
-
fn number_to_ruby(ruby: &Ruby, number: &Number) -> Result<Value, Error> {
|
|
803
|
+
pub(crate) fn number_to_ruby(ruby: &Ruby, number: &Number) -> Result<Value, Error> {
|
|
804
804
|
if let Some(i) = number.as_i64() {
|
|
805
805
|
return Ok(ruby.into_value(i));
|
|
806
806
|
}
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: jsonschema_rs
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.63.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Dmitry Dygalo
|
|
@@ -66,6 +66,7 @@ files:
|
|
|
66
66
|
- src/canonical.rs
|
|
67
67
|
- src/error_kind.rs
|
|
68
68
|
- src/evaluation.rs
|
|
69
|
+
- src/json_text.rs
|
|
69
70
|
- src/lib.rs
|
|
70
71
|
- src/options.rs
|
|
71
72
|
- src/registry.rs
|
|
@@ -80,7 +81,7 @@ metadata:
|
|
|
80
81
|
homepage_uri: https://github.com/Stranger6667/jsonschema/tree/master/crates/jsonschema-rb
|
|
81
82
|
source_code_uri: https://github.com/Stranger6667/jsonschema
|
|
82
83
|
changelog_uri: https://github.com/Stranger6667/jsonschema/blob/master/crates/jsonschema-rb/CHANGELOG.md
|
|
83
|
-
documentation_uri: https://github.com/Stranger6667/jsonschema/blob/ruby-v0.
|
|
84
|
+
documentation_uri: https://github.com/Stranger6667/jsonschema/blob/ruby-v0.63.1/crates/jsonschema-rb/README.md
|
|
84
85
|
bug_tracker_uri: https://github.com/Stranger6667/jsonschema/issues
|
|
85
86
|
funding_uri: https://github.com/sponsors/Stranger6667
|
|
86
87
|
rubygems_mfa_required: 'true'
|