-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Validate component properties #264 #340
Conversation
dash/dash.py
Outdated
@@ -805,6 +811,23 @@ def wrap_func(func): | |||
def add_context(*args, **kwargs): | |||
|
|||
output_value = func(*args, **kwargs) | |||
setattr( |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
dash/dash.py
Outdated
self._components[output.component_id]._validator.validate({ | ||
output.component_property: output_value | ||
}) | ||
if output.component_property == 'children': |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
dash/dash.py
Outdated
break | ||
matched_state_value = matched_state.get('value', None) | ||
args.append(matched_state_value) | ||
setattr( |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
dash/development/base_component.py
Outdated
pass | ||
|
||
|
||
cerberus.Validator.types_mapping['component'] =\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let us validate Component
objects with cerberus (useful for children
prop)
dash/development/base_component.py
Outdated
@@ -382,7 +488,8 @@ def __repr__(self): | |||
p not in keyword.kwlist and | |||
p not in ['dashEvents', 'fireEvent', 'setProps']] + ['**kwargs'] | |||
) | |||
|
|||
schema = {str(k): generate_property_schema(v) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make cerberus schema for this dash component type.
Hi, I tried to I typed the command |
dash/development/base_component.py
Outdated
|
||
|
||
|
||
# Forward declated Component class, see below. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did you mean 'declared'? PEP8 requires one space less :)
PS : this is my first review on dash code and I am mainly reading the code you wrote to learn more, my suggestion will mostly be about styling and spelling, I apologize in advance for that!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my suggestion will mostly be about styling and spelling, I apologize in advance for that!
Note that we enforce PEP8 in our tests here:
Lines 38 to 39 in 4d93652
pylint dash setup.py --rcfile=$PYLINTRC | |
flake8 dash setup.py |
So, it's not necessary to review PEP8 issues in our PRs as they will get fixed before it gets merged into master.
To validate the components in the callbacks, I think we'll need to pass the component name back in the So, that is, we could modify the Dash API to include and then do a component lookup in the callback decorator |
^ Definitely, I was going to add this to the next iteration. You don't need this to validate against components in the initial layout, but I don't think there is any other way to validate against dynamically generated components without updating state in the backend after initialization. |
PR which adds 'type' and 'namespace' to the |
|
Oh ok, I would still integrate with the dev tools so just setting debug=True is required for it. |
Oh I thought you meant something different, yeah it should be disabled in production. |
@rmarren1 How would the component as prop validate ? proptype is |
@T4rk1n There is a custom type called dash/dash/development/base_component.py Lines 380 to 397 in 588ce3d
|
Closing in favor of #452, which is the same PR but with a more readable commit history. Will link back to here for the relevant discussions. |
Allow tabs null children
PR for #264
Current Prerelease
Validation is on by default. To turn it off, you can set
app.config.disable_component_validation = True
Test Cases to run for demo
Example Error Messages
PropTypes to Cerberus reference
array
{'type': 'list'}
bool
{'type': 'boolean'}
func
{}
object
{'type': 'dict'}
dict
object. We cannot be more general (e.g.collections.abc.Mapping
) since the coreComponent
is an instance of that.string
{'type': 'string'}
symbol
{}
node
{'anyof': [{'type': 'component'}, {'type': 'boolean'}, {'type': 'number'}, {'type': 'string'}, {'type': 'list', 'schema': {'anyof': [{'type': 'component'}, {'type': 'boolean'}, {'type': 'number'}, {'type': 'string'}]}}]}
instanceOf(Object)
{}
oneOf(['val1', 2])
{'allowed': [None, 'val1', 2]}
'
characters stripped off each end. This is becausemetadata.json
generation serializes the literal values as json, so for examplePropTypes.oneOf(['News', 'Photos'])
serializes to["'News'", "'Photos'"]
. reactjs/react-docgen#57oneOfType( [ PropTypes.string, PropTypes.bool ] )
{'anyof': [{'type': 'string', 'type': 'boolean'}]}
PropType
that cannot be validated individually (e.g.PropType.func
), no validation will occur and the schema will effectively be{}
arrayOf( PropTypes.number )
{'type': 'list', 'schema': {'type': 'number'}}
objectOf( PropTypes.number )
{'type': 'dict', 'valueschema': {'type': 'number'}}
shape( { k1: PropTypes.string, k2: PropTypes.number } )
{'type': 'dict', 'allow_unknown': False, 'schema': {'k1': {'type': 'string}, 'k2': {'type': 'number'}}}
any
{'type': ('boolean', 'number', 'string', 'dict', 'list')}