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

fix: change the validation logic for python_date_format #25510

Merged
merged 21 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
30 changes: 15 additions & 15 deletions superset/datasets/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
# specific language governing permissions and limitations
# under the License.
import json
import re
from datetime import datetime
from typing import Any

from dateutil.parser import isoparse
from flask_babel import lazy_gettext as _
from marshmallow import fields, pre_load, Schema, ValidationError
from marshmallow.validate import Length
Expand All @@ -42,26 +43,25 @@
}


def validate_python_date_format(value: str) -> None:
regex = re.compile(
r"""
^(
epoch_s|epoch_ms|
(?P<date>%Y([-/]%m([-/]%d)?)?)([\sT](?P<time>%H(:%M(:%S(\.%f)?)?)?))?
)$
""",
re.VERBOSE,
)
match = regex.match(value or "")
if not match:
raise ValidationError([_("Invalid date/timestamp format")])
def validate_python_date_format(value: str) -> bool:
if value in ("epoch_s", "epoch_ms", None):
mapledan marked this conversation as resolved.
Show resolved Hide resolved
return True
try:
dt_str = datetime.now().strftime(value)
mapledan marked this conversation as resolved.
Show resolved Hide resolved
isoparse(dt_str)
mapledan marked this conversation as resolved.
Show resolved Hide resolved
except ValueError as ex:
raise ValidationError([_("Invalid date/timestamp format")]) from ex
return True


class DatasetColumnsPutSchema(Schema):
id = fields.Integer(required=False)
column_name = fields.String(required=True, validate=Length(1, 255))
type = fields.String(allow_none=True)
advanced_data_type = fields.String(allow_none=True, validate=Length(1, 255))
advanced_data_type = fields.String(
allow_none=True,
validate=Length(1, 255),
)
verbose_name = fields.String(allow_none=True, metadata={Length: (1, 1024)})
description = fields.String(allow_none=True)
expression = fields.String(allow_none=True)
Expand Down
49 changes: 49 additions & 0 deletions tests/unit_tests/datasets/schema_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# pylint: disable=import-outside-toplevel, invalid-name, unused-argument, redefined-outer-name
import pytest
from marshmallow import ValidationError

from superset.datasets.schemas import validate_python_date_format


# pylint: disable=too-few-public-methods
@pytest.mark.parametrize(
"payload",
[
None,
"epoch_ms",
"epoch_s",
"%Y/%m/%dT%H:%M:%S.%f",
mapledan marked this conversation as resolved.
Show resolved Hide resolved
"%Y-%m-%dT%H:%M:%S.%f",
"%Y%m%d",
],
)
def test_validate_python_date_format(payload) -> None:
assert validate_python_date_format(payload)


@pytest.mark.parametrize(
"payload",
[
"%d%m%Y",
],
)
def test_validate_python_date_format_raises(payload) -> None:
with pytest.raises(ValidationError):
validate_python_date_format(payload)
Loading