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 unsupported multi-type schema fields

MadrasLe69c2824a5e29
diff --git a/laya-ts/src/structured.ts b/laya-ts/src/structured.ts
index 7d1f4214..f1b6abcb 100644
--- a/laya-ts/src/structured.ts
+++ b/laya-ts/src/structured.ts
@@ -166,7 +166,13 @@ function fieldFor(path: string, name: string, prop: unknown): PlannedField {
return enumField(path, name, p.enum, description);
}
let jtype = p.type;
- if (Array.isArray(jtype)) jtype = jtype.find((t) => t !== "null"); // nullable: ["string", "null"]
+ if (Array.isArray(jtype)) {
+ const nonNullTypes = jtype.filter((t) => t !== "null"); // nullable: ["string", "null"]
+ if (nonNullTypes.length > 1) {
+ throw new SchemaError(`${path}: 'type' has multiple non-null types; unions are not supported`);
+ }
+ jtype = nonNullTypes[0];
+ }
if (jtype === "boolean") return noulField(name, description);
if (jtype === "string") {
throw new SchemaError(`${path}: a free string cannot be a fixed option set; use 'enum' or a boolean`);
diff --git a/laya-ts/tests/structured.test.ts b/laya-ts/tests/structured.test.ts
index 97485b1e..d093c254 100644
--- a/laya-ts/tests/structured.test.ts
+++ b/laya-ts/tests/structured.test.ts
@@ -68,6 +68,10 @@ describe("structured/mapping", () => {
const q = questionsFromJsonSchema({ type: "object", properties: { x: { enum: [true, false] } } });
expect(q.x.type).toBe("noul");
});
+ it("nullable boolean remains noul", () => {
+ const q = questionsFromJsonSchema({ type: "object", properties: { x: { type: ["null", "boolean"] } } });
+ expect(q.x.type).toBe("noul");
+ });
it("plan has one field per property", () => {
expect(planFromJsonSchema(SCHEMA)).toHaveLength(4);
});
@@ -105,6 +109,12 @@ describe("structured/rejections", () => {
expect(bad({ type: "object", properties: { a: { type: "string" } } }))
.toThrowError(/properties\.a: a free string cannot be a fixed option set/);
});
+ it("rejects multiple non-null types even in a nullable union", () => {
+ for (const types of [["boolean", "integer"], ["null", "integer", "boolean"]]) {
+ expect(bad({ type: "object", properties: { a: { type: types, minimum: 0, maximum: 2 } } }))
+ .toThrowError(/properties\.a: .*multiple non-null types/);
+ }
+ });
it("rejects arrays, nested objects and $ref", () => {
expect(bad({ type: "object", properties: { a: { type: "array", items: { type: "string" } } } }))
.toThrowError(SchemaError);
diff --git a/laya/structured.py b/laya/structured.py
index 5bb226cf..8e07bc0d 100644
--- a/laya/structured.py
+++ b/laya/structured.py
@@ -129,7 +129,10 @@ def _field(path: str, name: str, prop: Dict[str, Any]) -> _Field:
jtype = prop.get("type")
if isinstance(jtype, list): # nullable: ["string", "null"]
- jtype = next((t for t in jtype if t != "null"), None)
+ non_null_types = [t for t in jtype if t != "null"]
+ if len(non_null_types) > 1:
+ raise SchemaError("%s: 'type' has multiple non-null types; unions are not supported" % path)
+ jtype = non_null_types[0] if non_null_types else None
if jtype == "boolean":
return _noul_field(path, name, description)
if jtype == "string":
diff --git a/tests/test_structured.py b/tests/test_structured.py
index 5ab3ac8e..620537bb 100644
--- a/tests/test_structured.py
+++ b/tests/test_structured.py
@@ -70,6 +70,9 @@ check("map/boolean is noul", questions["needs_human"]["type"], "noul")
check("map/integer enum becomes a choice", questions["priority"]["type"], "choice")
check("map/integer enum labels are strings", list(questions["priority"]["criteria"]), ["1", "2", "3"])
check("map/plan has one field per property", len(plan_from_json_schema(SCHEMA)), 4)
+check("map/nullable boolean remains noul",
+ questions_from_json_schema({"type": "object", "properties": {"a": {"type": ["null", "boolean"]}}})["a"]["type"],
+ "noul")
# --------------------------------------------------------------- projection
@@ -101,6 +104,12 @@ def _bad(schema):
check_raises("reject/free string", SchemaError,
_bad({"type": "object", "properties": {"a": {"type": "string"}}}))
+check_raises("reject/multiple non-null types", SchemaError,
+ _bad({"type": "object", "properties": {"a": {"type": ["boolean", "integer"],
+ "minimum": 0, "maximum": 2}}}))
+check_raises("reject/nullable union of multiple types", SchemaError,
+ _bad({"type": "object", "properties": {"a": {"type": ["null", "integer", "boolean"],
+ "minimum": 0, "maximum": 2}}}))
check_raises("reject/array", SchemaError,
_bad({"type": "object", "properties": {"a": {"type": "array", "items": {"type": "string"}}}}))
check_raises("reject/nested object", SchemaError,