From 42cdafffff9b267184a624516da6b9fd44311af0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ianar=C3=A9=20S=C3=A9vi?= Date: Fri, 29 Aug 2025 17:32:40 +0200 Subject: [PATCH] :sparkles: add typed value accessors --- src/parsing/v2/field/simpleField.ts | 30 +++++++++++++++++++++++++++++ tests/parsing/v2/inference.spec.ts | 19 ++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/parsing/v2/field/simpleField.ts b/src/parsing/v2/field/simpleField.ts index e88da47c6..78a1ae4e0 100644 --- a/src/parsing/v2/field/simpleField.ts +++ b/src/parsing/v2/field/simpleField.ts @@ -10,6 +10,36 @@ export class SimpleField extends BaseField { serverResponse["value"] !== undefined ? (serverResponse["value"] as any) : null; } + /** + * Retrieves a string field value as a string. + */ + public get stringValue(): string | null { + if (this.value !== null && typeof this.value !== "string") { + throw new Error("Value is not a string"); + } + return this.value as string; + } + + /** + * Retrieves a number field value as a number. + */ + public get numberValue(): number | null { + if (this.value !== null && typeof this.value !== "number") { + throw new Error("Value is not a number"); + } + return this.value as number; + } + + /** + * Retrieves a boolean field value as a boolean. + */ + public get booleanValue(): boolean | null { + if (this.value !== null && typeof this.value !== "boolean") { + throw new Error("Value is not a boolean"); + } + return this.value as boolean; + } + toString(): string { if (this.value === null) { return ""; diff --git a/tests/parsing/v2/inference.spec.ts b/tests/parsing/v2/inference.spec.ts index 91953c865..99094d75f 100644 --- a/tests/parsing/v2/inference.spec.ts +++ b/tests/parsing/v2/inference.spec.ts @@ -87,6 +87,7 @@ describe("inference", async () => { const taxesList = fields.getListField("taxes"); expect(taxesList.items).to.have.lengthOf(1); + expect(taxesList.objectItems).to.have.lengthOf(1); expect(taxes?.toString()).to.be.a("string").and.not.be.empty; const firstTaxItem = taxesList.items[0]; @@ -160,21 +161,39 @@ describe("inference", async () => { expect(fields.get("field_simple_string")).to.be.instanceOf(SimpleField); const simpleFieldStr = fields.getSimpleField("field_simple_string"); expect(simpleFieldStr.value).to.be.eq("field_simple_string-value"); + expect(simpleFieldStr.stringValue).to.be.eq("field_simple_string-value"); + expect(() => simpleFieldStr.numberValue).to.throw("Value is not a number"); + expect(() => simpleFieldStr.booleanValue).to.throw("Value is not a boolean"); + expect(fields.get("field_simple_float")).to.be.instanceOf(SimpleField); const simpleFieldFloat = fields.getSimpleField("field_simple_float"); expect(simpleFieldFloat.value).to.be.eq(1.1); + expect(simpleFieldFloat.numberValue).to.be.eq(1.1); + expect(() => simpleFieldFloat.stringValue).to.throw("Value is not a string"); + expect(() => simpleFieldFloat.booleanValue).to.throw("Value is not a boolean"); + expect(fields.get("field_simple_int")).to.be.instanceOf(SimpleField); const simpleFieldInt = fields.getSimpleField("field_simple_int"); expect(simpleFieldInt.value).to.be.eq(12.0); + expect(fields.get("field_simple_zero")).to.be.instanceOf(SimpleField); const simpleFieldZero = fields.getSimpleField("field_simple_zero"); expect(simpleFieldZero.value).to.be.eq(0); + expect(simpleFieldZero.numberValue).to.be.eq(0); + expect(fields.get("field_simple_bool")).to.be.instanceOf(SimpleField); const simpleFieldBool = fields.getSimpleField("field_simple_bool"); expect(simpleFieldBool.value).to.be.eq(true); + expect(simpleFieldBool.booleanValue).to.be.eq(true); + expect(() => simpleFieldBool.stringValue).to.throw("Value is not a string"); + expect(() => simpleFieldBool.numberValue).to.throw("Value is not a number"); + expect(fields.get("field_simple_null")).to.be.instanceOf(SimpleField); const simpleFieldNull = fields.getSimpleField("field_simple_null"); expect(simpleFieldNull.value).to.be.eq(null); + expect(simpleFieldNull.stringValue).to.be.eq(null); + expect(simpleFieldNull.numberValue).to.be.eq(null); + expect(simpleFieldNull.booleanValue).to.be.eq(null); }); it("should recognize simple list fields", async () => {