Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider adding an isValid method #185

Open
RicoYao opened this issue Mar 14, 2019 · 4 comments
Open

Consider adding an isValid method #185

RicoYao opened this issue Mar 14, 2019 · 4 comments

Comments

@RicoYao
Copy link
Contributor

RicoYao commented Mar 14, 2019

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.

@rahul-malik
Copy link
Collaborator

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?

@RicoYao
Copy link
Contributor Author

RicoYao commented Mar 14, 2019

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.

@RicoYao
Copy link
Contributor Author

RicoYao commented Mar 14, 2019

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.

@shashachu
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants