Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/structured.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
5 changes: 5 additions & 0 deletions laya/structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ def _bad(schema):
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": {}}))

Expand Down
2 changes: 1 addition & 1 deletion tests/test_structured_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading