Skip to content

Commit 3f6d2c6

Browse files
committed
Removed NetJSON type from schema
This has a few advantages: * simplifies code a bit * simplifies automatically generated UI The type attribute is still added when using the json method.
1 parent ff15530 commit 3f6d2c6

File tree

4 files changed

+12
-23
lines changed

4 files changed

+12
-23
lines changed

docs/source/general/basics.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ The previous example describes a device named ``RouterA`` which has a single
5353
network interface named ``eth0`` with a statically assigned ip address ``192.168.1.1/24``
5454
(CIDR notation).
5555

56-
Because netjsonconfig deals only with ``DeviceConfiguration`` objects, the ``type``
57-
attribute can be omitted, the library will add the correct type automatically.
56+
Because netjsonconfig deals only with ``DeviceConfiguration`` objects, the ``type`` attribute can be omitted.
5857

5958
The previous configuration object therefore can be shortened to:
6059

@@ -81,7 +80,7 @@ The previous configuration object therefore can be shortened to:
8180
}
8281
8382
From now on we will use the term *configuration dictionary* to refer to
84-
NetJSON DeviceConfiguration objects.
83+
*NetJSON DeviceConfiguration* objects.
8584

8685
Backends
8786
--------

netjsonconfig/backends/openwrt/openwrt.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ def __init__(self, config, templates=[], context={}):
3939
"""
4040
# perform deepcopy to avoid modifying the original config argument
4141
config = deepcopy(self._load(config))
42-
# allow omitting NetJSON type
43-
if 'type' not in config:
44-
config.update({'type': 'DeviceConfiguration'})
4542
self.config = self._merge_config(config, templates)
4643
self.config = self._evaluate_vars(self.config, context)
4744
self.env = Environment(loader=PackageLoader('netjsonconfig.backends.openwrt',
@@ -139,7 +136,7 @@ def validate(self):
139136

140137
def json(self, validate=True, *args, **kwargs):
141138
"""
142-
returns a string formatted in **NetJSON**;
139+
returns a string formatted as **NetJSON DeviceConfiguration**;
143140
performs validation before returning output;
144141
145142
``*args`` and ``*kwargs`` will be passed to ``json.dumps``;
@@ -148,7 +145,10 @@ def json(self, validate=True, *args, **kwargs):
148145
"""
149146
if validate:
150147
self.validate()
151-
return json.dumps(self.config, *args, **kwargs)
148+
# automatically adds NetJSON type
149+
config = deepcopy(self.config)
150+
config.update({'type': 'DeviceConfiguration'})
151+
return json.dumps(config, *args, **kwargs)
152152

153153
@classmethod
154154
def get_packages(cls):

netjsonconfig/schema.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"""
2-
NetJSON DeviceConfiguration JSON-Schema definition
3-
this should be up to date with the official spec:
4-
5-
http://netjson.org/rfc.html#DeviceConfiguration-schema
2+
NetJSON DeviceConfiguration JSON-Schema implementation
3+
http://netjson.org/rfc.html
64
"""
75

86
schema = {
@@ -269,15 +267,7 @@
269267
]
270268
}
271269
},
272-
"required": [
273-
"type"
274-
],
275270
"properties": {
276-
"type": {
277-
"type": "string",
278-
"enum": ["DeviceConfiguration"],
279-
"propertyOrder": 0,
280-
},
281271
"general": {
282272
"type": "object",
283273
"title": "General",

tests/openwrt/test_backend.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ def test_string_argument(self):
4545
OpenWrt('{}')
4646

4747
def test_validate(self):
48-
o = OpenWrt({'type': 'WRONG'})
48+
o = OpenWrt({'interfaces': 'WRONG'})
4949
with self.assertRaises(ValidationError):
5050
o.validate()
5151

52-
o = OpenWrt({'type': 'DeviceConfiguration'})
52+
o = OpenWrt({'interfaces': []})
5353
o.validate()
54-
o.config['type'] = 'CHANGED'
5554

55+
o.config['interfaces'] = 'CHANGED'
5656
try:
5757
o.validate()
5858
except ValidationError as e:

0 commit comments

Comments
 (0)