Question about validators across $refs
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 35/100
- Tipo di issue
- Bug
- Chiarezza
- Da chiarire
- Stato di attività
- Ferma
- Stack tecnologico
- python
- Ambito
- backend-api-design
Direzione di ricerca
Inizia con gli outer.json, nested.json e lo script segnalati, quindi esegui il confronto con jsonschema 4.9.1 e 4.10.0. Traccia il modo in cui viene selezionato il validatore personalizzato Draft4 attraverso $ref e documenta se è possibile preservare il comportamento precedente di compilazione dei valori predefiniti senza rimuovere $schema.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Hi, I have a question regarding the change in 4.10.0 and the default filling custom validator suggested here.
Before this version, I could use this enhanced validator to fill in default values even across "$ref"s to different schema.
But 4.10.0 added support "for referencing schemas with $ref across different versions of the specification", which means that now the validator is determined dynamically based on the "$schema" and the custom one is not used for the referred schema.
Here is an example:
outer:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"count": {
"default": 0,
"type": "integer"
},
"nested": {
"$ref": "file://nested.json"
}
}
}
nested:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"nestedcount": {
"default": 0,
"type": "integer"
},
"name": {
"type": "string"
}
}
}
script:
import copy
import json
import jsonschema
def extend_with_default(validator_class):
validate_properties = validator_class.VALIDATORS["properties"]
def set_defaults(validator, properties, instance, schema):
for property, subschema in properties.items():
if "default" in subschema:
instance.setdefault(property, subschema["default"])
for error in validate_properties(
validator, properties, instance, schema,
):
yield error
return jsonschema.validators.extend(
validator_class, {"properties" : set_defaults},
)
Draft4ValidatorWithDefaults = extend_with_default(jsonschema.Draft4Validator)
def load_schema(uri):
with open(uri.removeprefix("file://")) as ifile:
return json.load(ifile)
schema = load_schema("outer.json")
resolver = jsonschema.RefResolver(
"outer.json", schema,
handlers={"file": load_schema})
validator_with_defaults = Draft4ValidatorWithDefaults(schema, resolver=resolver)
validator = jsonschema.Draft4Validator(schema, resolver=resolver)
content = {
"nested": {"name": "foo"}
}
print(jsonschema.__version__)
validator.validate(content)
print(content)
validator_with_defaults.validate(content)
print(content)
When I run this with jsonschema 4.9.1, I get:
4.9.1
{'nested': {'name': 'foo'}}
{'nested': {'name': 'foo', 'nestedcount': 0}, 'count': 0}
when I use 4.10.0, it looks like this:
4.10.0
{'nested': {'name': 'foo'}}
{'nested': {'name': 'foo'}, 'count': 0}
My question is, is there a way to revert to the previous behavior? I found out that when I delete the "$schema" key from the referred schema, it works as before, but that doesn't seem exactly nice (and I am not sure that won't cause other issues)...
- Lingua principale
- Python
- Stelle
- 5k
- Fork
- 671
- Merge medio
- 1g 1h
- PR unite (30g)
- 10
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python-jsonschema/jsonschema
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
python-jsonschema/jsonschema#1538 ·
-
Needs Test Upstream
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
python-jsonschema/jsonschema#1511 · 1 commento ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 68/100
python-jsonschema/jsonschema#1558 · 2 commenti ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
python-jsonschema/jsonschema#1547 ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
python-jsonschema/jsonschema#1536 · 1 commento ·
Tutte le issue di python-jsonschema/jsonschema
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
browser-use/browser-use#5905 ·
-
type: enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
ynput/ayon-python-api#363 ·
-
bug needs triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
modelscope/FunASR#3728 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
open-compass/opencompass#2655 ·