@struktur/sdk 0.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/fields.test.ts +18 -0
- package/src/fields.ts +8 -1
package/package.json
CHANGED
package/src/fields.test.ts
CHANGED
|
@@ -255,6 +255,12 @@ describe("parseFieldsString — arrays", () => {
|
|
|
255
255
|
]);
|
|
256
256
|
});
|
|
257
257
|
|
|
258
|
+
test("array shorthand defaults to string", () => {
|
|
259
|
+
expect(parseFieldsString("tags:array")).toEqual([
|
|
260
|
+
{ name: "tags", kind: "array", items: "string" },
|
|
261
|
+
]);
|
|
262
|
+
});
|
|
263
|
+
|
|
258
264
|
test("array of number", () => {
|
|
259
265
|
expect(parseFieldsString("scores:array{number}")).toEqual([
|
|
260
266
|
{ name: "scores", kind: "array", items: "number" },
|
|
@@ -523,6 +529,18 @@ describe("buildSchemaFromFields", () => {
|
|
|
523
529
|
});
|
|
524
530
|
});
|
|
525
531
|
|
|
532
|
+
test("array shorthand (user example: name,authors:array)", () => {
|
|
533
|
+
expect(buildSchemaFromFields("name,authors:array")).toEqual({
|
|
534
|
+
type: "object",
|
|
535
|
+
properties: {
|
|
536
|
+
name: { type: "string" },
|
|
537
|
+
authors: { type: "array", items: { type: "string" } },
|
|
538
|
+
},
|
|
539
|
+
required: ["name", "authors"],
|
|
540
|
+
additionalProperties: false,
|
|
541
|
+
});
|
|
542
|
+
});
|
|
543
|
+
|
|
526
544
|
test("all four types in one string", () => {
|
|
527
545
|
expect(buildSchemaFromFields(
|
|
528
546
|
"title, price:number, status:enum{a|b}, tags:array{string}",
|
package/src/fields.ts
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
* int count:int (integer + multipleOf:1 to disallow fractions)
|
|
12
12
|
* enum status:enum{draft|published|archived}
|
|
13
13
|
* array of scalar tags:array{string}
|
|
14
|
+
* array (shorthand) tags:array (defaults to array{string})
|
|
14
15
|
*
|
|
15
16
|
* Aliases:
|
|
16
17
|
* bool → boolean
|
|
@@ -23,6 +24,7 @@
|
|
|
23
24
|
* parseFieldsString("title , price: number , active:boolean")
|
|
24
25
|
* parseFieldsString("name, status:enum{draft|published}")
|
|
25
26
|
* parseFieldsString("name, tags:array{string}")
|
|
27
|
+
* parseFieldsString("name, tags:array")
|
|
26
28
|
* parseFieldsString("count:int, ratio:float, enabled:bool")
|
|
27
29
|
*/
|
|
28
30
|
|
|
@@ -88,7 +90,7 @@ const parseScalarType = (raw: string, fieldName: string): ScalarFieldType => {
|
|
|
88
90
|
throw new Error(
|
|
89
91
|
`Unknown type "${raw}" for field "${fieldName}". ` +
|
|
90
92
|
`Scalar types: ${allNames.join(", ")}. ` +
|
|
91
|
-
`Complex types: enum{a|b|c}, array{string}.`,
|
|
93
|
+
`Complex types: enum{a|b|c}, array{string}, or array (shorthand for array{string}).`,
|
|
92
94
|
);
|
|
93
95
|
}
|
|
94
96
|
return resolved as ScalarFieldType;
|
|
@@ -144,6 +146,11 @@ const parseField = (token: string): ParsedField => {
|
|
|
144
146
|
return { name, kind: "array", items: parseScalarType(itemType, name) };
|
|
145
147
|
}
|
|
146
148
|
|
|
149
|
+
// array (shorthand for array{string})
|
|
150
|
+
if (rawType === "array") {
|
|
151
|
+
return { name, kind: "array", items: "string" };
|
|
152
|
+
}
|
|
153
|
+
|
|
147
154
|
// plain scalar
|
|
148
155
|
return { name, kind: "scalar", type: parseScalarType(rawType, name) };
|
|
149
156
|
};
|