From 791488b4086ca44f392a84c0f6cb75d8c5e0380e Mon Sep 17 00:00:00 2001 From: Danielo Rodriguez Date: Mon, 30 Oct 2023 12:35:40 +0100 Subject: [PATCH] chore: test more cases of the parse semigroup --- src/std/index.test.ts | 33 +++++++++++++++++++++++++++++++-- src/std/index.ts | 11 ++++++----- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/std/index.test.ts b/src/std/index.test.ts index 4f58af87..b87c2028 100644 --- a/src/std/index.test.ts +++ b/src/std/index.test.ts @@ -1,4 +1,4 @@ -import { E, trySchemas } from "./index"; +import { E, pipe, trySchemas } from "./index"; import { string, number, array, boolean, object } from "valibot"; describe("trySchemas", () => { @@ -29,6 +29,7 @@ describe("trySchemas", () => { hobbies: ["reading", "swimming"], isEmployed: true, }; + console.log(' ====== 1 =====') const result = trySchemas([schema1, schema2, schema3])(input); expect(result).toEqual({ _tag: "Right", @@ -48,6 +49,7 @@ describe("trySchemas", () => { age: 25, isStudent: true, }; + console.log(' ====== 2 =====') const result = trySchemas([schema1, schema2, schema3])(input); expect(result).toEqual({ _tag: "Right", @@ -63,10 +65,11 @@ describe("trySchemas", () => { it("should return a Left with a ValiError if the input does not match any of the schemas", () => { const input = { name: "John Doe", - age: "30", + age: "30", // This is a string, not a number, will fail, is what I want hobbies: ["reading", "swimming"], isEmployed: true, }; + console.log(' ====== 3 =====') const result = trySchemas([schema1, schema2, schema3])(input); if (E.isLeft(result)) { expect(result.left.message).toEqual('Invalid type'); @@ -74,4 +77,30 @@ describe("trySchemas", () => { fail('expected a Left') } }); + + it("What if I only provide one schema and an input that matches?", () => { + const input = { + name: "John Doe", + age: 30, + hobbies: ["reading", "swimming"], + isEmployed: true, + }; + console.log(' ====== 4 =====') + const result = trySchemas([schema1])(input); + expect(result).toEqual(E.right(input)); + }); + + it("What if I only provide one schema and an input that fails?", () => { + const input = { + name: "John Doe", + age: "30", + hobbies: ["reading", "swimming"], + isEmployed: true, + }; + const result = trySchemas([schema1])(input); + pipe(result, E.bimap( + (x) => expect(x.message).toEqual('Invalid type'), + () => fail('expected a Left') + )) + }); }); diff --git a/src/std/index.ts b/src/std/index.ts index 737b2c3f..a243d211 100644 --- a/src/std/index.ts +++ b/src/std/index.ts @@ -1,7 +1,7 @@ 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 } from "fp-ts/Either"; -import { BaseSchema, ValiError, parse as parseV } from "valibot"; +import { isLeft, isRight, tryCatchK, map, getOrElse, right, left, mapLeft, Either, bimap, reduce } 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"; export type { NonEmptyArray } from 'fp-ts/NonEmptyArray' @@ -22,6 +22,7 @@ export const E = { getOrElse, map, mapLeft, + bimap, } @@ -39,13 +40,13 @@ type ParseOpts = Parameters[2] export function parseC(schema: S, options?: ParseOpts) { return (input: unknown) => parse(schema, input, options) } - +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. */ -class _EFunSemigroup implements Semigroup<(i: A) => Either> { - concat(f: (i: A) => Either, g: (i: A) => Either): (i: A) => Either { +class _EFunSemigroup implements Semigroup> { + concat(f: ParsingFn, g: ParsingFn): (i: unknown) => Either { return (i) => { const fRes = f(i) if (isRight(fRes)) return fRes