Skip to content
This repository was archived by the owner on May 12, 2020. It is now read-only.
This repository was archived by the owner on May 12, 2020. It is now read-only.

Adding field validation to support validation of nested models #41

@allenchen1154

Description

@allenchen1154

An issue I encountered when integrating RAVE was boilerplate for dealing with nested models. Let's say I have a class, ModelA, that contains an instance of ModelB. For an instance of ModelA to be considered valid, its instance of ModelB must also be valid.

@Validated(factory = SampleFactory.class)
public class ModelA {
    private ModelB mModelB;

    @MustBeTrue 
    public boolean isModelBValid() { 
        try {
            Rave.getInstance().validate(mModelB);
            return true;
        } catch (RaveException e) {
            return false;
        }
    }
}

@Validated(factory = SampleFactory.class)
public class ModelB {
    // ModelB can have its own arbitrary validation criteria, including nested models
}

There are some issues with the above code:

  • This doesn't scale well as ModelA accumulates more nested models. Each one requires its own @MustBeTrue-annotated getter, which must have the same code duplicated.
  • Every model class that contains an instance of ModelB must have the same boilerplate.

I think the solution to this issue is to have default, recursive validation of fields. The logic would be something like:

void validate(Object model)
    for field in model:
        if field is supported:
            validate(field)
    // current logic here for validation of additional getters 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions