Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(structured): reject a non-array 'enum' with a path-naming SchemaError

Aashish64077de9ca4f
diff --git a/docs/structured.md b/docs/structured.md
index 61c10ac5..25ac3a8d 100644
--- a/docs/structured.md
+++ b/docs/structured.md
@@ -85,6 +85,7 @@ A schema that cannot be answered from a fixed option set raises `laya.structured
| `{"$ref": "#/definitions/node"}` | `properties.name: $ref '#/definitions/node' does not resolve to an entry of this schema's '$defs' or 'definitions'` |
| `{"enum": [1, "1"]}` | `properties.name: enum values produce duplicate choice labels` |
| `{"enum": []}` | `properties.name: 'enum' must not be empty` |
+| `{"enum": "abc"}` | `properties.name: 'enum' must be an array of values, got str` |
| `{"type": "number"}` | `properties.name: a numeric field needs integer 'minimum' and 'maximum' to become a score` |
| `{"type": "integer", "minimum": 5, "maximum": 2}` | `properties.name: 'maximum' 2 is below 'minimum' 5` |
| `{"type": "integer", "minimum": 0, "maximum": 10}` | `properties.name: 11 levels exceeds MAX_SCORE_LEVELS=10; narrow the range or use an enum` |
diff --git a/laya/structured.py b/laya/structured.py
index 780e91f6..7a691886 100644
--- a/laya/structured.py
+++ b/laya/structured.py
@@ -99,6 +99,11 @@ def _schema_of(model: Any) -> Dict[str, Any]:
def _enum_field(path: str, name: str, values: Sequence[Any], description: Optional[str]) -> _Field:
+ # A JSON 'enum' is an array. Reject a bare scalar before len(): a str is iterable, so without
+ # this check it silently splits into per-character options instead of erroring (the module
+ # promises malformed schemas raise a SchemaError that names the path).
+ if not isinstance(values, (list, tuple)):
+ raise SchemaError("%s: 'enum' must be an array of values, got %s" % (path, type(values).__name__))
if len(values) > MAX_OPTIONS:
raise SchemaError("%s: %d options exceeds MAX_OPTIONS=%d" % (path, len(values), MAX_OPTIONS))
if not values:
diff --git a/tests/test_structured.py b/tests/test_structured.py
index 7b05453f..4fcc0e2e 100644
--- a/tests/test_structured.py
+++ b/tests/test_structured.py
@@ -143,6 +143,16 @@ try:
except SchemaError as exc:
check("reject/colliding enum names the field", str(exc),
"properties.x: enum values produce duplicate choice labels")
+# A JSON 'enum' is an array. A bare scalar used to leak TypeError from len(), and a bare string
+# silently split into per-character options; both must now raise a SchemaError naming the path.
+for scalar in (5, 1.5, True, None, "abc"):
+ check_raises("reject/non-array enum %r" % (scalar,), SchemaError,
+ _bad({"type": "object", "properties": {"x": {"enum": scalar}}}))
+try:
+ questions_from_json_schema({"type": "object", "properties": {"x": {"enum": "abc"}}})
+except SchemaError as exc:
+ check("reject/non-array enum names the field and type", str(exc),
+ "properties.x: 'enum' must be an array of values, got str")
check_raises("reject/non-object root", SchemaError, _bad({"type": "array"}))
check_raises("reject/empty properties", SchemaError, _bad({"type": "object", "properties": {}}))
diff --git a/tests/test_structured_docs.py b/tests/test_structured_docs.py
index 86a2ca77..f9f53943 100644
--- a/tests/test_structured_docs.py
+++ b/tests/test_structured_docs.py
@@ -291,7 +291,7 @@ def main() -> int:
check_true("subset table/parsed rows", len(subset) == 11, "got %d" % len(subset))
check_true("subset table/every row has three cells",
all(len(r) == 3 for r in subset), [len(r) for r in subset])
- check_true("rejections table/parsed rows", len(props) == 13, "got %d" % len(props))
+ check_true("rejections table/parsed rows", len(props) == 14, "got %d" % len(props))
check_true("rejections table/every row has two cells",
all(len(r) == 2 for r in props), [len(r) for r in props])
check_true("entry point table/parsed rows", len(calls) == 7, "got %d" % len(calls))