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/lib.rs
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
mod canonical;
|
|
9
9
|
mod error_kind;
|
|
10
10
|
mod evaluation;
|
|
11
|
+
mod json_text;
|
|
11
12
|
mod options;
|
|
12
13
|
mod registry;
|
|
13
14
|
mod retriever;
|
|
@@ -18,7 +19,8 @@ mod validator_map;
|
|
|
18
19
|
use jsonschema::{
|
|
19
20
|
json::{
|
|
20
21
|
magnus_child, magnus_invalidate_members_cache, magnus_is_object, magnus_probe_root,
|
|
21
|
-
magnus_take_pending_error,
|
|
22
|
+
magnus_take_pending_error, Array, Json, JsonText, JsonTextNode, Magnus,
|
|
23
|
+
MagnusPendingErrorScope, Node, Object, RbNode,
|
|
22
24
|
},
|
|
23
25
|
paths::LocationSegment,
|
|
24
26
|
ValidationOptions,
|
|
@@ -46,6 +48,7 @@ use std::{
|
|
|
46
48
|
use crate::{
|
|
47
49
|
error_kind::ValidationErrorKind,
|
|
48
50
|
evaluation::Evaluation,
|
|
51
|
+
json_text::text_node_to_ruby,
|
|
49
52
|
options::{
|
|
50
53
|
extract_evaluate_kwargs, extract_kwargs, extract_kwargs_no_draft, make_options_from_kwargs,
|
|
51
54
|
parse_draft_symbol, CallbackRoots, CompilationRoots, CompilationRootsRef, ExtractedKwargs,
|
|
@@ -86,21 +89,21 @@ define_rb_intern!(static ID_SYM_SCHEMA_PATH_POINTER: "schema_path_pointer");
|
|
|
86
89
|
define_rb_intern!(static ID_SYM_EVALUATION_PATH_POINTER: "evaluation_path_pointer");
|
|
87
90
|
define_rb_intern!(static ID_SYM_ABSOLUTE_KEYWORD_LOCATION: "absolute_keyword_location");
|
|
88
91
|
|
|
89
|
-
struct BuiltValidator {
|
|
90
|
-
validator: jsonschema::Validator<
|
|
92
|
+
struct BuiltValidator<F: Json = Magnus> {
|
|
93
|
+
validator: jsonschema::Validator<F>,
|
|
91
94
|
callback_roots: CallbackRoots,
|
|
92
95
|
compilation_roots: CompilationRootsRef,
|
|
93
96
|
}
|
|
94
97
|
|
|
95
|
-
fn build_validator(
|
|
98
|
+
fn build_validator<F: Json>(
|
|
96
99
|
ruby: &Ruby,
|
|
97
|
-
options: ValidationOptions<'_, Arc<dyn jsonschema::Retrieve>,
|
|
100
|
+
options: ValidationOptions<'_, Arc<dyn jsonschema::Retrieve>, F>,
|
|
98
101
|
registry: Option<&jsonschema::Registry<'_>>,
|
|
99
102
|
retriever: Option<RubyRetriever>,
|
|
100
103
|
callback_roots: CallbackRoots,
|
|
101
104
|
compilation_roots: Arc<CompilationRoots>,
|
|
102
105
|
schema: &serde_json::Value,
|
|
103
|
-
) -> Result<BuiltValidator
|
|
106
|
+
) -> Result<BuiltValidator<F>, Error> {
|
|
104
107
|
let mut options = match retriever {
|
|
105
108
|
Some(ret) => options.with_retriever(ret),
|
|
106
109
|
None => options,
|
|
@@ -128,11 +131,14 @@ fn build_validator(
|
|
|
128
131
|
})
|
|
129
132
|
}
|
|
130
133
|
|
|
131
|
-
fn build_parsed_options(
|
|
134
|
+
fn build_parsed_options<F: Json>(
|
|
132
135
|
ruby: &Ruby,
|
|
133
136
|
kw: ExtractedKwargs,
|
|
134
137
|
draft_override: Option<jsonschema::Draft>,
|
|
135
|
-
) -> Result<ParsedOptions<'_>, Error>
|
|
138
|
+
) -> Result<ParsedOptions<'_, F>, Error>
|
|
139
|
+
where
|
|
140
|
+
options::RubyKeyword: for<'i> jsonschema::Keyword<'i, F>,
|
|
141
|
+
{
|
|
136
142
|
let (
|
|
137
143
|
draft_val,
|
|
138
144
|
validate_formats,
|
|
@@ -248,7 +254,7 @@ fn build_verbose_message(
|
|
|
248
254
|
mut message: String,
|
|
249
255
|
schema_path: &jsonschema::paths::Location,
|
|
250
256
|
instance_path: &jsonschema::paths::Location,
|
|
251
|
-
root_instance: Option<
|
|
257
|
+
root_instance: Option<InstanceRoot<'_>>,
|
|
252
258
|
failing_instance: &serde_json::Value,
|
|
253
259
|
mask: Option<&str>,
|
|
254
260
|
) -> String {
|
|
@@ -316,14 +322,14 @@ fn build_verbose_message(
|
|
|
316
322
|
let segment = unescape_segment(segment);
|
|
317
323
|
let segment = segment.as_ref();
|
|
318
324
|
let is_index = match current {
|
|
319
|
-
Some(node) if
|
|
325
|
+
Some(node) if node.is_object() => false,
|
|
320
326
|
_ => is_index_segment(segment),
|
|
321
327
|
};
|
|
322
328
|
message.push('[');
|
|
323
329
|
push_segment(&mut message, segment, is_index);
|
|
324
330
|
message.push(']');
|
|
325
331
|
|
|
326
|
-
current = current.and_then(|node|
|
|
332
|
+
current = current.and_then(|node| node.child(segment, is_index));
|
|
327
333
|
}
|
|
328
334
|
message.push_str(":\n ");
|
|
329
335
|
|
|
@@ -346,6 +352,57 @@ fn error_message(error: &jsonschema::ValidationError<'_>, mask: Option<&str>) ->
|
|
|
346
352
|
}
|
|
347
353
|
}
|
|
348
354
|
|
|
355
|
+
/// Where an error reads its instance from: the Ruby object validated, or the JSON text.
|
|
356
|
+
#[derive(Clone, Copy)]
|
|
357
|
+
pub(crate) enum InstanceRoot<'a> {
|
|
358
|
+
Object(RbNode<'a>),
|
|
359
|
+
Text(JsonTextNode<'a>),
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
impl InstanceRoot<'_> {
|
|
363
|
+
fn is_object(self) -> bool {
|
|
364
|
+
match self {
|
|
365
|
+
InstanceRoot::Object(node) => magnus_is_object(node),
|
|
366
|
+
InstanceRoot::Text(node) => node.as_object().is_some(),
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
fn child(self, segment: &str, is_index: bool) -> Option<Self> {
|
|
371
|
+
match self {
|
|
372
|
+
InstanceRoot::Object(node) => magnus_child(node, segment, is_index).map(Self::Object),
|
|
373
|
+
InstanceRoot::Text(node) => if is_index {
|
|
374
|
+
node.as_array()?.elements().nth(segment.parse().ok()?)
|
|
375
|
+
} else {
|
|
376
|
+
node.as_object()?.get(&JsonText::prepare_key(segment))
|
|
377
|
+
}
|
|
378
|
+
.map(Self::Text),
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// The instance at `location`, or `None` to convert the error's JSON copy instead. JSON text
|
|
383
|
+
// builds only that value, as `JSON.parse` would, never the rest of the document.
|
|
384
|
+
fn instance_at(self, ruby: &Ruby, location: &str) -> Result<Option<Value>, Error> {
|
|
385
|
+
match self {
|
|
386
|
+
InstanceRoot::Object(root) => Ok(plain_instance_at(ruby, root, location)),
|
|
387
|
+
InstanceRoot::Text(root) => text_node_at(root, location)
|
|
388
|
+
.map(|node| text_node_to_ruby(ruby, &node))
|
|
389
|
+
.transpose(),
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
fn text_node_at<'a>(root: JsonTextNode<'a>, location: &str) -> Option<JsonTextNode<'a>> {
|
|
395
|
+
let mut current = root;
|
|
396
|
+
for segment in location.split('/').skip(1) {
|
|
397
|
+
current = if let Some(object) = current.as_object() {
|
|
398
|
+
object.get(&JsonText::prepare_key(&unescape_segment(segment)))?
|
|
399
|
+
} else {
|
|
400
|
+
current.as_array()?.elements().nth(parse_index(segment)?)?
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
Some(current)
|
|
404
|
+
}
|
|
405
|
+
|
|
349
406
|
// The Ruby value at `location` under `root` when it holds only plain JSON types; `None` makes the
|
|
350
407
|
// caller convert the JSON copy.
|
|
351
408
|
fn plain_instance_at(ruby: &Ruby, root: RbNode<'_>, location: &str) -> Option<Value> {
|
|
@@ -421,7 +478,7 @@ fn is_plain_json(ruby: &Ruby, value: Value, depth: u16) -> bool {
|
|
|
421
478
|
fn into_ruby_error(
|
|
422
479
|
ruby: &Ruby,
|
|
423
480
|
error: jsonschema::ValidationError<'_>,
|
|
424
|
-
root_instance: Option<
|
|
481
|
+
root_instance: Option<InstanceRoot<'_>>,
|
|
425
482
|
message: &str,
|
|
426
483
|
mask: Option<&str>,
|
|
427
484
|
) -> Result<Value, Error> {
|
|
@@ -455,8 +512,10 @@ fn into_ruby_error(
|
|
|
455
512
|
let kind_obj = ValidationErrorKind::new(ruby, &parts.kind, mask)?;
|
|
456
513
|
// Scalars convert faster than the path to them is followed.
|
|
457
514
|
let original = match parts.instance.as_ref() {
|
|
458
|
-
serde_json::Value::Array(_) | serde_json::Value::Object(_) => root_instance
|
|
459
|
-
|
|
515
|
+
serde_json::Value::Array(_) | serde_json::Value::Object(_) => match root_instance {
|
|
516
|
+
Some(root) => root.instance_at(ruby, parts.instance_path.as_str())?,
|
|
517
|
+
None => None,
|
|
518
|
+
},
|
|
460
519
|
_ => None,
|
|
461
520
|
};
|
|
462
521
|
let rb_instance = match original {
|
|
@@ -512,7 +571,7 @@ fn into_ruby_error(
|
|
|
512
571
|
fn to_ruby_error_value(
|
|
513
572
|
ruby: &Ruby,
|
|
514
573
|
error: jsonschema::ValidationError<'_>,
|
|
515
|
-
root_instance: Option<
|
|
574
|
+
root_instance: Option<InstanceRoot<'_>>,
|
|
516
575
|
mask: Option<&str>,
|
|
517
576
|
) -> Result<Value, Error> {
|
|
518
577
|
let message = error_message(&error, mask);
|
|
@@ -527,7 +586,7 @@ pub(crate) fn referencing_error(ruby: &Ruby, message: String) -> Error {
|
|
|
527
586
|
fn raise_validation_error(
|
|
528
587
|
ruby: &Ruby,
|
|
529
588
|
error: jsonschema::ValidationError<'_>,
|
|
530
|
-
root_instance: Option<
|
|
589
|
+
root_instance: Option<InstanceRoot<'_>>,
|
|
531
590
|
mask: Option<&str>,
|
|
532
591
|
) -> Error {
|
|
533
592
|
let message = error_message(&error, mask);
|
|
@@ -677,7 +736,7 @@ impl Validator {
|
|
|
677
736
|
Ok(Err(error)) => Err(raise_validation_error(
|
|
678
737
|
ruby,
|
|
679
738
|
error,
|
|
680
|
-
Some(node),
|
|
739
|
+
Some(InstanceRoot::Object(node)),
|
|
681
740
|
rb_self.mask.as_deref(),
|
|
682
741
|
)),
|
|
683
742
|
Err(err) => Err(handle_callback_panic(ruby, err)),
|
|
@@ -690,8 +749,11 @@ impl Validator {
|
|
|
690
749
|
let _scope = KeywordCauseScope::enter();
|
|
691
750
|
|
|
692
751
|
if ruby.block_given() {
|
|
693
|
-
// Lazy path: yield errors one at a time to the block
|
|
694
|
-
|
|
752
|
+
// Lazy path: yield errors one at a time to the block. Building the iterator
|
|
753
|
+
// validates, which can run Ruby callbacks.
|
|
754
|
+
let mut iter =
|
|
755
|
+
catch_unwind_silent(AssertUnwindSafe(|| rb_self.validator.iter_errors(node)))
|
|
756
|
+
.map_err(|err| handle_callback_panic(ruby, err))?;
|
|
695
757
|
loop {
|
|
696
758
|
let result = catch_unwind_silent(AssertUnwindSafe(|| iter.next()));
|
|
697
759
|
match result {
|
|
@@ -699,7 +761,7 @@ impl Validator {
|
|
|
699
761
|
let ruby_error = to_ruby_error_value(
|
|
700
762
|
ruby,
|
|
701
763
|
error,
|
|
702
|
-
Some(node),
|
|
764
|
+
Some(InstanceRoot::Object(node)),
|
|
703
765
|
rb_self.mask.as_deref(),
|
|
704
766
|
)?;
|
|
705
767
|
ruby.yield_value::<Value, Value>(ruby_error)?;
|
|
@@ -721,7 +783,7 @@ impl Validator {
|
|
|
721
783
|
arr.push(to_ruby_error_value(
|
|
722
784
|
ruby,
|
|
723
785
|
error,
|
|
724
|
-
Some(node),
|
|
786
|
+
Some(InstanceRoot::Object(node)),
|
|
725
787
|
rb_self.mask.as_deref(),
|
|
726
788
|
)?)?;
|
|
727
789
|
}
|
|
@@ -810,7 +872,7 @@ fn validator_for(ruby: &Ruby, args: &[Value]) -> Result<Validator, Error> {
|
|
|
810
872
|
let kw = extract_kwargs_no_draft(ruby, parsed_args.keywords)?;
|
|
811
873
|
|
|
812
874
|
let json_schema = to_schema_value(ruby, schema)?;
|
|
813
|
-
let parsed = build_parsed_options(ruby, kw, None)?;
|
|
875
|
+
let parsed = build_parsed_options::<Magnus>(ruby, kw, None)?;
|
|
814
876
|
let BuiltValidator {
|
|
815
877
|
validator,
|
|
816
878
|
callback_roots,
|
|
@@ -838,7 +900,7 @@ fn validator_map_for(ruby: &Ruby, args: &[Value]) -> Result<validator_map::Valid
|
|
|
838
900
|
let kw = extract_kwargs_no_draft(ruby, parsed_args.keywords)?;
|
|
839
901
|
|
|
840
902
|
let json_schema = to_schema_value(ruby, schema)?;
|
|
841
|
-
let parsed = build_parsed_options(ruby, kw, None)?;
|
|
903
|
+
let parsed = build_parsed_options::<Magnus>(ruby, kw, None)?;
|
|
842
904
|
let callback_roots = parsed.callback_roots;
|
|
843
905
|
let compilation_roots = parsed.compilation_roots;
|
|
844
906
|
let mask = parsed.mask;
|
|
@@ -879,7 +941,7 @@ fn bundle(ruby: &Ruby, args: &[Value]) -> Result<Value, Error> {
|
|
|
879
941
|
let kw = extract_kwargs(ruby, parsed_args.keywords)?;
|
|
880
942
|
|
|
881
943
|
let json_schema = to_schema_value(ruby, schema)?;
|
|
882
|
-
let parsed = build_parsed_options(ruby, kw, None)?;
|
|
944
|
+
let parsed = build_parsed_options::<Magnus>(ruby, kw, None)?;
|
|
883
945
|
let mut options = match parsed.retriever {
|
|
884
946
|
Some(ret) => parsed.options.with_retriever(ret),
|
|
885
947
|
None => parsed.options,
|
|
@@ -902,7 +964,7 @@ fn dereference(ruby: &Ruby, args: &[Value]) -> Result<Value, Error> {
|
|
|
902
964
|
let kw = extract_kwargs(ruby, parsed_args.keywords)?;
|
|
903
965
|
|
|
904
966
|
let json_schema = to_schema_value(ruby, schema)?;
|
|
905
|
-
let parsed = build_parsed_options(ruby, kw, None)?;
|
|
967
|
+
let parsed = build_parsed_options::<Magnus>(ruby, kw, None)?;
|
|
906
968
|
let mut options = match parsed.retriever {
|
|
907
969
|
Some(ret) => parsed.options.with_retriever(ret),
|
|
908
970
|
None => parsed.options,
|
|
@@ -934,7 +996,7 @@ fn is_valid(ruby: &Ruby, args: &[Value]) -> Result<bool, Error> {
|
|
|
934
996
|
let kw = extract_kwargs(ruby, parsed_args.keywords)?;
|
|
935
997
|
|
|
936
998
|
let json_schema = to_schema_value(ruby, schema)?;
|
|
937
|
-
let parsed = build_parsed_options(ruby, kw, None)?;
|
|
999
|
+
let parsed = build_parsed_options::<Magnus>(ruby, kw, None)?;
|
|
938
1000
|
let BuiltValidator {
|
|
939
1001
|
validator,
|
|
940
1002
|
compilation_roots: _compilation_roots,
|
|
@@ -962,7 +1024,7 @@ fn validate(ruby: &Ruby, args: &[Value]) -> Result<(), Error> {
|
|
|
962
1024
|
let kw = extract_kwargs(ruby, parsed_args.keywords)?;
|
|
963
1025
|
|
|
964
1026
|
let json_schema = to_schema_value(ruby, schema)?;
|
|
965
|
-
let parsed = build_parsed_options(ruby, kw, None)?;
|
|
1027
|
+
let parsed = build_parsed_options::<Magnus>(ruby, kw, None)?;
|
|
966
1028
|
let BuiltValidator {
|
|
967
1029
|
validator,
|
|
968
1030
|
compilation_roots: _compilation_roots,
|
|
@@ -983,7 +1045,7 @@ fn validate(ruby: &Ruby, args: &[Value]) -> Result<(), Error> {
|
|
|
983
1045
|
Ok(Err(error)) => Err(raise_validation_error(
|
|
984
1046
|
ruby,
|
|
985
1047
|
error,
|
|
986
|
-
Some(node),
|
|
1048
|
+
Some(InstanceRoot::Object(node)),
|
|
987
1049
|
parsed.mask.as_deref(),
|
|
988
1050
|
)),
|
|
989
1051
|
Err(err) => Err(handle_callback_panic(ruby, err)),
|
|
@@ -998,7 +1060,7 @@ fn each_error(ruby: &Ruby, args: &[Value]) -> Result<Value, Error> {
|
|
|
998
1060
|
let kw = extract_kwargs(ruby, parsed_args.keywords)?;
|
|
999
1061
|
|
|
1000
1062
|
let json_schema = to_schema_value(ruby, schema)?;
|
|
1001
|
-
let parsed = build_parsed_options(ruby, kw, None)?;
|
|
1063
|
+
let parsed = build_parsed_options::<Magnus>(ruby, kw, None)?;
|
|
1002
1064
|
let BuiltValidator {
|
|
1003
1065
|
validator,
|
|
1004
1066
|
compilation_roots: _compilation_roots,
|
|
@@ -1020,8 +1082,12 @@ fn each_error(ruby: &Ruby, args: &[Value]) -> Result<Value, Error> {
|
|
|
1020
1082
|
loop {
|
|
1021
1083
|
match catch_unwind_silent(AssertUnwindSafe(|| iter.next())) {
|
|
1022
1084
|
Ok(Some(error)) => {
|
|
1023
|
-
let ruby_error =
|
|
1024
|
-
|
|
1085
|
+
let ruby_error = to_ruby_error_value(
|
|
1086
|
+
ruby,
|
|
1087
|
+
error,
|
|
1088
|
+
Some(InstanceRoot::Object(node)),
|
|
1089
|
+
parsed.mask.as_deref(),
|
|
1090
|
+
)?;
|
|
1025
1091
|
ruby.yield_value::<Value, Value>(ruby_error)?;
|
|
1026
1092
|
}
|
|
1027
1093
|
Ok(None) => break,
|
|
@@ -1040,7 +1106,7 @@ fn each_error(ruby: &Ruby, args: &[Value]) -> Result<Value, Error> {
|
|
|
1040
1106
|
arr.push(to_ruby_error_value(
|
|
1041
1107
|
ruby,
|
|
1042
1108
|
error,
|
|
1043
|
-
Some(node),
|
|
1109
|
+
Some(InstanceRoot::Object(node)),
|
|
1044
1110
|
parsed.mask.as_deref(),
|
|
1045
1111
|
)?)?;
|
|
1046
1112
|
}
|
|
@@ -1058,7 +1124,7 @@ fn evaluate(ruby: &Ruby, args: &[Value]) -> Result<Evaluation, Error> {
|
|
|
1058
1124
|
let kw = extract_evaluate_kwargs(ruby, parsed_args.keywords)?;
|
|
1059
1125
|
|
|
1060
1126
|
let json_schema = to_schema_value(ruby, schema)?;
|
|
1061
|
-
let parsed = build_parsed_options(ruby, kw, None)?;
|
|
1127
|
+
let parsed = build_parsed_options::<Magnus>(ruby, kw, None)?;
|
|
1062
1128
|
let BuiltValidator {
|
|
1063
1129
|
validator,
|
|
1064
1130
|
compilation_roots: _compilation_roots,
|
|
@@ -1102,7 +1168,7 @@ macro_rules! define_draft_validator {
|
|
|
1102
1168
|
let kw = extract_kwargs_no_draft(ruby, parsed_args.keywords)?;
|
|
1103
1169
|
|
|
1104
1170
|
let json_schema = to_schema_value(ruby, schema)?;
|
|
1105
|
-
let parsed = build_parsed_options(ruby, kw, Some($draft))?;
|
|
1171
|
+
let parsed = build_parsed_options::<Magnus>(ruby, kw, Some($draft))?;
|
|
1106
1172
|
let BuiltValidator {
|
|
1107
1173
|
validator,
|
|
1108
1174
|
callback_roots,
|
|
@@ -1263,7 +1329,7 @@ fn meta_validate(ruby: &Ruby, args: &[Value]) -> Result<(), Error> {
|
|
|
1263
1329
|
}
|
|
1264
1330
|
#[allow(unsafe_code)]
|
|
1265
1331
|
let node = RbNode::new(schema.as_raw());
|
|
1266
|
-
raise_validation_error(ruby, error, Some(node), None)
|
|
1332
|
+
raise_validation_error(ruby, error, Some(InstanceRoot::Object(node)), None)
|
|
1267
1333
|
};
|
|
1268
1334
|
if let Some(registry) = registry {
|
|
1269
1335
|
let json_schema = to_schema_value(ruby, schema)?;
|
|
@@ -1395,6 +1461,7 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
|
1395
1461
|
canonical::init_canonical(ruby, &module)?;
|
|
1396
1462
|
|
|
1397
1463
|
// Validator class
|
|
1464
|
+
json_text::define_class(ruby, &module)?;
|
|
1398
1465
|
let validator_class = module.define_class("Validator", ruby.class_object())?;
|
|
1399
1466
|
validator_class.define_method("valid?", method!(Validator::is_valid, 1))?;
|
|
1400
1467
|
validator_class.define_method("validate!", method!(Validator::validate, 1))?;
|