`Validator`s pubilc API
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 25/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Ferma
- Stack tecnologico
- python
- Ambito
- backend-api-design
Direzione di ricerca
Inizia con il protocollo Validator, validators.extend, Draft202012Validator e il validatore delle proprietà personalizzate mostrato nell’issue. Confronta l’API pubblica con l’uso di validator.descend e determina come dovrebbero funzionare gli errori personalizzati consapevoli del percorso. Il lavoro è completato quando l’approccio supportato o la modifica necessaria all’API o alla documentazione è chiaramente stabilita.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Hello, I'm struggling to enforce some custom validation on properties through extending a Validator and maybe someone can help me out or clarify how extending should work. Note that I want to add validation for a JSONSchema authored by a user - so it's more "meta validation".
Essentially I want to do something like enforcing that if a property is marked as "required" it should not specify a default, and conversely if it specify a default it should not appear in the required array at its level".
I can get something that seems to be working at a high level below:
from jsonschema import Draft202012Validator, validators, exceptions
from jsonschema.protocols import Validator
validate_properties = Draft202012Validator.VALIDATORS["properties"]
def custom_properties_validator(validator, properties, instance, schema):
# Maintain old behaviour
yield from validate_properties(validator, properties, instance, schema)
# Extend behaviour
if not validator.is_type(instance, "object"):
return
current_properties = instance.get("properties", {})
required_properties = instance.get("required", [])
for prop_name, prop_schema in current_properties.items():
is_required = prop_name in required_properties
has_default = "default" in prop_schema
if not is_required and not has_default:
yield exceptions.ValidationError(
f"Primitive property '{prop_name}' is not required and must have a 'default' value."
)
if has_default and is_required:
yield exceptions.ValidationError(
f"Primitive property '{prop_name}' has a 'default' value and must not be in the 'required' list."
)
if __name__ == "__main__":
user_schema = {
"type": "object",
"title": "test",
"properties": {
"test": {"type": "string"},
},
"required": []
}
my_validator: Validator = validators.extend(
Draft202012Validator,
{"properties": custom_properties_validator},
)
errors = list(my_validator(Draft202012Validator.META_SCHEMA).iter_errors(user_schema))
print(f"Found {len(errors)} validation errors:")
for i, error in enumerate(errors, 1):
path_str = " -> ".join(map(str, error.path))
print(f" {i}. JSONPath: {error.json_path}")
print(f" {i}. Path: {path_str}")
print(f" Error: {error.message}")
But there's a big caveat as in I can't really know what's the path of the property I am failing to validate!
I can kinda start to cheat my way into it by doing something more like
if not is_required and not has_default:
for err in validator.descend(
required_properties,
{"contains": {"const": prop_name}},
path="properties",
):
err.message = f"'{prop_name}' is not required and must have a 'default' value."
yield err
in the custom validation steps, but
- I can only descend one level - maybe I can call it twice in this case but it will seem even hackyer
- I am using descend, which is not documented in the validator protocol public API
Is my only way around this implementing the schema-walking part myself so that I can keep track of my own custom errors? Or am I simply making use of the library in an improper way?
- Lingua principale
- Python
- Stelle
- 5k
- Fork
- 694
- Merge medio
- 1g 20h
- PR unite (30g)
- 6
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi 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
-
Iterator error contexts lose parent links and absolute pathsForse già presa @DawnofGenX l’ha presa 5 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
python-jsonschema/jsonschema#1584 · 3 commenti ·
-
regex format checker crashes with RecursionError on deeply nested patternsForse già presa @DawnofGenX l’ha presa 5 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
python-jsonschema/jsonschema#1538 · 1 commento ·
-
is_duration raises an uncaught decimal.Overflow on a duration with a large exponentForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. ApertaNeeds Test Upstream
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
python-jsonschema/jsonschema#1511 · 1 commento ·
-
regex format checker crashes with ValueError on conflicting inline flagsForse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
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 ·
Tutte le issue di python-jsonschema/jsonschema
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
NousResearch/hermes-agent#136483 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
I maintainer di solito rispondono entro 1 giorno
-
[BUG] LazyStackedTensorDictStore zeroes the last byte of a new key set on the last elementForse già presa @peterdsharpe l’ha presa oggi. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
pytorch/tensordict#2307 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
GrokModel.generate/a_generate pass an OpenAI-style list-of-dicts to xai_sdk.chat.user(), so every call crashes with a protobuf TypeError before any network I/OForse già presa @Christian-Sidak l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
confident-ai/deepeval#3436 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno