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:
Denis Defolie
2026-10-11 16:47:28 +02:00
parent a1f749de40
commit d057c8ee77
31 changed files with 938 additions and 35 deletions
+122
View File
@@ -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"]])