Process validation attributes in newtype structs
This commit is contained in:
parent
60a9869448
commit
31a5893d10
3 changed files with 18 additions and 2 deletions
8
schemars/tests/expected/validate_newtype.json
Normal file
8
schemars/tests/expected/validate_newtype.json
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
|
"title": "NewType",
|
||||||
|
"type": "integer",
|
||||||
|
"format": "uint8",
|
||||||
|
"maximum": 10.0,
|
||||||
|
"minimum": 0.0
|
||||||
|
}
|
|
@ -46,3 +46,11 @@ pub struct Inner {
|
||||||
fn validate() -> TestResult {
|
fn validate() -> TestResult {
|
||||||
test_default_generated_schema::<Struct>("validate")
|
test_default_generated_schema::<Struct>("validate")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, JsonSchema)]
|
||||||
|
pub struct NewType(#[validate(range(max = 10))] u8);
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn validate_newtype() -> TestResult {
|
||||||
|
test_default_generated_schema::<NewType>("validate_newtype")
|
||||||
|
}
|
||||||
|
|
|
@ -61,7 +61,7 @@ fn expr_for_field(field: &Field, allow_ref: bool) -> TokenStream {
|
||||||
let span = field.original.span();
|
let span = field.original.span();
|
||||||
let gen = quote!(gen);
|
let gen = quote!(gen);
|
||||||
|
|
||||||
if allow_ref {
|
field.validation_attrs.apply_to_schema(if allow_ref {
|
||||||
quote_spanned! {span=>
|
quote_spanned! {span=>
|
||||||
{
|
{
|
||||||
#type_def
|
#type_def
|
||||||
|
@ -75,7 +75,7 @@ fn expr_for_field(field: &Field, allow_ref: bool) -> TokenStream {
|
||||||
<#ty as schemars::JsonSchema>::json_schema(#gen)
|
<#ty as schemars::JsonSchema>::json_schema(#gen)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn type_for_field_schema(field: &Field, local_id: usize) -> (syn::Type, Option<TokenStream>) {
|
pub fn type_for_field_schema(field: &Field, local_id: usize) -> (syn::Type, Option<TokenStream>) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue