jsonschema_rs 0.48.5 → 0.49.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 +30 -1
- data/Cargo.toml +2 -2
- data/ext/jsonschema/Cargo.lock +15 -13
- data/ext/jsonschema/Cargo.toml +3 -3
- data/lib/jsonschema/version.rb +1 -1
- data/sig/jsonschema.rbs +58 -5
- data/src/canonical.rs +515 -18
- data/src/lib.rs +110 -295
- data/src/options.rs +63 -47
- data/src/validator_map.rs +1 -4
- metadata +1 -1
data/src/canonical.rs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
use jsonschema::{
|
|
2
|
-
canonical::{
|
|
2
|
+
canonical::{
|
|
3
|
+
CanonicalSchema, CanonicalView, CanonicalizationError, ContainsView as CoreContainsView,
|
|
4
|
+
},
|
|
3
5
|
JsonType,
|
|
4
6
|
};
|
|
5
7
|
use magnus::{
|
|
@@ -12,13 +14,17 @@ use magnus::{
|
|
|
12
14
|
};
|
|
13
15
|
|
|
14
16
|
use crate::{
|
|
15
|
-
options::parse_draft_symbol,
|
|
17
|
+
options::{extract_pattern_options, parse_draft_symbol, RbPatternOptions},
|
|
16
18
|
ser::{to_schema_value, value_to_ruby},
|
|
17
19
|
static_id::define_rb_intern,
|
|
18
20
|
};
|
|
19
21
|
|
|
20
22
|
define_rb_intern!(static CANONICAL_KW_DRAFT: "draft");
|
|
21
23
|
define_rb_intern!(static CANONICAL_KW_VALIDATE_FORMATS: "validate_formats");
|
|
24
|
+
define_rb_intern!(static CANONICAL_KW_PATTERN_OPTIONS: "pattern_options");
|
|
25
|
+
|
|
26
|
+
type CanonicalKwArgs =
|
|
27
|
+
magnus::scan_args::KwArgs<(), (Option<Value>, Option<bool>, Option<Value>), ()>;
|
|
22
28
|
|
|
23
29
|
macro_rules! canonical_error_class {
|
|
24
30
|
($static_name:ident, $class_name:literal) => {
|
|
@@ -40,6 +46,7 @@ macro_rules! canonical_error_class {
|
|
|
40
46
|
|
|
41
47
|
canonical_error_class!(CANONICALIZATION_ERROR_CLASS, "CanonicalizationError");
|
|
42
48
|
canonical_error_class!(INVALID_SCHEMA_TYPE_CLASS, "InvalidSchemaType");
|
|
49
|
+
canonical_error_class!(INVALID_PATTERN_CLASS, "InvalidPattern");
|
|
43
50
|
|
|
44
51
|
fn canonicalization_error(ruby: &Ruby, error: CanonicalizationError) -> Error {
|
|
45
52
|
if let CanonicalizationError::ValidationError(validation_error) = error {
|
|
@@ -50,6 +57,9 @@ fn canonicalization_error(ruby: &Ruby, error: CanonicalizationError) -> Error {
|
|
|
50
57
|
CanonicalizationError::InvalidSchemaType(_) => {
|
|
51
58
|
Error::new(ruby.get_inner(&INVALID_SCHEMA_TYPE_CLASS), message)
|
|
52
59
|
}
|
|
60
|
+
CanonicalizationError::InvalidPattern { .. } => {
|
|
61
|
+
Error::new(ruby.get_inner(&INVALID_PATTERN_CLASS), message)
|
|
62
|
+
}
|
|
53
63
|
// `ValidationError` returns above; future variants fall back to the base canonical error.
|
|
54
64
|
_ => Error::new(ruby.get_inner(&CANONICALIZATION_ERROR_CLASS), message),
|
|
55
65
|
}
|
|
@@ -130,18 +140,50 @@ impl RbCanonicalSchema {
|
|
|
130
140
|
min_length: view.min_length,
|
|
131
141
|
max_length: view.max_length,
|
|
132
142
|
patterns: view.patterns,
|
|
143
|
+
formats: view.formats,
|
|
144
|
+
content_media_types: view.content_media_types,
|
|
145
|
+
content_encodings: view.content_encodings,
|
|
146
|
+
})
|
|
147
|
+
.as_value(),
|
|
148
|
+
CanonicalView::Number(view) => ruby
|
|
149
|
+
.obj_wrap(NumberView {
|
|
150
|
+
minimum: view.minimum,
|
|
151
|
+
exclusive_minimum: view.exclusive_minimum,
|
|
152
|
+
maximum: view.maximum,
|
|
153
|
+
exclusive_maximum: view.exclusive_maximum,
|
|
154
|
+
multiple_of: view.multiple_of,
|
|
133
155
|
})
|
|
134
156
|
.as_value(),
|
|
135
157
|
CanonicalView::Integer(view) => ruby
|
|
136
158
|
.obj_wrap(IntegerView {
|
|
137
159
|
minimum: view.minimum,
|
|
138
160
|
maximum: view.maximum,
|
|
161
|
+
multiple_of: view.multiple_of,
|
|
162
|
+
})
|
|
163
|
+
.as_value(),
|
|
164
|
+
CanonicalView::Array(view) => ruby
|
|
165
|
+
.obj_wrap(ArrayView {
|
|
166
|
+
min_items: view.min_items,
|
|
167
|
+
max_items: view.max_items,
|
|
168
|
+
unique_items: view.unique_items,
|
|
169
|
+
prefix_items: view.prefix_items,
|
|
170
|
+
items: view.items,
|
|
171
|
+
contains: view.contains,
|
|
172
|
+
})
|
|
173
|
+
.as_value(),
|
|
174
|
+
CanonicalView::Object(view) => ruby
|
|
175
|
+
.obj_wrap(ObjectView {
|
|
176
|
+
min_properties: view.min_properties,
|
|
177
|
+
max_properties: view.max_properties,
|
|
178
|
+
required: view.required,
|
|
179
|
+
property_names: view.property_names,
|
|
180
|
+
properties: view.properties,
|
|
181
|
+
pattern_properties: view.pattern_properties,
|
|
182
|
+
additional_properties: view.additional_properties,
|
|
139
183
|
})
|
|
140
184
|
.as_value(),
|
|
141
185
|
CanonicalView::AnyOf(branches) => ruby.obj_wrap(AnyOfView { branches }).as_value(),
|
|
142
186
|
CanonicalView::Raw(schema) => ruby.obj_wrap(RawView { schema }).as_value(),
|
|
143
|
-
// TODO(canonical): new `CanonicalView` variants need view classes here.
|
|
144
|
-
other => unreachable!("unsupported canonical view: {other:?}"),
|
|
145
187
|
}
|
|
146
188
|
}
|
|
147
189
|
|
|
@@ -289,13 +331,36 @@ fn bound_to_ruby(ruby: &Ruby, bound: Option<&serde_json::Number>) -> Result<Valu
|
|
|
289
331
|
}
|
|
290
332
|
}
|
|
291
333
|
|
|
292
|
-
|
|
334
|
+
fn divisors_to_ruby(ruby: &Ruby, divisors: &[serde_json::Number]) -> Result<Value, Error> {
|
|
335
|
+
let array = ruby.ary_new_capa(divisors.len());
|
|
336
|
+
for divisor in divisors {
|
|
337
|
+
array.push(value_to_ruby(
|
|
338
|
+
ruby,
|
|
339
|
+
&serde_json::Value::Number(divisor.clone()),
|
|
340
|
+
)?)?;
|
|
341
|
+
}
|
|
342
|
+
Ok(array.as_value())
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
fn strings_to_ruby(ruby: &Ruby, values: &[String]) -> Result<Value, Error> {
|
|
346
|
+
let array = ruby.ary_new_capa(values.len());
|
|
347
|
+
for value in values {
|
|
348
|
+
array.push(ruby.str_new(value).as_value())?;
|
|
349
|
+
}
|
|
350
|
+
Ok(array.as_value())
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/// A string value within a length window, matching every pattern, format, media type, and
|
|
354
|
+
/// encoding.
|
|
293
355
|
#[derive(magnus::TypedData)]
|
|
294
356
|
#[magnus(class = "JSONSchema::Canonical::StringView", free_immediately)]
|
|
295
357
|
pub struct StringView {
|
|
296
358
|
min_length: Option<serde_json::Number>,
|
|
297
359
|
max_length: Option<serde_json::Number>,
|
|
298
360
|
patterns: Vec<String>,
|
|
361
|
+
formats: Vec<String>,
|
|
362
|
+
content_media_types: Vec<String>,
|
|
363
|
+
content_encodings: Vec<String>,
|
|
299
364
|
}
|
|
300
365
|
|
|
301
366
|
impl DataTypeFunctions for StringView {}
|
|
@@ -310,19 +375,30 @@ impl StringView {
|
|
|
310
375
|
}
|
|
311
376
|
|
|
312
377
|
fn patterns(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
378
|
+
strings_to_ruby(ruby, &rb_self.patterns)
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
fn formats(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
382
|
+
strings_to_ruby(ruby, &rb_self.formats)
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
fn content_media_types(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
386
|
+
strings_to_ruby(ruby, &rb_self.content_media_types)
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
fn content_encodings(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
390
|
+
strings_to_ruby(ruby, &rb_self.content_encodings)
|
|
318
391
|
}
|
|
319
392
|
|
|
320
393
|
fn inspect(ruby: &Ruby, rb_self: &Self) -> Result<String, Error> {
|
|
321
394
|
Ok(format!(
|
|
322
|
-
"#<JSONSchema::Canonical::StringView min_length={} max_length={} patterns={}>",
|
|
395
|
+
"#<JSONSchema::Canonical::StringView min_length={} max_length={} patterns={} formats={} content_media_types={} content_encodings={}>",
|
|
323
396
|
Self::min_length(ruby, rb_self)?.inspect(),
|
|
324
397
|
Self::max_length(ruby, rb_self)?.inspect(),
|
|
325
|
-
Self::patterns(ruby, rb_self)?.inspect()
|
|
398
|
+
Self::patterns(ruby, rb_self)?.inspect(),
|
|
399
|
+
Self::formats(ruby, rb_self)?.inspect(),
|
|
400
|
+
Self::content_media_types(ruby, rb_self)?.inspect(),
|
|
401
|
+
Self::content_encodings(ruby, rb_self)?.inspect()
|
|
326
402
|
))
|
|
327
403
|
}
|
|
328
404
|
|
|
@@ -331,16 +407,91 @@ impl StringView {
|
|
|
331
407
|
hash.aset(ruby.sym_new("min_length"), Self::min_length(ruby, rb_self)?)?;
|
|
332
408
|
hash.aset(ruby.sym_new("max_length"), Self::max_length(ruby, rb_self)?)?;
|
|
333
409
|
hash.aset(ruby.sym_new("patterns"), Self::patterns(ruby, rb_self)?)?;
|
|
410
|
+
hash.aset(ruby.sym_new("formats"), Self::formats(ruby, rb_self)?)?;
|
|
411
|
+
hash.aset(
|
|
412
|
+
ruby.sym_new("content_media_types"),
|
|
413
|
+
Self::content_media_types(ruby, rb_self)?,
|
|
414
|
+
)?;
|
|
415
|
+
hash.aset(
|
|
416
|
+
ruby.sym_new("content_encodings"),
|
|
417
|
+
Self::content_encodings(ruby, rb_self)?,
|
|
418
|
+
)?;
|
|
419
|
+
Ok(hash)
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/// A number value within a real interval.
|
|
424
|
+
#[derive(magnus::TypedData)]
|
|
425
|
+
#[magnus(class = "JSONSchema::Canonical::NumberView", free_immediately)]
|
|
426
|
+
pub struct NumberView {
|
|
427
|
+
minimum: Option<serde_json::Number>,
|
|
428
|
+
exclusive_minimum: bool,
|
|
429
|
+
maximum: Option<serde_json::Number>,
|
|
430
|
+
exclusive_maximum: bool,
|
|
431
|
+
multiple_of: Vec<serde_json::Number>,
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
impl DataTypeFunctions for NumberView {}
|
|
435
|
+
|
|
436
|
+
impl NumberView {
|
|
437
|
+
fn minimum(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
438
|
+
bound_to_ruby(ruby, rb_self.minimum.as_ref())
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
fn exclusive_minimum(ruby: &Ruby, rb_self: &Self) -> Value {
|
|
442
|
+
ruby.into_value(rb_self.exclusive_minimum)
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
fn maximum(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
446
|
+
bound_to_ruby(ruby, rb_self.maximum.as_ref())
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
fn exclusive_maximum(ruby: &Ruby, rb_self: &Self) -> Value {
|
|
450
|
+
ruby.into_value(rb_self.exclusive_maximum)
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
fn multiple_of(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
454
|
+
divisors_to_ruby(ruby, &rb_self.multiple_of)
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
fn inspect(ruby: &Ruby, rb_self: &Self) -> Result<String, Error> {
|
|
458
|
+
Ok(format!(
|
|
459
|
+
"#<JSONSchema::Canonical::NumberView minimum={} exclusive_minimum={} maximum={} exclusive_maximum={} multiple_of={}>",
|
|
460
|
+
Self::minimum(ruby, rb_self)?.inspect(),
|
|
461
|
+
Self::exclusive_minimum(ruby, rb_self).inspect(),
|
|
462
|
+
Self::maximum(ruby, rb_self)?.inspect(),
|
|
463
|
+
Self::exclusive_maximum(ruby, rb_self).inspect(),
|
|
464
|
+
Self::multiple_of(ruby, rb_self)?.inspect()
|
|
465
|
+
))
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
fn deconstruct_keys(ruby: &Ruby, rb_self: &Self, _keys: Value) -> Result<RHash, Error> {
|
|
469
|
+
let hash = ruby.hash_new();
|
|
470
|
+
hash.aset(ruby.sym_new("minimum"), Self::minimum(ruby, rb_self)?)?;
|
|
471
|
+
hash.aset(
|
|
472
|
+
ruby.sym_new("exclusive_minimum"),
|
|
473
|
+
Self::exclusive_minimum(ruby, rb_self),
|
|
474
|
+
)?;
|
|
475
|
+
hash.aset(ruby.sym_new("maximum"), Self::maximum(ruby, rb_self)?)?;
|
|
476
|
+
hash.aset(
|
|
477
|
+
ruby.sym_new("exclusive_maximum"),
|
|
478
|
+
Self::exclusive_maximum(ruby, rb_self),
|
|
479
|
+
)?;
|
|
480
|
+
hash.aset(
|
|
481
|
+
ruby.sym_new("multiple_of"),
|
|
482
|
+
Self::multiple_of(ruby, rb_self)?,
|
|
483
|
+
)?;
|
|
334
484
|
Ok(hash)
|
|
335
485
|
}
|
|
336
486
|
}
|
|
337
487
|
|
|
338
|
-
/// An integer value within a closed interval.
|
|
488
|
+
/// An integer value within a closed interval, optionally a multiple of a divisor.
|
|
339
489
|
#[derive(magnus::TypedData)]
|
|
340
490
|
#[magnus(class = "JSONSchema::Canonical::IntegerView", free_immediately)]
|
|
341
491
|
pub struct IntegerView {
|
|
342
492
|
minimum: Option<serde_json::Number>,
|
|
343
493
|
maximum: Option<serde_json::Number>,
|
|
494
|
+
multiple_of: Vec<serde_json::Number>,
|
|
344
495
|
}
|
|
345
496
|
|
|
346
497
|
impl DataTypeFunctions for IntegerView {}
|
|
@@ -354,11 +505,16 @@ impl IntegerView {
|
|
|
354
505
|
bound_to_ruby(ruby, rb_self.maximum.as_ref())
|
|
355
506
|
}
|
|
356
507
|
|
|
508
|
+
fn multiple_of(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
509
|
+
divisors_to_ruby(ruby, &rb_self.multiple_of)
|
|
510
|
+
}
|
|
511
|
+
|
|
357
512
|
fn inspect(ruby: &Ruby, rb_self: &Self) -> Result<String, Error> {
|
|
358
513
|
Ok(format!(
|
|
359
|
-
"#<JSONSchema::Canonical::IntegerView minimum={} maximum={}>",
|
|
514
|
+
"#<JSONSchema::Canonical::IntegerView minimum={} maximum={} multiple_of={}>",
|
|
360
515
|
Self::minimum(ruby, rb_self)?.inspect(),
|
|
361
|
-
Self::maximum(ruby, rb_self)?.inspect()
|
|
516
|
+
Self::maximum(ruby, rb_self)?.inspect(),
|
|
517
|
+
Self::multiple_of(ruby, rb_self)?.inspect()
|
|
362
518
|
))
|
|
363
519
|
}
|
|
364
520
|
|
|
@@ -366,6 +522,275 @@ impl IntegerView {
|
|
|
366
522
|
let hash = ruby.hash_new();
|
|
367
523
|
hash.aset(ruby.sym_new("minimum"), Self::minimum(ruby, rb_self)?)?;
|
|
368
524
|
hash.aset(ruby.sym_new("maximum"), Self::maximum(ruby, rb_self)?)?;
|
|
525
|
+
hash.aset(
|
|
526
|
+
ruby.sym_new("multiple_of"),
|
|
527
|
+
Self::multiple_of(ruby, rb_self)?,
|
|
528
|
+
)?;
|
|
529
|
+
Ok(hash)
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/// An array value's constraints.
|
|
534
|
+
#[derive(magnus::TypedData)]
|
|
535
|
+
#[magnus(class = "JSONSchema::Canonical::ArrayView", free_immediately)]
|
|
536
|
+
#[allow(clippy::struct_field_names)]
|
|
537
|
+
pub struct ArrayView {
|
|
538
|
+
min_items: Option<serde_json::Number>,
|
|
539
|
+
max_items: Option<serde_json::Number>,
|
|
540
|
+
unique_items: bool,
|
|
541
|
+
prefix_items: Vec<CanonicalSchema>,
|
|
542
|
+
items: Option<CanonicalSchema>,
|
|
543
|
+
contains: Vec<CoreContainsView>,
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
impl DataTypeFunctions for ArrayView {}
|
|
547
|
+
|
|
548
|
+
impl ArrayView {
|
|
549
|
+
fn min_items(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
550
|
+
bound_to_ruby(ruby, rb_self.min_items.as_ref())
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
fn max_items(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
554
|
+
bound_to_ruby(ruby, rb_self.max_items.as_ref())
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
fn unique_items(ruby: &Ruby, rb_self: &Self) -> Value {
|
|
558
|
+
ruby.into_value(rb_self.unique_items)
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
fn prefix_items(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
562
|
+
let array = ruby.ary_new_capa(rb_self.prefix_items.len());
|
|
563
|
+
for schema in &rb_self.prefix_items {
|
|
564
|
+
array.push(
|
|
565
|
+
ruby.obj_wrap(RbCanonicalSchema {
|
|
566
|
+
inner: schema.clone(),
|
|
567
|
+
})
|
|
568
|
+
.as_value(),
|
|
569
|
+
)?;
|
|
570
|
+
}
|
|
571
|
+
Ok(array.as_value())
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
fn items(ruby: &Ruby, rb_self: &Self) -> Value {
|
|
575
|
+
match &rb_self.items {
|
|
576
|
+
Some(items) => ruby
|
|
577
|
+
.obj_wrap(RbCanonicalSchema {
|
|
578
|
+
inner: items.clone(),
|
|
579
|
+
})
|
|
580
|
+
.as_value(),
|
|
581
|
+
None => ruby.qnil().as_value(),
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
fn contains(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
586
|
+
let array = ruby.ary_new_capa(rb_self.contains.len());
|
|
587
|
+
for facet in &rb_self.contains {
|
|
588
|
+
array.push(ruby.obj_wrap(ContainsView::from_core(facet)).as_value())?;
|
|
589
|
+
}
|
|
590
|
+
Ok(array.as_value())
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
fn inspect(ruby: &Ruby, rb_self: &Self) -> Result<String, Error> {
|
|
594
|
+
Ok(format!(
|
|
595
|
+
"#<JSONSchema::Canonical::ArrayView min_items={} max_items={} unique_items={} prefix_items={} items={} contains={}>",
|
|
596
|
+
Self::min_items(ruby, rb_self)?.inspect(),
|
|
597
|
+
Self::max_items(ruby, rb_self)?.inspect(),
|
|
598
|
+
Self::unique_items(ruby, rb_self).inspect(),
|
|
599
|
+
Self::prefix_items(ruby, rb_self)?.inspect(),
|
|
600
|
+
Self::items(ruby, rb_self).inspect(),
|
|
601
|
+
Self::contains(ruby, rb_self)?.inspect()
|
|
602
|
+
))
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
fn deconstruct_keys(ruby: &Ruby, rb_self: &Self, _keys: Value) -> Result<RHash, Error> {
|
|
606
|
+
let hash = ruby.hash_new();
|
|
607
|
+
hash.aset(ruby.sym_new("min_items"), Self::min_items(ruby, rb_self)?)?;
|
|
608
|
+
hash.aset(ruby.sym_new("max_items"), Self::max_items(ruby, rb_self)?)?;
|
|
609
|
+
hash.aset(
|
|
610
|
+
ruby.sym_new("unique_items"),
|
|
611
|
+
Self::unique_items(ruby, rb_self),
|
|
612
|
+
)?;
|
|
613
|
+
hash.aset(
|
|
614
|
+
ruby.sym_new("prefix_items"),
|
|
615
|
+
Self::prefix_items(ruby, rb_self)?,
|
|
616
|
+
)?;
|
|
617
|
+
hash.aset(ruby.sym_new("items"), Self::items(ruby, rb_self))?;
|
|
618
|
+
hash.aset(ruby.sym_new("contains"), Self::contains(ruby, rb_self)?)?;
|
|
619
|
+
Ok(hash)
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/// One `contains` demand of an array. An absent minimum spells the default of one.
|
|
624
|
+
#[derive(magnus::TypedData)]
|
|
625
|
+
#[magnus(class = "JSONSchema::Canonical::ContainsView", free_immediately)]
|
|
626
|
+
pub struct ContainsView {
|
|
627
|
+
schema: CanonicalSchema,
|
|
628
|
+
min_contains: Option<serde_json::Number>,
|
|
629
|
+
max_contains: Option<serde_json::Number>,
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
impl DataTypeFunctions for ContainsView {}
|
|
633
|
+
|
|
634
|
+
impl ContainsView {
|
|
635
|
+
fn from_core(facet: &CoreContainsView) -> Self {
|
|
636
|
+
Self {
|
|
637
|
+
schema: facet.schema.clone(),
|
|
638
|
+
min_contains: facet.min_contains.clone(),
|
|
639
|
+
max_contains: facet.max_contains.clone(),
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
fn schema(ruby: &Ruby, rb_self: &Self) -> Value {
|
|
644
|
+
ruby.obj_wrap(RbCanonicalSchema {
|
|
645
|
+
inner: rb_self.schema.clone(),
|
|
646
|
+
})
|
|
647
|
+
.as_value()
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
fn min_contains(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
651
|
+
bound_to_ruby(ruby, rb_self.min_contains.as_ref())
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
fn max_contains(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
655
|
+
bound_to_ruby(ruby, rb_self.max_contains.as_ref())
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
fn inspect(ruby: &Ruby, rb_self: &Self) -> Result<String, Error> {
|
|
659
|
+
Ok(format!(
|
|
660
|
+
"#<JSONSchema::Canonical::ContainsView schema={} min_contains={} max_contains={}>",
|
|
661
|
+
Self::schema(ruby, rb_self).inspect(),
|
|
662
|
+
Self::min_contains(ruby, rb_self)?.inspect(),
|
|
663
|
+
Self::max_contains(ruby, rb_self)?.inspect()
|
|
664
|
+
))
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
fn deconstruct_keys(ruby: &Ruby, rb_self: &Self, _keys: Value) -> Result<RHash, Error> {
|
|
668
|
+
let hash = ruby.hash_new();
|
|
669
|
+
hash.aset(ruby.sym_new("schema"), Self::schema(ruby, rb_self))?;
|
|
670
|
+
hash.aset(
|
|
671
|
+
ruby.sym_new("min_contains"),
|
|
672
|
+
Self::min_contains(ruby, rb_self)?,
|
|
673
|
+
)?;
|
|
674
|
+
hash.aset(
|
|
675
|
+
ruby.sym_new("max_contains"),
|
|
676
|
+
Self::max_contains(ruby, rb_self)?,
|
|
677
|
+
)?;
|
|
678
|
+
Ok(hash)
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/// An object value's constraints.
|
|
683
|
+
#[derive(magnus::TypedData)]
|
|
684
|
+
#[magnus(class = "JSONSchema::Canonical::ObjectView", free_immediately)]
|
|
685
|
+
pub struct ObjectView {
|
|
686
|
+
min_properties: Option<serde_json::Number>,
|
|
687
|
+
max_properties: Option<serde_json::Number>,
|
|
688
|
+
required: Vec<String>,
|
|
689
|
+
property_names: Option<CanonicalSchema>,
|
|
690
|
+
properties: std::collections::BTreeMap<String, CanonicalSchema>,
|
|
691
|
+
pattern_properties: std::collections::BTreeMap<String, CanonicalSchema>,
|
|
692
|
+
additional_properties: Option<CanonicalSchema>,
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
impl DataTypeFunctions for ObjectView {}
|
|
696
|
+
|
|
697
|
+
impl ObjectView {
|
|
698
|
+
fn min_properties(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
699
|
+
bound_to_ruby(ruby, rb_self.min_properties.as_ref())
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
fn max_properties(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
703
|
+
bound_to_ruby(ruby, rb_self.max_properties.as_ref())
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
fn required(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
707
|
+
let array = ruby.ary_new_capa(rb_self.required.len());
|
|
708
|
+
for key in &rb_self.required {
|
|
709
|
+
array.push(ruby.str_new(key))?;
|
|
710
|
+
}
|
|
711
|
+
Ok(array.as_value())
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
fn property_names(ruby: &Ruby, rb_self: &Self) -> Value {
|
|
715
|
+
match &rb_self.property_names {
|
|
716
|
+
Some(names) => ruby
|
|
717
|
+
.obj_wrap(RbCanonicalSchema {
|
|
718
|
+
inner: names.clone(),
|
|
719
|
+
})
|
|
720
|
+
.as_value(),
|
|
721
|
+
None => ruby.qnil().as_value(),
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
fn properties(ruby: &Ruby, rb_self: &Self) -> Result<RHash, Error> {
|
|
726
|
+
let hash = ruby.hash_new();
|
|
727
|
+
for (key, schema) in &rb_self.properties {
|
|
728
|
+
let wrapped = ruby.obj_wrap(RbCanonicalSchema {
|
|
729
|
+
inner: schema.clone(),
|
|
730
|
+
});
|
|
731
|
+
hash.aset(key.as_str(), wrapped)?;
|
|
732
|
+
}
|
|
733
|
+
Ok(hash)
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
fn pattern_properties(ruby: &Ruby, rb_self: &Self) -> Result<RHash, Error> {
|
|
737
|
+
let hash = ruby.hash_new();
|
|
738
|
+
for (pattern, schema) in &rb_self.pattern_properties {
|
|
739
|
+
let wrapped = ruby.obj_wrap(RbCanonicalSchema {
|
|
740
|
+
inner: schema.clone(),
|
|
741
|
+
});
|
|
742
|
+
hash.aset(pattern.as_str(), wrapped)?;
|
|
743
|
+
}
|
|
744
|
+
Ok(hash)
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
fn additional_properties(ruby: &Ruby, rb_self: &Self) -> Value {
|
|
748
|
+
match &rb_self.additional_properties {
|
|
749
|
+
Some(shield) => ruby
|
|
750
|
+
.obj_wrap(RbCanonicalSchema {
|
|
751
|
+
inner: shield.clone(),
|
|
752
|
+
})
|
|
753
|
+
.as_value(),
|
|
754
|
+
None => ruby.qnil().as_value(),
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
fn inspect(ruby: &Ruby, rb_self: &Self) -> Result<String, Error> {
|
|
759
|
+
Ok(format!(
|
|
760
|
+
"#<JSONSchema::Canonical::ObjectView min_properties={} max_properties={} required={} property_names={} properties={} pattern_properties={}>",
|
|
761
|
+
Self::min_properties(ruby, rb_self)?.inspect(),
|
|
762
|
+
Self::max_properties(ruby, rb_self)?.inspect(),
|
|
763
|
+
Self::required(ruby, rb_self)?.inspect(),
|
|
764
|
+
Self::property_names(ruby, rb_self).inspect(),
|
|
765
|
+
Self::properties(ruby, rb_self)?.inspect(),
|
|
766
|
+
Self::pattern_properties(ruby, rb_self)?.inspect()
|
|
767
|
+
))
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
fn deconstruct_keys(ruby: &Ruby, rb_self: &Self, _keys: Value) -> Result<RHash, Error> {
|
|
771
|
+
let hash = ruby.hash_new();
|
|
772
|
+
hash.aset(
|
|
773
|
+
ruby.sym_new("min_properties"),
|
|
774
|
+
Self::min_properties(ruby, rb_self)?,
|
|
775
|
+
)?;
|
|
776
|
+
hash.aset(
|
|
777
|
+
ruby.sym_new("max_properties"),
|
|
778
|
+
Self::max_properties(ruby, rb_self)?,
|
|
779
|
+
)?;
|
|
780
|
+
hash.aset(ruby.sym_new("required"), Self::required(ruby, rb_self)?)?;
|
|
781
|
+
hash.aset(
|
|
782
|
+
ruby.sym_new("property_names"),
|
|
783
|
+
Self::property_names(ruby, rb_self),
|
|
784
|
+
)?;
|
|
785
|
+
hash.aset(ruby.sym_new("properties"), Self::properties(ruby, rb_self)?)?;
|
|
786
|
+
hash.aset(
|
|
787
|
+
ruby.sym_new("pattern_properties"),
|
|
788
|
+
Self::pattern_properties(ruby, rb_self)?,
|
|
789
|
+
)?;
|
|
790
|
+
hash.aset(
|
|
791
|
+
ruby.sym_new("additional_properties"),
|
|
792
|
+
Self::additional_properties(ruby, rb_self),
|
|
793
|
+
)?;
|
|
369
794
|
Ok(hash)
|
|
370
795
|
}
|
|
371
796
|
}
|
|
@@ -475,12 +900,16 @@ fn canonicalize(ruby: &Ruby, args: &[Value]) -> Result<Value, Error> {
|
|
|
475
900
|
let parsed = scan_args::<(Value,), (), (), (), _, ()>(args)?;
|
|
476
901
|
let (schema_arg,) = parsed.required;
|
|
477
902
|
let keywords: RHash = parsed.keywords;
|
|
478
|
-
let base_kwargs:
|
|
903
|
+
let base_kwargs: CanonicalKwArgs = get_kwargs(
|
|
479
904
|
keywords,
|
|
480
905
|
&[],
|
|
481
|
-
&[
|
|
906
|
+
&[
|
|
907
|
+
*CANONICAL_KW_DRAFT,
|
|
908
|
+
*CANONICAL_KW_VALIDATE_FORMATS,
|
|
909
|
+
*CANONICAL_KW_PATTERN_OPTIONS,
|
|
910
|
+
],
|
|
482
911
|
)?;
|
|
483
|
-
let (draft_val, validate_formats) = base_kwargs.optional;
|
|
912
|
+
let (draft_val, validate_formats, pattern_options) = base_kwargs.optional;
|
|
484
913
|
|
|
485
914
|
let schema_value = to_schema_value(ruby, schema_arg)?;
|
|
486
915
|
let mut options = jsonschema::canonical::options();
|
|
@@ -490,6 +919,12 @@ fn canonicalize(ruby: &Ruby, args: &[Value]) -> Result<Value, Error> {
|
|
|
490
919
|
if let Some(validate_formats) = validate_formats {
|
|
491
920
|
options = options.should_validate_formats(validate_formats);
|
|
492
921
|
}
|
|
922
|
+
if let Some(val) = pattern_options {
|
|
923
|
+
match extract_pattern_options(ruby, val)? {
|
|
924
|
+
RbPatternOptions::Fancy(inner) => options = options.with_pattern_options(inner),
|
|
925
|
+
RbPatternOptions::Regex(inner) => options = options.with_pattern_options(inner),
|
|
926
|
+
}
|
|
927
|
+
}
|
|
493
928
|
options
|
|
494
929
|
.canonicalize(&schema_value)
|
|
495
930
|
.map(|inner| ruby.obj_wrap(RbCanonicalSchema { inner }).as_value())
|
|
@@ -502,6 +937,7 @@ pub(crate) fn init_canonical(ruby: &Ruby, module: &RModule) -> Result<(), Error>
|
|
|
502
937
|
let base_error =
|
|
503
938
|
canonical_module.define_error("CanonicalizationError", ruby.exception_standard_error())?;
|
|
504
939
|
canonical_module.define_error("InvalidSchemaType", base_error)?;
|
|
940
|
+
canonical_module.define_error("InvalidPattern", base_error)?;
|
|
505
941
|
|
|
506
942
|
let canonical_schema = canonical_module.define_class("CanonicalSchema", ruby.class_object())?;
|
|
507
943
|
canonical_schema.define_method(
|
|
@@ -560,18 +996,79 @@ pub(crate) fn init_canonical(ruby: &Ruby, module: &RModule) -> Result<(), Error>
|
|
|
560
996
|
string_view.define_method("min_length", method!(StringView::min_length, 0))?;
|
|
561
997
|
string_view.define_method("max_length", method!(StringView::max_length, 0))?;
|
|
562
998
|
string_view.define_method("patterns", method!(StringView::patterns, 0))?;
|
|
999
|
+
string_view.define_method("formats", method!(StringView::formats, 0))?;
|
|
1000
|
+
string_view.define_method(
|
|
1001
|
+
"content_media_types",
|
|
1002
|
+
method!(StringView::content_media_types, 0),
|
|
1003
|
+
)?;
|
|
1004
|
+
string_view.define_method(
|
|
1005
|
+
"content_encodings",
|
|
1006
|
+
method!(StringView::content_encodings, 0),
|
|
1007
|
+
)?;
|
|
563
1008
|
string_view.define_method("inspect", method!(StringView::inspect, 0))?;
|
|
564
1009
|
string_view.define_method("deconstruct_keys", method!(StringView::deconstruct_keys, 1))?;
|
|
565
1010
|
|
|
1011
|
+
let number_view = canonical_module.define_class("NumberView", ruby.class_object())?;
|
|
1012
|
+
number_view.define_method("minimum", method!(NumberView::minimum, 0))?;
|
|
1013
|
+
number_view.define_method(
|
|
1014
|
+
"exclusive_minimum",
|
|
1015
|
+
method!(NumberView::exclusive_minimum, 0),
|
|
1016
|
+
)?;
|
|
1017
|
+
number_view.define_method("maximum", method!(NumberView::maximum, 0))?;
|
|
1018
|
+
number_view.define_method(
|
|
1019
|
+
"exclusive_maximum",
|
|
1020
|
+
method!(NumberView::exclusive_maximum, 0),
|
|
1021
|
+
)?;
|
|
1022
|
+
number_view.define_method("multiple_of", method!(NumberView::multiple_of, 0))?;
|
|
1023
|
+
number_view.define_method("inspect", method!(NumberView::inspect, 0))?;
|
|
1024
|
+
number_view.define_method("deconstruct_keys", method!(NumberView::deconstruct_keys, 1))?;
|
|
1025
|
+
|
|
566
1026
|
let integer_view = canonical_module.define_class("IntegerView", ruby.class_object())?;
|
|
567
1027
|
integer_view.define_method("minimum", method!(IntegerView::minimum, 0))?;
|
|
568
1028
|
integer_view.define_method("maximum", method!(IntegerView::maximum, 0))?;
|
|
1029
|
+
integer_view.define_method("multiple_of", method!(IntegerView::multiple_of, 0))?;
|
|
569
1030
|
integer_view.define_method("inspect", method!(IntegerView::inspect, 0))?;
|
|
570
1031
|
integer_view.define_method(
|
|
571
1032
|
"deconstruct_keys",
|
|
572
1033
|
method!(IntegerView::deconstruct_keys, 1),
|
|
573
1034
|
)?;
|
|
574
1035
|
|
|
1036
|
+
let array_view = canonical_module.define_class("ArrayView", ruby.class_object())?;
|
|
1037
|
+
array_view.define_method("min_items", method!(ArrayView::min_items, 0))?;
|
|
1038
|
+
array_view.define_method("max_items", method!(ArrayView::max_items, 0))?;
|
|
1039
|
+
array_view.define_method("unique_items", method!(ArrayView::unique_items, 0))?;
|
|
1040
|
+
array_view.define_method("prefix_items", method!(ArrayView::prefix_items, 0))?;
|
|
1041
|
+
array_view.define_method("items", method!(ArrayView::items, 0))?;
|
|
1042
|
+
array_view.define_method("contains", method!(ArrayView::contains, 0))?;
|
|
1043
|
+
array_view.define_method("inspect", method!(ArrayView::inspect, 0))?;
|
|
1044
|
+
array_view.define_method("deconstruct_keys", method!(ArrayView::deconstruct_keys, 1))?;
|
|
1045
|
+
let contains_view = canonical_module.define_class("ContainsView", ruby.class_object())?;
|
|
1046
|
+
contains_view.define_method("schema", method!(ContainsView::schema, 0))?;
|
|
1047
|
+
contains_view.define_method("min_contains", method!(ContainsView::min_contains, 0))?;
|
|
1048
|
+
contains_view.define_method("max_contains", method!(ContainsView::max_contains, 0))?;
|
|
1049
|
+
contains_view.define_method("inspect", method!(ContainsView::inspect, 0))?;
|
|
1050
|
+
contains_view.define_method(
|
|
1051
|
+
"deconstruct_keys",
|
|
1052
|
+
method!(ContainsView::deconstruct_keys, 1),
|
|
1053
|
+
)?;
|
|
1054
|
+
|
|
1055
|
+
let object_view = canonical_module.define_class("ObjectView", ruby.class_object())?;
|
|
1056
|
+
object_view.define_method("min_properties", method!(ObjectView::min_properties, 0))?;
|
|
1057
|
+
object_view.define_method("max_properties", method!(ObjectView::max_properties, 0))?;
|
|
1058
|
+
object_view.define_method("required", method!(ObjectView::required, 0))?;
|
|
1059
|
+
object_view.define_method("property_names", method!(ObjectView::property_names, 0))?;
|
|
1060
|
+
object_view.define_method("properties", method!(ObjectView::properties, 0))?;
|
|
1061
|
+
object_view.define_method(
|
|
1062
|
+
"pattern_properties",
|
|
1063
|
+
method!(ObjectView::pattern_properties, 0),
|
|
1064
|
+
)?;
|
|
1065
|
+
object_view.define_method(
|
|
1066
|
+
"additional_properties",
|
|
1067
|
+
method!(ObjectView::additional_properties, 0),
|
|
1068
|
+
)?;
|
|
1069
|
+
object_view.define_method("inspect", method!(ObjectView::inspect, 0))?;
|
|
1070
|
+
object_view.define_method("deconstruct_keys", method!(ObjectView::deconstruct_keys, 1))?;
|
|
1071
|
+
|
|
575
1072
|
let any_of_view = canonical_module.define_class("AnyOfView", ruby.class_object())?;
|
|
576
1073
|
any_of_view.define_method("branches", method!(AnyOfView::branches, 0))?;
|
|
577
1074
|
any_of_view.define_method("inspect", method!(AnyOfView::inspect, 0))?;
|