diff --git a/src/core/findInputDefinitionSchema.ts b/src/core/findInputDefinitionSchema.ts index 700556d7..181828f9 100644 --- a/src/core/findInputDefinitionSchema.ts +++ b/src/core/findInputDefinitionSchema.ts @@ -1,4 +1,4 @@ -import { A, NonEmptyArray, parse, pipe } from "@std"; +import { A, NonEmptyArray, ParsingFn, parse, pipe } from "@std"; import * as E from "fp-ts/Either"; import { ValiError, BaseSchema } from "valibot"; import { FieldMinimal, FieldMinimalSchema, InputTypeToParserMap } from "./formDefinitionSchema"; @@ -63,7 +63,7 @@ function isValidInputType(input: unknown): input is AllFieldTypes { * @param fieldDefinition a field definition to find the input schema for * @returns a tuple of the basic field definition and the input schema */ -export function findInputDefinitionSchema(fieldDefinition: unknown): E.Either { +export function findInputDefinitionSchema(fieldDefinition: unknown): E.Either]> { return pipe( parse(FieldMinimalSchema, fieldDefinition), E.mapLeft(InvalidFieldError.of(fieldDefinition)), @@ -87,8 +87,8 @@ export function findFieldErrors(fields: unknown[]) { A.map((fieldUnparsed) => { return pipe( findInputDefinitionSchema(fieldUnparsed), - E.chainW(([field, inputSchema]) => pipe( - parse(inputSchema, field.input), + E.chainW(([field, parser]) => pipe( + parser(field.input), E.bimap( (error) => new InvalidInputError(field, error), () => field diff --git a/src/core/formDefinitionSchema.ts b/src/core/formDefinitionSchema.ts index b087bcf9..81e1ff8a 100644 --- a/src/core/formDefinitionSchema.ts +++ b/src/core/formDefinitionSchema.ts @@ -1,5 +1,5 @@ import * as E from "fp-ts/Either"; -import { pipe, parse } from "@std"; +import { pipe, parse, trySchemas, ParsingFn, parseC } from "@std"; import { object, number, literal, type Output, is, array, string, union, optional, minLength, toTrimmed, merge, unknown, ValiError, BaseSchema, enumType, passthrough } from "valibot"; import { AllFieldTypes, FormDefinition } from "./formDefinition"; import { findFieldErrors } from "./findInputDefinitionSchema"; @@ -41,19 +41,19 @@ export const InputTypeSchema = union([ InputSelectFixedSchema, MultiselectSchema ]); -export const InputTypeToParserMap: Record = { - number: InputBasicSchema, - text: InputBasicSchema, - date: InputBasicSchema, - time: InputBasicSchema, - datetime: InputBasicSchema, - textarea: InputBasicSchema, - toggle: InputBasicSchema, - note: InputNoteFromFolderSchema, - slider: InputSliderSchema, - select: SelectFromNotesSchema, - dataview: InputDataviewSourceSchema, - multiselect: MultiselectSchema, +export const InputTypeToParserMap: Record> = { + number: parseC(InputBasicSchema), + text: parseC(InputBasicSchema), + date: parseC(InputBasicSchema), + time: parseC(InputBasicSchema), + datetime: parseC(InputBasicSchema), + textarea: parseC(InputBasicSchema), + toggle: parseC(InputBasicSchema), + note: parseC(InputNoteFromFolderSchema), + slider: parseC(InputSliderSchema), + select: trySchemas([SelectFromNotesSchema, InputSelectFixedSchema]), + dataview: parseC(InputDataviewSourceSchema), + multiselect: parseC(MultiselectSchema), }; export const FieldDefinitionSchema = object({ @@ -67,7 +67,7 @@ export const FieldDefinitionSchema = object({ */ export const FieldMinimalSchema = passthrough(merge([ FieldDefinitionSchema, - object({ input: object({ type: string() }) }) + object({ input: passthrough(object({ type: string() })) }) ])); export type FieldMinimal = Output; diff --git a/src/std/index.ts b/src/std/index.ts index a243d211..50363fed 100644 --- a/src/std/index.ts +++ b/src/std/index.ts @@ -1,6 +1,6 @@ import { pipe as p } from "fp-ts/function"; import { partitionMap, partition, map as mapArr } from "fp-ts/Array"; -import { isLeft, isRight, tryCatchK, map, getOrElse, right, left, mapLeft, Either, bimap, reduce } from "fp-ts/Either"; +import { isLeft, isRight, tryCatchK, map, getOrElse, right, left, mapLeft, Either, bimap } from "fp-ts/Either"; import { BaseSchema, Output, ValiError, parse as parseV } from "valibot"; import { Semigroup, concatAll } from "fp-ts/Semigroup"; import { NonEmptyArray } from "fp-ts/NonEmptyArray"; @@ -40,7 +40,7 @@ type ParseOpts = Parameters[2] export function parseC(schema: S, options?: ParseOpts) { return (input: unknown) => parse(schema, input, options) } -type ParsingFn = (input: unknown) => Either> +export type ParsingFn = (input: unknown) => Either> /** * Concatenates two parsing functions that return Either into one. * If the first function returns a Right, the second function is not called.