import { Static, TSchema, Type } from "@sinclair/typebox"; export function StringEnum(values: [...T]) { return Type.Unsafe({ type: "string", enum: values }); } export function StringKey(object: Record) { return Type.Unsafe({ type: "string", enum: Object.keys(object) }); } export interface JSONDeserialisable { fromJSON(json: unknown): T; jsonSchema: object; } export function jsonArray(ItemClass: JSONDeserialisable) { return { jsonSchema: { type: "array", items: ItemClass.jsonSchema, }, fromJSON(json: Array) { return json.map(i => ItemClass.fromJSON(i)); }, }; } export function jsonPrimitive(type: U) { return { jsonSchema: { type }, fromJSON(json: T) { return json; }, }; } export const JsonBoolean = jsonPrimitive("boolean"); export const JsonNumber = jsonPrimitive("number"); export const JsonString = jsonPrimitive("string"); export function plainJson(schema: T) { return { jsonSchema: schema, fromJSON(json: Static) { return json; }, }; }