Process validation attributes in newtype structs

This commit is contained in:
Graham Esau 2021-04-16 12:30:52 +01:00
parent 60a9869448
commit 31a5893d10
3 changed files with 18 additions and 2 deletions

View 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
}

View file

@ -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")
}

View file

@ -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>) {