schemars icon indicating copy to clipboard operation
schemars copied to clipboard

Fallible visitors

Open nightkr opened this issue 3 years ago • 3 comments

When writing somewhat more complex Visitors (such as https://github.com/kube-rs/kube-rs/pull/779) there are scenarios where we simply can't apply the transform (like when properties end up conflicting...). It would be nice if we could return proper structured errors in those cases, rather than panicking or silently ending up with a corrupt schema.

nightkr avatar Jan 14 '22 10:01 nightkr

Something like this?

pub trait OutputVisitor {
    type Output;
    fn visit_root_schema(&mut self, root: &mut RootSchema) -> Self::Output;
    fn visit_schema(&mut self, schema: &mut Schema) -> Self::Output;
    fn visit_schema_object(&mut self, schema: &mut SchemaObject) -> Self::Output;
}

Perhaps the existing visitor could simply be a wrapper where Output = ()

ahl avatar Jul 19 '22 15:07 ahl

I think we would need to do something like this to ensure that schemars knows how to identify and propagate errors:

pub trait OutputVisitor {
    type Error;
    fn visit_root_schema(&mut self, root: &mut RootSchema) -> Result<(), Self::Error>;
    fn visit_schema(&mut self, schema: &mut Schema) -> Result<(), Self::Error>;
    fn visit_schema_object(&mut self, schema: &mut SchemaObject) -> Result<(), Self::Error>;
}

Infallible visitors could set type Error = Infallible;, which would ensure that the error case is compiled away.

nightkr avatar Jul 19 '22 18:07 nightkr