Consider adding an isValid method
It would be nice if there was a method had to check if the model is "valid" in the sense that it contains the non-optional set of fields as defined by the schema. This method would allow for defense against invalid-json or mis-use of builders.
Yeah a validation method would be nice and there are things beyond nullability that we might need to check.
A few ideas:
For invalid JSON: The initializer that takes a json object should either throw an exception or fail to initialize a model (return null) in the case of an invalid input
Builders: The generated setters for the builders should check for null in non null properties and fail accordingly (assert, throws, etc)
Thoughts?
For invalid JSON: The initializer that takes a json object should either throw an exception or fail to initialize a model (return null) in the case of an invalid input
I like the idea, but it might be a bit too severe to throw out the entire model if, for example, a non-optional field is missing, but the rest of the model may still be of value. A minor back-end bug could lead to a more severe failure than is necessary. Of course that cuts both ways, because failing out early could also save us from severe bugs. I figured that having a model.isValid() method would let the implementer decide on that tradeoff.
Builders: The generated setters for the builders should check for null in non null properties and fail accordingly (assert, throws, etc)
Makes sense. In addition to validation in the setters, I think we'd also want to validate during the build() phase. That way we also catch the condition where you failed to call the setter at all.
Just in case it wasn't clear, by "non-optional" field, I'm referring to the fields in the "required": [] block of the json schema.
Having a Builder.build() method that can fail would require a bit more thought, i.e. it'd have to have a throws signature or be marked nullable. Both things could make the calling code a bit annoying.