Support additional (Python) types in more than the `type` validator
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Anfängerfreundlichkeit
- 30/100
Rechercherichtung
Beginne mit dem Lesen von Issue #364 und dem zugehörigen Pull Request und reproduziere anschließend die NamedTuple-Beispiele in diesem Issue mit den required- und additionalProperties-Validatoren. Lege fest, wie NamedTuple-Eigenschaften konsistent identifiziert und abgerufen werden sollen, und betrachte die Arbeit als abgeschlossen, wenn beide Beispiele die vorgesehenen Eigenschaften validieren, ohne ihre Werte als Eigenschaftsnamen zu behandeln.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
In issue #364 and the related PR the type checker was expanded to allow redefining how jsonschema identifies/validates a type, with the intention of allowing a named tuple to be used as a jsonschema "object" type. In practice, this does not appear to be enough, as other portions of jsonschema use the python "in" expression to test for or iterate over properties in an object. In a NamedTuple, this will actually test the values of the properties. Two specific examples (required and additionalProperties) highlight this:
from typing import NamedTuple
from jsonschema import Draft7Validator
from jsonschema.validators import extend
from jsonschema.exceptions import ValidationError
# dict or tuple w/ _fields is object (allowed NamedTuple as object)
def is_object_or_namedtuple(checker, instance):
return isinstance(instance, dict) or (
isinstance(instance, tuple) and hasattr(instance, '_fields'))
# redefine object to allow namedtuple
type_checker = Draft7Validator.TYPE_CHECKER.redefine(
'object', is_object_or_namedtuple)
# extend validator with new type checker
Validator = extend(Draft7Validator, type_checker=type_checker)
# validator with foo as required property
validator_foo_required = Validator(schema={
"type": "object",
"properties": {
"foo": {
"type": "string"
}
},
"required": ["foo"]
})
# validator with foo as optional property, no additional
validator_foo_optional_no_additional = Validator(schema={
"type": "object",
"properties": {
"foo": {
"type": "string"
}
},
"additionalProperties": False
})
class Example(NamedTuple):
foo: str
example = Example(foo="bar")
try:
validator_foo_required.validate(example)
except ValidationError as e:
print(f"Error: {e.message}")
try:
validator_foo_optional_no_additional.validate(example)
except ValidationError as e:
print(f"Error: {e.message}")
# the problem is that 'foo' is not "in" example..
print('Is foo in example? ', ('foo' in example))
# ... but 'bar' is
print('Is bar in example? ', ('bar' in example))
This is more of an FYI, as #364 was targeted at supporting NamedTuple, but it looks like there would be some bigger changes required to facilitate redefining how properties are identified/accessed in the NamedTuple.
- Vorherrschende Sprache
- Python
- Sterne
- 5k
- Forks
- 700
- Ø Merge
- 2 T. 1 Std.
- Gemergte PRs (30 T.)
- 5
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus python-jsonschema/jsonschema
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
python-jsonschema/jsonschema#1588 · 2 Kommentare ·
-
Iterator error contexts lose parent links and absolute pathsEvtl. wieder frei @DawnofGenX hat das vor 11 Tagen übernommen, und es ist kein Pull Request offen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
python-jsonschema/jsonschema#1584 · 3 Kommentare ·
-
regex format checker crashes with RecursionError on deeply nested patternsEvtl. vergeben @DawnofGenX hat das vor 11 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
python-jsonschema/jsonschema#1538 · 1 Kommentar ·
-
is_duration raises an uncaught decimal.Overflow on a duration with a large exponentEvtl. wieder frei Ein Pull Request dazu wurde ohne Merge geschlossen. OffenNeeds Test Upstream
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
python-jsonschema/jsonschema#1511 · 1 Kommentar ·
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 55/100
python-jsonschema/jsonschema#1590 ·
Alle Issues in python-jsonschema/jsonschema
Ähnliche Issues
-
docs: the dev-setup table and AGENTS.md name the reviewed_by guard that CI's Changelog job runsOffenarea: tools documentation good first issue ready for a contributor
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
eyalgolan/statecraft-israel#873 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
enhancement
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 5 Tagen
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
bug good first issue
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag