Skip to content

Commit

Permalink
[schema] Validate general hostname format #42
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Apr 3, 2016
1 parent 195055d commit ef8c296
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions netjsonconfig/backends/openwrt/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import six
from jinja2 import Environment, PackageLoader
from jsonschema import validate
from jsonschema import FormatChecker, validate
from jsonschema.exceptions import ValidationError as JsonSchemaError

from . import renderers
Expand Down Expand Up @@ -130,7 +130,7 @@ def _render_files(self):

def validate(self):
try:
validate(self.config, self.schema)
validate(self.config, self.schema, format_checker=FormatChecker())
except JsonSchemaError as e:
raise ValidationError(e)

Expand Down
1 change: 1 addition & 0 deletions netjsonconfig/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@
"type": "string",
"maxLength": 63,
"minLength": 1,
"format": "hostname",
"propertyOrder": 1,
},
"ula_prefix": {
Expand Down
17 changes: 17 additions & 0 deletions tests/openwrt/test_formats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unittest

from netjsonconfig import OpenWrt
from netjsonconfig.exceptions import ValidationError
from netjsonconfig.utils import _TabsMixin


class TestFormats(unittest.TestCase, _TabsMixin):
maxDiff = None

def test_general_hostname(self):
o = OpenWrt({"general": {"hostname": "invalid hostname"}})
with self.assertRaises(ValidationError):
o.validate()
o.config['general']['hostname'] = 'valid'
o.validate()

0 comments on commit ef8c296

Please sign in to comment.