From d8e43fc3a2a1b3803c50ac5c254d94cd4cde58db Mon Sep 17 00:00:00 2001 From: Fabian Hiller Date: Mon, 29 Jul 2024 18:55:36 +0200 Subject: [PATCH] Fix bug in fallback method for specific schemas #752 --- library/CHANGELOG.md | 1 + library/src/methods/fallback/fallback.test.ts | 7 +++++-- library/src/methods/fallback/fallback.ts | 8 ++++---- library/src/methods/fallback/fallbackAsync.test.ts | 4 ++-- library/src/methods/fallback/fallbackAsync.ts | 8 ++++---- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/library/CHANGELOG.md b/library/CHANGELOG.md index a17509b06..0fc1e0976 100644 --- a/library/CHANGELOG.md +++ b/library/CHANGELOG.md @@ -9,6 +9,7 @@ All notable changes to the library will be documented in this file. - Change `unknown[]` in `LengthInput` type to `ArrayLike` - Change `ArrayInput` and `ContentInput` type to use `MaybeReadonly` - Change `EMOJI_REGEX` to be more accurate and strict (pull request #666) +- Fix bug in `fallback` and `fallbackAsync` method for specific schemas (pull request #752) - Fix bug in `fallbackAsync` method for async schemas (pull request #732) ## v0.36.0 (July 05, 2024) diff --git a/library/src/methods/fallback/fallback.test.ts b/library/src/methods/fallback/fallback.test.ts index 8c3c8cc31..64c674bc2 100644 --- a/library/src/methods/fallback/fallback.test.ts +++ b/library/src/methods/fallback/fallback.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from 'vitest'; import { transform } from '../../actions/index.ts'; -import { number } from '../../schemas/index.ts'; +import { boolean, number, union } from '../../schemas/index.ts'; import { pipe } from '../pipe/index.ts'; import { fallback, type SchemaWithFallback } from './fallback.ts'; @@ -29,7 +29,10 @@ describe('fallback', () => { }); }); - const schema = fallback(pipe(number(), transform(String)), '123'); + const schema = fallback( + pipe(union([number(), boolean()]), transform(String)), + '123' + ); describe('should return default dataset', () => { test('for valid input', () => { diff --git a/library/src/methods/fallback/fallback.ts b/library/src/methods/fallback/fallback.ts index d91c57819..5a480b889 100644 --- a/library/src/methods/fallback/fallback.ts +++ b/library/src/methods/fallback/fallback.ts @@ -52,10 +52,10 @@ export function fallback< ...schema, fallback, _run(dataset, config) { - schema._run(dataset, config); - return dataset.issues - ? { typed: true, value: getFallback(this, dataset, config) } - : dataset; + const outputDataset = schema._run(dataset, config); + return outputDataset.issues + ? { typed: true, value: getFallback(this, outputDataset, config) } + : outputDataset; }, }; } diff --git a/library/src/methods/fallback/fallbackAsync.test.ts b/library/src/methods/fallback/fallbackAsync.test.ts index eeb9017d6..222fa2010 100644 --- a/library/src/methods/fallback/fallbackAsync.test.ts +++ b/library/src/methods/fallback/fallbackAsync.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from 'vitest'; import { transformAsync } from '../../actions/index.ts'; -import { number } from '../../schemas/index.ts'; +import { boolean, number, union } from '../../schemas/index.ts'; import { pipeAsync } from '../pipe/index.ts'; import { fallbackAsync, @@ -49,7 +49,7 @@ describe('fallbackAsync', () => { const schema = fallbackAsync( pipeAsync( - number(), + union([number(), boolean()]), transformAsync(async (input) => String(input)) ), async () => '123' diff --git a/library/src/methods/fallback/fallbackAsync.ts b/library/src/methods/fallback/fallbackAsync.ts index dacb2222a..346751039 100644 --- a/library/src/methods/fallback/fallbackAsync.ts +++ b/library/src/methods/fallback/fallbackAsync.ts @@ -79,11 +79,11 @@ export function fallbackAsync< fallback, async: true, async _run(dataset, config) { - await schema._run(dataset, config); - return dataset.issues + const outputDataset = await schema._run(dataset, config); + return outputDataset.issues ? // @ts-expect-error - { typed: true, value: await getFallback(this, dataset, config) } - : dataset; + { typed: true, value: await getFallback(this, outputDataset, config) } + : outputDataset; }, }; }