Add null type parser #32

Merged
fredsonnenwald merged 2 commits from add-null into main 2025-08-19 02:39:12 +00:00
fredsonnenwald commented 2025-06-30 11:35:55 +00:00 (Migrated from github.com)

I was trying to create a model with a schema containing fields like

'a_thing': {'anyOf': [{'type': 'number'},
                      {'type': 'null'}],
            'default': None}

but failing with the error

ValueError: Unknown type

To address this I've added a parser for the null type based on the boolean and integer parsers.

With this change, the example

from jambo import SchemaConverter


schema = {
    "title": "Person",
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer"},
        "void": {"type": "null"},
    },
    "required": ["name"],
}

Person = SchemaConverter.build(schema)

obj = Person(name="Alice", age=30)
print(obj)

as expected produces

$ python jambo_test.py 
name='Alice' age=30 void=None

PS - this library looks like it'll be really useful, thanks!

I was trying to create a model with a schema containing fields like ``` 'a_thing': {'anyOf': [{'type': 'number'}, {'type': 'null'}], 'default': None} ``` but failing with the error ``` ValueError: Unknown type ``` To address this I've added a parser for the [null](https://json-schema.org/understanding-json-schema/reference/null) type based on the boolean and integer parsers. With this change, the example ```python from jambo import SchemaConverter schema = { "title": "Person", "type": "object", "properties": { "name": {"type": "string"}, "age": {"type": "integer"}, "void": {"type": "null"}, }, "required": ["name"], } Person = SchemaConverter.build(schema) obj = Person(name="Alice", age=30) print(obj) ``` as expected produces ```console $ python jambo_test.py name='Alice' age=30 void=None ``` PS - this library looks like it'll be really useful, thanks!
codecov-commenter commented 2025-08-19 02:35:29 +00:00 (Migrated from github.com)

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

:warning: Please install the !['codecov app svg image'](https://github.com/codecov/engineering-team/assets/152432831/e90313f4-9d3a-4b63-8b54-cfe14e7ec20d) to ensure uploads and comments are reliably processed by Codecov. ## [Codecov](https://app.codecov.io/gh/HideyoshiNakazone/jambo/pull/32?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Vitor+Hideyoshi) Report :white_check_mark: All modified and coverable lines are covered by tests. :loudspeaker: Thoughts on this report? [Let us know!](https://github.com/codecov/feedback/issues/255)
HideyoshiNakazone commented 2025-08-19 02:38:44 +00:00 (Migrated from github.com)

Technically you could solve your initial requirement using the following schema:

        schema = {
            "title": "Test",
            "type": "object",
            "properties": {
                "a_thing": {"type": "number", "default": None},
            },
            "required": ["a_thing"],
        }

That said, I also understand that for this tool to be compliant with the JSON Schema specification, we’ll eventually need to support all types defined in the spec.

I tested your code and found a few issues, which I’ve fixed in this PR. Since the fixes are included here, I’ll go ahead and merge your changes.

Technically you could solve your initial requirement using the following schema: ``` schema = { "title": "Test", "type": "object", "properties": { "a_thing": {"type": "number", "default": None}, }, "required": ["a_thing"], } ``` That said, I also understand that for this tool to be compliant with the JSON Schema specification, we’ll eventually need to support all types defined in the spec. I tested your code and found a few issues, which I’ve fixed in this PR. Since the fixes are included here, I’ll go ahead and merge your changes.
HideyoshiNakazone (Migrated from github.com) approved these changes 2025-08-19 02:39:02 +00:00
Sign in to join this conversation.