Add some doc comments

This commit is contained in:
Graham Esau 2021-04-24 11:46:07 +01:00
parent 7914593d89
commit d99a96fc8a
4 changed files with 22 additions and 11 deletions

View file

@ -6,12 +6,11 @@ use crate::JsonSchema;
// Helper for generating schemas for flattened `Option` fields. // Helper for generating schemas for flattened `Option` fields.
pub fn json_schema_for_flatten<T: ?Sized + JsonSchema>( pub fn json_schema_for_flatten<T: ?Sized + JsonSchema>(
gen: &mut SchemaGenerator, gen: &mut SchemaGenerator,
required: Option<bool>, required: bool,
) -> Schema { ) -> Schema {
let mut schema = T::_schemars_private_non_optional_json_schema(gen); let mut schema = T::_schemars_private_non_optional_json_schema(gen);
let required = required.unwrap_or_else(|| !T::_schemars_private_is_option()); if T::_schemars_private_is_option() && !required {
if !required {
if let Schema::Object(SchemaObject { if let Schema::Object(SchemaObject {
object: Some(ref mut object_validation), object: Some(ref mut object_validation),
.. ..

View file

@ -231,7 +231,11 @@ impl SchemaObject {
self.reference.is_some() self.reference.is_some()
} }
// TODO document /// Returns `true` if `self` accepts values of the given type, according to the [`instance_type`] field.
///
/// This is a basic check that always returns `true` if no `instance_type` is specified on the schema,
/// and does not check any subschemas. Because of this, both `{}` and `{"not": {}}` accept any type according
/// to this method.
pub fn has_type(&self, ty: InstanceType) -> bool { pub fn has_type(&self, ty: InstanceType) -> bool {
self.instance_type self.instance_type
.as_ref() .as_ref()
@ -522,7 +526,20 @@ impl<T> From<Vec<T>> for SingleOrVec<T> {
} }
impl<T: PartialEq> SingleOrVec<T> { impl<T: PartialEq> SingleOrVec<T> {
// TODO document /// Returns `true` if `self` is either a `Single` equal to `x`, or a `Vec` containing `x`.
///
/// # Examples
///
/// ```
/// let s = SingleOrVec::Single(10);
/// assert!(s.contains(&10));
/// assert!(!s.contains(&20));
///
/// let v = SingleOrVec::Vec(vec![10, 20]);
/// assert!(s.contains(&10));
/// assert!(s.contains(&20));
/// assert!(!s.contains(&30));
/// ```
pub fn contains(&self, x: &T) -> bool { pub fn contains(&self, x: &T) -> bool {
match self { match self {
SingleOrVec::Single(s) => s.deref() == x, SingleOrVec::Single(s) => s.deref() == x,

View file

@ -22,7 +22,6 @@ pub struct ValidationAttrs {
impl ValidationAttrs { impl ValidationAttrs {
pub fn new(attrs: &[syn::Attribute], errors: &Ctxt) -> Self { pub fn new(attrs: &[syn::Attribute], errors: &Ctxt) -> Self {
// TODO allow setting "validate" attributes through #[schemars(...)]
ValidationAttrs::default() ValidationAttrs::default()
.populate(attrs, "schemars", false, errors) .populate(attrs, "schemars", false, errors)
.populate(attrs, "validate", true, errors) .populate(attrs, "validate", true, errors)

View file

@ -489,11 +489,7 @@ fn expr_for_struct(
.map(|field| { .map(|field| {
let (ty, type_def) = type_for_field_schema(field); let (ty, type_def) = type_for_field_schema(field);
let required = if field.validation_attrs.required { let required = field.validation_attrs.required;
quote!(Some(true))
} else {
quote!(None)
};
let args = quote!(gen, #required); let args = quote!(gen, #required);
let mut schema_expr = quote_spanned! {ty.span()=> let mut schema_expr = quote_spanned! {ty.span()=>