fix(js-client): Handle null as user input value (#431)

Handle null as user input value
This commit is contained in:
Akim 2024-02-09 17:19:56 +07:00 committed by GitHub
parent 0417ba410a
commit d7070fd71e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 4 deletions

View File

@ -179,6 +179,7 @@ describe("Conversion from aqua to typescript", () => {
test.each`
aqua | ts | type
${1} | ${1} | ${i32}
${null} | ${null} | ${opt_i32}
${[]} | ${null} | ${opt_i32}
${[1]} | ${1} | ${opt_i32}
${[1, 2, 3]} | ${[1, 2, 3]} | ${array_i32}
@ -205,7 +206,11 @@ describe("Conversion from aqua to typescript", () => {
// assert
expect(tsFromAqua).toStrictEqual(ts);
expect(aquaFromTs).toStrictEqual(aqua);
// 'null' -> 'null' -> [] ; 'null' not equal []
if (aqua !== null || ts !== null) {
expect(aquaFromTs).toStrictEqual(aqua);
}
},
);
});

View File

@ -102,11 +102,11 @@ export function aqua2js(
if (schema.tag === "nil") {
return null;
} else if (schema.tag === "option") {
if (!Array.isArray(value)) {
throw new SchemaValidationError(path, schema, "array", value);
if (!Array.isArray(value) && value !== null) {
throw new SchemaValidationError(path, schema, "array or null", value);
}
if ("0" in value) {
if (value !== null && "0" in value) {
return aqua2js(value[0], schema.type, { path: [...path, "?"] });
} else {
return null;