{"id":19110793,"url":"https://github.com/h2co3/magnet","last_synced_at":"2025-04-30T20:47:29.002Z","repository":{"id":45957987,"uuid":"128973505","full_name":"H2CO3/magnet","owner":"H2CO3","description":"A JSON/BSON schema generator","archived":false,"fork":false,"pushed_at":"2021-11-24T14:35:33.000Z","size":110,"stargazers_count":17,"open_issues_count":4,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-20T17:40:55.350Z","etag":null,"topics":["bson","crates","database","json-schema","mongodb","schema"],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/H2CO3.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-10T17:48:19.000Z","updated_at":"2024-05-29T03:02:05.000Z","dependencies_parsed_at":"2022-08-28T19:51:32.753Z","dependency_job_id":null,"html_url":"https://github.com/H2CO3/magnet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/H2CO3%2Fmagnet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/H2CO3%2Fmagnet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/H2CO3%2Fmagnet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/H2CO3%2Fmagnet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/H2CO3","download_url":"https://codeload.github.com/H2CO3/magnet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251780703,"owners_count":21642823,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bson","crates","database","json-schema","mongodb","schema"],"created_at":"2024-11-09T04:26:04.029Z","updated_at":"2025-04-30T20:47:28.981Z","avatar_url":"https://github.com/H2CO3.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Magnet, a JSON schema generator\n\n[![Magnet on crates.io](https://img.shields.io/crates/v/magnet_schema.svg)](https://crates.io/crates/magnet_schema)\n[![Magnet on docs.rs](https://docs.rs/magnet_schema/badge.svg)](https://docs.rs/magnet_schema)\n[![Magnet Download](https://img.shields.io/crates/d/magnet_schema.svg)](https://crates.io/crates/magnet_schema)\n[![Magnet License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/H2CO3/magnet/blob/master/LICENSE.txt)\n[![Lines of Code](https://tokei.rs/b1/github/H2CO3/magnet)](https://github.com/Aaronepower/tokei)\n[![Twitter](https://img.shields.io/badge/twitter-@H2CO3_iOS-blue.svg?style=flat\u0026colorB=64A5DE\u0026label=Twitter)](http://twitter.com/H2CO3_iOS)\n\nThese two related crates, `magnet_derive` and `magnet_schema` help you define (and, in most cases, automatically derive) MongoDB-flavored [JSON schemas](https://docs.mongodb.com/manual/reference/operator/query/jsonSchema/#extensions) 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](https://docs.rs/avocado/) or the [MongoDB Rust driver](https://docs.rs/mongodb/).\n\nThe 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:\n\n```rust\n#[macro_use]\nextern crate serde_derive;\nextern crate serde;\n#[macro_use]\nextern crate bson;\n#[macro_use]\nextern crate magnet_derive;\nextern crate magnet_schema;\nextern crate mongodb;\n\nuse std::collections::HashSet;\nuse magnet_schema::BsonSchema;\n\nuse mongodb::{ Client, ThreadedClient, CommandType };\nuse mongodb::db::{ ThreadedDatabase };\n\n#[derive(BsonSchema)]\nstruct Person {\n    name: String,\n    nicknames: HashSet\u003cString\u003e,\n    age: usize,\n    contact: Option\u003cContact\u003e,\n}\n\n#[derive(BsonSchema, Serialize, Deserialize)]\n#[serde(tag = \"type\", content = \"value\")]\nenum Contact {\n    Email(String),\n    Phone(u64),\n}\n\nfn main() {\n    let schema = Person::bson_schema();\n    let spec = doc! {\n        \"create\": \"Person\",\n        \"validator\": { \"$jsonSchema\": schema },\n    };\n    let client = Client::connect(\"localhost\", 27017).expect(\"can't connect to mongod\");\n    let db = client.db(\"Example\");\n    db.command(spec, CommandType::CreateCollection, None).expect(\"network error\");\n    // etc.\n}\n```\n\nFor milestones and custom `#[attributes]`, please see the [documentation](https://docs.rs/magnet_schema).\n\n## Release Notes\n\n### v0.8.0\n\n* Implement `BsonSchema` for `VecDeque`, `BinaryHeap`, `LinkedList`, `Range`, `RangeInclusive`, and `PhantomData`\n* Add `Eq + Hash` and `Ord` bounds on map keys and set elements where appropriate\n\n### v0.7.0\n\n* Upgrade `uuid` dependency to 0.7.1, and include `v4` and `serde` features\n* Upgrade `url` dependency to `1.7.2`\n\n### v0.6.0\n\n* `impl BsonSchema` for arrays of size 2\u003csup\u003eN\u003c/sup\u003e between 128 and 65536; and sizes 1.5 * 2\u003csup\u003eN\u003c/sup\u003e between 96 and 1536.\n* Rewrite generics handling using `syn::Generics::split_for_impl`\n* Use scoped lints in `magnet_schema` as well\n\n### v0.5.0\n\n* Handle generic types with default generic parameters correctly, by not including the defaults in the generated `impl` (which would result in a compiler error)\n* Use scoped lints for Clippy\n* Update some dependencies\n\n### v0.4.0\n\n* Update `bson` to `0.13.0` and require its `u2i` feature. This version fixes a\n  bug where unit struct were serialized as 1-element arrays. The `u2i` feature\n  allows unsigned integers (within the appropriate range) to be serialized as\n  signed integers.\n\n### v0.3.3\n\n* Fix a bug where `Option\u003cenum\u003e` was not allowed to be `null`/`None` by the\n  generated BSON schema\n* Remove an incorrect item from the documentation\n* Fix several Clippy lints\n* Update dependencies\n\n### v0.3.2\n\n* `impl BsonSchema for Document`\n* `impl BsonSchema for ObjectId`\n* Documentation improvements\n* Update dependencies\n\n### v0.3.1\n\n* Relax `Display` bound for `HashMap`/`BTreeMap` keys, use `ToString` instead\n* Update `proc_macro2` dependency so that we can use `TokenStream::default()`\n\n### v0.3.0\n\n* Remove `#[magnet(rename = \"...\")]` attribute\n* `UnorderedDoc::eq()`, `assert_doc_eq!` and `assert_doc_ne!` no longer clone their arguments\n* Update `syn` and `quote` dependencies\n* Improve documentation\n\n### v0.2.1\n\n* Update `bson` dependency\n\n### v0.2.0\n\n* Support for generic types\n\n### v0.1.4\n\n* Unit tests and a test suite have been added.\n* Bug fix: `Option::bson_schema()` didn't handle the `bsonType` field, so `Option\u003cinteger\u003e` wasn't allowed to be `null`. This has been corrected.\n* Bug fix: every generated schema now uses `Bson::I64` for representing array lengths / collection counts\n* Enhancement: `impl BsonSchema for { HashMap, BTreeMap }` now has a less stringent trait bound on the key. It is now `Display` instead of `AsRef\u003cstr\u003e`.\n\n### v0.1.3\n\n* Add support for `#[magnet(min_incl = \"...\", min_excl = \"...\", max_incl = \"...\", max_excl = \"...\")]` attributes on struct fields (named as well as newtype and tuple)\n\n### v0.1.2\n\n* Add support for `enum`s, respecting Serde's tagging conventions (untagged/external/internal/adjacent), except newtype variants around other (inner) `enum`s\n* Refactoring, code quality improvements\n\n### v0.1.1\n\n* Add support for newtype structs and tuple structs\n* Respect `#[serde(rename_all = \"...\")]` and `#[serde(rename = \"...\")]` attributes\n* Add Serde-conform case conversion\n* Code formatting / organization and documentation improvements\n\n### v0.1.0\n\n* Initial release, only regular structs with named fields are supported\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fh2co3%2Fmagnet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fh2co3%2Fmagnet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fh2co3%2Fmagnet/lists"}