mirror of
https://github.com/deunix-educ/aventuriers-des-mots.git
synced 2026-10-13 02:11:58 +02:00
Add JSON schemas, data validator, and first CE1 content
Introduces a generic JSON-Schema-subset validator in GDScript (scripts/data/schema_validator.gd) used both by `make validate-data` and, later, by ExerciseEngine at load time, plus semantic_checks.gd for cross-field constraints a schema can't express (answer index bounds, at least one faulty token in sentence correction, word count matching between a dictation's target_text and its annotations). Schemas cover rule, exercise (dispatched per type: multiple_choice, sentence_correction), dictation, curriculum, and save (save.schema.json is not yet wired into validate-data; saves live in user://, handled at the Profiles/Saves step). First draft content for the CE1 "accord_adjectif" rule (one rule, two multiple-choice exercises, one sentence correction, one dictation, one curriculum entry) exercises the full pipeline end to end and seeds docs/review/content-review.md, regenerated by the new tools/generate_content_review.py (`make content-review`). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
class_name DataLoader
|
||||
extends RefCounted
|
||||
## Charge et valide les fichiers JSON de data/ contre leurs schemas (data/schemas/).
|
||||
## Meme code utilise par `make validate-data` (headless) et par ExerciseEngine a
|
||||
## l'execution : une seule source de verite pour la validation (voir
|
||||
## docs/specs/decisions.md, point 2).
|
||||
|
||||
const SCHEMA_DIR := "res://data/schemas"
|
||||
|
||||
static var _schema_cache: Dictionary = {}
|
||||
|
||||
|
||||
## Mappe un type d'exercice (champ "type" des donnees) vers le nom de son schema.
|
||||
## Chaine vide si le type n'est pas reconnu : la liste des types est fermee ici,
|
||||
## pas devinee ailleurs.
|
||||
static func exercise_schema_name_for_type(exercise_type: String) -> String:
|
||||
match exercise_type:
|
||||
"multiple_choice":
|
||||
return "exercise_multiple_choice"
|
||||
"sentence_correction":
|
||||
return "exercise_sentence_correction"
|
||||
_:
|
||||
return ""
|
||||
|
||||
|
||||
static func load_schema(schema_name: String) -> Dictionary:
|
||||
if _schema_cache.has(schema_name):
|
||||
return _schema_cache[schema_name]
|
||||
var path := "%s/%s.schema.json" % [SCHEMA_DIR, schema_name]
|
||||
var file := FileAccess.open(path, FileAccess.READ)
|
||||
if file == null:
|
||||
push_error("Schema introuvable : %s" % path)
|
||||
return {}
|
||||
var parsed: Variant = JSON.parse_string(file.get_as_text())
|
||||
var schema: Dictionary = parsed if typeof(parsed) == TYPE_DICTIONARY else {}
|
||||
_schema_cache[schema_name] = schema
|
||||
return schema
|
||||
|
||||
|
||||
## Lit un fichier JSON et le valide contre le schema nomme. Retourne la liste des
|
||||
## erreurs (vide si valide) ; ne retourne jamais les donnees analysees, pour que
|
||||
## l'appelant ne puisse pas accidentellement utiliser une donnee non validee.
|
||||
static func validate_file(path: String, schema_name: String) -> Array[String]:
|
||||
var parsed: Variant = read_json(path)
|
||||
if parsed == null:
|
||||
return ["%s : lecture impossible ou JSON invalide" % path]
|
||||
var schema := load_schema(schema_name)
|
||||
return SchemaValidator.validate(parsed, schema, path)
|
||||
|
||||
|
||||
static func read_json(path: String) -> Variant:
|
||||
var file := FileAccess.open(path, FileAccess.READ)
|
||||
if file == null:
|
||||
return null
|
||||
var text := file.get_as_text()
|
||||
if text.strip_edges().is_empty():
|
||||
return null
|
||||
return JSON.parse_string(text)
|
||||
|
||||
|
||||
static func find_json_files(root_path: String) -> Array[String]:
|
||||
var result: Array[String] = []
|
||||
var dir := DirAccess.open(root_path)
|
||||
if dir == null:
|
||||
return result
|
||||
dir.list_dir_begin()
|
||||
var entry := dir.get_next()
|
||||
while entry != "":
|
||||
if entry != "." and entry != "..":
|
||||
var full_path := root_path.path_join(entry)
|
||||
if dir.current_is_dir():
|
||||
result.append_array(find_json_files(full_path))
|
||||
elif entry.ends_with(".json"):
|
||||
result.append(full_path)
|
||||
entry = dir.get_next()
|
||||
return result
|
||||
@@ -0,0 +1 @@
|
||||
uid://l56rn1o7hqm0
|
||||
@@ -0,0 +1,122 @@
|
||||
class_name SchemaValidator
|
||||
extends RefCounted
|
||||
## Validateur de donnees contre un sous-ensemble de JSON Schema : type, required,
|
||||
## properties, additionalProperties, items, enum, const, minimum, maximum,
|
||||
## minItems, maxItems, minLength, maxLength, pattern.
|
||||
## Les schemas ne decrivent jamais de code a executer : seulement une structure
|
||||
## de donnees a verifier (voir l'invariant d'architecture dans CLAUDE.md).
|
||||
|
||||
|
||||
static func validate(value: Variant, schema: Dictionary, path: String = "$") -> Array[String]:
|
||||
var errors: Array[String] = []
|
||||
_validate_node(value, schema, path, errors)
|
||||
return errors
|
||||
|
||||
|
||||
static func _validate_node(
|
||||
value: Variant, schema: Dictionary, path: String, errors: Array[String]
|
||||
) -> void:
|
||||
if schema.has("const"):
|
||||
if value != schema["const"]:
|
||||
errors.append("%s : doit valoir exactement %s" % [path, schema["const"]])
|
||||
return
|
||||
|
||||
if schema.has("enum"):
|
||||
var allowed: Array = schema["enum"]
|
||||
if not allowed.has(value):
|
||||
errors.append("%s : valeur '%s' absente de l'enum %s" % [path, value, allowed])
|
||||
return
|
||||
|
||||
if schema.has("type"):
|
||||
if not _check_type(value, schema["type"]):
|
||||
errors.append(
|
||||
"%s : type attendu '%s', trouve '%s'" % [path, schema["type"], typeof(value)]
|
||||
)
|
||||
return
|
||||
|
||||
match typeof(value):
|
||||
TYPE_DICTIONARY:
|
||||
_validate_object(value, schema, path, errors)
|
||||
TYPE_ARRAY:
|
||||
_validate_array(value, schema, path, errors)
|
||||
TYPE_STRING:
|
||||
_validate_string(value, schema, path, errors)
|
||||
TYPE_INT, TYPE_FLOAT:
|
||||
_validate_number(value, schema, path, errors)
|
||||
|
||||
|
||||
static func _check_type(value: Variant, expected: String) -> bool:
|
||||
match expected:
|
||||
"object":
|
||||
return typeof(value) == TYPE_DICTIONARY
|
||||
"array":
|
||||
return typeof(value) == TYPE_ARRAY
|
||||
"string":
|
||||
return typeof(value) == TYPE_STRING
|
||||
"boolean":
|
||||
return typeof(value) == TYPE_BOOL
|
||||
"integer":
|
||||
if typeof(value) == TYPE_INT:
|
||||
return true
|
||||
if typeof(value) == TYPE_FLOAT:
|
||||
var number: float = value
|
||||
return number == floor(number)
|
||||
return false
|
||||
"number":
|
||||
return typeof(value) == TYPE_INT or typeof(value) == TYPE_FLOAT
|
||||
_:
|
||||
return true
|
||||
|
||||
|
||||
static func _validate_object(
|
||||
value: Dictionary, schema: Dictionary, path: String, errors: Array[String]
|
||||
) -> void:
|
||||
if schema.has("required"):
|
||||
for key: String in schema["required"]:
|
||||
if not value.has(key):
|
||||
errors.append("%s : propriete requise manquante '%s'" % [path, key])
|
||||
|
||||
var properties: Dictionary = schema.get("properties", {})
|
||||
var additional_allowed: Variant = schema.get("additionalProperties", true)
|
||||
for key: String in value.keys():
|
||||
if properties.has(key):
|
||||
_validate_node(value[key], properties[key], "%s.%s" % [path, key], errors)
|
||||
elif additional_allowed == false:
|
||||
errors.append("%s : propriete inattendue '%s'" % [path, key])
|
||||
|
||||
|
||||
static func _validate_array(
|
||||
value: Array, schema: Dictionary, path: String, errors: Array[String]
|
||||
) -> void:
|
||||
if schema.has("minItems") and value.size() < int(schema["minItems"]):
|
||||
errors.append("%s : au moins %d element(s) attendu(s)" % [path, schema["minItems"]])
|
||||
if schema.has("maxItems") and value.size() > int(schema["maxItems"]):
|
||||
errors.append("%s : au plus %d element(s) attendu(s)" % [path, schema["maxItems"]])
|
||||
if schema.has("items"):
|
||||
var item_schema: Dictionary = schema["items"]
|
||||
for i in value.size():
|
||||
_validate_node(value[i], item_schema, "%s[%d]" % [path, i], errors)
|
||||
|
||||
|
||||
static func _validate_string(
|
||||
value: String, schema: Dictionary, path: String, errors: Array[String]
|
||||
) -> void:
|
||||
if schema.has("minLength") and value.length() < int(schema["minLength"]):
|
||||
errors.append("%s : longueur minimale %d non atteinte" % [path, schema["minLength"]])
|
||||
if schema.has("maxLength") and value.length() > int(schema["maxLength"]):
|
||||
errors.append("%s : longueur maximale %d depassee" % [path, schema["maxLength"]])
|
||||
if schema.has("pattern"):
|
||||
var regex := RegEx.new()
|
||||
regex.compile(schema["pattern"])
|
||||
if regex.search(value) == null:
|
||||
errors.append("%s : ne correspond pas au motif '%s'" % [path, schema["pattern"]])
|
||||
|
||||
|
||||
static func _validate_number(
|
||||
value: Variant, schema: Dictionary, path: String, errors: Array[String]
|
||||
) -> void:
|
||||
var number: float = value
|
||||
if schema.has("minimum") and number < float(schema["minimum"]):
|
||||
errors.append("%s : doit etre >= %s" % [path, schema["minimum"]])
|
||||
if schema.has("maximum") and number > float(schema["maximum"]):
|
||||
errors.append("%s : doit etre <= %s" % [path, schema["maximum"]])
|
||||
@@ -0,0 +1 @@
|
||||
uid://dl6pewdq2dyus
|
||||
@@ -0,0 +1,49 @@
|
||||
class_name SemanticChecks
|
||||
extends RefCounted
|
||||
## Contraintes croisees entre champs qu'un schema JSON (voir SchemaValidator) ne
|
||||
## peut pas exprimer : index valide, coherence entre deux tableaux, etc.
|
||||
## Appele uniquement apres une validation de schema reussie : ces fonctions
|
||||
## supposent une structure deja conforme.
|
||||
|
||||
|
||||
static func check_multiple_choice(data: Dictionary, path: String) -> Array[String]:
|
||||
var errors: Array[String] = []
|
||||
var answers: Array = data.get("answers", [])
|
||||
var correct_answer: int = data.get("correct_answer", -1)
|
||||
if correct_answer < 0 or correct_answer >= answers.size():
|
||||
errors.append(
|
||||
"%s : correct_answer (%d) hors des bornes de answers (%d element(s))"
|
||||
% [path, correct_answer, answers.size()]
|
||||
)
|
||||
return errors
|
||||
|
||||
|
||||
static func check_sentence_correction(data: Dictionary, path: String) -> Array[String]:
|
||||
var errors: Array[String] = []
|
||||
var tokens: Array = data.get("tokens", [])
|
||||
var has_error_token := false
|
||||
for i in tokens.size():
|
||||
var token: Dictionary = tokens[i]
|
||||
if token.get("correct", true) == false:
|
||||
has_error_token = true
|
||||
if not token.has("expected"):
|
||||
errors.append(
|
||||
"%s.tokens[%d] : token fautif sans correction attendue ('expected')"
|
||||
% [path, i]
|
||||
)
|
||||
if not has_error_token:
|
||||
errors.append("%s : aucun token fautif, rien a corriger" % path)
|
||||
return errors
|
||||
|
||||
|
||||
static func check_dictation(data: Dictionary, path: String) -> Array[String]:
|
||||
var errors: Array[String] = []
|
||||
var target_text: String = data.get("target_text", "")
|
||||
var words: Array = data.get("words", [])
|
||||
var expected_word_count := target_text.split(" ", false).size()
|
||||
if words.size() != expected_word_count:
|
||||
errors.append(
|
||||
"%s : %d mot(s) annote(s) pour %d mot(s) dans target_text"
|
||||
% [path, words.size(), expected_word_count]
|
||||
)
|
||||
return errors
|
||||
@@ -0,0 +1 @@
|
||||
uid://c2ekxtn3qh5ku
|
||||
@@ -1,46 +1,68 @@
|
||||
extends SceneTree
|
||||
## Outil headless invoque par `make validate-data`.
|
||||
## A ce stade, verifie seulement que les fichiers data/**/*.json sont du JSON bien forme.
|
||||
## La validation contre les schemas (rule, exercise, dictation, curriculum, save) sera
|
||||
## ajoutee a l'etape "Donnees" (voir Ordre de construction, docs/specs/decisions.md point 2).
|
||||
## Valide chaque fichier JSON de data/ contre son schema (data/schemas/), puis
|
||||
## applique les contraintes croisees que le schema ne peut pas exprimer.
|
||||
|
||||
|
||||
func _init() -> void:
|
||||
var json_paths: Array[String] = _find_json_files("res://data")
|
||||
var failures: Array[String] = []
|
||||
for path in json_paths:
|
||||
var file := FileAccess.open(path, FileAccess.READ)
|
||||
if file == null:
|
||||
failures.append("%s : lecture impossible" % path)
|
||||
continue
|
||||
var text := file.get_as_text()
|
||||
if text.strip_edges().is_empty():
|
||||
continue
|
||||
if JSON.parse_string(text) == null:
|
||||
failures.append("%s : JSON invalide" % path)
|
||||
var checked := 0
|
||||
|
||||
checked += _validate_simple_dir("res://data/rules", "rule", failures)
|
||||
checked += _validate_simple_dir("res://data/curriculum", "curriculum", failures)
|
||||
checked += _validate_dictations("res://data/dictations", failures)
|
||||
checked += _validate_exercises("res://data/exercises", failures)
|
||||
|
||||
if failures.is_empty():
|
||||
print("validate-data : %d fichier(s) JSON valide(s)" % json_paths.size())
|
||||
print("validate-data : %d fichier(s) JSON valide(s)" % checked)
|
||||
quit(0)
|
||||
else:
|
||||
for failure in failures:
|
||||
printerr(failure)
|
||||
printerr("validate-data : %d erreur(s)" % failures.size())
|
||||
quit(1)
|
||||
|
||||
|
||||
func _find_json_files(root_path: String) -> Array[String]:
|
||||
var result: Array[String] = []
|
||||
var dir := DirAccess.open(root_path)
|
||||
if dir == null:
|
||||
return result
|
||||
dir.list_dir_begin()
|
||||
var entry := dir.get_next()
|
||||
while entry != "":
|
||||
if entry != "." and entry != "..":
|
||||
var full_path := root_path.path_join(entry)
|
||||
if dir.current_is_dir():
|
||||
result.append_array(_find_json_files(full_path))
|
||||
elif entry.ends_with(".json"):
|
||||
result.append(full_path)
|
||||
entry = dir.get_next()
|
||||
return result
|
||||
func _validate_simple_dir(dir_path: String, schema_name: String, failures: Array[String]) -> int:
|
||||
var count := 0
|
||||
for path in DataLoader.find_json_files(dir_path):
|
||||
count += 1
|
||||
failures.append_array(DataLoader.validate_file(path, schema_name))
|
||||
return count
|
||||
|
||||
|
||||
func _validate_dictations(dir_path: String, failures: Array[String]) -> int:
|
||||
var count := 0
|
||||
for path in DataLoader.find_json_files(dir_path):
|
||||
count += 1
|
||||
var schema_errors := DataLoader.validate_file(path, "dictation")
|
||||
failures.append_array(schema_errors)
|
||||
if schema_errors.is_empty():
|
||||
var data: Dictionary = DataLoader.read_json(path)
|
||||
failures.append_array(SemanticChecks.check_dictation(data, path))
|
||||
return count
|
||||
|
||||
|
||||
func _validate_exercises(dir_path: String, failures: Array[String]) -> int:
|
||||
var count := 0
|
||||
for path in DataLoader.find_json_files(dir_path):
|
||||
count += 1
|
||||
var data: Variant = DataLoader.read_json(path)
|
||||
if data == null or typeof(data) != TYPE_DICTIONARY or not data.has("type"):
|
||||
failures.append("%s : champ 'type' manquant ou JSON invalide" % path)
|
||||
continue
|
||||
var exercise_type: String = data["type"]
|
||||
var schema_name := DataLoader.exercise_schema_name_for_type(exercise_type)
|
||||
if schema_name.is_empty():
|
||||
failures.append("%s : type d'exercice inconnu '%s'" % [path, exercise_type])
|
||||
continue
|
||||
var schema_errors := DataLoader.validate_file(path, schema_name)
|
||||
failures.append_array(schema_errors)
|
||||
if not schema_errors.is_empty():
|
||||
continue
|
||||
match exercise_type:
|
||||
"multiple_choice":
|
||||
failures.append_array(SemanticChecks.check_multiple_choice(data, path))
|
||||
"sentence_correction":
|
||||
failures.append_array(SemanticChecks.check_sentence_correction(data, path))
|
||||
return count
|
||||
|
||||
Reference in New Issue
Block a user