These two related crates, magnet_derive and magnet_schema help you define (and, in most cases, automatically derive) MongoDB-flavored JSON schemas for your domain model types. Currently, the primary use case for this library is to make it easy to validate serializeable types when using Avocado or the MongoDB Rust driver.
The defined BsonSchema trait defines a single function, bson_schema, which should/will return a Bson Document that is a valid JSON schema describing the structure of the implementing type. Example:
#[macro_use]
extern crate serde_derive;
extern crate serde;
#[macro_use]
extern crate bson;
#[macro_use]
extern crate magnet_derive;
extern crate magnet_schema;
extern crate mongodb;
use std::collections::HashSet;
use magnet_schema::BsonSchema;
use mongodb::{ Client, ThreadedClient, CommandType };
use mongodb::db::{ ThreadedDatabase };
#[derive(BsonSchema)]
struct Person {
name: String,
nicknames: HashSet<String>,
age: usize,
contact: Option<Contact>,
}
#[derive(BsonSchema, Serialize, Deserialize)]
#[serde(tag = "type", content = "value")]
enum Contact {
Email(String),
Phone(u64),
}
fn main() {
let schema = Person::bson_schema();
let spec = doc! {
"create": "Person",
"validator": { "$jsonSchema": schema },
};
let client = Client::connect("localhost", 27017).expect("can't connect to mongod");
let db = client.db("Example");
db.command(spec, CommandType::CreateCollection, None).expect("network error");
// etc.
}For milestones and custom #[attributes], please see the documentation.
- Implement
BsonSchemaforVecDeque,BinaryHeap,LinkedList,Range,RangeInclusive, andPhantomData - Add
Eq + HashandOrdbounds on map keys and set elements where appropriate
- Upgrade
uuiddependency to 0.7.1, and includev4andserdefeatures - Upgrade
urldependency to1.7.2
impl BsonSchemafor arrays of size 2N between 128 and 65536; and sizes 1.5 * 2N between 96 and 1536.- Rewrite generics handling using
syn::Generics::split_for_impl - Use scoped lints in
magnet_schemaas well
- Handle generic types with default generic parameters correctly, by not including the defaults in the generated
impl(which would result in a compiler error) - Use scoped lints for Clippy
- Update some dependencies
- Update
bsonto0.13.0and require itsu2ifeature. This version fixes a bug where unit struct were serialized as 1-element arrays. Theu2ifeature allows unsigned integers (within the appropriate range) to be serialized as signed integers.
- Fix a bug where
Option<enum>was not allowed to benull/Noneby the generated BSON schema - Remove an incorrect item from the documentation
- Fix several Clippy lints
- Update dependencies
impl BsonSchema for Documentimpl BsonSchema for ObjectId- Documentation improvements
- Update dependencies
- Relax
Displaybound forHashMap/BTreeMapkeys, useToStringinstead - Update
proc_macro2dependency so that we can useTokenStream::default()
- Remove
#[magnet(rename = "...")]attribute UnorderedDoc::eq(),assert_doc_eq!andassert_doc_ne!no longer clone their arguments- Update
synandquotedependencies - Improve documentation
- Update
bsondependency
- Support for generic types
- Unit tests and a test suite have been added.
- Bug fix:
Option::bson_schema()didn't handle thebsonTypefield, soOption<integer>wasn't allowed to benull. This has been corrected. - Bug fix: every generated schema now uses
Bson::I64for representing array lengths / collection counts - Enhancement:
impl BsonSchema for { HashMap, BTreeMap }now has a less stringent trait bound on the key. It is nowDisplayinstead ofAsRef<str>.
- Add support for
#[magnet(min_incl = "...", min_excl = "...", max_incl = "...", max_excl = "...")]attributes on struct fields (named as well as newtype and tuple)
- Add support for
enums, respecting Serde's tagging conventions (untagged/external/internal/adjacent), except newtype variants around other (inner)enums - Refactoring, code quality improvements
- Add support for newtype structs and tuple structs
- Respect
#[serde(rename_all = "...")]and#[serde(rename = "...")]attributes - Add Serde-conform case conversion
- Code formatting / organization and documentation improvements
- Initial release, only regular structs with named fields are supported