Skip to content

Commit

Permalink
Merge 58b2009 into 5174499
Browse files Browse the repository at this point in the history
  • Loading branch information
Grokzen committed Oct 9, 2019
2 parents 5174499 + 58b2009 commit c77017d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/release-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Next release
- Dropped support for pyyaml
- Added new cli argument "--encoding ENCODING" that specifies what encoding to open data and schema files with
- Enum error strings now output all possible values for easier debugging
- Impelment new type url that uses a relative simple regex to validate url:s according to RFC 1808


1.7.0 (October 3, 2018)
Expand Down
4 changes: 4 additions & 0 deletions docs/validation-rules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ The following types are available:
- **timestamp**
- Validates for basic timestamp formats

- **url**
- Validates data is a valid URL based on RFC 1808. Uses following regex
http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+


Example

Expand Down
13 changes: 12 additions & 1 deletion pykwalify/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
""" pyKwalify - types.py """

# python stdlib
import re
import datetime
from pykwalify.compat import basestring, bytes

Expand Down Expand Up @@ -33,7 +34,8 @@ class text(object):
"text": text,
"any": object,
"enum": str,
"none": None
"none": None,
"url": str,
}


Expand Down Expand Up @@ -144,6 +146,14 @@ def is_date(obj):
return isinstance(obj, basestring) or isinstance(obj, datetime.date)


def is_url(obj):
"""
:param obj: Object that is to be validated
:return: True/False if obj is valid
"""
return re.match('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', obj)


tt = {
"str": is_string,
"int": is_int,
Expand All @@ -157,4 +167,5 @@ def is_date(obj):
"timestamp": is_timestamp,
"scalar": is_scalar,
"date": is_date,
"url": is_url,
}
11 changes: 11 additions & 0 deletions tests/files/fail/test_type_url.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
name: url2
desc: basic url type validation. Fails as not confirms with RFC 1808
#
schema:
type: url
#
data:
"www.google.com"
errors:
- "Value 'www.google.com' is not of type 'url'. Path: ''"
9 changes: 9 additions & 0 deletions tests/files/success/test_type_url.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
name: url1
desc: basic url type validation
#
schema:
type: url
#
data:
"http://www.google.com"
4 changes: 4 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ def test_core_files(self):
"test_type_text.yaml",
# All tests for TYPE: timestamp
"test_type_timestamp.yaml",
# All tests for TYPE: url
"test_type_url.yaml",
]

_fail_tests = [
Expand Down Expand Up @@ -518,6 +520,8 @@ def test_core_files(self):
("test_type_text.yaml", SchemaError),
# All tests for TYPE: timestamp
("test_type_timestamp.yaml", SchemaError),
# All tests for TYPE: url
("test_type_url.yaml", SchemaError),
]

# Add override magic to make it easier to test a specific file
Expand Down

0 comments on commit c77017d

Please sign in to comment.