Skip to content

Commit

Permalink
Validate start/end when scheduling queries (#7508)
Browse files Browse the repository at this point in the history
* Validate start/end when scheduling queries

* Use chrono instead of Sugar
  • Loading branch information
betodealmeida authored May 16, 2019
1 parent a4bddc6 commit c7f8f27
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
20 changes: 18 additions & 2 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -858,13 +858,19 @@ To allow scheduled queries, add the following to your `config.py`:
},
'start_date': {
'type': 'string',
'format': 'date-time',
'title': 'Start date',
# date-time is parsed using the chrono library, see
# https://www.npmjs.com/package/chrono-node#usage
'format': 'date-time',
'default': 'tomorrow at 9am',
},
'end_date': {
'type': 'string',
'format': 'date-time',
'title': 'End date',
# date-time is parsed using the chrono library, see
# https://www.npmjs.com/package/chrono-node#usage
'format': 'date-time',
'default': '9am in 30 days',
},
'schedule_interval': {
'type': 'string',
Expand All @@ -890,6 +896,16 @@ To allow scheduled queries, add the following to your `config.py`:
),
},
},
'VALIDATION': [
# ensure that start_date <= end_date
{
'name': 'less_equal',
'arguments': ['start_date', 'end_date'],
'message': 'End date cannot be before start date',
# this is where the error message is shown
'container': 'end_date',
},
],
},
}
Expand Down
15 changes: 15 additions & 0 deletions superset/assets/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions superset/assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"bootstrap": "^3.3.6",
"bootstrap-slider": "^10.0.0",
"brace": "^0.11.1",
"chrono-node": "^1.3.11",
"classnames": "^2.2.5",
"d3-array": "^1.2.4",
"d3-color": "^1.2.0",
Expand Down
50 changes: 48 additions & 2 deletions superset/assets/src/SqlLab/components/ScheduleQueryButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,56 @@
import React from 'react';
import PropTypes from 'prop-types';
import Form from 'react-jsonschema-form';
import chrono from 'chrono-node';
import { t } from '@superset-ui/translation';

import Button from '../../components/Button';
import ModalTrigger from '../../components/ModalTrigger';

const validators = {
greater: (a, b) => a > b,
greater_equal: (a, b) => a >= b,
less: (a, b) => a < b,
less_equal: (a, b) => a <= b,
};

function getJSONSchema() {
const jsonSchema = window.featureFlags.SCHEDULED_QUERIES.JSONSCHEMA;
// parse date-time into usable value (eg, 'today' => `new Date()`)
Object.entries(jsonSchema.properties).forEach(([key, properties]) => {
if (properties.default && properties.format === 'date-time') {
jsonSchema.properties[key] = {
...properties,
default: chrono.parseDate(properties.default).toISOString(),
};
}
});
return jsonSchema;
}

function getUISchema() {
return window.featureFlags.SCHEDULED_QUERIES.UISCHEMA;
}

function getValidationRules() {
return window.featureFlags.SCHEDULED_QUERIES.VALIDATION || [];
}

function getValidator() {
const rules = getValidationRules();
return (formData, errors) => {
rules.forEach((rule) => {
const test = validators[rule.name];
const args = rule.arguments.map(name => formData[name]);
const container = rule.container || rule.arguments.slice(-1)[0];
if (!test(...args)) {
errors[container].addError(rule.message);
}
});
return errors;
};
}

const propTypes = {
defaultLabel: PropTypes.string,
sql: PropTypes.string.isRequired,
Expand Down Expand Up @@ -79,9 +124,10 @@ class ScheduleQueryButton extends React.PureComponent {
renderModalBody() {
return (
<Form
schema={window.featureFlags.SCHEDULED_QUERIES.JSONSCHEMA}
uiSchema={window.featureFlags.SCHEDULED_QUERIES.UISCHEMA}
schema={getJSONSchema()}
uiSchema={getUISchema()}
onSubmit={this.onSchedule}
validate={getValidator()}
/>
);
}
Expand Down

0 comments on commit c7f8f27

Please sign in to comment.