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

Adding the ability to set a 'required' boolean kwarg in the Object.add_object method #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Contributors
- `Heiho1 <https://github.com/heiho1>`_
- `YehudaCorsia <https://github.com/YehudaCorsia>`_
- `KOLANICH <https://github.com/KOLANICH>`_
- `Shane Curtin <https://github.com/sj-curtin>`_
5 changes: 3 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,13 @@ Merge in a JSON schema. This can be a ``dict`` or another ``SchemaBuilder`` obje
There is no schema validation. If you pass in a bad schema,
you might get back a bad schema.

``add_object(obj)``
^^^^^^^^^^^^^^^^^^^
``add_object(obj, **kwargs)``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Modify the schema to accommodate an object.

:param obj: any object or scalar that can be serialized in JSON
:param \**required (bool): if set to `False` the required keyword in the schema will not be included

``to_schema()``
^^^^^^^^^^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions genson/schema/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ def add_schema(self, schema):
del schema['$schema']
self._root_node.add_schema(schema)

def add_object(self, obj):
def add_object(self, obj, **kwargs):
"""
Modify the schema to accommodate an object.

:param obj: any object or scalar that can be serialized in JSON
"""
self._root_node.add_object(obj)
self._root_node.add_object(obj, **kwargs)

def to_schema(self):
"""
Expand Down
4 changes: 2 additions & 2 deletions genson/schema/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def add_schema(self, schema):
# return self for easy method chaining
return self

def add_object(self, obj):
def add_object(self, obj, **kwargs):
"""
Modify the schema to accommodate an object.

Expand All @@ -47,7 +47,7 @@ def add_object(self, obj):

# delegate to SchemaType object
active_strategy = self._get_strategy_for_object(obj)
active_strategy.add_object(obj)
active_strategy.add_object(obj, **kwargs)

# return self for easy method chaining
return self
Expand Down
4 changes: 2 additions & 2 deletions genson/schema/strategies/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def add_schema(self, schema):
if 'items' in schema:
self._items.add_schema(schema['items'])

def add_object(self, obj):
def add_object(self, obj, **kwargs):
for item in obj:
self._items.add_object(item)
self._items.add_object(item, **kwargs)

def items_to_schema(self):
return self._items.to_schema()
Expand Down
2 changes: 1 addition & 1 deletion genson/schema/strategies/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _add_extra_keywords(self, schema):
'values ({1!r} vs. {2!r}). Using {1!r}').format(
keyword, self._extra_keywords[keyword], value))

def add_object(self, obj):
def add_object(self, obj, **kwargs):
pass

def to_schema(self):
Expand Down
11 changes: 7 additions & 4 deletions genson/schema/strategies/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def add_schema(self, schema):
else:
self._required &= required

def add_object(self, obj):
def add_object(self, obj, **kwargs):
properties = set()
for prop, subobj in obj.items():
pattern = None
Expand All @@ -55,12 +55,15 @@ def add_object(self, obj):
pattern = self._matching_pattern(prop)

if pattern is not None:
self._pattern_properties[pattern].add_object(subobj)
self._pattern_properties[pattern].add_object(subobj, **kwargs)
else:
properties.add(prop)
self._properties[prop].add_object(subobj)
self._properties[prop].add_object(subobj, **kwargs)

if self._required is None:
# If 'required' is set to False they will not be added to the schema
if kwargs.get("required") is False:
self._required = set()
elif self._required is None:
self._required = properties
else:
self._required &= properties
Expand Down
2 changes: 1 addition & 1 deletion genson/schema/strategies/scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def add_schema(self, schema):
if schema.get('type') == 'number':
self._type = 'number'

def add_object(self, obj):
def add_object(self, obj, **kwargs):
if isinstance(obj, float):
self._type = 'number'

Expand Down
4 changes: 2 additions & 2 deletions test/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def setUp(self):
def set_schema_options(self, **options):
self.builder = SchemaNode(**options)

def add_object(self, obj):
self.builder.add_object(obj)
def add_object(self, obj, **kwargs):
self.builder.add_object(obj, **kwargs)
self._objects.append(obj)

def add_schema(self, schema):
Expand Down
33 changes: 33 additions & 0 deletions test/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,39 @@ def test_add_object(self):
"$schema": SchemaBuilder.DEFAULT_URI,
"type": "null"})

def test_add_object_required_false(self):
mock_dict = {
"mock_value_one": "abc"
}
self.add_object(mock_dict, required=False)

self.assertResult({
"$schema": SchemaBuilder.DEFAULT_URI,
"type": "object",
"properties": {
"mock_value_one": {
"type": "string"
}
}
})

def test_add_object_required_true(self):
mock_dict = {
"mock_value_one": "abc"
}
self.add_object(mock_dict, required=True)

self.assertResult({
"$schema": SchemaBuilder.DEFAULT_URI,
"type": "object",
"properties": {
"mock_value_one": {
"type": "string"
}
},
"required": ["mock_value_one"]
})

def test_to_json(self):
self.assertEqual(
self.builder.to_json(),
Expand Down