magnet_schema

Crates.iomagnet_schema
lib.rsmagnet_schema
version0.8.0
sourcesrc
created_at2018-04-10 22:59:10.595385
updated_at2019-01-02 10:55:50.874478
descriptionMagnet, a JSON/BSON schema generator
homepagehttps://h2co3.github.io/magnet/
repositoryhttps://github.com/H2CO3/magnet.git
max_upload_size
id60017
size53,971
Árpád Goretity  (H2CO3)

documentation

https://docs.rs/magnet_schema

README

Magnet, a JSON schema generator

Magnet on crates.io Magnet on docs.rs Magnet Download Magnet License Lines of Code Twitter

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.

Release Notes

v0.8.0

  • Implement BsonSchema for VecDeque, BinaryHeap, LinkedList, Range, RangeInclusive, and PhantomData
  • Add Eq + Hash and Ord bounds on map keys and set elements where appropriate

v0.7.0

  • Upgrade uuid dependency to 0.7.1, and include v4 and serde features
  • Upgrade url dependency to 1.7.2

v0.6.0

  • impl BsonSchema for 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_schema as well

v0.5.0

  • 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

v0.4.0

  • Update bson to 0.13.0 and require its u2i feature. This version fixes a bug where unit struct were serialized as 1-element arrays. The u2i feature allows unsigned integers (within the appropriate range) to be serialized as signed integers.

v0.3.3

  • Fix a bug where Option<enum> was not allowed to be null/None by the generated BSON schema
  • Remove an incorrect item from the documentation
  • Fix several Clippy lints
  • Update dependencies

v0.3.2

  • impl BsonSchema for Document
  • impl BsonSchema for ObjectId
  • Documentation improvements
  • Update dependencies

v0.3.1

  • Relax Display bound for HashMap/BTreeMap keys, use ToString instead
  • Update proc_macro2 dependency so that we can use TokenStream::default()

v0.3.0

  • Remove #[magnet(rename = "...")] attribute
  • UnorderedDoc::eq(), assert_doc_eq! and assert_doc_ne! no longer clone their arguments
  • Update syn and quote dependencies
  • Improve documentation

v0.2.1

  • Update bson dependency

v0.2.0

  • Support for generic types

v0.1.4

  • Unit tests and a test suite have been added.
  • Bug fix: Option::bson_schema() didn't handle the bsonType field, so Option<integer> wasn't allowed to be null. This has been corrected.
  • Bug fix: every generated schema now uses Bson::I64 for representing array lengths / collection counts
  • Enhancement: impl BsonSchema for { HashMap, BTreeMap } now has a less stringent trait bound on the key. It is now Display instead of AsRef<str>.

v0.1.3

  • Add support for #[magnet(min_incl = "...", min_excl = "...", max_incl = "...", max_excl = "...")] attributes on struct fields (named as well as newtype and tuple)

v0.1.2

  • Add support for enums, respecting Serde's tagging conventions (untagged/external/internal/adjacent), except newtype variants around other (inner) enums
  • Refactoring, code quality improvements

v0.1.1

  • 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

v0.1.0

  • Initial release, only regular structs with named fields are supported
Commit count: 93

cargo fmt