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
30 changes: 30 additions & 0 deletions src/parsing/v2/field/simpleField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
Expand Down
19 changes: 19 additions & 0 deletions tests/parsing/v2/inference.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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 () => {
Expand Down
Loading