|
10 | 10 | //! |
11 | 11 | //! The rest of the utility methods work with specific keywords, like `$id` and `$defs`. |
12 | 12 |
|
13 | | -use core::{clone::Clone, iter::Iterator, option::Option::None}; |
| 13 | +use core::{clone::Clone, convert::TryInto, iter::Iterator, option::Option::None}; |
14 | 14 | use std::string::String; |
15 | 15 |
|
16 | 16 | use schemars::Schema; |
@@ -537,6 +537,103 @@ pub trait SchemaUtilityExtensions { |
537 | 537 | /// ) |
538 | 538 | /// ``` |
539 | 539 | fn get_keyword_as_string(&self, key: &str) -> Option<String>; |
| 540 | + /// Checks a JSON Schema for a given keyword and returns the value of that keyword, if it |
| 541 | + /// exists, as a [`Schema`]. |
| 542 | + /// |
| 543 | + /// If the keyword doesn't exist or isn't a subchema, this function returns [`None`]. |
| 544 | + /// |
| 545 | + /// # Examples |
| 546 | + /// |
| 547 | + /// When the given keyword exists and is a subschema, the function returns the subschema. |
| 548 | + /// |
| 549 | + /// ```rust |
| 550 | + /// use schemars::json_schema; |
| 551 | + /// use dsc_lib_jsonschema::schema_utility_extensions::SchemaUtilityExtensions; |
| 552 | + /// |
| 553 | + /// let ref schema = json_schema!({ |
| 554 | + /// "type": "array", |
| 555 | + /// "items": { |
| 556 | + /// "type": "string" |
| 557 | + /// } |
| 558 | + /// }); |
| 559 | + /// assert_eq!( |
| 560 | + /// schema.get_keyword_as_subschema("items"), |
| 561 | + /// Some(&json_schema!({"type": "string"})) |
| 562 | + /// ); |
| 563 | + /// ``` |
| 564 | + /// |
| 565 | + /// When the given keyword doesn't exist or has the wrong data type, the function returns |
| 566 | + /// [`None`]. |
| 567 | + /// |
| 568 | + /// ```rust |
| 569 | + /// use schemars::json_schema; |
| 570 | + /// use dsc_lib_jsonschema::schema_utility_extensions::SchemaUtilityExtensions; |
| 571 | + /// |
| 572 | + /// let ref schema = json_schema!({ |
| 573 | + /// "items": "invalid" |
| 574 | + /// }); |
| 575 | + /// |
| 576 | + /// assert_eq!( |
| 577 | + /// schema.get_keyword_as_subschema("not_exist"), |
| 578 | + /// None |
| 579 | + /// ); |
| 580 | + /// |
| 581 | + /// assert_eq!( |
| 582 | + /// schema.get_keyword_as_subschema("items"), |
| 583 | + /// None |
| 584 | + /// ) |
| 585 | + /// ``` |
| 586 | + fn get_keyword_as_subschema(&self, key: &str) -> Option<&Schema>; |
| 587 | + /// Checks a JSON Schema for a given keyword and mutably borrows the value of that keyword, |
| 588 | + /// if it exists, as a [`Schema`]. |
| 589 | + /// |
| 590 | + /// If the keyword doesn't exist or isn't a subschema, this function returns [`None`]. |
| 591 | + /// |
| 592 | + /// # Examples |
| 593 | + /// |
| 594 | + /// When the given keyword exists and is a subschema, the function returns the subschema. |
| 595 | + /// |
| 596 | + /// ```rust |
| 597 | + /// use schemars::json_schema; |
| 598 | + /// use serde_json::json; |
| 599 | + /// use dsc_lib_jsonschema::schema_utility_extensions::SchemaUtilityExtensions; |
| 600 | + /// |
| 601 | + /// let ref mut subschema = json_schema!({ |
| 602 | + /// "type": "string" |
| 603 | + /// }); |
| 604 | + /// let ref mut schema = json_schema!({ |
| 605 | + /// "type": "array", |
| 606 | + /// "items": subschema |
| 607 | + /// }); |
| 608 | + /// assert_eq!( |
| 609 | + /// schema.get_keyword_as_subschema_mut("items"), |
| 610 | + /// Some(subschema) |
| 611 | + /// ); |
| 612 | + /// ``` |
| 613 | + /// |
| 614 | + /// When the given keyword doesn't exist or has the wrong data type, the function returns |
| 615 | + /// [`None`]. |
| 616 | + /// |
| 617 | + /// ```rust |
| 618 | + /// use schemars::json_schema; |
| 619 | + /// use serde_json::json; |
| 620 | + /// use dsc_lib_jsonschema::schema_utility_extensions::SchemaUtilityExtensions; |
| 621 | + /// |
| 622 | + /// let ref mut schema = json_schema!({ |
| 623 | + /// "items": "invalid" |
| 624 | + /// }); |
| 625 | + /// |
| 626 | + /// assert_eq!( |
| 627 | + /// schema.get_keyword_as_object_mut("not_exist"), |
| 628 | + /// None |
| 629 | + /// ); |
| 630 | + /// |
| 631 | + /// assert_eq!( |
| 632 | + /// schema.get_keyword_as_object_mut("items"), |
| 633 | + /// None |
| 634 | + /// ) |
| 635 | + /// ``` |
| 636 | + fn get_keyword_as_subschema_mut(&mut self, key: &str) -> Option<&mut Schema>; |
540 | 637 | /// Checks a JSON schema for a given keyword and returns the value of that keyword, if it |
541 | 638 | /// exists, as a [`u64`]. |
542 | 639 | /// |
@@ -1222,6 +1319,14 @@ impl SchemaUtilityExtensions for Schema { |
1222 | 1319 | .and_then(Value::as_str) |
1223 | 1320 | .map(std::string::ToString::to_string) |
1224 | 1321 | } |
| 1322 | + fn get_keyword_as_subschema(&self, key: &str) -> Option<&Schema> { |
| 1323 | + self.get(key) |
| 1324 | + .and_then(|v| <&Value as TryInto<&Schema>>::try_into(v).ok()) |
| 1325 | + } |
| 1326 | + fn get_keyword_as_subschema_mut(&mut self, key: &str) -> Option<&mut Schema> { |
| 1327 | + self.get_mut(key) |
| 1328 | + .and_then(|v| <&mut Value as TryInto<&mut Schema>>::try_into(v).ok()) |
| 1329 | + } |
1225 | 1330 | fn get_keyword_as_u64(&self, key: &str) -> Option<u64> { |
1226 | 1331 | self.get(key) |
1227 | 1332 | .and_then(Value::as_u64) |
|
0 commit comments