-
Notifications
You must be signed in to change notification settings - Fork 275
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
Add input validation to simple metadata api #1140
Comments
See: Add root metadata class to new TUF metadata model theupdateframework#1137 Add classes for complex metadata fields theupdateframework#1139 Add input validation to simple metadata api theupdateframework#1140 Signed-off-by: Lukas Puehringer <[email protected]>
See: Add root metadata class to new TUF metadata model theupdateframework#1137 Add classes for complex metadata fields theupdateframework#1139 Add input validation to simple metadata api theupdateframework#1140 Signed-off-by: Lukas Puehringer <[email protected]>
This looks like a nice pattern. Each class defines appropriate validation methods, which are called by the Mixin's Any thoughts on whether a decorator might be a more idiomatic approach than a Mixin? We could wrap classes with a decorator which performs validation after init, before returning the instantiated object. |
Another idea is to copy validation techniques Django/Rails use... |
Nice idea. I took a brief look those look to be quite complicated and not equivalent to what we're trying to do here Django Model validation. |
Good thinking, @trishankatdatadog. But I agree with @joshuagl that we might not need something as powerful (Django's validation is really tailored towards web forms and/or ORM, similar is true for I think the in-toto approach is not so bad. It's also featured in this quite interesting blog post about different instance attribute validation techniques, at least the "individual validation functions" aspect (not the neat self-inspecting mixin part). The blog also mentions two promising validation libraries, The blog also mentions a Python built-in feature, i.e. What I like about both the decorator and descriptor approach(es) is that they make the constraints on the attributes more visible (in the head of the class definition) than vanilla validation methods. |
Descriptors look nice, and I'm all for avoid additional dependencies. I believe the pattern of: initialising empty objects, assigning values, and then validating should still work – so long as our empty objects have sane defaults. Based on 5mins experimentation in the Python interpreter:
|
The |
I wonder if sane defaults will always be possible, e.g. when thinking of the newly added But maybe the usage pattern that requires initialization of empty objects is suboptimal. I must say that it would be quite nice to always have certainty about the validity of tuf objects. |
I had to give default function arguments for all class arguments assigned in __init__ for Root, Snapshot, Targets, and Timestamp classes. I chose "None" as the easiest solution as a default argument, but we definitely want to add proper validation which will ensure we are not creating empty or partially populated objects. I didn't want to create a discussion for sensible defaults and argument validation here. There is already existing issue for that: theupdateframework#1140 Signed-off-by: Martin Vrachev <[email protected]>
In the Signed.from_dict function we are passing signed - a dictionary. Dictionaries are passed by reference and thus we want to make sure we are not changing the dictionary passed as a function argument. I had to give default function arguments for all class arguments assigned in __init__ for Root, Snapshot, Targets, and Timestamp classes. I chose "None" as the easiest solution as a default argument, but we definitely want to add proper validation which will ensure we are not creating empty or partially populated objects. I didn't want to create a discussion for sensible defaults and argument validation here. There is already existing issue for that: theupdateframework#1140 Signed-off-by: Martin Vrachev <[email protected]>
In the Signed.from_dict function we are passing signed - a dictionary. Dictionaries are passed by reference and thus we want to make sure we are not changing the dictionary passed as a function argument. I had to give default function arguments for all class arguments assigned in __init__ for Root, Snapshot, Targets, and Timestamp classes. I chose "None" as the easiest solution as a default argument, but we definitely want to add proper validation which will ensure we are not creating empty or partially populated objects. I didn't want to create a discussion for sensible defaults and argument validation here. There is already existing issue for that: theupdateframework#1140 Signed-off-by: Martin Vrachev <[email protected]>
In the Signed.from_dict function we are passing signed - a dictionary. Dictionaries are passed by reference and thus we want to make sure we are not changing the dictionary passed as a function argument. I had to give default function arguments for all class arguments assigned in __init__ for Root, Snapshot, Targets, and Timestamp classes. I chose "None" as the easiest solution as a default argument, but we definitely want to add proper validation which will ensure we are not creating empty or partially populated objects. I didn't want to create a discussion for sensible defaults and argument validation here. There is already existing issue for that: theupdateframework#1140 Signed-off-by: Martin Vrachev <[email protected]>
In the Signed.from_dict function we are passing signed - a dictionary. Dictionaries are passed by reference and thus we want to make sure we are not changing the dictionary passed as a function argument. I had to give default function arguments for all class arguments assigned in __init__ for Root, Snapshot, Targets, and Timestamp classes. I chose "None" as the easiest solution as a default argument, but we definitely want to add proper validation which will ensure we are not creating empty or partially populated objects. I didn't want to create a discussion for sensible defaults and argument validation here. There is already existing issue for that: theupdateframework#1140 Signed-off-by: Martin Vrachev <[email protected]>
In 7cfd100#r541018751 we discussed whether |
In a discussion with @jku, we noticed that our tests for the metadata classes don't test instantiating metadata objects without their required fields. |
I think we can lose this issue as we discussed that each of the cases has to be discussed separately. We had discussions validation for all cases separately:
|
Agreed, thanks Martin. Closing because we have implemented validation on a case-by-case basis. |
Coordinate with validation guidelines #1130
Description of issue or feature request:
Some suggestions:
from_json_file
/to_json_file
method, but with option todisable check as there might be a justified reason to read or write WIP
metadata to json.
work in progress. E.g. it might be convenient to create empty metadata
and assign attributes later on.
ValidationMixin
(see themixin
and it's usage for details).Current behavior:
No input validation
Expected behavior:
Add input validation
The text was updated successfully, but these errors were encountered: