Page MenuHomeSealhub

checkboxed-list.ts
No OneTemporary

checkboxed-list.ts

import { is, predicates } from "@sealcode/ts-predicates";
import { Context } from "koa";
import { FormDataValue } from "../form-types.js";
import {
FieldParseResult,
FormField,
FormFieldValidationResponse,
} from "./field.js";
export type CheckboxedListItem = {
value: string;
label?: string;
group?: string;
};
export class CheckboxedListField<Required extends boolean> extends FormField<
Required,
Record<string, "on">
> {
constructor(
public required: Required,
public generateOptions: (ctx: Context) => Promise<CheckboxedListItem[]>,
public isVisible: (ctx: Context) => Promise<boolean> = () =>
Promise.resolve(true)
) {
super(required);
}
predicate = predicates.array(predicates.string); // TODO: this is probably wrong
public async isValueValid(
_: Context,
value: unknown
): Promise<FormFieldValidationResponse> {
if (is(value, predicates.string)) {
return { valid: false, message: "you need an array" };
}
if (is(value, predicates.null)) {
return { valid: false, message: "you need an array" };
}
return { valid: true, message: "" };
}
async parse(
_: Context,
raw_value: FormDataValue
): Promise<FieldParseResult<Record<string, "on">>> {
if (is(raw_value, predicates.object)) {
return {
parsable: true,
parsed_value: Object.fromEntries(
Object.entries(raw_value).filter(
([, value]) => value === "on"
)
) as Record<string, "on">,
error: null,
};
} else {
return {
parsed_value: null,
parsable: false,
error: "Expected an object",
};
}
}
public getEmptyValue(): Record<string, "on"> {
return {};
}
}

File Metadata

Mime Type
text/x-java
Expires
Wed, May 7, 19:48 (1 d, 1 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
628504
Default Alt Text
checkboxed-list.ts (1 KB)

Event Timeline