Skip to content

Validate component properties #264 #340

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

Closed
wants to merge 96 commits into from
Closed
Show file tree
Hide file tree
Changes from 92 commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
49d8c21
Update layout on server with each callback, for ids in initial layout.
rmarren1 Aug 15, 2018
12dc611
Update layout for dynamically created elements with ids.
rmarren1 Aug 15, 2018
a0e2f4f
Cerberus validation of initial layout.
rmarren1 Aug 16, 2018
7eb0dbd
Recursive schema generation, schema test case.
rmarren1 Aug 18, 2018
a3ccd76
Custom validation
rmarren1 Aug 23, 2018
377ec7b
Fix validation import
rmarren1 Aug 23, 2018
ce2a255
Merge branch 'master' into validate
rmarren1 Aug 26, 2018
9d0fa01
Make test cases pass
rmarren1 Aug 26, 2018
5665fa4
Fix pylint errors
rmarren1 Aug 26, 2018
f88b068
iteritems -> items for python3 compatibility.
rmarren1 Aug 26, 2018
6205742
Fix unicode and ordering issues with oyaml.
rmarren1 Aug 27, 2018
6b65a67
use dash-renderer version with namespace and type
rmarren1 Aug 27, 2018
4df2bb8
Only validate if 'namespace' and 'type' in json body.
rmarren1 Aug 27, 2018
9e5aa93
Change loader back to json, add hook to transform unicode to string.
rmarren1 Aug 28, 2018
b045753
Do not try to match schema string exactly.
rmarren1 Aug 28, 2018
89d7137
Component validation test cases
rmarren1 Aug 29, 2018
ffd63ba
Required prop type test case
rmarren1 Aug 29, 2018
954aae3
Pylint fixes
rmarren1 Aug 29, 2018
93d4bf6
Fix dict
rmarren1 Aug 29, 2018
a934c26
Improve error message, custom exceptions.
rmarren1 Aug 29, 2018
4bb97ef
Figure validation
rmarren1 Aug 30, 2018
4cb76fe
Fix import order for Pylint.
rmarren1 Aug 30, 2018
233c145
Extra newlines in exceptions for Pylint.
rmarren1 Aug 30, 2018
cf47627
Rename exceptions import for Pylint.
rmarren1 Aug 30, 2018
2817398
Only validate against callback property.
rmarren1 Aug 30, 2018
ee3d1ab
Allow 'None' in children lists.
rmarren1 Aug 30, 2018
9240436
If prop doesn't exist in schema, don't validate callback (for wildcards)
rmarren1 Aug 30, 2018
1373fc2
Fix tests.
rmarren1 Aug 30, 2018
7e804d0
Fix Pylint.
rmarren1 Aug 30, 2018
bc947e6
Add Plotly Figure validation correctly, print error from Plotly.py
rmarren1 Aug 30, 2018
c9b693b
Update renderer requirement
rmarren1 Aug 30, 2018
6c37980
Workaround for PropTypes.null
rmarren1 Sep 2, 2018
fa74afd
Allow 'None' in children arrays.
rmarren1 Sep 3, 2018
5bcec21
Merge branch 'master' into validate
rmarren1 Sep 3, 2018
dd13a80
Fix test cases
rmarren1 Sep 3, 2018
0a33769
Bump dash-html-components version to version published from 3.7.
rmarren1 Sep 5, 2018
c42c3d5
Update integration tests.
rmarren1 Sep 5, 2018
19910ad
Try not installing virtualenv
rmarren1 Sep 6, 2018
4b02ddb
Try venv in home directory
rmarren1 Sep 6, 2018
fcbebd9
Update plotly figure validator.
rmarren1 Sep 7, 2018
54558fe
Bump version and fix pylint
rmarren1 Sep 7, 2018
603aac2
Bump core component versions.
rmarren1 Sep 7, 2018
f6ecaa7
'{}' is not a valid figure.
rmarren1 Sep 7, 2018
5d7625a
Fix figure validator and bump dash-core-components version.
rmarren1 Sep 7, 2018
357ee94
Update wildcard callback test.
rmarren1 Sep 7, 2018
f067f91
Merge branch 'master' into validate
rmarren1 Sep 7, 2018
9b5b9f2
Add Cerberus to dev requirements files
rmarren1 Sep 7, 2018
f6c15da
Allow for required `children`.
rmarren1 Sep 20, 2018
f99490f
Pylint fixes
rmarren1 Sep 21, 2018
886c83a
Merge branch 'master' into validate
rmarren1 Sep 21, 2018
e75d6f1
Fix circle
rmarren1 Sep 21, 2018
8d1727e
Ignore too-many-lines
rmarren1 Sep 21, 2018
87db60a
disable too many lines in .pylintrc
rmarren1 Sep 21, 2018
6967dad
too many lines in .pylint37 too
rmarren1 Sep 21, 2018
42fab98
Add tests for numpy ndarray / pandas series as list.
rmarren1 Sep 28, 2018
06682d2
Update list validator to work for pd.Series, np.ndarray
rmarren1 Sep 28, 2018
906e7c9
Add tests for int / float in enum.
rmarren1 Sep 28, 2018
03a7c08
Update enum to work with int / float
rmarren1 Sep 28, 2018
b1d1337
Add tests for numpy int / float
rmarren1 Sep 28, 2018
426e4f3
Update number validation to support numpy int / float
rmarren1 Sep 28, 2018
b0b5385
Change name `_validate_callback` -> `_validate_callback_definition`
rmarren1 Oct 1, 2018
d98a1a7
`_validate_callback_output` -> `_validate_callback_serialization_error`
rmarren1 Oct 1, 2018
0523beb
Add initial layout validation test.
rmarren1 Oct 1, 2018
b5b7935
Move validation to method in base component.
rmarren1 Oct 1, 2018
d8eb35f
Add disable_component_validation config option.
rmarren1 Oct 1, 2018
b8e2c67
Run component validation on initial layout.
rmarren1 Oct 1, 2018
0fd11b7
Update initial layout validation test.
rmarren1 Oct 1, 2018
2049dfb
Add callback output validation test.
rmarren1 Oct 2, 2018
a3d1402
Bump dash-html-component version in dev.
rmarren1 Oct 2, 2018
015674e
Move component validation to its own method.
rmarren1 Oct 2, 2018
8c2d97f
Move initial layout test, add initialization in callback test
rmarren1 Oct 2, 2018
9fcfa96
Change name of initialization validation error
rmarren1 Oct 2, 2018
95f20cd
Add callback component initialization validation
rmarren1 Oct 2, 2018
f92812b
Make sure children is component before validation.
rmarren1 Oct 2, 2018
0a57cc5
Add Cerberus to setup.py install_requires.
rmarren1 Oct 5, 2018
64b4f5c
Give more information in validation error messages.
rmarren1 Oct 5, 2018
0a515d2
` -> * for error message blocks, so you can copy paste to github.
rmarren1 Oct 5, 2018
dce6a06
Rebase
rmarren1 Oct 18, 2018
708c2f7
Un-comment broken test.
rmarren1 Oct 18, 2018
c8f2953
Add test for using boolean in enum
rmarren1 Oct 18, 2018
c64bc6d
Update Table test component for boolean enum values
rmarren1 Oct 18, 2018
8202256
Forgot to add changed metadata_test.py file (Table component)
rmarren1 Oct 18, 2018
e3aa8d9
Add support for boolean in enum to schema generation.
rmarren1 Oct 18, 2018
f9db291
Add a test for 'options_with_unique_values' validator.
rmarren1 Oct 19, 2018
41d0e81
Add 'options_with_unique_values' validator.
rmarren1 Oct 19, 2018
616583a
lines too long fixes.
rmarren1 Oct 19, 2018
588ce3d
pylint fixes
rmarren1 Oct 19, 2018
cead8e7
Rebase
rmarren1 Nov 2, 2018
9962b2a
Change `disable_component_validation` to `suppress_validation_callbacks`
rmarren1 Nov 2, 2018
3bcfefb
Small typo in test react component
rmarren1 Nov 2, 2018
a5093a0
Monkey patch schemas onto dash_html_components for test
rmarren1 Nov 2, 2018
4af81bf
Use sys.modules rather than dynamic import.
rmarren1 Nov 2, 2018
d81fadb
Disable validation exceptions when in production.
rmarren1 Nov 2, 2018
944bcc3
Tell user how to turn off validation exceptions in the message.
rmarren1 Nov 2, 2018
b785e1b
Pylint fixes
rmarren1 Nov 2, 2018
ea5cf00
Fix line too long
rmarren1 Nov 2, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jobs:
python --version
python -m unittest tests.development.test_base_component
python -m unittest tests.development.test_component_loader
python -m unittest tests.development.test_component_validation
python -m unittest tests.test_integration
python -m unittest tests.test_resources
python -m unittest tests.test_configs
Expand Down
3 changes: 3 additions & 0 deletions .circleci/requirements/dev-requirements-py37.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ mock
tox
tox-pyenv
six
numpy
pandas
plotly>=2.0.8
requests[security]
flake8
pylint==2.1.1
astroid==2.0.4
Cerberus==1.2
3 changes: 3 additions & 0 deletions .circleci/requirements/dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ tox
tox-pyenv
mock
six
numpy
pandas
plotly>=2.0.8
requests[security]
flake8
pylint==1.9.2
Cerberus==1.2
1 change: 1 addition & 0 deletions .pylintrc37
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ confidence=
disable=invalid-name,
missing-docstring,
print-statement,
too-many-lines,
parameter-unpacking,
unpacking-in-except,
old-raise-syntax,
Expand Down
110 changes: 100 additions & 10 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import collections
import importlib
import json
import pprint
import pkgutil
import warnings
import re
Expand All @@ -20,6 +21,8 @@
from .dependencies import Event, Input, Output, State
from .resources import Scripts, Css
from .development.base_component import Component
from .development.validator import (DashValidator,
generate_validation_error_message)
from . import exceptions
from ._utils import AttributeDict as _AttributeDict
from ._utils import interpolate_str as _interpolate
Expand Down Expand Up @@ -84,6 +87,7 @@ def __init__(
external_scripts=None,
external_stylesheets=None,
suppress_callback_exceptions=None,
suppress_validation_exceptions=None,
components_cache_max_age=None,
**kwargs):

Expand Down Expand Up @@ -126,6 +130,10 @@ def __init__(
'suppress_callback_exceptions',
suppress_callback_exceptions, env_configs, False
),
'suppress_validation_exceptions': _configs.get_config(
'suppress_validation_exceptions',
suppress_validation_exceptions, env_configs, False
),
'routes_pathname_prefix': routes_pathname_prefix,
'requests_pathname_prefix': requests_pathname_prefix,
'include_assets_files': _configs.get_config(
Expand Down Expand Up @@ -168,6 +176,7 @@ def _handle_error(error):
self.assets_ignore = assets_ignore

self.registered_paths = {}
self.namespaces = {}

# urls
self.routes = []
Expand Down Expand Up @@ -256,7 +265,6 @@ def layout(self, value):
'a dash component.')

self._layout = value

layout_value = self._layout_value()
# pylint: disable=protected-access
self.css._update_layout(layout_value)
Expand Down Expand Up @@ -575,7 +583,7 @@ def react(self, *args, **kwargs):
'Use `callback` instead. `callback` has a new syntax too, '
'so make sure to call `help(app.callback)` to learn more.')

def _validate_callback(self, output, inputs, state, events):
def _validate_callback_definition(self, output, inputs, state, events):
# pylint: disable=too-many-branches
layout = self._cached_layout or self._layout_value()

Expand Down Expand Up @@ -713,7 +721,7 @@ def _validate_callback(self, output, inputs, state, events):
output.component_id,
output.component_property).replace(' ', ''))

def _validate_callback_output(self, output_value, output):
def _debug_callback_serialization_error(self, output_value, output):
valid = [str, dict, int, float, type(None), Component]

def _raise_invalid(bad_val, outer_val, bad_type, path, index=None,
Expand Down Expand Up @@ -831,7 +839,7 @@ def _validate_value(val, index=None):
# relationships
# pylint: disable=dangerous-default-value
def callback(self, output, inputs=[], state=[], events=[]):
self._validate_callback(output, inputs, state, events)
self._validate_callback_definition(output, inputs, state, events)

callback_id = '{}.{}'.format(
output.component_id, output.component_property
Expand All @@ -853,13 +861,11 @@ def callback(self, output, inputs=[], state=[], events=[]):

def wrap_func(func):
@wraps(func)
def add_context(*args, **kwargs):

output_value = func(*args, **kwargs)
def add_context(validated_output):
response = {
'response': {
'props': {
output.component_property: output_value
output.component_property: validated_output
}
}
}
Expand All @@ -870,7 +876,10 @@ def add_context(*args, **kwargs):
cls=plotly.utils.PlotlyJSONEncoder
)
except TypeError:
self._validate_callback_output(output_value, output)
self._debug_callback_serialization_error(
validated_output,
output
)
raise exceptions.InvalidCallbackReturnValue('''
The callback for property `{property:s}`
of component `{id:s}` returned a value
Expand All @@ -887,6 +896,7 @@ def add_context(*args, **kwargs):
mimetype='application/json'
)

self.callback_map[callback_id]['func'] = func
self.callback_map[callback_id]['callback'] = add_context

return add_context
Expand Down Expand Up @@ -915,7 +925,82 @@ def dispatch(self):
c['id'] == component_registration['id']
][0])

return self.callback_map[target_id]['callback'](*args)
output_value = self.callback_map[target_id]['func'](*args)

# Only validate if we get required information from renderer
# and validation is not turned off by user
if (
(not self.config.suppress_validation_exceptions) and
'namespace' in output and
'type' in output
):
# Python2.7 might make these keys and values unicode
namespace = str(output['namespace'])
component_type = str(output['type'])
component_id = str(output['id'])
component_property = str(output['property'])
callback_func_name = self.callback_map[target_id]['func'].__name__
self._validate_callback_output(namespace, component_type,
component_id, component_property,
callback_func_name,
args, output_value)

return self.callback_map[target_id]['callback'](output_value)

def _validate_callback_output(self, namespace, component_type,
component_id, component_property,
callback_func_name, args, value):
module = sys.modules[namespace]
component = getattr(module, component_type)
# pylint: disable=protected-access
validator = DashValidator({
component_property: component._schema.get(component_property, {})
})
valid = validator.validate({component_property: value})
if not valid:
error_message = """


A Dash Callback produced an invalid value!

Dash tried to update the `{component_property}` prop of the
`{component_name}` with id `{component_id}` by calling the
`{callback_func_name}` function with `{args}` as arguments.

This function call returned `{value}`, which did not pass
validation tests for the `{component_name}` component.

The expected schema for the `{component_property}` prop of the
`{component_name}` component is:

***************************************************************
{component_schema}
***************************************************************

""".replace(' ', '').format(
component_property=component_property,
component_name=component.__name__,
component_id=component_id,
callback_func_name=callback_func_name,
args='({})'.format(", ".join(map(repr, args))),
value=value,
component_schema=pprint.pformat(
component._schema[component_property]
)
)
error_message +=\
"The errors in validation are as follows:\n\n"

raise exceptions.CallbackOutputValidationError(
generate_validation_error_message(
validator.errors, 0, error_message))
# Must also validate initialization of newly created components
if component_property == 'children':
if isinstance(value, Component):
value.validate()
for component in value.traverse():
if isinstance(component, Component):
component.validate()

def _validate_layout(self):
if self.layout is None:
Expand All @@ -932,6 +1017,11 @@ def _validate_layout(self):

component_ids = {layout_id} if layout_id else set()
for component in to_validate.traverse():
if (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally there is no () after if in python.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how else to style since the if statement is > 80 characters. PEP isn't super specific about what to do here: https://www.python.org/dev/peps/pep-0008/#multiline-if-statements.

not self.config.suppress_validation_exceptions and
isinstance(component, Component)
):
component.validate()
component_id = getattr(component, 'id', None)
if component_id and component_id in component_ids:
raise exceptions.DuplicateIdError(
Expand Down
Loading