jsonschema_rs 0.61.2 → 0.63.0
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 +110 -1
- data/Cargo.toml +2 -2
- data/MIGRATION.md +10 -12
- data/README.md +67 -31
- data/ext/jsonschema/Cargo.lock +20 -20
- data/ext/jsonschema/Cargo.toml +3 -3
- data/lib/jsonschema/version.rb +1 -1
- data/sig/jsonschema.rbs +139 -69
- data/src/canonical.rs +19 -19
- data/src/error_kind.rs +4 -2
- data/src/evaluation.rs +4 -8
- data/src/json_text.rs +239 -0
- data/src/lib.rs +118 -111
- data/src/options.rs +199 -62
- data/src/registry.rs +24 -3
- data/src/ser.rs +3 -425
- data/src/static_id.rs +2 -5
- 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, 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
|
};
|
|
@@ -48,6 +51,7 @@ define_rb_intern!(static KW_MINIMUM_SUB_DOMAINS: "minimum_sub_domains");
|
|
|
48
51
|
define_rb_intern!(static KW_SIZE_LIMIT: "size_limit");
|
|
49
52
|
define_rb_intern!(static KW_DFA_SIZE_LIMIT: "dfa_size_limit");
|
|
50
53
|
define_rb_intern!(static KW_BACKTRACK_LIMIT: "backtrack_limit");
|
|
54
|
+
define_rb_intern!(static KW_SYNTAX: "syntax");
|
|
51
55
|
// HttpOptions kwargs
|
|
52
56
|
define_rb_intern!(static KW_TIMEOUT: "timeout");
|
|
53
57
|
define_rb_intern!(static KW_CONNECT_TIMEOUT: "connect_timeout");
|
|
@@ -58,14 +62,11 @@ define_rb_intern!(static SYM_CALL: "call");
|
|
|
58
62
|
define_rb_intern!(static SYM_NEW: "new");
|
|
59
63
|
define_rb_intern!(static SYM_VALIDATE: "validate");
|
|
60
64
|
|
|
61
|
-
pub struct ParsedOptions<'i> {
|
|
65
|
+
pub struct ParsedOptions<'i, F: Json = Magnus> {
|
|
62
66
|
pub mask: Option<String>,
|
|
63
|
-
pub options: jsonschema::ValidationOptions<'i, Arc<dyn jsonschema::Retrieve>,
|
|
67
|
+
pub options: jsonschema::ValidationOptions<'i, Arc<dyn jsonschema::Retrieve>, F>,
|
|
64
68
|
pub registry: Option<&'i jsonschema::Registry<'static>>,
|
|
65
69
|
pub retriever: Option<RubyRetriever>,
|
|
66
|
-
// Runtime callbacks invoked during `validator.*` calls (formats / custom keywords).
|
|
67
|
-
// Retriever callbacks are used at build time and do not affect GVL behavior at runtime.
|
|
68
|
-
pub has_ruby_callbacks: bool,
|
|
69
70
|
pub callback_roots: CallbackRoots,
|
|
70
71
|
pub compilation_roots: CompilationRootsRef,
|
|
71
72
|
}
|
|
@@ -79,16 +80,13 @@ pub struct ParsedOptions<'i> {
|
|
|
79
80
|
/// Two complementary collections keep every callback alive:
|
|
80
81
|
///
|
|
81
82
|
/// * **`CallbackRoots`** — held by the `Validator` wrapper and marked during Ruby's
|
|
82
|
-
/// GC mark phase (`DataTypeFunctions::mark`).
|
|
83
|
-
/// mechanism for preventing collection of objects referenced by native extensions.
|
|
84
|
-
/// For one-off validation functions (module-level `valid?`, `validate!`, etc.),
|
|
85
|
-
/// where no persistent `Validator` exists, a [`CallbackRootGuard`](crate::CallbackRootGuard)
|
|
86
|
-
/// temporarily registers the same roots via `gc_register_address` for the duration
|
|
87
|
-
/// of the call.
|
|
83
|
+
/// GC mark phase (`DataTypeFunctions::mark`).
|
|
88
84
|
///
|
|
89
85
|
/// * **`CompilationRoots`** — registered via `gc_register_address` immediately when
|
|
90
86
|
/// added, so callbacks are protected during schema compilation (before the
|
|
91
|
-
/// `Validator` wrapper and its mark function exist)
|
|
87
|
+
/// `Validator` wrapper and its mark function exist) and for the whole of a one-off
|
|
88
|
+
/// call (module-level `valid?`, `validate!`, etc.), which has no wrapper.
|
|
89
|
+
/// Unregistered on drop.
|
|
92
90
|
///
|
|
93
91
|
/// Both collections hold the **same** Ruby objects; they differ only in *when* and
|
|
94
92
|
/// *how* they protect them from the GC.
|
|
@@ -376,8 +374,18 @@ struct RubyFormatChecker {
|
|
|
376
374
|
impl RubyFormatChecker {
|
|
377
375
|
fn check(&self, value: &str) -> bool {
|
|
378
376
|
let ruby = Ruby::get().expect("Ruby VM should be initialized");
|
|
379
|
-
|
|
380
|
-
|
|
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 {
|
|
381
389
|
magnus_invalidate_members_cache();
|
|
382
390
|
match result {
|
|
383
391
|
Ok(v) => v,
|
|
@@ -391,13 +399,39 @@ impl RubyFormatChecker {
|
|
|
391
399
|
}
|
|
392
400
|
}
|
|
393
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
|
+
|
|
394
428
|
/// Wrapper for a Ruby custom keyword validator factory.
|
|
395
429
|
struct RubyKeywordFactory {
|
|
396
430
|
class: Opaque<Value>,
|
|
397
431
|
}
|
|
398
432
|
|
|
399
433
|
/// Wrapper for a Ruby custom keyword validator instance.
|
|
400
|
-
struct RubyKeyword {
|
|
434
|
+
pub struct RubyKeyword {
|
|
401
435
|
instance: Opaque<Value>,
|
|
402
436
|
}
|
|
403
437
|
|
|
@@ -408,13 +442,14 @@ fn node_to_ruby(node: RbNode<'_>) -> Value {
|
|
|
408
442
|
}
|
|
409
443
|
}
|
|
410
444
|
|
|
411
|
-
impl
|
|
412
|
-
fn
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
445
|
+
impl RubyKeyword {
|
|
446
|
+
fn validate_object<'i>(
|
|
447
|
+
&self,
|
|
448
|
+
ruby: &Ruby,
|
|
449
|
+
instance: Value,
|
|
450
|
+
) -> Result<(), jsonschema::ValidationError<'i>> {
|
|
416
451
|
let keyword = ruby.get_inner(self.instance);
|
|
417
|
-
let result: Result<Value, _> = keyword.funcall("validate", (
|
|
452
|
+
let result: Result<Value, _> = keyword.funcall("validate", (instance,));
|
|
418
453
|
magnus_invalidate_members_cache();
|
|
419
454
|
match result {
|
|
420
455
|
Ok(_) => Ok(()),
|
|
@@ -428,28 +463,25 @@ impl<'i> jsonschema::Keyword<'i, Magnus> for RubyKeyword {
|
|
|
428
463
|
}
|
|
429
464
|
}
|
|
430
465
|
|
|
431
|
-
fn
|
|
432
|
-
let
|
|
433
|
-
let
|
|
434
|
-
let inst = ruby.get_inner(self.instance);
|
|
435
|
-
let result: Result<Value, _> = inst.funcall("validate", (rb_instance,));
|
|
466
|
+
fn is_valid_object(&self, ruby: &Ruby, instance: Value) -> bool {
|
|
467
|
+
let keyword = ruby.get_inner(self.instance);
|
|
468
|
+
let result: Result<Value, _> = keyword.funcall("validate", (instance,));
|
|
436
469
|
magnus_invalidate_members_cache();
|
|
437
470
|
result.is_ok()
|
|
438
471
|
}
|
|
439
472
|
|
|
440
|
-
fn
|
|
473
|
+
fn iter_errors_object<'i>(
|
|
441
474
|
&self,
|
|
442
|
-
|
|
475
|
+
ruby: &Ruby,
|
|
476
|
+
instance: Value,
|
|
443
477
|
) -> Box<dyn Iterator<Item = jsonschema::ValidationError<'i>> + 'i> {
|
|
444
|
-
let ruby = Ruby::get().expect("Ruby VM should be initialized");
|
|
445
478
|
let keyword = ruby.get_inner(self.instance);
|
|
446
479
|
// Without an `iter_errors` method, fall back to the single-error `validate` path.
|
|
447
480
|
if !keyword.respond_to("iter_errors", false).unwrap_or(false) {
|
|
448
|
-
return Box::new(self.
|
|
481
|
+
return Box::new(self.validate_object(ruby, instance).err().into_iter());
|
|
449
482
|
}
|
|
450
|
-
let rb_instance = node_to_ruby(instance);
|
|
451
483
|
let array = keyword
|
|
452
|
-
.funcall::<_, _, Value>("iter_errors", (
|
|
484
|
+
.funcall::<_, _, Value>("iter_errors", (instance,))
|
|
453
485
|
.and_then(|result| result.funcall::<_, _, RArray>("to_a", ()));
|
|
454
486
|
magnus_invalidate_members_cache();
|
|
455
487
|
let array = match array {
|
|
@@ -468,7 +500,7 @@ impl<'i> jsonschema::Keyword<'i, Magnus> for RubyKeyword {
|
|
|
468
500
|
})
|
|
469
501
|
.and_then(|index| array.entry::<Value>(index));
|
|
470
502
|
let error = match item {
|
|
471
|
-
Ok(item) => ruby_value_to_error(
|
|
503
|
+
Ok(item) => ruby_value_to_error(ruby, item),
|
|
472
504
|
Err(error) => error,
|
|
473
505
|
};
|
|
474
506
|
errors.push(custom_error_from_ruby(error));
|
|
@@ -477,8 +509,55 @@ impl<'i> jsonschema::Keyword<'i, Magnus> for RubyKeyword {
|
|
|
477
509
|
}
|
|
478
510
|
}
|
|
479
511
|
|
|
512
|
+
impl<'i> jsonschema::Keyword<'i, Magnus> for RubyKeyword {
|
|
513
|
+
fn validate(&self, instance: RbNode<'i>) -> Result<(), jsonschema::ValidationError<'i>> {
|
|
514
|
+
let ruby = Ruby::get().expect("Ruby VM should be initialized");
|
|
515
|
+
self.validate_object(&ruby, node_to_ruby(instance))
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
fn is_valid(&self, instance: RbNode<'i>) -> bool {
|
|
519
|
+
let ruby = Ruby::get().expect("Ruby VM should be initialized");
|
|
520
|
+
self.is_valid_object(&ruby, node_to_ruby(instance))
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
fn iter_errors(
|
|
524
|
+
&self,
|
|
525
|
+
instance: RbNode<'i>,
|
|
526
|
+
) -> Box<dyn Iterator<Item = jsonschema::ValidationError<'i>> + 'i> {
|
|
527
|
+
let ruby = Ruby::get().expect("Ruby VM should be initialized");
|
|
528
|
+
self.iter_errors_object(&ruby, node_to_ruby(instance))
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// The keyword class reads Ruby objects, so a node of JSON text becomes one first.
|
|
533
|
+
impl<'i> jsonschema::Keyword<'i, JsonText> for RubyKeyword {
|
|
534
|
+
fn validate(&self, instance: JsonTextNode<'i>) -> Result<(), jsonschema::ValidationError<'i>> {
|
|
535
|
+
let ruby = Ruby::get().expect("Ruby VM should be initialized");
|
|
536
|
+
match text_node_to_ruby(&ruby, &instance) {
|
|
537
|
+
Ok(object) => self.validate_object(&ruby, object),
|
|
538
|
+
Err(error) => Err(custom_error_from_ruby(error)),
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
fn is_valid(&self, instance: JsonTextNode<'i>) -> bool {
|
|
543
|
+
let ruby = Ruby::get().expect("Ruby VM should be initialized");
|
|
544
|
+
text_node_to_ruby(&ruby, &instance).is_ok_and(|object| self.is_valid_object(&ruby, object))
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
fn iter_errors(
|
|
548
|
+
&self,
|
|
549
|
+
instance: JsonTextNode<'i>,
|
|
550
|
+
) -> Box<dyn Iterator<Item = jsonschema::ValidationError<'i>> + 'i> {
|
|
551
|
+
let ruby = Ruby::get().expect("Ruby VM should be initialized");
|
|
552
|
+
match text_node_to_ruby(&ruby, &instance) {
|
|
553
|
+
Ok(object) => self.iter_errors_object(&ruby, object),
|
|
554
|
+
Err(error) => Box::new(std::iter::once(custom_error_from_ruby(error))),
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
480
559
|
// Wrap a yielded Ruby value as an `Error`, preserving exception instances so they surface as
|
|
481
|
-
// `#cause`; a non-exception
|
|
560
|
+
// `#cause`; a non-exception becomes a `RuntimeError` holding its text.
|
|
482
561
|
fn ruby_value_to_error(ruby: &Ruby, value: Value) -> Error {
|
|
483
562
|
if let Ok(exception) = Exception::try_convert(value) {
|
|
484
563
|
return Error::from(exception);
|
|
@@ -486,7 +565,13 @@ fn ruby_value_to_error(ruby: &Ruby, value: Value) -> Error {
|
|
|
486
565
|
let message = value
|
|
487
566
|
.funcall::<_, _, String>("to_s", ())
|
|
488
567
|
.unwrap_or_else(|_| "custom keyword error".to_string());
|
|
489
|
-
|
|
568
|
+
match ruby
|
|
569
|
+
.exception_runtime_error()
|
|
570
|
+
.new_instance((message.as_str(),))
|
|
571
|
+
{
|
|
572
|
+
Ok(exception) => Error::from(exception),
|
|
573
|
+
Err(error) => error,
|
|
574
|
+
}
|
|
490
575
|
}
|
|
491
576
|
|
|
492
577
|
// Build a custom `ValidationError` from a Ruby error, queuing the exception as the error's cause so
|
|
@@ -498,7 +583,7 @@ fn custom_error_from_ruby<'i>(error: Error) -> jsonschema::ValidationError<'i> {
|
|
|
498
583
|
}
|
|
499
584
|
|
|
500
585
|
#[allow(clippy::too_many_arguments)]
|
|
501
|
-
pub fn make_options_from_kwargs(
|
|
586
|
+
pub fn make_options_from_kwargs<F: Json>(
|
|
502
587
|
ruby: &Ruby,
|
|
503
588
|
draft: Option<jsonschema::Draft>,
|
|
504
589
|
validate_formats: Option<bool>,
|
|
@@ -514,7 +599,10 @@ pub fn make_options_from_kwargs(
|
|
|
514
599
|
email_options_val: Option<Value>,
|
|
515
600
|
http_options_val: Option<Value>,
|
|
516
601
|
offline: Option<bool>,
|
|
517
|
-
) -> Result<ParsedOptions<'_>, Error>
|
|
602
|
+
) -> Result<ParsedOptions<'_, F>, Error>
|
|
603
|
+
where
|
|
604
|
+
RubyKeyword: for<'i> jsonschema::Keyword<'i, F>,
|
|
605
|
+
{
|
|
518
606
|
let offline = offline.unwrap_or(false);
|
|
519
607
|
if offline && retriever_val.is_some() {
|
|
520
608
|
return Err(Error::new(
|
|
@@ -522,11 +610,10 @@ pub fn make_options_from_kwargs(
|
|
|
522
610
|
"`offline` cannot be used together with `retriever`",
|
|
523
611
|
));
|
|
524
612
|
}
|
|
525
|
-
let mut opts = jsonschema::options_for::<
|
|
613
|
+
let mut opts = jsonschema::options_for::<F>().with_retriever(DetachedRetriever(None));
|
|
526
614
|
let mut registry = None;
|
|
527
615
|
let mut retriever = None;
|
|
528
616
|
let retriever_was_provided = retriever_val.is_some();
|
|
529
|
-
let mut has_ruby_callbacks = false;
|
|
530
617
|
let callback_roots = Arc::new(Mutex::new(Vec::new()));
|
|
531
618
|
let compilation_roots = Arc::new(CompilationRoots::default());
|
|
532
619
|
|
|
@@ -612,10 +699,15 @@ pub fn make_options_from_kwargs(
|
|
|
612
699
|
|
|
613
700
|
if let Some(formats_hash) = formats {
|
|
614
701
|
for item in formats_hash.enumeratorize("each", ()) {
|
|
615
|
-
has_ruby_callbacks = true;
|
|
616
702
|
let pair: magnus::RArray = magnus::TryConvert::try_convert(item?)?;
|
|
617
703
|
let name: String = pair.entry(0)?;
|
|
618
|
-
let callback: Value = pair.entry(1)?;
|
|
704
|
+
let mut callback: Value = pair.entry(1)?;
|
|
705
|
+
let reads_numbers = if let Ok(number_format) = <&NumberFormat>::try_convert(callback) {
|
|
706
|
+
callback = ruby.get_inner(number_format.check);
|
|
707
|
+
true
|
|
708
|
+
} else {
|
|
709
|
+
false
|
|
710
|
+
};
|
|
619
711
|
|
|
620
712
|
let responds_to_call: bool =
|
|
621
713
|
callback.funcall("respond_to?", (SYM_CALL.to_symbol(),))?;
|
|
@@ -657,13 +749,18 @@ pub fn make_options_from_kwargs(
|
|
|
657
749
|
proc: Opaque::from(proc),
|
|
658
750
|
};
|
|
659
751
|
|
|
660
|
-
opts =
|
|
752
|
+
opts = if reads_numbers {
|
|
753
|
+
opts.with_number_format(name, move |number: &serde_json::Number| {
|
|
754
|
+
checker.check_number(number)
|
|
755
|
+
})
|
|
756
|
+
} else {
|
|
757
|
+
opts.with_format(name, move |value: &str| checker.check(value))
|
|
758
|
+
};
|
|
661
759
|
}
|
|
662
760
|
}
|
|
663
761
|
|
|
664
762
|
if let Some(keywords_hash) = keywords {
|
|
665
763
|
for item in keywords_hash.enumeratorize("each", ()) {
|
|
666
|
-
has_ruby_callbacks = true;
|
|
667
764
|
let pair: magnus::RArray = magnus::TryConvert::try_convert(item?)?;
|
|
668
765
|
let name: String = pair.entry(0)?;
|
|
669
766
|
let callback: Value = pair.entry(1)?;
|
|
@@ -729,14 +826,12 @@ pub fn make_options_from_kwargs(
|
|
|
729
826
|
))
|
|
730
827
|
})?;
|
|
731
828
|
|
|
732
|
-
// Convert keyword value to Ruby
|
|
733
829
|
let rb_value = value_to_ruby(&inner_ruby, value).map_err(|e| {
|
|
734
830
|
jsonschema::ValidationError::custom(format!(
|
|
735
831
|
"Failed to convert keyword value to Ruby: {e}"
|
|
736
832
|
))
|
|
737
833
|
})?;
|
|
738
834
|
|
|
739
|
-
// Convert path to Ruby array
|
|
740
835
|
let rb_path =
|
|
741
836
|
inner_ruby.ary_from_iter(path.iter().map(|segment| match segment {
|
|
742
837
|
jsonschema::paths::LocationSegment::Property(p) => {
|
|
@@ -771,7 +866,7 @@ pub fn make_options_from_kwargs(
|
|
|
771
866
|
Ok(Box::new(RubyKeyword {
|
|
772
867
|
instance: opaque_inst,
|
|
773
868
|
})
|
|
774
|
-
as Box<dyn for<'i> jsonschema::Keyword<'i,
|
|
869
|
+
as Box<dyn for<'i> jsonschema::Keyword<'i, F>>)
|
|
775
870
|
}
|
|
776
871
|
Err(e) => Err(jsonschema::ValidationError::custom(format!(
|
|
777
872
|
"Failed to instantiate keyword class '{name_err}': {e}"
|
|
@@ -854,7 +949,6 @@ pub fn make_options_from_kwargs(
|
|
|
854
949
|
options: opts,
|
|
855
950
|
registry,
|
|
856
951
|
retriever,
|
|
857
|
-
has_ruby_callbacks,
|
|
858
952
|
callback_roots,
|
|
859
953
|
compilation_roots,
|
|
860
954
|
})
|
|
@@ -936,21 +1030,49 @@ impl EmailOptions {
|
|
|
936
1030
|
pub struct RegexOptions {
|
|
937
1031
|
pub size_limit: Option<usize>,
|
|
938
1032
|
pub dfa_size_limit: Option<usize>,
|
|
1033
|
+
pub syntax: jsonschema::PatternSyntax,
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
fn parse_syntax(ruby: &Ruby, syntax: Option<Symbol>) -> Result<jsonschema::PatternSyntax, Error> {
|
|
1037
|
+
let Some(syntax) = syntax else {
|
|
1038
|
+
return Ok(jsonschema::PatternSyntax::Ecma262);
|
|
1039
|
+
};
|
|
1040
|
+
match &*syntax.name()? {
|
|
1041
|
+
"ecma262" => Ok(jsonschema::PatternSyntax::Ecma262),
|
|
1042
|
+
"engine" => Ok(jsonschema::PatternSyntax::Engine),
|
|
1043
|
+
name => Err(Error::new(
|
|
1044
|
+
ruby.exception_arg_error(),
|
|
1045
|
+
format!("Unknown pattern syntax: :{name}. Expected :ecma262 or :engine"),
|
|
1046
|
+
)),
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
fn syntax_name(syntax: jsonschema::PatternSyntax) -> &'static str {
|
|
1051
|
+
match syntax {
|
|
1052
|
+
jsonschema::PatternSyntax::Engine => "engine",
|
|
1053
|
+
_ => "ecma262",
|
|
1054
|
+
}
|
|
939
1055
|
}
|
|
940
1056
|
|
|
941
1057
|
impl RegexOptions {
|
|
942
|
-
|
|
1058
|
+
#[allow(clippy::type_complexity)]
|
|
1059
|
+
fn new_impl(ruby: &Ruby, args: &[Value]) -> Result<Self, Error> {
|
|
943
1060
|
let parsed = scan_args::<(), (), (), (), _, ()>(args)?;
|
|
944
|
-
let ids = [*KW_SIZE_LIMIT, *KW_DFA_SIZE_LIMIT];
|
|
945
|
-
let kw: KwArgs<(), (Option<usize>, Option<usize>), ()> =
|
|
1061
|
+
let ids = [*KW_SIZE_LIMIT, *KW_DFA_SIZE_LIMIT, *KW_SYNTAX];
|
|
1062
|
+
let kw: KwArgs<(), (Option<usize>, Option<usize>, Option<Symbol>), ()> =
|
|
946
1063
|
get_kwargs(parsed.keywords, &[], &ids)?;
|
|
947
|
-
let (size_limit, dfa_size_limit) = kw.optional;
|
|
1064
|
+
let (size_limit, dfa_size_limit, syntax) = kw.optional;
|
|
948
1065
|
Ok(RegexOptions {
|
|
949
1066
|
size_limit,
|
|
950
1067
|
dfa_size_limit,
|
|
1068
|
+
syntax: parse_syntax(ruby, syntax)?,
|
|
951
1069
|
})
|
|
952
1070
|
}
|
|
953
1071
|
|
|
1072
|
+
fn syntax(ruby: &Ruby, rb_self: &Self) -> Symbol {
|
|
1073
|
+
ruby.to_symbol(syntax_name(rb_self.syntax))
|
|
1074
|
+
}
|
|
1075
|
+
|
|
954
1076
|
fn size_limit(&self) -> Option<usize> {
|
|
955
1077
|
self.size_limit
|
|
956
1078
|
}
|
|
@@ -971,7 +1093,7 @@ impl RegexOptions {
|
|
|
971
1093
|
Some(n) => write!(s, "{n}").expect("Failed to write dfa_size_limit"),
|
|
972
1094
|
None => s.push_str("nil"),
|
|
973
1095
|
}
|
|
974
|
-
s.
|
|
1096
|
+
write!(s, ", syntax=:{}>", syntax_name(self.syntax)).expect("Failed to write syntax");
|
|
975
1097
|
s
|
|
976
1098
|
}
|
|
977
1099
|
}
|
|
@@ -984,7 +1106,7 @@ pub(crate) enum RbPatternOptions {
|
|
|
984
1106
|
|
|
985
1107
|
pub(crate) fn extract_pattern_options(ruby: &Ruby, val: Value) -> Result<RbPatternOptions, Error> {
|
|
986
1108
|
if let Ok(fancy_opts) = <&FancyRegexOptions>::try_convert(val) {
|
|
987
|
-
let mut po = jsonschema::PatternOptions::fancy_regex();
|
|
1109
|
+
let mut po = jsonschema::PatternOptions::fancy_regex().syntax(fancy_opts.syntax);
|
|
988
1110
|
if let Some(limit) = fancy_opts.backtrack_limit {
|
|
989
1111
|
po = po.backtrack_limit(limit);
|
|
990
1112
|
}
|
|
@@ -996,7 +1118,7 @@ pub(crate) fn extract_pattern_options(ruby: &Ruby, val: Value) -> Result<RbPatte
|
|
|
996
1118
|
}
|
|
997
1119
|
Ok(RbPatternOptions::Fancy(po))
|
|
998
1120
|
} else if let Ok(regex_opts) = <&RegexOptions>::try_convert(val) {
|
|
999
|
-
let mut po = jsonschema::PatternOptions::regex();
|
|
1121
|
+
let mut po = jsonschema::PatternOptions::regex().syntax(regex_opts.syntax);
|
|
1000
1122
|
if let Some(limit) = regex_opts.size_limit {
|
|
1001
1123
|
po = po.size_limit(limit);
|
|
1002
1124
|
}
|
|
@@ -1017,23 +1139,34 @@ pub struct FancyRegexOptions {
|
|
|
1017
1139
|
pub backtrack_limit: Option<usize>,
|
|
1018
1140
|
pub size_limit: Option<usize>,
|
|
1019
1141
|
pub dfa_size_limit: Option<usize>,
|
|
1142
|
+
pub syntax: jsonschema::PatternSyntax,
|
|
1020
1143
|
}
|
|
1021
1144
|
|
|
1022
1145
|
impl FancyRegexOptions {
|
|
1023
1146
|
#[allow(clippy::type_complexity)]
|
|
1024
|
-
fn new_impl(args: &[Value]) -> Result<Self, Error> {
|
|
1147
|
+
fn new_impl(ruby: &Ruby, args: &[Value]) -> Result<Self, Error> {
|
|
1025
1148
|
let parsed = scan_args::<(), (), (), (), _, ()>(args)?;
|
|
1026
|
-
let ids = [
|
|
1027
|
-
|
|
1149
|
+
let ids = [
|
|
1150
|
+
*KW_BACKTRACK_LIMIT,
|
|
1151
|
+
*KW_SIZE_LIMIT,
|
|
1152
|
+
*KW_DFA_SIZE_LIMIT,
|
|
1153
|
+
*KW_SYNTAX,
|
|
1154
|
+
];
|
|
1155
|
+
let kw: KwArgs<(), (Option<usize>, Option<usize>, Option<usize>, Option<Symbol>), ()> =
|
|
1028
1156
|
get_kwargs(parsed.keywords, &[], &ids)?;
|
|
1029
|
-
let (backtrack_limit, size_limit, dfa_size_limit) = kw.optional;
|
|
1157
|
+
let (backtrack_limit, size_limit, dfa_size_limit, syntax) = kw.optional;
|
|
1030
1158
|
Ok(FancyRegexOptions {
|
|
1031
1159
|
backtrack_limit,
|
|
1032
1160
|
size_limit,
|
|
1033
1161
|
dfa_size_limit,
|
|
1162
|
+
syntax: parse_syntax(ruby, syntax)?,
|
|
1034
1163
|
})
|
|
1035
1164
|
}
|
|
1036
1165
|
|
|
1166
|
+
fn syntax(ruby: &Ruby, rb_self: &Self) -> Symbol {
|
|
1167
|
+
ruby.to_symbol(syntax_name(rb_self.syntax))
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1037
1170
|
fn backtrack_limit(&self) -> Option<usize> {
|
|
1038
1171
|
self.backtrack_limit
|
|
1039
1172
|
}
|
|
@@ -1063,7 +1196,7 @@ impl FancyRegexOptions {
|
|
|
1063
1196
|
Some(n) => write!(s, "{n}").expect("Failed to write dfa_size_limit"),
|
|
1064
1197
|
None => s.push_str("nil"),
|
|
1065
1198
|
}
|
|
1066
|
-
s.
|
|
1199
|
+
write!(s, ", syntax=:{}>", syntax_name(self.syntax)).expect("Failed to write syntax");
|
|
1067
1200
|
s
|
|
1068
1201
|
}
|
|
1069
1202
|
}
|
|
@@ -1138,6 +1271,8 @@ impl HttpOptions {
|
|
|
1138
1271
|
}
|
|
1139
1272
|
|
|
1140
1273
|
pub fn define_classes(ruby: &Ruby, module: &RModule) -> Result<(), Error> {
|
|
1274
|
+
module.define_class("NumberFormat", ruby.class_object())?;
|
|
1275
|
+
module.define_singleton_method("number_format", function!(number_format, 1))?;
|
|
1141
1276
|
let email_class = module.define_class("EmailOptions", ruby.class_object())?;
|
|
1142
1277
|
email_class.define_singleton_method("new", function!(EmailOptions::new_impl, -1))?;
|
|
1143
1278
|
email_class.define_method("require_tld", method!(EmailOptions::require_tld, 0))?;
|
|
@@ -1159,6 +1294,7 @@ pub fn define_classes(ruby: &Ruby, module: &RModule) -> Result<(), Error> {
|
|
|
1159
1294
|
regex_class.define_singleton_method("new", function!(RegexOptions::new_impl, -1))?;
|
|
1160
1295
|
regex_class.define_method("size_limit", method!(RegexOptions::size_limit, 0))?;
|
|
1161
1296
|
regex_class.define_method("dfa_size_limit", method!(RegexOptions::dfa_size_limit, 0))?;
|
|
1297
|
+
regex_class.define_method("syntax", method!(RegexOptions::syntax, 0))?;
|
|
1162
1298
|
regex_class.define_method("inspect", method!(RegexOptions::inspect, 0))?;
|
|
1163
1299
|
|
|
1164
1300
|
let fancy_regex_class = module.define_class("FancyRegexOptions", ruby.class_object())?;
|
|
@@ -1172,6 +1308,7 @@ pub fn define_classes(ruby: &Ruby, module: &RModule) -> Result<(), Error> {
|
|
|
1172
1308
|
"dfa_size_limit",
|
|
1173
1309
|
method!(FancyRegexOptions::dfa_size_limit, 0),
|
|
1174
1310
|
)?;
|
|
1311
|
+
fancy_regex_class.define_method("syntax", method!(FancyRegexOptions::syntax, 0))?;
|
|
1175
1312
|
fancy_regex_class.define_method("inspect", method!(FancyRegexOptions::inspect, 0))?;
|
|
1176
1313
|
|
|
1177
1314
|
let http_class = module.define_class("HttpOptions", ruby.class_object())?;
|
data/src/registry.rs
CHANGED
|
@@ -207,6 +207,20 @@ impl Resolver {
|
|
|
207
207
|
|
|
208
208
|
#[allow(clippy::needless_pass_by_value)]
|
|
209
209
|
fn lookup(ruby: &Ruby, rb_self: &Resolver, reference: String) -> Result<Resolved, Error> {
|
|
210
|
+
rb_self.resolve(ruby, &reference, false)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
#[allow(clippy::needless_pass_by_value)]
|
|
214
|
+
fn lookup_dynamic_ref(
|
|
215
|
+
ruby: &Ruby,
|
|
216
|
+
rb_self: &Resolver,
|
|
217
|
+
reference: String,
|
|
218
|
+
) -> Result<Resolved, Error> {
|
|
219
|
+
rb_self.resolve(ruby, &reference, true)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
fn resolve(&self, ruby: &Ruby, reference: &str, dynamic: bool) -> Result<Resolved, Error> {
|
|
223
|
+
let rb_self = self;
|
|
210
224
|
let resolver = if rb_self.0.dynamic_scope.is_empty() {
|
|
211
225
|
rb_self
|
|
212
226
|
.0
|
|
@@ -237,9 +251,12 @@ impl Resolver {
|
|
|
237
251
|
resolver
|
|
238
252
|
};
|
|
239
253
|
|
|
240
|
-
let resolved =
|
|
241
|
-
.
|
|
242
|
-
|
|
254
|
+
let resolved = if dynamic {
|
|
255
|
+
resolver.lookup_dynamic_ref(reference)
|
|
256
|
+
} else {
|
|
257
|
+
resolver.lookup(reference)
|
|
258
|
+
};
|
|
259
|
+
let resolved = resolved.map_err(|e| crate::referencing_error(ruby, e.to_string()))?;
|
|
243
260
|
let (contents, resolver, draft) = resolved.into_inner();
|
|
244
261
|
|
|
245
262
|
let contents_val = value_to_ruby(ruby, contents)?;
|
|
@@ -286,6 +303,10 @@ pub fn define_class(ruby: &Ruby, module: &RModule) -> Result<(), Error> {
|
|
|
286
303
|
resolver_class.define_method("base_uri", method!(Resolver::base_uri, 0))?;
|
|
287
304
|
resolver_class.define_method("dynamic_scope", method!(Resolver::dynamic_scope, 0))?;
|
|
288
305
|
resolver_class.define_method("lookup", method!(Resolver::lookup, 1))?;
|
|
306
|
+
resolver_class.define_method(
|
|
307
|
+
"lookup_dynamic_ref",
|
|
308
|
+
method!(Resolver::lookup_dynamic_ref, 1),
|
|
309
|
+
)?;
|
|
289
310
|
|
|
290
311
|
let resolved_class = module.define_class("Resolved", ruby.class_object())?;
|
|
291
312
|
resolved_class.define_method("contents", method!(Resolved::contents, 0))?;
|