An implementation of Amazon Ion Schema in Rust.
This package is considered experimental, under active/early development, and the API is subject to change.
The following rust code sample is a simple example of how to use this API.
my_schema.isl
This file (my_schema.isl
) defines a new type (my_int_type
) based on Ion's int
type.
```
schema_header::{
imports: [],
}
type::{ name: myinttype, type: int, }
schema_footer::{ } ```
```rust use ionschema::authority::{DocumentAuthority, FileSystemDocumentAuthority}; use ionschema::external::ionrs::value::owned::OwnedElement; use ionschema::result::{ValidationResult, IonSchemaResult}; use ionschema::types::TypeRef; use ionschema::schema::Schema; use ion_schema::system::SchemaSystem; use std::path::Path; use std::rc::Rc;
fn main() -> IonSchemaResult<()> {
// Create authorities vector containing all the authorities that will be used to load a schema based on schema id
let document_authorities: Vec
// Create a new schema system from given document authorities
let mut schema_system = SchemaSystem::new(document_authorities);
// Provide schema id for the schema you want to load (schema_id is the schema file name here)
let schema_id = "my_schema.isl";
// Load schema
let schema: Rc<Schema> = schema_system.load_schema(schema_id)?;
// Retrieve a particular type from this schema
let type_ref: TypeRef = schema.get_type("my_int_type").unwrap();
// Validate data based on the type: 'my_int_type'
check_value(5.into(), &type_ref); // this validation passes as the value satisfies integer type constraint
check_value(5e3.into(), &type_ref); // this returns violation as 'my_int_type' expects an integer value
Ok(())
}
// Verify if the given value is valid and print violation for invalid value fn checkvalue(value: OwnedElement, typeref: &TypeRef) { let validationresult: ValidationResult = typeref.validate(&value); if let Err(violation) = validation_result { println!("{:?}", value); println!("{:#?}", violation); } } ```
When run, the code above produces the following output:
OwnedElement { annotations: [], value: Float(5000.0) }
Violation {
constraint: "my_int_type",
code: TypeConstraintsUnsatisfied,
message: "value didn't satisfy type constraint(s)",
violations: [
Violation {
constraint: "type_constraint",
code: TypeMismatched,
message: "expected type Integer, found Float",
violations: [],
},
],
}
This repository contains git submodules
called ion-schema-schemas
and ion-schema-tests
, which holds test data used by
this library's unit tests.
The easiest way to clone the ion-schema-rust
repository and initialize its submodules
is to run the following command:
bash
$ git clone --recursive https://github.com/amzn/ion-schema-rust.git ion-schema-rust
Alternatively, the submodule may be initialized independently from the clone by running the following commands:
bash
$ git submodule init
$ git submodule update
Building the project,
bash
$ cargo build --workspace --all-targets
Running all tests for ion-schema-rust
,
bash
$ cargo test --workspace
The repository contains an examples/
folder which is a CLI tool to load and validate schema.
To load a schema using the examples CLI:
bash
$ cargo run --package ion-schema --example schema load --directory <DIRECTORY> --schema <SCHEMA_FILE>
To validate an ion value against a schema type using the examples CLI:
bash
$ cargo run --package ion-schema --example schema validate --directory <DIRECTORY> --schema <SCHEMA_FILE> --input <INPUT_FILE> --type <TYPE>
For more information on how to use the examples CLI, run the following command:
bash
$ cargo run --package ion-schema --example schema help
This library is licensed under the Apache-2.0 License.