arktypea
arktype11mo ago
papy0977

Scope and narrow

Hello,
need to have nested objects but with only 1 key in each, i tried using narrow:
export const SQLBoolOperatorSchema = type({
    _eq: "string|number|boolean",
})
    .or({ _neq: "string|number|boolean" })
    .or({ _gt: "string|number" });

const $boolField = scope({
    ops: SQLBoolOperatorSchema,
    record: {
        "[string]": "value",
    },
    value: "ops | record",
    field: () =>
        $boolField
            .type("record")
            .narrow((x, ctx) => {
                console.log(x);
                return (
                    Object.keys(x).length === 1 ||
                    ctx.mustBe("accepting only one field for column comparison")
                );
            })
});

export const { field: SQLBoolFieldSchema } = $boolField.export();

// should error as access have multiple field
const foo = SQLBoolFieldSchema({
    access: { id: { _eq: "$id" }, foo: { _eq: "12" } },
} as const);
if (foo instanceof ArkErrors) {
    throw new Error(foo.summary);
}

it doesnt work after 1st level, as i think i also need to narrow the "value" field.
Was this page helpful?