Skip to content

Commit

Permalink
chore: map to parser rather than schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed Oct 30, 2023
1 parent 791488b commit 595acf9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/core/findInputDefinitionSchema.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<InvalidFieldError | InvalidInputTypeError, [FieldMinimal, BaseSchema]> {
export function findInputDefinitionSchema(fieldDefinition: unknown): E.Either<InvalidFieldError | InvalidInputTypeError, [FieldMinimal, ParsingFn<BaseSchema>]> {
return pipe(
parse(FieldMinimalSchema, fieldDefinition),
E.mapLeft(InvalidFieldError.of(fieldDefinition)),
Expand All @@ -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
Expand Down
30 changes: 15 additions & 15 deletions src/core/formDefinitionSchema.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -41,19 +41,19 @@ export const InputTypeSchema = union([
InputSelectFixedSchema,
MultiselectSchema
]);
export const InputTypeToParserMap: Record<AllFieldTypes, BaseSchema> = {
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<AllFieldTypes, ParsingFn<BaseSchema>> = {
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({
Expand All @@ -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<typeof FieldMinimalSchema>;
Expand Down
4 changes: 2 additions & 2 deletions src/std/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -40,7 +40,7 @@ type ParseOpts = Parameters<typeof parse>[2]
export function parseC<S extends BaseSchema>(schema: S, options?: ParseOpts) {
return (input: unknown) => parse(schema, input, options)
}
type ParsingFn<S extends BaseSchema> = (input: unknown) => Either<ValiError, Output<S>>
export type ParsingFn<S extends BaseSchema> = (input: unknown) => Either<ValiError, Output<S>>
/**
* Concatenates two parsing functions that return Either<ValiError, B> into one.
* If the first function returns a Right, the second function is not called.
Expand Down

0 comments on commit 595acf9

Please sign in to comment.