Rewrite attribute handling code (#330)

This commit is contained in:
Graham Esau 2024-08-27 16:50:47 +01:00 committed by GitHub
parent fb6bd6d439
commit d07a1be031
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
33 changed files with 1195 additions and 1099 deletions

View file

@ -5,7 +5,8 @@
"prefixItems": [
{
"type": "number",
"format": "float"
"format": "float",
"writeOnly": true
},
{
"type": "null"

View file

@ -23,10 +23,6 @@
"type": "string",
"pattern": "^[Hh]ello\\b"
},
"regex_str3": {
"type": "string",
"pattern": "^\\d+$"
},
"contains_str1": {
"type": "string",
"pattern": "substring\\.\\.\\."
@ -39,10 +35,6 @@
"type": "string",
"format": "email"
},
"tel": {
"type": "string",
"format": "phone"
},
"homepage": {
"type": "string",
"format": "uri"
@ -88,11 +80,9 @@
"min_max2",
"regex_str1",
"regex_str2",
"regex_str3",
"contains_str1",
"contains_str2",
"email_address",
"tel",
"homepage",
"non_empty_str",
"non_empty_str2",

View file

@ -20,10 +20,6 @@
"pattern": "^[Hh]ello\\b"
},
"regex_str2": {
"type": "string",
"pattern": "^[Hh]ello\\b"
},
"regex_str3": {
"type": "string",
"pattern": "^\\d+$"
},
@ -39,10 +35,6 @@
"type": "string",
"format": "email"
},
"tel": {
"type": "string",
"format": "phone"
},
"homepage": {
"type": "string",
"format": "uri"
@ -88,11 +80,9 @@
"min_max2",
"regex_str1",
"regex_str2",
"regex_str3",
"contains_str1",
"contains_str2",
"email_address",
"tel",
"homepage",
"non_empty_str",
"non_empty_str2",

View file

@ -1,7 +1,5 @@
use schemars::JsonSchema;
// FIXME validation attrs like `email` should be disallowed non structs/enums/variants
#[derive(JsonSchema)]
#[validate(email)]
pub struct Struct1(#[validate(regex, foo, length(min = 1, equal = 2, bar))] String);
@ -15,6 +13,7 @@ pub struct Struct3(
#[validate(
regex = "foo",
contains = "bar",
regex(pattern = "baz"),
regex(path = "baz"),
phone,
email,
@ -29,6 +28,7 @@ pub struct Struct4(
regex = "foo",
contains = "bar",
regex(path = "baz"),
regex(pattern = "baz"),
phone,
email,
url

View file

@ -1,53 +1,95 @@
error: unknown schemars attribute `foo`
--> tests/ui/invalid_validation_attrs.rs:11:38
|
11 | pub struct Struct2(#[schemars(regex, foo, length(min = 1, equal = 2, bar))] String);
| ^^^
error: expected validate regex attribute item to be of the form `regex(...)`
--> tests/ui/invalid_validation_attrs.rs:5:31
|
5 | pub struct Struct1(#[validate(regex, foo, length(min = 1, equal = 2, bar))] String);
| ^^^^^
error: could not parse `regex` item in schemars attribute
--> tests/ui/invalid_validation_attrs.rs:11:31
|
11 | pub struct Struct2(#[schemars(regex, foo, length(min = 1, equal = 2, bar))] String);
| ^^^^^
error: expected schemars regex attribute item to be of the form `regex(...)`
--> tests/ui/invalid_validation_attrs.rs:9:31
|
9 | pub struct Struct2(#[schemars(regex, foo, length(min = 1, equal = 2, bar))] String);
| ^^^^^
error: schemars attribute cannot contain both `equal` and `min`
--> tests/ui/invalid_validation_attrs.rs:11:59
|
11 | pub struct Struct2(#[schemars(regex, foo, length(min = 1, equal = 2, bar))] String);
| ^^^^^
--> tests/ui/invalid_validation_attrs.rs:9:59
|
9 | pub struct Struct2(#[schemars(regex, foo, length(min = 1, equal = 2, bar))] String);
| ^^^^^^^^^
error: unknown item in schemars length attribute
--> tests/ui/invalid_validation_attrs.rs:11:70
|
11 | pub struct Struct2(#[schemars(regex, foo, length(min = 1, equal = 2, bar))] String);
| ^^^
error: unknown item in schemars length attribute: `bar`
--> tests/ui/invalid_validation_attrs.rs:9:70
|
9 | pub struct Struct2(#[schemars(regex, foo, length(min = 1, equal = 2, bar))] String);
| ^^^
error: schemars attribute cannot contain both `contains` and `regex`
error: unknown schemars attribute `foo`
--> tests/ui/invalid_validation_attrs.rs:9:38
|
9 | pub struct Struct2(#[schemars(regex, foo, length(min = 1, equal = 2, bar))] String);
| ^^^
error: unknown schemars attribute `email`
--> tests/ui/invalid_validation_attrs.rs:8:12
|
8 | #[schemars(email)]
| ^^^^^
error: expected validate regex attribute item to be of the form `regex(...)`
--> tests/ui/invalid_validation_attrs.rs:14:9
|
14 | regex = "foo",
| ^^^^^^^^^^^^^
error: expected validate contains attribute item to be of the form `contains(...)`
--> tests/ui/invalid_validation_attrs.rs:15:9
|
15 | contains = "bar",
| ^^^^^^^^^^^^^^^^
error: `pattern` is not supported in `validate(regex(...))` attribute - use either `validate(regex(path = ...))` or `schemars(regex(pattern = ...))` instead
--> tests/ui/invalid_validation_attrs.rs:16:15
|
16 | regex(pattern = "baz"),
| ^^^^^^^^^^^^^^^
error: `validate(regex(...))` attribute requires `path = ...`
--> tests/ui/invalid_validation_attrs.rs:16:9
|
16 | regex(pattern = "baz"),
| ^^^^^^^^^^^^^^^^^^^^^^
error: expected schemars regex attribute item to be of the form `regex(...)`
--> tests/ui/invalid_validation_attrs.rs:28:9
|
28 | regex = "foo",
| ^^^^^^^^^^^^^
error: expected schemars contains attribute item to be of the form `contains(...)`
--> tests/ui/invalid_validation_attrs.rs:29:9
|
29 | contains = "bar",
| ^^^^^^^^^^^^^^^^
error: `path` is not supported in `schemars(regex(...))` attribute - use `schemars(regex(pattern = ...))` instead
--> tests/ui/invalid_validation_attrs.rs:30:15
|
30 | regex(path = "baz"),
| ^^^^^^^^^^^^
error: `schemars(regex(...))` attribute requires `pattern = ...`
--> tests/ui/invalid_validation_attrs.rs:30:9
|
30 | contains = "bar",
| ^^^^^^^^
30 | regex(path = "baz"),
| ^^^^^^^^^^^^^^^^^^^
error: duplicate schemars attribute `regex`
--> tests/ui/invalid_validation_attrs.rs:31:9
|
31 | regex(path = "baz"),
| ^^^^^
error: schemars attribute cannot contain both `phone` and `email`
--> tests/ui/invalid_validation_attrs.rs:33:9
|
33 | email,
| ^^^^^
error: schemars attribute cannot contain both `phone` and `url`
error: schemars attribute cannot contain both `url` and `email`
--> tests/ui/invalid_validation_attrs.rs:34:9
|
34 | url
| ^^^
error[E0425]: cannot find value `foo` in this scope
--> tests/ui/invalid_validation_attrs.rs:16:17
error: unknown schemars attribute `phone`
--> tests/ui/invalid_validation_attrs.rs:32:9
|
16 | regex = "foo",
| ^^^^^ not found in this scope
32 | phone,
| ^^^^^

View file

@ -16,20 +16,16 @@ pub struct Struct {
min_max: f32,
#[validate(range(min = "MIN", max = "MAX"))]
min_max2: f32,
#[validate(regex = &*STARTS_WITH_HELLO)]
#[validate(regex(path = &*STARTS_WITH_HELLO))]
regex_str1: String,
#[validate(regex(path = "STARTS_WITH_HELLO", code = "foo"))]
regex_str2: String,
#[validate(regex(pattern = r"^\d+$"))]
regex_str3: String,
#[validate(contains = "substring...")]
#[validate(contains(pattern = "substring..."))]
contains_str1: String,
#[validate(contains(pattern = "substring...", message = "bar"))]
contains_str2: String,
#[validate(email)]
email_address: String,
#[validate(phone)]
tel: String,
#[validate(url)]
homepage: String,
#[validate(length(min = 1, max = 100))]
@ -38,7 +34,7 @@ pub struct Struct {
non_empty_str2: String,
#[validate(length(equal = 2))]
pair: Vec<i32>,
#[validate(contains = "map_key")]
#[validate(contains(pattern = "map_key"))]
map_contains: BTreeMap<String, ()>,
#[validate(required)]
required_option: Option<bool>,
@ -66,22 +62,18 @@ pub struct Struct2 {
min_max: f32,
#[schemars(range(min = "MIN", max = "MAX"))]
min_max2: f32,
#[validate(regex = "overridden")]
#[schemars(regex = "STARTS_WITH_HELLO")]
#[validate(regex(path = overridden))]
#[schemars(regex(pattern = &*STARTS_WITH_HELLO))]
regex_str1: String,
#[schemars(regex(path = "STARTS_WITH_HELLO"))]
regex_str2: String,
#[schemars(regex(pattern = r"^\d+$"))]
regex_str3: String,
#[validate(regex = "overridden")]
#[schemars(contains = "substring...")]
regex_str2: String,
#[validate(contains(pattern = "overridden"))]
#[schemars(contains(pattern = "substring..."))]
contains_str1: String,
#[schemars(contains(pattern = "substring..."))]
contains_str2: String,
#[schemars(email)]
email_address: String,
#[schemars(phone)]
tel: String,
#[schemars(url)]
homepage: String,
#[schemars(length(min = 1, max = 100))]
@ -90,7 +82,7 @@ pub struct Struct2 {
non_empty_str2: String,
#[schemars(length(equal = 2))]
pair: Vec<i32>,
#[schemars(contains = "map_key")]
#[schemars(contains(pattern = "map_key"))]
map_contains: BTreeMap<String, ()>,
#[schemars(required)]
required_option: Option<bool>,

View file

@ -13,7 +13,7 @@ pub struct Struct<'a> {
array_str_length: [&'a str; 2],
#[schemars(inner(contains(pattern = "substring...")))]
slice_str_contains: &'a [&'a str],
#[schemars(inner(regex = "STARTS_WITH_HELLO"))]
#[schemars(inner(regex(pattern = STARTS_WITH_HELLO)))]
vec_str_regex: Vec<String>,
#[schemars(inner(length(min = 1, max = 100)))]
vec_str_length: Vec<&'a str>,