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

Feature/Question: Adding Records to Array Objects #278

Closed
rmspeers opened this issue Feb 19, 2024 · 4 comments · Fixed by #279
Closed

Feature/Question: Adding Records to Array Objects #278

rmspeers opened this issue Feb 19, 2024 · 4 comments · Fixed by #279

Comments

@rmspeers
Copy link
Contributor

What are you trying to do?
I have a schema which has a list of other objects in it. This is read in and recognized as an item by the library as type=='array', and the array of typed objects are under the 'data' attribute.

Do you have an idea about how this should work?

I'd like to be able to programmatically add new elements to the array. I may be missing something - likely am - but don't see how I can ask the object to let me initalize a new object of the list element, so that I can add it.

For example:
Data has:

"itemList":
   .type = 'array'
   .data = [
      <item name='example', id=56>
   ]

I wish to programatically add to the itemList array to add:
<item name='another', id=59>

Is there a generator/init accessible that I'm missing somewhere?

@swkeypair
Copy link

This feature or capability is also important for me as instances of the array need to be dynamically updated.

cwacek added a commit that referenced this issue Feb 24, 2024
Also a bug fix to handle literals being passed in
@cwacek
Copy link
Owner

cwacek commented Feb 24, 2024

The way you do this is by setting the item validation to a reference in the schema so it gets a named object. You can then instantiate that object and add it to the array.

You can also do this with the anonymous object, but if the array item type is complicated (i.e. an object) that isn't going to be as obvious.

See an example of how this works here (specifically line 178):

def test_arrays_can_have_reffed_items_of_mixed_type():
schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "test",
"type": "object",
"properties": {
"list": {
"type": "array",
"items": {
"oneOf": [
{"$ref": "#/definitions/foo"},
{
"type": "object",
"properties": {"bar": {"type": "string"}},
"required": ["bar"],
},
]
},
}
},
"definitions": {"foo": {"type": "string"}},
}
builder = pjs.ObjectBuilder(schema)
ns = builder.build_classes()
x = ns.Test(list=["foo", "bar"])
ns.Test(list=[{"bar": "nice"}, "bar"])
with pytest.raises(pjs.ValidationError):
ns.Test(list=[100])
assert x.list == ["foo", "bar"]
x.list.append(ns.Foo("bleh"))
assert x.list == ["foo", "bar", "bleh"]

I just added a fix to handle an instantiation error I saw when making sure that example worked, as part of the linked PR.

@cwacek cwacek linked a pull request Feb 24, 2024 that will close this issue
cwacek added a commit that referenced this issue Feb 24, 2024
tests: Add a test to validate expected behavior on #278
@cwacek
Copy link
Owner

cwacek commented Feb 24, 2024

Fix included in 0.5.3.

@rmspeers
Copy link
Contributor Author

Thanks, really appreciate the pointer and the fix, working great. I am inferring the type name and using getattr to get it from the schema, there is probably a place in the special vars to grab that name from (e.g., in the list, vs inferring it), but it's working fine as is for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants