From f442c8cd6de090a9cf55c1dfd4fc319ad1d035f4 Mon Sep 17 00:00:00 2001 From: Danielo Rodriguez Date: Tue, 24 Oct 2023 17:47:19 +0200 Subject: [PATCH] chore: use more fp-ts --- src/core/settings.ts | 20 ++++++++++++-------- src/main.ts | 9 +++++---- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/core/settings.ts b/src/core/settings.ts index a849f948..54829ca1 100644 --- a/src/core/settings.ts +++ b/src/core/settings.ts @@ -1,5 +1,6 @@ -import { Output, array, enumType, is, object, optional, safeParse, unknown } from "valibot"; +import { Output, ValiError, array, enumType, is, object, optional, parse, unknown } from "valibot"; import type { FormDefinition } from "./formDefinition"; +import * as E from 'fp-ts/Either'; const OpenPositionSchema = enumType(['left', 'right', 'mainView']); export type OpenPosition = Output; @@ -15,22 +16,25 @@ export function isValidOpenPosition(position: string): position is OpenPosition return is(OpenPositionSchema, position); } +// We intentionally don't use the FormDefinitionSchema here, +// because we don't want to parse the form definitions here. +// They will be parsed later so we can show a nice error message. +// This is due to a valibot limitation. const ModalFormSettingsSchema = object({ editorPosition: optional(OpenPositionSchema, 'right'), formDefinitions: array(unknown()), }); +type ModalFormSettingsPartial = Output; + const DEFAULT_SETTINGS: ModalFormSettings = { editorPosition: 'right', formDefinitions: [], }; -export function parseSettings(maybeSettings: unknown) { - if (maybeSettings === null) return { - success: true, - issues: null, - output: DEFAULT_SETTINGS, - }; - return safeParse(ModalFormSettingsSchema, { ...DEFAULT_SETTINGS, ...maybeSettings }); +export function parseSettings(maybeSettings: unknown): E.Either { + if (maybeSettings === null) return E.right(DEFAULT_SETTINGS) + ; + return E.tryCatch(() => parse(ModalFormSettingsSchema, { ...DEFAULT_SETTINGS, ...maybeSettings }), e => e as ValiError); } export interface ModalFormSettings { diff --git a/src/main.ts b/src/main.ts index 473967a7..d8551a82 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,7 +9,8 @@ import { ModalFormError } from "src/utils/Error"; import { formNeedsMigration, type FormDefinition, migrateToLatest } from "src/core/formDefinition"; import { parseSettings, type ModalFormSettings, type OpenPosition } from "src/core/settings"; import { log_error, log_notice } from "./utils/Log"; -import { pipe } from "fp-ts/lib/function"; +import * as E from "fp-ts/Either"; +import { pipe } from "fp-ts/function"; import * as A from "fp-ts/Array" type ViewType = typeof EDIT_FORM_VIEW | typeof MANAGE_FORMS_VIEW; @@ -123,12 +124,12 @@ export default class ModalFormPlugin extends Plugin { async getSettings(): Promise { const data = await this.loadData(); const settingsParsed = parseSettings(data); - if (!settingsParsed.success) { - const error = new ModalFormError('Settings are not valid, check the errors', JSON.stringify(settingsParsed.issues)) + if (E.isLeft(settingsParsed)) { + const error = new ModalFormError('Settings are not valid, check the errors', JSON.stringify(settingsParsed.left.issues)) log_error(error) throw error; } - const settings = settingsParsed.output; + const settings = settingsParsed.right; const migrationIsNeeded = settings.formDefinitions.some(formNeedsMigration); // Migrate to latest also validates and parses the form definitions, so we always execute it const formDefinitions = pipe(settings.formDefinitions, A.partitionMap(migrateToLatest))