diff --git a/Cargo.lock b/Cargo.lock index 466879ca..e1643102 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + [[package]] name = "arrayvec" version = "0.5.2" @@ -230,6 +239,12 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" +[[package]] +name = "memchr" +version = "2.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" + [[package]] name = "num-bigint" version = "0.4.4" @@ -283,6 +298,7 @@ dependencies = [ "indexmap 2.0.2", "oxc_schemars_derive", "pretty_assertions", + "regex", "rust_decimal", "semver", "serde", @@ -340,6 +356,35 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "regex" +version = "1.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" + [[package]] name = "rust_decimal" version = "1.32.0" diff --git a/docs/_includes/examples/doc_comments.schema.json b/docs/_includes/examples/doc_comments.schema.json index 121cdb42..b25bd74c 100644 --- a/docs/_includes/examples/doc_comments.schema.json +++ b/docs/_includes/examples/doc_comments.schema.json @@ -1,7 +1,7 @@ { "$schema": "http://json-schema.org/draft-07/schema#", "title": "My Amazing Struct", - "description": "This struct shows off generating a schema with a custom title and description.", + "description": "This struct shows off generating a schema with\na custom title and description.", "type": "object", "required": [ "my_bool", @@ -48,7 +48,7 @@ "additionalProperties": false }, { - "description": "A struct-like enum variant which contains some floats", + "description": "A struct-like enum variant which contains\nsome floats", "type": "object", "required": [ "StructVariant" diff --git a/schemars/Cargo.toml b/schemars/Cargo.toml index 4420a73e..7a69fb8f 100644 --- a/schemars/Cargo.toml +++ b/schemars/Cargo.toml @@ -40,6 +40,7 @@ bigdecimal04 = { version = "0.4", default-features = false, optional = true, pac enumset = { version = "1.0", optional = true } smol_str = { version = "0.1.17", optional = true } semver = { version = "1.0.9", features = ["serde"], optional = true } +regex = { version = "1.12", optional = true } [dev-dependencies] pretty_assertions = "1.2.1" @@ -123,6 +124,10 @@ required-features = ["enumset"] name = "smol_str" required-features = ["smol_str"] +[[test]] +name = "regex" +required-features = ["regex"] + [[test]] name = "semver" required-features = ["semver"] diff --git a/schemars/examples/doc_comments.schema.json b/schemars/examples/doc_comments.schema.json index 121cdb42..b25bd74c 100644 --- a/schemars/examples/doc_comments.schema.json +++ b/schemars/examples/doc_comments.schema.json @@ -1,7 +1,7 @@ { "$schema": "http://json-schema.org/draft-07/schema#", "title": "My Amazing Struct", - "description": "This struct shows off generating a schema with a custom title and description.", + "description": "This struct shows off generating a schema with\na custom title and description.", "type": "object", "required": [ "my_bool", @@ -48,7 +48,7 @@ "additionalProperties": false }, { - "description": "A struct-like enum variant which contains some floats", + "description": "A struct-like enum variant which contains\nsome floats", "type": "object", "required": [ "StructVariant" diff --git a/schemars/src/json_schema_impls/mod.rs b/schemars/src/json_schema_impls/mod.rs index ae990e74..f9f62bff 100644 --- a/schemars/src/json_schema_impls/mod.rs +++ b/schemars/src/json_schema_impls/mod.rs @@ -87,3 +87,5 @@ mod uuid08; #[cfg(feature = "uuid1")] mod uuid1; mod wrapper; +#[cfg(feature = "regex")] +mod regex; diff --git a/schemars/src/json_schema_impls/regex.rs b/schemars/src/json_schema_impls/regex.rs new file mode 100644 index 00000000..0d3188ad --- /dev/null +++ b/schemars/src/json_schema_impls/regex.rs @@ -0,0 +1,6 @@ +use crate::r#gen::SchemaGenerator; +use crate::schema::*; +use crate::JsonSchema; +use regex::Regex; + +forward_impl!(Regex => String); diff --git a/schemars/tests/expected/doc_comments_enum.json b/schemars/tests/expected/doc_comments_enum.json index 53aaa86b..2ac1412a 100644 --- a/schemars/tests/expected/doc_comments_enum.json +++ b/schemars/tests/expected/doc_comments_enum.json @@ -1,7 +1,7 @@ { "$schema": "http://json-schema.org/draft-07/schema#", "title": "This is the enum's title", - "description": "This is the enum's description.", + "description": "This is\nthe enum's description.", "oneOf": [ { "type": "string", @@ -30,7 +30,7 @@ "properties": { "my_nullable_string": { "title": "A nullable string", - "description": "This field is a nullable string.\n\nThis is the second line!\n\nAnd this is the third!", + "description": "This field is a nullable string.\n\nThis\nis\nthe second\nline!\n\n\n\n\nAnd this is the third!", "type": [ "string", "null" diff --git a/schemars/tests/expected/schema-2019_09.json b/schemars/tests/expected/schema-2019_09.json index cf11d352..ce4217a5 100644 --- a/schemars/tests/expected/schema-2019_09.json +++ b/schemars/tests/expected/schema-2019_09.json @@ -12,14 +12,14 @@ ] }, "definitions": { - "description": "The `definitions` keyword.\n\nIn JSON Schema draft 2019-09 this was replaced by $defs, but in Schemars this is still serialized as `definitions` for backward-compatibility.\n\nSee [JSON Schema 8.2.5. Schema Re-Use With \"$defs\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-8.2.5), and [JSON Schema (draft 07) 9. Schema Re-Use With \"definitions\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9).", + "description": "The `definitions` keyword.\n\nIn JSON Schema draft 2019-09 this was replaced by $defs, but in Schemars this is still\nserialized as `definitions` for backward-compatibility.\n\nSee [JSON Schema 8.2.5. Schema Re-Use With \"$defs\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-8.2.5),\nand [JSON Schema (draft 07) 9. Schema Re-Use With \"definitions\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9).", "type": "object", "additionalProperties": { "$ref": "#/definitions/Schema" } }, "type": { - "description": "The `type` keyword.\n\nSee [JSON Schema Validation 6.1.1. \"type\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-6.1.1) and [JSON Schema 4.2.1. Instance Data Model](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-4.2.1).", + "description": "The `type` keyword.\n\nSee [JSON Schema Validation 6.1.1. \"type\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-6.1.1)\nand [JSON Schema 4.2.1. Instance Data Model](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-4.2.1).", "anyOf": [ { "$ref": "#/definitions/SingleOrVec_for_InstanceType" @@ -361,7 +361,7 @@ "description": "A JSON Schema.", "anyOf": [ { - "description": "A trivial boolean JSON Schema.\n\nThe schema `true` matches everything (always passes validation), whereas the schema `false` matches nothing (always fails validation).", + "description": "A trivial boolean JSON Schema.\n\nThe schema `true` matches everything (always passes validation), whereas the schema `false`\nmatches nothing (always fails validation).", "type": "boolean" }, { @@ -375,7 +375,7 @@ "type": "object", "properties": { "type": { - "description": "The `type` keyword.\n\nSee [JSON Schema Validation 6.1.1. \"type\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-6.1.1) and [JSON Schema 4.2.1. Instance Data Model](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-4.2.1).", + "description": "The `type` keyword.\n\nSee [JSON Schema Validation 6.1.1. \"type\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-6.1.1)\nand [JSON Schema 4.2.1. Instance Data Model](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-4.2.1).", "anyOf": [ { "$ref": "#/definitions/SingleOrVec_for_InstanceType" diff --git a/schemars/tests/expected/schema-openapi3.json b/schemars/tests/expected/schema-openapi3.json index 0548828a..36eccec2 100644 --- a/schemars/tests/expected/schema-openapi3.json +++ b/schemars/tests/expected/schema-openapi3.json @@ -10,14 +10,14 @@ "nullable": true }, "definitions": { - "description": "The `definitions` keyword.\n\nIn JSON Schema draft 2019-09 this was replaced by $defs, but in Schemars this is still serialized as `definitions` for backward-compatibility.\n\nSee [JSON Schema 8.2.5. Schema Re-Use With \"$defs\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-8.2.5), and [JSON Schema (draft 07) 9. Schema Re-Use With \"definitions\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9).", + "description": "The `definitions` keyword.\n\nIn JSON Schema draft 2019-09 this was replaced by $defs, but in Schemars this is still\nserialized as `definitions` for backward-compatibility.\n\nSee [JSON Schema 8.2.5. Schema Re-Use With \"$defs\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-8.2.5),\nand [JSON Schema (draft 07) 9. Schema Re-Use With \"definitions\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9).", "type": "object", "additionalProperties": { "$ref": "#/components/schemas/Schema" } }, "type": { - "description": "The `type` keyword.\n\nSee [JSON Schema Validation 6.1.1. \"type\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-6.1.1) and [JSON Schema 4.2.1. Instance Data Model](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-4.2.1).", + "description": "The `type` keyword.\n\nSee [JSON Schema Validation 6.1.1. \"type\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-6.1.1)\nand [JSON Schema 4.2.1. Instance Data Model](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-4.2.1).", "allOf": [ { "$ref": "#/components/schemas/SingleOrVec_for_InstanceType" @@ -297,7 +297,7 @@ "description": "A JSON Schema.", "anyOf": [ { - "description": "A trivial boolean JSON Schema.\n\nThe schema `true` matches everything (always passes validation), whereas the schema `false` matches nothing (always fails validation).", + "description": "A trivial boolean JSON Schema.\n\nThe schema `true` matches everything (always passes validation), whereas the schema `false`\nmatches nothing (always fails validation).", "type": "boolean" }, { @@ -315,7 +315,7 @@ "type": "object", "properties": { "type": { - "description": "The `type` keyword.\n\nSee [JSON Schema Validation 6.1.1. \"type\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-6.1.1) and [JSON Schema 4.2.1. Instance Data Model](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-4.2.1).", + "description": "The `type` keyword.\n\nSee [JSON Schema Validation 6.1.1. \"type\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-6.1.1)\nand [JSON Schema 4.2.1. Instance Data Model](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-4.2.1).", "allOf": [ { "$ref": "#/components/schemas/SingleOrVec_for_InstanceType" diff --git a/schemars/tests/expected/schema.json b/schemars/tests/expected/schema.json index 6cf840bd..c1ed9c06 100644 --- a/schemars/tests/expected/schema.json +++ b/schemars/tests/expected/schema.json @@ -12,14 +12,14 @@ ] }, "definitions": { - "description": "The `definitions` keyword.\n\nIn JSON Schema draft 2019-09 this was replaced by $defs, but in Schemars this is still serialized as `definitions` for backward-compatibility.\n\nSee [JSON Schema 8.2.5. Schema Re-Use With \"$defs\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-8.2.5), and [JSON Schema (draft 07) 9. Schema Re-Use With \"definitions\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9).", + "description": "The `definitions` keyword.\n\nIn JSON Schema draft 2019-09 this was replaced by $defs, but in Schemars this is still\nserialized as `definitions` for backward-compatibility.\n\nSee [JSON Schema 8.2.5. Schema Re-Use With \"$defs\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-8.2.5),\nand [JSON Schema (draft 07) 9. Schema Re-Use With \"definitions\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9).", "type": "object", "additionalProperties": { "$ref": "#/definitions/Schema" } }, "type": { - "description": "The `type` keyword.\n\nSee [JSON Schema Validation 6.1.1. \"type\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-6.1.1) and [JSON Schema 4.2.1. Instance Data Model](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-4.2.1).", + "description": "The `type` keyword.\n\nSee [JSON Schema Validation 6.1.1. \"type\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-6.1.1)\nand [JSON Schema 4.2.1. Instance Data Model](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-4.2.1).", "anyOf": [ { "$ref": "#/definitions/SingleOrVec_for_InstanceType" @@ -361,7 +361,7 @@ "description": "A JSON Schema.", "anyOf": [ { - "description": "A trivial boolean JSON Schema.\n\nThe schema `true` matches everything (always passes validation), whereas the schema `false` matches nothing (always fails validation).", + "description": "A trivial boolean JSON Schema.\n\nThe schema `true` matches everything (always passes validation), whereas the schema `false`\nmatches nothing (always fails validation).", "type": "boolean" }, { @@ -379,7 +379,7 @@ "type": "object", "properties": { "type": { - "description": "The `type` keyword.\n\nSee [JSON Schema Validation 6.1.1. \"type\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-6.1.1) and [JSON Schema 4.2.1. Instance Data Model](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-4.2.1).", + "description": "The `type` keyword.\n\nSee [JSON Schema Validation 6.1.1. \"type\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-6.1.1)\nand [JSON Schema 4.2.1. Instance Data Model](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-4.2.1).", "anyOf": [ { "$ref": "#/definitions/SingleOrVec_for_InstanceType" diff --git a/schemars/tests/regex.rs b/schemars/tests/regex.rs new file mode 100644 index 00000000..e9bf235a --- /dev/null +++ b/schemars/tests/regex.rs @@ -0,0 +1,8 @@ +mod util; +use regex::Regex; +use util::*; + +#[test] +fn regex() -> TestResult { + test_default_generated_schema::("regex") +}