1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
//! Represents a [`Schema`] which is collection of zero or more [`TypeDefinition`]s.
//! Provides functions to get the underlying [`TypeDefinition`]s from the [`Schema`] that can be used to validate an Ion value.
//!
//! * `get_types`: This function returns an [`SchemaTypeIterator`] which can be used to iterate over the [`TypeDefinition`]s.
//! * `get_type`: This function requires to pass the name of a type definition that you want to use for validation.
//! It returns the [`TypeDefinition`] if it is defined in the [`Schema`] otherwise returns [`None`].
//!
use crate::import::Import;
use crate::system::{TypeId, TypeStore};
use crate::types::{TypeDefinition, TypeDefinitionImpl};
use std::sync::Arc;
/// A Schema is a collection of zero or more [`TypeDefinition`]s.
///
/// Each type may refer to other types within the same schema,
/// or types imported into this schema from other schemas.
/// To instantiate a [`Schema`], see [`SchemaSystem`].
///
/// [`SchemaSystem`]: crate::system::SchemaSystem
#[derive(Debug, Clone)]
pub struct Schema {
id: String,
types: Arc<TypeStore>,
}
impl Schema {
pub(crate) fn new<A: AsRef<str>>(id: A, types: Arc<TypeStore>) -> Self {
Self {
id: id.as_ref().to_owned(),
types,
}
}
/// Returns the id for this Schema
pub fn id(&self) -> &str {
&self.id
}
/// Returns an [Import] representing all the types imported from
/// the specified schema [id].
fn import(&self, id: String) -> Option<Import> {
todo!()
}
/// Returns an iterator over the imports of this [`Schema`].
fn imports(&self) -> SchemaTypeIterator {
todo!()
}
/// Returns an iterator over the imported types of this [`Schema`].
fn imported_types(&self) -> SchemaTypeIterator {
SchemaTypeIterator::new(Arc::clone(&self.types), self.types.get_imports())
}
/// Returns the requested type, if present in this schema or a a built in type;
/// otherwise returns None.
pub fn get_type<A: AsRef<str>>(&self, name: A) -> Option<TypeDefinition> {
let type_id = self
.types
.get_built_in_type_id_or_defined_type_id_by_name(name.as_ref())?;
Some(TypeDefinition::new(*type_id, Arc::clone(&self.types)))
}
/// Returns an iterator over the types in this schema.
// This only includes named types defined within this schema.
pub fn get_types(&self) -> SchemaTypeIterator {
SchemaTypeIterator::new(Arc::clone(&self.types), self.types.get_types())
}
/// Returns a new [`Schema`] instance containing all the types of this
/// instance plus the provided type. Note that the added type
/// in the returned instance will hide a type of the same name
/// from this instance.
fn plus_type(&self, schema_type: TypeDefinitionImpl) -> Self {
todo!()
}
}
/// Provides an Iterator which returns [`TypeDefinition`]s inside a [`Schema`]
pub struct SchemaTypeIterator {
type_store: Arc<TypeStore>,
index: usize,
types: Vec<TypeId>,
}
impl SchemaTypeIterator {
fn new(type_store: Arc<TypeStore>, types: Vec<TypeId>) -> Self {
Self {
type_store,
index: 0,
types,
}
}
}
impl Iterator for SchemaTypeIterator {
type Item = TypeDefinition;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.types.len() {
return None;
}
self.index += 1;
Some(TypeDefinition::new(
self.types[self.index - 1],
Arc::clone(&self.type_store),
))
}
}
#[cfg(test)]
mod schema_tests {
use super::*;
use crate::authority::MapDocumentAuthority;
use crate::system::{Resolver, SchemaSystem};
use ion_rs::element::Element;
use rstest::*;
use std::sync::Arc;
// helper function to be used by schema tests
fn load(text: &str) -> Vec<Element> {
Element::read_all(text.as_bytes()).expect("parsing failed unexpectedly")
}
// helper function to be used by validation tests
fn load_schema_from_text(text: &str) -> Arc<Schema> {
// map with (id, ion content)
let map_authority = [("sample.isl", text)];
let mut schema_system =
SchemaSystem::new(vec![Box::new(MapDocumentAuthority::new(map_authority))]);
schema_system.load_schema("sample.isl").unwrap()
}
#[rstest(
owned_elements, total_types,
case::type_constraint_with_named_type(
load(r#" // For a schema with named type as below:
type:: { name: my_type, type: any }
"#).into_iter(),
1 // this includes the named type my_int
),
case::type_constraint_with_self_reference_type(
load(r#" // For a schema with self reference type as below:
type:: { name: my_int, type: my_int }
"#).into_iter(),
1 // this includes only my_int type
),
case::type_constraint_with_nested_self_reference_type(
load(r#" // For a schema with nested self reference type as below:
type:: { name: my_int, type: { type: my_int } }
"#).into_iter(),
1 // this includes my_int type
),
case::type_constraint_with_nested_type(
load(r#" // For a schema with nested types as below:
type:: { name: my_int, type: { type: int } }
"#).into_iter(),
1 // this includes my_int type
),
case::type_constraint_with_nested_multiple_types(
load(r#" // For a schema with nested multiple types as below:
type:: { name: my_int, type: { type: int }, type: { type: my_int } }
"#).into_iter(),
1 // this includes my_int type
),
case::type_constraint_with_multiple_types(
load(r#" // For a schema with multiple type as below:
type:: { name: my_int, type: int }
type:: { name: my_bool, type: bool }
"#).into_iter(),
2
),
case::all_of_constraint(
load(r#" // For a schema with all_of type as below:
type:: { name: all_of_type, all_of: [{ type: int }] }
"#).into_iter(),
1 // this includes named type all_of_type
),
case::any_of_constraint(
load(r#" // For a schema with any_of constraint as below:
type:: { name: any_of_type, any_of: [{ type: int }, { type: decimal }] }
"#).into_iter(),
1 // this includes named type any_of_type
),
case::one_of_constraint(
load(r#" // For a schema with one_of constraint as below:
type:: { name: one_of_type, one_of: [{ type: int }, { type: decimal }] }
"#).into_iter(),
1 // this includes named type one_of_type
),
case::not_constraint(
load(r#" // For a schema with not constraint as below:
type:: { name: not_type, not: { type: int } }
"#).into_iter(),
1 // this includes named type not_type
),
case::ordred_elements_constraint(
load(r#" // For a schema with ordered_elements constraint as below:
type:: { name: ordred_elements_type, ordered_elements: [ symbol, { type: int, occurs: optional }, ] }
"#).into_iter(),
1 // this includes named type ordered_elements_type
),
case::fields_constraint(
load(r#" // For a schema with fields constraint as below:
type:: { name: fields_type, fields: { name: string, id: int} }
"#).into_iter(),
1 // this includes named type fields_type
),
case::field_names_constraint(
load(r#" // For a schema with field_names constraint as below:
$ion_schema_2_0
type:: { name: field_names_type, field_names: distinct::symbol }
"#).into_iter(),
1 // this includes named type field_names_type
),
case::contains_constraint(
load(r#" // For a schema with contains constraint as below:
type:: { name: contains_type, contains: [true, 1, "hello"] }
"#).into_iter(),
1 // this includes named type contains_type
),
case::container_length_constraint(
load(r#" // For a schema with container_length constraint as below:
type:: { name: container_length_type, container_length: 3 }
"#).into_iter(),
1 // this includes named type container_length_type
),
case::byte_length_constraint(
load(r#" // For a schema with byte_length constraint as below:
type:: { name: byte_length_type, byte_length: 3 }
"#).into_iter(),
1 // this includes named type byte_length_type
),
case::codepoint_length_constraint(
load(r#" // For a schema with codepoint_length constraint as below:
type:: { name: codepoint_length_type, codepoint_length: 3 }
"#).into_iter(),
1 // this includes named type codepoint_length_type
),
case::element_constraint(
load(r#" // For a schema with element constraint as below:
type:: { name: element_type, element: int }
"#).into_iter(),
1 // this includes named type element_type
),
case::distinct_element_constraint(
load(r#" // For a schema with distinct element constraint as below:
$ion_schema_2_0
type:: { name: distinct_element_type, element: distinct::int }
"#).into_iter(),
1 // this includes named type distinct_element_type
),
case::annotations_constraint(
load(r#" // For a schema with annotations constraint as below:
type:: { name: annotations_type, annotations: closed::[red, blue, green] }
"#).into_iter(),
1 // this includes named type annotations_type
),
case::precision_constraint(
load(r#" // For a schema with precision constraint as below:
type:: { name: precision_type, precision: 2 }
"#).into_iter(),
1 // this includes named type precision_type
),
case::scale_constraint(
load(r#" // For a schema with scale constraint as below:
type:: { name: scale_type, scale: 2 }
"#).into_iter(),
1 // this includes named type scale_type
),
case::exponent_constraint(
load(r#" // For a schema with exponent constraint as below:
$ion_schema_2_0
type:: { name: exponent_type, exponent: -2 }
"#).into_iter(),
1 // this includes named type exponent_type
),
case::timestamp_precision_constraint(
load(r#" // For a schema with timestamp_precision constraint as below:
type:: { name: timestamp_precision_type, timestamp_precision: month }
"#).into_iter(),
1 // this includes named type timestamp_precision_type
),
case::valid_values_constraint(
load(r#" // For a schema with valid_values constraint as below:
type:: { name: valid_values_type, valid_values: range::[1, 3] }
"#).into_iter(),
1 // this includes named type valid_values_type
),
case::utf8_byte_length_constraint(
load(r#" // For a schema with utf8_byte_length constraint as below:
type:: { name: utf8_byte_length_type, utf8_byte_length: 3 }
"#).into_iter(),
1 // this includes named type utf8_byte_length_type
),
case::regex_constraint(
load(r#" // For a schema with regex constraint as below:
type:: { name: regex_type, regex: "[abc]" }
"#).into_iter(),
1 // this includes named type regex_type
),
case::timestamp_offset_constraint(
load(r#" // For a schema with timestamp_offset constraint as below:
type:: { name: timestamp_offset_type, timestamp_offset: ["+07:00", "+08:00", "+08:45", "+09:00"] }
"#).into_iter(),
1 // this includes named type regex_type
),
case::ieee754_float_constraint(
load(r#" // For a schema with ieee754_float constraint as below:
$ion_schema_2_0
type:: { name: ieee754_float_type, ieee754_float: binary16 }
"#).into_iter(),
1 // this includes named type ieee754_float_type
),
)]
fn owned_elements_to_schema<I: Iterator<Item = Element>>(
owned_elements: I,
total_types: usize,
) {
// create a type_store and resolver instance to be used for loading Elements as schema
let type_store = &mut TypeStore::default();
let mut resolver = Resolver::new(vec![]);
// create a isl from owned_elements and verifies if the result is `ok`
let isl_result = resolver.isl_schema_from_elements(owned_elements, "my_schema.isl");
assert!(isl_result.is_ok());
let isl = isl_result.expect("ISL schema should be syntactically correct");
// create a schema from isl and verifies if the result is `ok`
let schema = resolver.schema_from_isl_schema(isl.version(), isl, type_store, None);
assert!(schema.is_ok());
// check if the types of the created schema matches with the actual types specified by test case
assert_eq!(schema.unwrap().get_types().count(), total_types);
}
#[rstest(
valid_values, invalid_values, schema, type_name,
case::built_in_type(
load(r#"
5
0
-2
"#),
load(r#"
false
"hello"
5.4
"#),
load_schema_from_text(r#" // No schema defined, uses built-in types"#),
"int"
),
case::type_constraint(
load(r#"
5
0
-2
"#),
load(r#"
false
"hello"
5.4
"#),
load_schema_from_text(r#" // For a schema with named type as below:
type:: { name: my_int, type: int }
"#),
"my_int"
),
case::nullable_annotation_int_type_constraint(
load(r#"
null
null.null
null.int
0
-5
"#),
load(r#"
null.decimal
a
"hello"
false
"#),
load_schema_from_text(r#" // For a schema with named type and `nullable` annotation as below:
type:: { name: my_int, type: nullable::int }
"#),
"my_int"
),
case::null_or_annotation_int_type_constraint(
load(r#"
null
null.null
0
-5
"#),
load(r#"
null.decimal
a
"hello"
false
"#),
load_schema_from_text(r#" // For a schema with named type and `$null_or` annotation as below:
$ion_schema_2_0
type:: { name: my_int, type: $null_or::int }
"#),
"my_int"
),
case::nullable_annotation_float_type_constraint(
load(r#"
null
null.null
null.float
5e2
"#),
load(r#"
null.decimal
a
"hello"
false
10
"#),
load_schema_from_text(r#" // For a schema with named type and `nullable` annotation as below:
type:: { name: my_float, type: nullable::float }
"#),
"my_float"
),
case::nullable_annotation_string_type_constraint(
load(r#"
null
null.null
null.string
"hi"
"#),
load(r#"
null.decimal
null.symbol
hello
false
10
"#),
load_schema_from_text(r#" // For a schema with named type and `nullable` annotation as below:
type:: { name: my_string, type: nullable::string }
"#),
"my_string"
),
case::nullable_annotation_symbol_type_constraint(
load(r#"
null
null.null
null.symbol
a
"#),
load(r#"
null.string
10.5
"hello"
false
10
"#),
load_schema_from_text(r#" // For a schema with named type and `nullable` annotation as below:
type:: { name: my_symbol, type: nullable::symbol }
"#),
"my_symbol"
),
case::nullable_annotation_decimal_type_constraint(
load(r#"
null
null.null
null.decimal
10.5
"#),
load(r#"
null.int
a
"hello"
false
10
"#),
load_schema_from_text(r#" // For a schema with named type and `nullable` annotation as below:
type:: { name: my_decimal, type: nullable::decimal }
"#),
"my_decimal"
),
case::nullable_annotation_timestamp_type_constraint(
load(r#"
null
null.null
null.timestamp
2000-01T
"#),
load(r#"
null.decimal
a
"hello"
false
10
"#),
load_schema_from_text(r#" // For a schema with named type and `nullable` annotation as below:
type:: { name: my_timestamp, type: nullable::timestamp }
"#),
"my_timestamp"
),
case::nullable_annotation_blob_type_constraint(
load(r#"
null
null.null
null.blob
{{ aGVsbG8= }}
"#),
load(r#"
null.clob
a
"hello"
false
10
"#),
load_schema_from_text(r#" // For a schema with named type and `nullable` annotation as below:
type:: { name: my_blob, type: nullable::blob }
"#),
"my_blob"
),
case::nullable_annotation_clob_type_constraint(
load(r#"
null
null.null
null.clob
{{"12345"}}
"#),
load(r#"
null.blob
a
"hello"
false
10
"#),
load_schema_from_text(r#" // For a schema with named type and `nullable` annotation as below:
type:: { name: my_clob, type: nullable::clob }
"#),
"my_clob"
),
case::nullable_annotation_text_type_constraint(
load(r#"
null
null.null
null.symbol
null.string
"hello"
hello
"#),
load(r#"
null.decimal
10.5
false
10
"#),
load_schema_from_text(r#" // For a schema with named type and `nullable` annotation as below:
type:: { name: my_text, type: nullable::text }
"#),
"my_text"
),
case::nullable_annotation_lob_type_constraint(
load(r#"
null
null.null
null.blob
null.clob
{{ aGVsbG8= }}
{{"12345"}}
"#),
load(r#"
null.decimal
10.5
false
10
"#),
load_schema_from_text(r#" // For a schema with named type and `nullable` annotation as below:
type:: { name: my_lob, type: nullable::lob }
"#),
"my_lob"
),
case::nullable_annotation_number_type_constraint(
load(r#"
null
null.null
null.int
null.float
null.decimal
10.5
10e2
5
"#),
load(r#"
null.string
"hello"
false
a
"#),
load_schema_from_text(r#" // For a schema with named type and `nullable` annotation as below:
type:: { name: my_number, type: nullable::number }
"#),
"my_number"
),
case::nullable_atomic_type_constraint(
load(r#"
5
0
-2
null.int
"#),
load(r#"
false
"hello"
5.4
"#),
load_schema_from_text(r#" // For a schema with named type as below:
type:: { name: my_nullable_int, type: $int }
"#),
"my_nullable_int"
),
case::nullable_derived_type_constraint(
load(r#"
"hello"
hello
null.string
null.symbol
"#),
load(r#"
false
5
null.int
null.decimal
null.null
5.4
"#),
load_schema_from_text(r#" // For a schema with named type as below:
type:: { name: my_nullable_text, type: $text }
"#),
"my_nullable_text"
),
case::not_constraint(
load(r#"
true
"hello"
5.4
6e10
"#),
load(r#"
5
0
-1
"#),
load_schema_from_text(r#" // For a schema with not constraint as below:
type:: { name: not_type, not: { type: int } }
"#),
"not_type"
),
case::one_of_constraint(
load(r#"
5
-5
5.4
-5.4
"#),
load(r#"
false
"hello"
hey
null.int
"#),
load_schema_from_text(r#" // For a schema with one_of constraint as below:
type:: { name: one_of_type, one_of: [int, decimal] }
"#),
"one_of_type"
),
// TODO: add a test case for all_of constraint
case::any_of_constraint(
load(r#"
5
5.4
true
"#),
load(r#"
"hello"
hey
6e10
"#),
load_schema_from_text(r#" // For a schema with any_of constraint as below:
type:: { name: any_of_type, any_of: [int, decimal, bool] }
"#),
"any_of_type"
),
case::ordered_elements_constraint(
load(r#"
[true, 5, 6, 7, "hey"]
[false, 5, 6, 7]
[false, 7, 8, "hello"]
[true, 7, "hi"]
[true, 8]
"#),
load(r#"
[true]
[5, true, "hey"]
[null.bool, 5]
["hello", 5]
[true, "hey"]
"hello"
hey
6e10
null.list
"#),
load_schema_from_text(r#" // For a schema with ordered_elements constraint as below:
type:: { name: ordered_elements_type, ordered_elements: [bool, { type: int, occurs: range::[1, 3] }, { type: string, occurs: optional } ] }
"#),
"ordered_elements_type"
),
case::ordered_elements_constraint_for_overlapping_types(
load(r#"
[1, 2, 3]
[1, 2, foo]
[1.0, foo]
[1, 2.0, 3]
[1, 2]
[1, foo]
"#),
load(r#"
[1]
[foo]
[true, 1, foo]
"#),
load_schema_from_text(r#" // For a schema with ordered_elements constraint as below:
type:: { name: ordered_elements_type, ordered_elements:[{ type: int, occurs: optional }, { type: number, occurs: required }, { type: any, occurs: required }] }
"#),
"ordered_elements_type"
),
case::fields_constraint(
load(r#"
{ name: "Ion", id: 1 }
{ id: 1 }
{ name: "Ion" }
{ name: "Ion", id: 1, name: "Schema" }
{ } // This is valid because all fields are optional
{ greetings: "hello" } // This is valid because open content is allowed by default
"#),
load(r#"
null.struct
null
{ name: "Ion", id: 1, id: 2 }
"#),
load_schema_from_text(r#" // For a schema with fields constraint as below:
type:: { name: fields_type, fields: { name: { type: string, occurs: range::[0,2] }, id: int } }
"#),
"fields_type"
),
case::fields_constraint_with_closed_content(
load(r#"
{ name: "Ion", id: 1 }
{ id: 1 }
{ name: "Ion" }
{ name: "Ion", id: 1, name: "Schema" }
{ } // This is valid because all fields are optional
{ greetings: "hello" } // This is valid because open content is allowed by default
"#),
load(r#"
null.struct
null
{ name: "Ion", id: 1, id: 2 }
"#),
load_schema_from_text(r#" // For a schema with fields constraint as below:
type:: { name: fields_type, fields: { name: { type: string, occurs: range::[0,2] }, id: int } }
"#),
"fields_type"
),
case::fields_constraint_with_closed_annotation(
load(r#"
{ name: "Ion", id: 1 }
{ id: 1 }
{ name: "Ion" }
{ name: "Ion", id: 1, name: "Schema" }
{ }
"#),
load(r#"
null.struct
null
{ name: "Ion", id: 1, id: 2 }
{ greetings: "hello" }
"#),
load_schema_from_text(r#" // For a schema with fields constraint with `closed` annotation as below:
$ion_schema_2_0
type:: { name: fields_type, fields: closed::{ name: { type: string, occurs: range::[0,2] }, id: int } }
"#),
"fields_type"
),
case::field_names_constraint(
load(r#"
{ name: "Ion", id: 1 }
{ id: 1 }
{ name: "Ion" }
{ }
"#),
load(r#"
null.struct
null
{ name: "Ion", id: 1, name: "Schema" }
{ name: "Ion", id: 1, id: 2 }
"#),
load_schema_from_text(r#" // For a schema with field_names constraint as below:
$ion_schema_2_0
type:: { name: field_names_type, field_names: distinct::symbol }
"#),
"field_names_type"
),
case::contains_constraint(
load(r#"
[[5], '3', {a: 7}, true, 2.0, "4", (6), 1, extra_value]
([5] '3' {a: 7} true 2.0 "4" (6) 1 extra_value)
"#),
load(r#"
null
null.null
null.int
null.list
null.sexp
null.struct
[true, 1, 2.0, '3', "4", [5], (6)]
"#),
load_schema_from_text(r#" // For a schema with contains constraint as below:
type::{ name: contains_type, contains: [true, 1, 2.0, '3', "4", [5], (6), {a: 7} ] }
"#),
"contains_type"
),
case::container_length_with_range_constraint(
load(r#"
[1]
[1, 2]
[1, 2, 3]
(4)
(4 5)
(4 5 6)
{ a: 7 }
{ a: 7, b: 8 }
{ a: 7, b: 8, c: 9 }
"#),
load(r#"
null
null.bool
null.null
null.list
null.sexp
null.struct
[]
()
{}
[1, 2, 3, 4]
(1 2 3 4)
{ a: 1, b:2, c:3, d:4}
"#),
load_schema_from_text(r#" // For a schema with contianer_length constraint as below:
type::{ name: container_length_type, container_length: range::[1,3] }
"#),
"container_length_type"
),
case::container_length_exact_constraint(
load(r#"
[null, null, null]
[1, 2, 3]
(4 5 6)
{ a: 7, b: 8, c: 9 }
"#),
load(r#"
null
null.bool
null.null
null.list
null.sexp
null.struct
[]
()
{}
[1]
(1)
{ a: 1 }
[1, 2, 3, 4]
(1 2 3 4)
{ a: 1, b:2, c:3, d:4}
"#),
load_schema_from_text(r#" // For a schema with contianer_length constraint as below:
type::{ name: container_length_type, container_length: 3 }
"#),
"container_length_type"
),
case::byte_length_constraint(
load(r#"
{{"12345"}}
{{ aGVsbG8= }}
"#),
load(r#"
null
null.bool
null.null
null.clob
null.blob
{{}}
{{"1234"}}
{{"123456"}}
"#),
load_schema_from_text(r#" // For a schema with byte_length constraint as below:
type::{ name: byte_length_type, byte_length: 5 }
"#),
"byte_length_type"
),
case::codepoint_length_constraint(
load(r#"
'12345'
"12345"
"1234😎"
"हैलो!"
"#),
load(r#"
null
null.bool
null.null
null.string
null.symbol
""
"😎"
'1234'
"123456"
"#),
load_schema_from_text(r#" // For a schema with codepoint_length constraint as below:
type::{ name: codepoint_length_type, codepoint_length: 5 }
"#),
"codepoint_length_type"
),
case::element_constraint(
load(r#"
[]
[1]
[1, 2, 3]
()
(1)
(1 2 3)
{ a: 1, b: 2, c: 3 }
"#),
load(r#"
null.list
[1.]
[1e0]
[1, 2, null.int]
(1 2 3 true 4)
{ a: 1, b: 2, c: true }
{ a: 1, b: 2, c: null.int }
"#),
load_schema_from_text(r#" // For a schema with element constraint as below:
type::{ name: element_type, element: int }
"#),
"element_type"
),
case::distinct_element_constraint(
load(r#"
[]
[1]
[1, 2, 3]
()
(1)
(1 2 3)
{ a: 1, b: 2, c: 3 }
"#),
load(r#"
null.list
[1.]
[1e0]
[1, 1, 2, 3]
[a::1, b::1, a::2, a::2, 3]
[1, 2, null.int]
(1 2 3 true 4)
(1 1 2 2 3)
(a::1 b::1 a::2 a::2 3)
{ a: 1, b: 2, c: true }
{ a: 1, b: 1 }
{ a: c::1, b: c::1 }
{ a: 1, b: 2, c: null.int }
"#),
load_schema_from_text(r#" // For a schema with distinct element constraint as below:
$ion_schema_2_0
type::{ name: distinct_element_type, element: distinct::int }
"#),
"distinct_element_type"
),
case::element_with_self_ref_type_constraint(
load(r#"
5
"hello"
[1, 5]
["hi", "hello"]
"#),
load(r#"
5.5
null
null.list
null.int
null.string
(1 2 3)
"#),
load_schema_from_text(r#" // For a schema with element constraint with self referencing typeas below:
type::{ name: my_type, one_of: [ int, string, { type: list, element: my_type } ] }
"#),
"my_type"
),
case::fields_with_self_ref_type_constraint(
load(r#"
5
"hello"
{ foo: "hi" }
{ foo: 5 }
{ foo: { foo: 5 } }
"#),
load(r#"
5.5
null
null.struct
{ foo: bar }
{ foo: 5.5 }
"#),
load_schema_from_text(r#" // For a schema with fields constraint with self referencing typeas below:
type::{ name: my_type, one_of: [ int, string, { type: struct, fields: { foo: my_type} } ] }
"#),
"my_type"
),
case::annotations_constraint(
load(r#"
b::d::5
a::b::d::5
b::c::d::5
a::b::c::d::5
b::a::d::5 // 'a' is treated as open content
c::b::d::5 // 'c' is treated as open content
c::b::a::d::5 // 'a' and 'c' are treated as open content
open_content::open_content::b::d::5
b::d::3.5
b::d::"hello"
"#),
load(r#"
b::5
d::5
d::b::5
5
"#),
load_schema_from_text(r#" // For a schema with annotations constraint as below:
type::{ name: annotations_type, annotations: ordered::[a, required::b, c, required::d] }
"#),
"annotations_type"
),
case::precision_constraint(
load(r#"
42.
42d0
42d-0
4.2d1
0.42d2
"#),
load(r#"
null
null.null
null.decimal
null.string
4.
42.0
"#),
load_schema_from_text(r#" // For a schema with precision constraint as below:
type::{ name: precision_type, precision: 2 }
"#),
"precision_type"
),
case::scale_constraint(
load(r#"
0.4
0.42
0.432
0.4321
43d3
0d0
"#),
load(r#"
null
null.null
null.decimal
null.symbol
0.43210
"#),
load_schema_from_text(r#" // For a schema with scale constraint as below:
type::{ name: scale_type, scale: range::[min, 4] }
"#),
"scale_type"
),
case::exponent_constraint(
load(r#"
0.4
0.42
0.432
0.4321
43d3
0d0
"#),
load(r#"
null
null.null
null.decimal
null.symbol
0.43210
"#),
load_schema_from_text(r#" // For a schema with exponent constraint as below:
$ion_schema_2_0
type::{ name: exponent_type, exponent: range::[-4, 4] }
"#),
"exponent_type"
),
case::timestamp_precision_constraint(
load(r#"
2000-01T
2000-01-01T
2000-01-01T00:00Z
2000-01-01T00:00:00Z
"#),
load(r#"
2000T
2000-01-01T00:00:00.0Z
null
null.timestamp
null.symbol
"#),
load_schema_from_text(r#" // For a schema with timestamp precision constraint as below:
type::{ name: timestamp_precision_type, timestamp_precision: range::[month, second] }
"#),
"timestamp_precision_type"
),
case::utf8_byte_length_constraint(
load(r#"
"hello"
hello
world
"world"
'\u00A2\u20AC'
"#),
load(r#"
null
null.bool
null.null
null.string
null.symbol
""
"hi"
hi
"greetings"
greetings
'\u20AC\u20AC'
"#),
load_schema_from_text(r#" // For a schema with byte_length constraint as below:
type::{ name: utf8_byte_length_type, utf8_byte_length: 5 }
"#),
"utf8_byte_length_type"
),
case::valid_values_constraint(
load(r#"
2
3
5.5
"hello"
"#),
load(r#"
5.6
1
[1, 2 ,3]
{ greetings: "hello" }
{{"hello"}}
null
"#),
load_schema_from_text(r#" // For a schema with valid values constraint as below:
type::{ name: valid_values_type, valid_values: [2, 3, 5.5, "hello"] }
"#),
"valid_values_type"
),
case::valid_values_with_range_constraint(
load(r#"
1
2
3
4
5
4.5
2d0
30e-1
"#),
load(r#"
0
-2
5.6
6
null
"#),
load_schema_from_text(r#" // For a schema with valid values constraint as below:
type::{ name: valid_values_type, valid_values: range::[1, 5.5] }
"#),
"valid_values_type"
),
case::regex_constraint(
load(r#"
"ab"
"cd"
"ef"
"#),
load(r#"
"a"
"ac"
"ace"
"bdf"
"#),
load_schema_from_text(r#" // For a schema with regex constraint as below:
type::{ name: regex_type, regex: "ab|cd|ef" }
"#),
"regex_type"
),
case::regex_v2_0_constraint(
load(r#"
"/"
":"
"@"
"["
"`"
"{"
" "
"#),
load(r#"
"a"
"A"
"z"
"Z"
"0"
"9"
"_"
"#),
load_schema_from_text(r#" // For a schema with regex constraint as below:
$ion_schema_2_0
type::{ name: regex_type, regex: "\\W" }
"#),
"regex_type"
),
case::timestamp_offset_constraint(
load(r#"
2000T
2000-01-01T00:00:00-00:00 // unknown local offset
2000-01-01T00:00:00Z // UTC
2000-01-01T00:00:00+00:00 // UTC
2000-01-01T00:00:00+01:00
2000-01-01T00:00:00-01:01
"#),
load(r#"
2000-01-01T00:00:00-01:00
2000-01-01T00:00:00+01:01
2000-01-01T00:00:00+07:00
"#),
load_schema_from_text(r#" // For a schema with timestamp_offset constraint as below:
type::{ name: timestamp_offset_type, timestamp_offset: ["-00:00", "+00:00", "+01:00", "-01:01"] }
"#),
"timestamp_offset_type"
),
case::ieee754_float_constraint(
load(r#"
1e0
-1e0
65504e0
-65504e0
nan
+inf
-inf
"#),
load(r#"
null.float
5
5d0
(5e0)
[5e0]
65505e0
-65505e0
"#),
load_schema_from_text(r#" // For a schema with timestamp precision constraint as below:
$ion_schema_2_0
type::{ name: ieee754_float_type, ieee754_float: binary16 }
"#),
"ieee754_float_type"
),
case::annotations_constraint_with_standard_syntax(
load(r#"
a::0
b::1
c::2
"#),
load(r#"
0
$a::1
_c::2
''::3
"#),
load_schema_from_text(r#" // For a schema with annotations constraint as below:
$ion_schema_2_0
type::{ name: standard_annotations_type, annotations: { element: { regex: "^[a-z]$" }, container_length: 1 } }
"#),
"standard_annotations_type"
)
)]
fn type_validation(
valid_values: Vec<Element>,
invalid_values: Vec<Element>,
schema: Arc<Schema>,
type_name: &str,
) {
let type_ref: TypeDefinition = schema.get_type(type_name).unwrap();
// check for validation without any violations
for valid_value in valid_values.iter() {
// there is only a single type in each schema defined above hence validate with that type
let validation_result = type_ref.validate(valid_value);
assert!(validation_result.is_ok());
}
// check for violations due to invalid values
for invalid_value in invalid_values.iter() {
// there is only a single type in each schema defined above hence validate with that type
let validation_result = type_ref.validate(invalid_value);
assert!(validation_result.is_err());
}
}
}