Skip to content

Commit

Permalink
chore: default settings are just a function
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed Oct 25, 2023
1 parent 22d1dbf commit 0b92d9a
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/core/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,25 @@ const ModalFormSettingsSchema = object({

type ModalFormSettingsPartial = Output<typeof ModalFormSettingsSchema>;

const DEFAULT_SETTINGS: ModalFormSettings = {
editorPosition: 'right',
formDefinitions: [],
};

export function getDefaultSettings(): ModalFormSettings {
return { ...DEFAULT_SETTINGS };
return { editorPosition: 'right', formDefinitions: []};
}

export class NullSettingsError {
readonly _tag = 'NullSettingsError';
}

/**
* Parses the settings and returns a validation error if the settings are invalid.
* The reason why we don't return default settings when the settings are invalid is because
* in case of default settings there are several operations that could be skipped,
* like migrations and validation.
*/
export function parseSettings(maybeSettings: unknown): E.Either<ValiError | NullSettingsError, ModalFormSettingsPartial> {
return pipe(
maybeSettings,
E.fromNullable(new NullSettingsError()),
E.chainW((s) => parse(ModalFormSettingsSchema, { ...DEFAULT_SETTINGS, ...s })),
E.chainW((s) => parse(ModalFormSettingsSchema, { ...getDefaultSettings(), ...s })),
)
}

Expand Down

0 comments on commit 0b92d9a

Please sign in to comment.