Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Support additional (Python) types in more than the `type` validator

Aperta
#727 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
30/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Inizia leggendo l’issue #364 e la relativa pull request, quindi riproduci gli esempi di NamedTuple presenti in questa issue con i validator required e additionalProperties. Definisci come identificare e accedere in modo coerente alle proprietà di NamedTuple e considera completato il lavoro quando entrambi gli esempi convalidano le proprietà previste senza trattarne i valori come nomi di proprietà.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Dialects v2 Enhancement

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.

Lingua principale
Python
Stelle
5k
Fork
700
Merge medio
2g 1h
PR unite (30g)
5

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python-jsonschema/jsonschema

Tutte le issue di python-jsonschema/jsonschema

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.