Skip to content

Commit

Permalink
chore: use more fp-ts
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed Oct 24, 2023
1 parent b4e6c96 commit f442c8c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
20 changes: 12 additions & 8 deletions src/core/settings.ts
Original file line number Diff line number Diff line change
@@ -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<typeof OpenPositionSchema>;
Expand All @@ -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<typeof ModalFormSettingsSchema>;

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<ValiError, ModalFormSettingsPartial> {
if (maybeSettings === null) return E.right(DEFAULT_SETTINGS)
;
return E.tryCatch(() => parse(ModalFormSettingsSchema, { ...DEFAULT_SETTINGS, ...maybeSettings }), e => e as ValiError);
}

export interface ModalFormSettings {
Expand Down
9 changes: 5 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -123,12 +124,12 @@ export default class ModalFormPlugin extends Plugin {
async getSettings(): Promise<ModalFormSettings> {
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))
Expand Down

0 comments on commit f442c8c

Please sign in to comment.