forked from baron/baron-sso
75 lines
1.8 KiB
TypeScript
75 lines
1.8 KiB
TypeScript
export type SchemaFieldType =
|
|
| "text"
|
|
| "number"
|
|
| "boolean"
|
|
| "date"
|
|
| "float"
|
|
| "datetime";
|
|
|
|
export type SchemaField = {
|
|
id: string;
|
|
key: string;
|
|
label: string;
|
|
type: SchemaFieldType;
|
|
required: boolean;
|
|
adminOnly: boolean;
|
|
validation?: string;
|
|
unsigned?: boolean;
|
|
isLoginId?: boolean;
|
|
indexed?: boolean;
|
|
};
|
|
|
|
function createFieldId() {
|
|
if (typeof crypto !== "undefined" && "randomUUID" in crypto) {
|
|
return crypto.randomUUID();
|
|
}
|
|
return `${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
}
|
|
|
|
export function isSchemaFieldType(value: unknown): value is SchemaFieldType {
|
|
return (
|
|
value === "text" ||
|
|
value === "number" ||
|
|
value === "boolean" ||
|
|
value === "date" ||
|
|
value === "float" ||
|
|
value === "datetime"
|
|
);
|
|
}
|
|
|
|
export function normalizeSchemaField(field: unknown): SchemaField {
|
|
const source =
|
|
typeof field === "object" && field !== null
|
|
? (field as Record<string, unknown>)
|
|
: {};
|
|
const type = isSchemaFieldType(source.type) ? source.type : "text";
|
|
const isLoginId = Boolean(source.isLoginId);
|
|
|
|
return {
|
|
id: typeof source.id === "string" ? source.id : createFieldId(),
|
|
key: typeof source.key === "string" ? source.key : "",
|
|
label: typeof source.label === "string" ? source.label : "",
|
|
type,
|
|
required: Boolean(source.required),
|
|
adminOnly: Boolean(source.adminOnly),
|
|
validation: typeof source.validation === "string" ? source.validation : "",
|
|
unsigned: Boolean(source.unsigned),
|
|
isLoginId,
|
|
indexed: isLoginId || Boolean(source.indexed),
|
|
};
|
|
}
|
|
|
|
export function createSchemaField(): SchemaField {
|
|
return {
|
|
id: createFieldId(),
|
|
key: "",
|
|
label: "",
|
|
type: "text",
|
|
required: false,
|
|
adminOnly: false,
|
|
validation: "",
|
|
unsigned: false,
|
|
indexed: false,
|
|
};
|
|
}
|